/*
 * Aexpo gallery
 * 
 * 2007 - Magic Wand
 */
ns('Aexpo');

ns.Aexpo.Gallery = function(img_work_company) {
  this.img_work_company = img_work_company;
  this.work_loader = null;
  this.triggered_project = false;
  this.triggered_company = false;
  
  this._init();  
}

ns.Aexpo.Gallery.prototype = {
  _init: function() {
    var _this = this;
    $('#gallery').load(root_url + 'portfolio/images/', {}, function() {
      _this._initComponents();
    });
  },
  
  _initComponents: function() {
    this._initOrderImage();
    this._initCarousell();
    this._initProjectGallery();
    this._initCompanyGallery();
    this._initWorkLoader();    
    $('#gallery').trigger('portfolio_ready');
  }, 
  
  _initCarousell: function() {
    var gal = $('#gallery').jCarouselLite({
        btnNext: '#next_img',
        btnPrev: '#prev_img',
        mouseWheel: true,
        vertical: true,
        visible: 4,
        circular: false
    });
    var height = gal.height();
    gal.parent().height(height);            
  },
  
  _initProjectGallery: function() {
    new ns.MagicWand.Gallery.ProjectGallery('gallery', '#pic_big', '#image_name');
    var _this = this;
    $('#gallery').bind('pict_activated', function(e, gallery) {
      if (!_this.triggered_project) {
        _this.onSelectProject(gallery.getActive());
      } else _this.triggered_project = false;
    });
  },
  
  _initCompanyGallery: function() {
    new ns.MagicWand.Gallery.CompanyGallery('company_gallery', '#cur_company', '#company_info', root_url + 'portfolio/company/');
    var _this = this;
    $('#company_gallery').bind('pict_activated', function(e, gallery) {
      if (!_this.triggered_company) {
        _this.onSelectCompany(gallery.getActive());
      } else _this.triggered_company = false;
    });
  },
  
  _initWorkLoader: function() {
    this.work_loader = new ns.Aexpo.WorkLoader('#work_info', root_url + 'portfolio/work/');    
  },
  
  _initOrderImage: function() {
    var _this = this;
    $('#gallery .caro_img').each(function(i) {
      var img = _this.img_work_company[this.id];
      if(img) img[2] = i;
    });
  },
  
  onSelectProject: function(img_id) {
    if (!this.triggered_company) {
      this.triggered_company = true;
      this.work_loader.load(this.img_work_company[img_id][0]);
      this.selectCompany(this.img_work_company[img_id][1]);
    }    
    this.triggered_project = false;
  },
  
  onSelectCompany: function(img_id) {
    var rel = this.getCompanyRelations(img_id);
    if(!rel) return;
    if (!this.triggered_project) {
      this.triggered_project = true;
      this.work_loader.load(rel.work);
      this.selectProject(rel.project);
    }
    this.triggered_company = false;
  },
  
  selectProject: function(img_id) {
    $('#gallery').trigger('activate_pict', [img_id]).trigger('go_carousell', [this.img_work_company[img_id][2]]);
  },
  
  selectCompany: function(img_id) {
    $('#company_gallery').trigger('activate_pict', [img_id]);
  },
  
  selectFirstWorksProject: function(id) {
    var rel = this.getWorkRelations(id);
    if(!rel) return;
    this.selectProject(rel.project);
  },
  
  selectFirstProject: function() {
    var img_id = this.getFirstProject();
    if(img_id)
      this.selectProject(img_id);
  },
  
  getFirstProject: function() {
    for (var i in this.img_work_company) {
      var rel = this.img_work_company[i];
      if (rel instanceof Array && rel[2] == 0) {
        return i;
      }
    }
    return false;
  },
  
  getCompanyRelations: function(img_id)
  {
    for(var i in this.img_work_company) {
      var rel = this.img_work_company[i];
      if(rel instanceof Array && rel[1] == img_id) {
        return {
          project: i,
          work: rel[0],
          num: rel[2]
        }
      }
    }
    return null;
  },
  
  getWorkRelations: function(id) {
    for(var i in this.img_work_company) {
      var rel = this.img_work_company[i];
      if(rel instanceof Array && rel[0] == id) {
        return {
          project: i,
          company: rel[1],
          num: rel[2]
        }
      }
    }
    return null;
  }
}

