// Effect.js extension
Effect.BMove = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    if (!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'relative'
    }, arguments[1] || { });
    this.start(options);
  },
  setup: function() {
    this.element.makePositioned();
    this.originalLeft = parseFloat(this.element.getStyle('left') || '0');
    this.originalBottom  = parseFloat(this.element.getStyle('bottom')  || '0');
    if (this.options.mode == 'absolute') {
      this.options.x = this.options.x - this.originalLeft;
      this.options.y = this.options.y - this.originalBottom;
    }
  },
  update: function(position) {
    this.element.setStyle({
      left: (this.options.x  * position + this.originalLeft).round() + 'px',
      bottom:  (this.options.y  * position + this.originalBottom).round()  + 'px'
    });
  }
});

// for backwards compatibility
Effect.BMoveBy = function(element, toTop, toLeft) {
  return new Effect.BMove(element,
    Object.extend({ x: toLeft, y: toTop }, arguments[3] || { }));
};


/*
 * Constants
 */
var PAGE_FA_SPEED = 0.4;
var PHOTO_FA_SPEED = 0.6;

/*
 * Document Onload
 */
var effect_processing = 0;
function onLoad() {
//  $('home_page').show();
//  $('logo').hide();

  $('loading').fade();
  //document.onselectstart = function(e){return false}; // ie
  //document.onmousedown = function(e){return false}; // mozilla
  document.oncontextmenu = function(e){Event.stop(e);return false};
  effect_processing+= 1;
  //new Effect.MoveBy('logo', 290, 0, {duration: 1.8, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
  //effect_processing+= 1;
  setTimeout("$('nav').appear({afterFinish: decEffect})", 1000);
  setTimeout("queue.show(true)", 1250);
}

function decEffect() {effect_processing-= 1};

var preload_images = [], preload_loading = 0, total_preload_images = 0, total_preloaded_images = 0;
function preloader() {
  for (var i=0; i<preload_images.length; i++) {
    if (preload_loading > 5) break;
    if (preload_images[i].loading) continue;
    preload_images[i].loading = true;
    preload_loading++;
    var img = new Image();
    preload_images[i].img = img;
    img.onload = function() {
      for (var i=0; i<preload_images.length; i++)
        if (preload_images[i].src == this.src) {preload_images[i].loaded = true; break;}
      preload_loading--;
      total_preloaded_images++;
      document.getElementById('loading').innerHTML = 'Complete: ' + Math.round(total_preloaded_images * 100 / total_preload_images) + '%';
    }
    img.src = preload_images[i].src;
  }
  setTimeout('preloader()', 500);
};
setTimeout('preloader()', 500);

function preload(images) {
  for (var i=0; i<images.length; i++) {
    total_preload_images++;
    preload_images[preload_images.length] = {src: images[i], loading: false, loaded: false};
  }
}

/*
 * Menu
 */
var menu = {
  id: 0,
  timers: {},
  processing: {},
  active: null,
  sub_active: null,

  over: function(item) {
    if (!item.id) item.id = 'menu_item_' + (menu.id++);
    if (item.id == menu.sub_active || item.id == menu.active || item.id == clients.active || menu.processing[item.id] == 'over') return;
    clearTimeout(menu.timers[item.id]);
    menu.processing[item.id] = 'over';
    menu.timers[item.id] = setTimeout("menu.overstep($('" + item.id + "'))", 20);
  },

  out: function(item) {
    if (!item.id) item.id = 'menu_item_' + (menu.id++);
    if (item.id == menu.sub_active || item.id == menu.active || item.id == clients.active || menu.processing[item.id] == 'out') return;
    clearTimeout(menu.timers[item.id]);
    menu.processing[item.id] = 'out';
    menu.timers[item.id] = setTimeout("menu.outstep($('" + item.id + "'))", 20);
  },

  // private
  overstep: function(item) {
    var i = item.className.substr(4) * 1;
    if (i == 5) {
      menu.processing[item.id] = null;
      return;
    };
    item.className = "menu" + (i+1);
    menu.timers[item.id] = setTimeout("menu.overstep($('" + item.id + "'))", 20);
  },

  // private
  outstep: function(item) {
    var i = item.className.substr(4) * 1;
    if (i == 0) {
      menu.processing[item.id] = null;
      return;
    };
    item.className = "menu" + (i-1);
    menu.timers[item.id] = setTimeout("menu.outstep($('" + item.id + "'))", 100);
  },

  click: function(item, is_sub, callback, args) {
    if (effect_processing != 0) return;
    if (!item.id) item.id = 'menu_item_' + (menu.id++);
    clearTimeout(menu.timers[item.id]);
    if (clients.active && $(clients.active)) {
      $(clients.active).className = 'menu0';
      $($(clients.active).parentNode).removeClassName('selected');
    }
    if (menu.sub_active) {
      $(menu.sub_active).className = 'menu0';
      $($(menu.sub_active).parentNode).removeClassName('selected');
    }
    if (!is_sub) {
      if (menu.active) {
        $(menu.active).className = 'menu0';
        $($(menu.active).parentNode).removeClassName('selected');
      }
      menu.active = item.id;
    } else {
      menu.sub_active = item.id;
    }
    menu.processing[item.id] = null;
    //item
    callback(args);
    item.className = 'selected';
    $(item.parentNode).addClassName('selected');
  },

  activeSubActive: function(item) {
    if (!item.id) item.id = 'menu_item_' + (menu.id++);
    clearTimeout(menu.timers[item.id]);
    if (menu.sub_active) $(menu.sub_active).className = 'menu0';
    menu.sub_active = item.id;
    menu.processing[item.id] = null;
    item.className = 'selected';
    $(item.parentNode).addClassName('selected');
  },

  disactiveMenu: function() {
    if (menu.active) {
      $(menu.active).className = 'menu0';
      $($(menu.active).parentNode).removeClassName('selected');
    }
    menu.active = null;
    //if (menu.sub_active) $(menu.sub_active).className = 'menu0';
    //menu.sub_active = null;
  }
};

/*
 * Photos
 */
var photos = {
  id: 0,
  timers: {},
  processing: {},
  active: null,

  over: function(item) {
    if (!item.id) item.id = 'photo_item_' + (photos.id++);
    if (item.id == photos.active || photos.processing[item.id] == 'over') return;
    clearTimeout(photos.timers[item.id]);
    photos.processing[item.id] = 'over';
    photos.timers[item.id] = setTimeout("photos.overstep($('" + item.id + "'))", 10);
  },

  out: function(item) {
    if (!item.id) item.id = 'photo_item_' + (photos.id++);
    if (item.id == photos.active || photos.processing[item.id] == 'out') return;
    clearTimeout(photos.timers[item.id]);
    photos.processing[item.id] = 'out';
    photos.timers[item.id] = setTimeout("photos.outstep($('" + item.id + "'))", 20);
  },

  // private
  overstep: function(item) {
    var i = item.getOpacity();
    if (i >= 0.999) {
      photos.processing[item.id] = null;
      return;
    };
    item.setOpacity(i + 0.05);
    photos.timers[item.id] = setTimeout("photos.overstep($('" + item.id + "'))", 20);
  },

  // private
  outstep: function(item) {
    var i = item.getOpacity();
    if (i < 0.61) {
      photos.processing[item.id] = null;
      return;
    };
    item.setOpacity(i - 0.05);
    photos.timers[item.id] = setTimeout("photos.outstep($('" + item.id + "'))", 75);
  }
};

/*
 * Portfolios
 */
var portfolios = {
  expanded: false,
  photos_expand_timer: null,
  shown_id: null,
  shown_page: null,
  pages_count: 0,
  photo_num: 0,
  loading_photo: null,
  current_options: null,
  PHOTOS_PER_PAGE: 9,

  expand: function(options) {
    if (portfolios.expanded) return;
    if (!portfolios.shown_id) {
      portfolios.show(options);
      var d = $$('#portfolios li span')[0];
      menu.activeSubActive(d);
    } else {
      effect_processing+= 2 - 1;
      $('b_nav').appear({duration: 0.1, afterFinish: decEffect});
      //portfolios.photos_expand_timer = setTimeout('portfolios.photos_expand(0)', 20);
      if (portfolios.shown_id)
        menu.activeSubActive($$('#portfolio_' + portfolios.shown_id + ' span')[0]);
    }
    //
    //
    queue.hide();
    information.collapse();
    clients.collapse();
    send_mail.collapse();
    effect_processing+= 4;
    Effect.Appear('portfolios', {duration: 0.1, afterFinish: decEffect});
    $('b_nav').appear({duration: 0.2, afterFinish: decEffect});
    $$('#b_nav span')[0].appear({duration: 0.2, afterFinish: decEffect});
    $('portfolio').appear({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    //
    portfolios.expanded = true;
  },

  collapse: function() {
    if (!portfolios.expanded) return;
    portfolios.expanded = false;
    effect_processing+= 6 - 2;
    Effect.Fade('portfolios', {duration: 0.1, afterFinish: decEffect});
    Effect.Fade('b_nav', {duration: 0.2, afterFinish: decEffect});
    //portfolios.photos_expand_timer = setTimeout('portfolios.photos_collapse(0)', 20);
    $('nav_information').style.marginBottom = null;
    $('b_nav').fade({duration: 0.2, afterFinish: decEffect});
    $('portfolio').fade({duration: PAGE_FA_SPEED, afterFinish: decEffect}); $('b_nav').hide();
  },

  show: function(options, page) {
    if (effect_processing != 0) return;
    portfolios.current_options = options;
    if (!page) page = 0;
    var PER_PAGE = portfolios.PHOTOS_PER_PAGE;
    if (options.id == portfolios.shown_id && page == portfolios.shown_page) return;
    portfolios.shown_id = options.id;
    portfolios.shown_page = page;
    // show thumbnails
    $('port_pix').innerHTML = "";
    $('port_pages').innerHTML = "";
    var phts_div = $('port_pix'), pages_count = Math.ceil(options.photos.length / PER_PAGE);
    portfolios.pages_count = pages_count;
    var phts = options.photos.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE);
    var titles = options.titles.slice(page * PER_PAGE, page * PER_PAGE + PER_PAGE);
    var empties_cnt = PER_PAGE - phts.length;
    for (var i=0; i<empties_cnt; i++) {
      var dom = new Element('a');
      dom.className = 'port_pic_empty';
      dom.innerHTML = "<div style='width:45px; height:45px'></div>";
      dom.style.cursor = 'default';
      phts_div.appendChild(dom);
    }
    for (var i=0; i<phts.length; i++) {
      var dom = new Element('a');
      dom.className = 'port_pic';
      dom.innerHTML = "<img src='" + phts[i][0] + "' />";
      dom.title = titles[i] || "";
      dom.photo_url = phts[i][1];
      if (phts[i].length == 3) dom.right_photo_url = phts[i][2];
      else dom.right_photo_url = '';
      dom.onclick = function() {
        if (effect_processing != 0) return;
        var a = $$('#port_pix a.active')[0];
        if (a) {a.removeClassName('active'); a.setOpacity(0.6);}
        this.className = 'port_pic active';
        this.setOpacity(1);
        portfolios.show_photo(this.photo_url, this.right_photo_url, this.title);
      }
      dom.onmouseover = function() {if (!this.hasClassName('active')) photos.over(this)};
      dom.onmouseout = function() {if (!this.hasClassName('active')) photos.out(this)};
      dom.setOpacity(0.6);
      phts_div.appendChild(dom);
    }
    // show first photo from portfolio
    if (phts.length > 0) {
	  // show either the first photo from portfolio, or the one specified in 'selected_id'
	  selected_id = options.selected_id || 0;
	  options.selected_id = null; //need to reset: we don't necessarily want to select the same next time
	  $$('#port_pix a.port_pic')[selected_id].addClassName('active');
	  $$('#port_pix a.port_pic')[selected_id].setOpacity(1);
      portfolios.show_photo(phts[selected_id][1], phts[selected_id][2], titles[selected_id]);
    } else {
      effect_processing+= 2;
      $('portfolio_photo_0').fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
      $('portfolio_photo_1').fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    };
    effect_processing+= 2 - 1;
    phts_div.appear({duration: 0.1, afterFinish: decEffect});
    //portfolios.photos_expand_timer = setTimeout('portfolios.photos_expand(0)', 20);
    // paginator
    if (phts.length > 0) {
      var pg = $$('#b_nav #port_pages')[0];
      if (pages_count > 1) {
        pg.innerHTML = 'SET:&nbsp; ';
        for (var i=1; i<=pages_count; i++) {
          var d = new Element('span');
          d.portfolio_id = options.id;
          d.innerHTML = i;
          d.onclick = function() {
            portfolios.show({id: this.portfolio_id*1, photos: portfolio_photos[this.portfolio_id*1], titles: portfolio_titles[this.portfolio_id*1]}, this.innerHTML*1 - 1);
          };
          if (i - 1 == page) {
            d.className = 'arrow active';
            d.setOpacity(1);
            first = false;
          };
          pg.appendChild(d);
        }
      }
      var d = new Element('span');
      d.onclick = function() {
        portfolios.prev();
      };
      var i = new Element('img');
      i.src = larrowh;
      d.appendChild(i);
      pg.appendChild(d);
      var d = new Element('span');
      d.onclick = function() {
        portfolios.next();
      };
      var i = new Element('img');
      i.src = rarrowh;
      d.appendChild(i);
      pg.appendChild(d);
      pg.show();
    }
  },

  next: function() {
    if (effect_processing != 0) return;
    var a = $$('#port_pix a.port_pic');
    if (a.length <= 1) return;
    for (var i=0; i<a.length; i++) {
      if (a[i].className.indexOf('active') >= 0) {
        a[i].removeClassName('active');
        a[i].setOpacity(0.6);
        if (i+1 >= a.length && portfolios.pages_count > 1) { // Current photo is last for this page
          if (portfolios.shown_page == portfolios.pages_count - 1) portfolios.show(portfolios.current_options, 0);
          else portfolios.show(portfolios.current_options, portfolios.shown_page + 1);
          return;
        }
        var d = a[i+1 >= a.length ? 0 : i+1];
        d.addClassName('active');
        d.setOpacity(1);
        portfolios.show_photo(d.photo_url, d.right_photo_url, d.title);
        return;
      }
    };
  },

  prev: function() {
    if (effect_processing != 0) return;
    var a = $$('#port_pix a.port_pic');
    if (a.length <= 1) return;
    for (var i=0; i<a.length; i++) {
      if (a[i].className.indexOf('active') >= 0) {
        a[i].removeClassName('active');
        a[i].setOpacity(0.6);
        if (i-1 < 0 && portfolios.pages_count > 1) { // Current photo is first for this page
		  prev_options = portfolios.current_options;
		  per_page = portfolios.PHOTOS_PER_PAGE;
          if (portfolios.shown_page == 0){ //if we are on the first page...
			//..calculate which is the last photo of the last page
			photos_length = prev_options.photos.length - 1; //index is 0
			prev_options.selected_id = photos_length - per_page * (portfolios.pages_count-1);
			portfolios.show(prev_options, portfolios.pages_count - 1);
		  } else { 
			//we know there's gonna be 'per_page' photos in the page as long as it's not the last page.
			prev_options.selected_id = per_page - 1 ; //select the last photo (index 0)
			portfolios.show(prev_options, portfolios.shown_page - 1);
		  }
          return;
        }
        var d = a[i-1 < 0 ? a.length-1 : i-1];
        d.addClassName('active');
        d.setOpacity(1);
        portfolios.show_photo(d.photo_url, d.right_photo_url, d.title);
        return;
      }
    };
  },

  // private
  show_photo: function(photo_url, right_photo_url, title) {
    if (effect_processing != 0) return;
    var d = $('portfolio_photo_' + portfolios.photo_num);
    d.innerHTML = "";
    // left photo
    var img = new Element('img');
    portfolios.loading_photo = photo_url;
    img.onload = function() {
      if (this.src != portfolios.loading_photo || !this.parentNode) return;
      effect_processing+= 1;
      this.parentNode.appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    };
    d.appendChild(img);
    var dtitle = new Element('div');
    dtitle.className = 'title';
    dtitle.innerHTML = title;
    d.appendChild(dtitle);
    img.src = photo_url;
    // right photo
    if (right_photo_url && right_photo_url != '') {
      img.style.width = '390px';
      var img = new Element('img');
      portfolios.loading_photo = photo_url;
      img.onload = function() {
        if (this.src != portfolios.loading_photo || !this.parentNode) return;
        effect_processing+= 1;
        this.parentNode.appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
      };
      img.style.position = 'absolute';
      img.style.left = '400px';
      img.style.top = '0px';
      img.style.width = '390px';
      d.appendChild(img);
      img.src = right_photo_url;
    }
    //
    portfolios.photo_num = (portfolios.photo_num + 1) & 1;
    effect_processing+= 1;
    var d = $('portfolio_photo_' + portfolios.photo_num);
    d.fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
  },

  // private
  photos_expand: function(i, from, to) {
    if (!from) from = $$('#b_nav #port_wrap').offsetHeight;
    if (!to) to = $('port_pix').offsetHeight + 4;
    if (to > 300) to = 0;
    var pix = $('port_wrap');
    var photos = $('port_pix');
    i+= 0.2;
    pix.style.height = (from + (to - from) * i) + 'px';
    if (i >= 1) {effect_processing-= 1; return};
    portfolios.photos_expand_timer = setTimeout('portfolios.photos_expand(' + i + ',' + from + ',' + to + ')', 20);
  },

  // private
  photos_collapse: function(i) {
    var pix = $('port_wrap');
    var photos = $('port_pix');
    var to = photos.offsetHeight;
    if (to > 300) to = 0;
    i+= 0.2;
    pix.style.height = ((to + 15) * (1 - i)) + 'px';
    if (i >= 1) {effect_processing-= 1; return};
    //portfolios.photos_expand_timer = setTimeout('portfolios.photos_collapse(' + i + ')', 20);
  }
};


/*
 * Clients
 */
var clients = {
  expanded: false,
  photo_shown: false,
  photo_num: 0,
  client_id: null,
  current_num: null,
  loading_photo: null,

  expand: function(options) {
    if (clients.expanded_id == options.id) return;
    if (effect_processing != 0) return;
    queue.hide();
    portfolios.collapse();
    information.collapse();
    send_mail.collapse();
    effect_processing+= 1;
    Effect.BlindDown('clients', {duration: 0.1, afterFinish: decEffect});

    clients.sub_expand(options);
    var d = $$('#clients li span')[0];
    menu.activeSubActive(d);

    clients.expanded_id = options.id;
  },

  collapse: function() {
    if (!clients.expanded_id) return;
    clients.sub_collapse();
    clients.expanded_id = false;
    effect_processing+= 1;
    Effect.BlindUp('clients', {duration: 0.1, afterFinish: decEffect});
  },

  sub_expand: function(options) {
    clients._options = options;
    if (clients.expanded_id == options.id) return;
    effect_processing+= 1;
    $('client').fade({duration: PAGE_FA_SPEED, afterFinish: function(){
      clients.client_id = null;
      clients.photo_shown = null;
      $('client_paging').style.display = 'none';
      $('clients_wrapper').style.left = '-350px';
      $('client_pic_0').style.display = 'none';
      $('client_pic_1').style.display = 'none';
      clients.osub_expand(clients._options);
      decEffect();
    }});
    clients.expanded_id = options.id;
  },

  osub_expand: function(options) {
    clients.category = options.category;
    clients.clients = [];
    var i, d = $('clients_list');
    d.innerHTML = "";
    var ul = new Element('ul');
    d.appendChild(ul);
    for (i=0; i<options.category.length; i++) {
      clients.clients[options.category[i][0]] = options.category[i][2];
      var li = new Element('li');
      li.id = "client_" + options.category[i][0];
      if (options.category[i][2].length != 0) {
        li.className = 'cl';
        li.innerHTML = "<span id='s" + li.id + "' class='menu0' onclick='clients.click({id:clients.category[" + i + "][0], photos:clients.category[" + i + "][2]})' onmouseover='menu.over(this)' onmouseout='menu.out(this)'></span>";
      } else
        li.innerHTML = "<span class='menu0'></span>";
      ul.appendChild(li);
    }

    effect_processing+= 1;
    $('client').appear({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    if (clients.client_id) {
      menu.activeSubActive($$('#mclient_' + clients.client_id + ' span')[0]);
    }
    clients.click({id: clients.category[0][0], photos: clients.category[0][2]}, null, true);
  },

  sub_collapse: function() {
    effect_processing+= 1;
    $('client').fade({duration: PAGE_FA_SPEED, afterFinish: decEffect});
  },

  click: function(options, num, ignore_effects) {
    if (!ignore_effects && effect_processing != 0) return;
    if (clients.active && $(clients.active)) {
      $(clients.active).className = 'menu0';
      $($(clients.active).parentNode).removeClassName('selected');
    }
    clients.active = 'sclient_' + options.id;
    $(clients.active).className = 'selected';
    $($(clients.active).parentNode).addClassName('selected');

    if (!num) num = 0;
    if (options.id == clients.client_id && num == clients.current_num) return;
    clients.client_id = options.id;
    clients.current_num = num;
    // paging
    if (options.photos.length > 1) {
      var pg = $('client_paging');
      pg.innerHTML = "";
      for (var i=1; i<options.photos.length+1; i++) {
        var d = new Element('span');
        if (i - 1 == num) d.className = 'pg active';
        else d.className = 'pg';
        d.innerHTML = i;
        d.client_id = options.id;
        d.onclick = function() {
          clients.click({id: this.client_id*1, photos: clients.clients[this.client_id*1]}, this.innerHTML*1 - 1);
        };
        pg.appendChild(d);
      }
      
      var d = new Element('span');
      d.className = 'arrow';
      d.onclick = clients.prev;
      d.innerHTML = "<img src='" + larrowh + "' alt='Previous' title='Previous' />";
      pg.appendChild(d);
      
      var d = new Element('span');
      d.className = 'arrow';
      d.onclick = clients.next;
      d.innerHTML = "<img src='" + rarrowh + "' alt='Next' title='Next' />";
      pg.appendChild(d);

      effect_processing+= 1;
      //$$('#client_pic .fright')[0].appear({duration: 0.2, afterFinish: decEffect});
      $('client_paging').appear({duration: 0.2, afterFinish: decEffect});
    } else {
      effect_processing+= 1;
      //$$('#client_pic .fright')[0].fade({duration: 0.2, afterFinish: decEffect});
      $('client_paging').fade({duration: 0.2, afterFinish: decEffect});
    }
    // show photo
    clients.loading_photo = options.photos[num];
    if (clients.photo_shown) {
      var a = $$('#clients_list li.active')[0];
      if (a) a.removeClassName('active');
      $('client_' + options.id).addClassName('active');
      var d = $("client_pic_" + clients.photo_num);
      d.src = "about:blank";
      d.style.width = '350px';
      d.onload = function() {
        if (this.src != clients.loading_photo) return;
        effect_processing+= 1;
        this.appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
      };
      d.src = options.photos[num];
      clients.photo_num = (clients.photo_num + 1) & 1;
      effect_processing+= 1;
      $("client_pic_" + clients.photo_num).fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    } else {
      clients.photo_shown = true;
      $('clients_list').className = '';
      $('client_' + options.id).addClassName('active');
      $('client_pic').show();
      var d = $("client_pic_" + clients.photo_num);
      d.src = "about:blank";
      d.hide();
      d.style.width = '350px';
      d.onload = function() {
        if (this.src != clients.loading_photo) return;
        effect_processing+= 2;
        new Effect.BMoveBy('clients_wrapper', 0, 350, {duration: 0.5, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
        this.appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
      };
      d.src = options.photos[0];
      clients.photo_num = (clients.photo_num + 1) & 1;
    }
  },

  prev: function() {
    if (effect_processing != 0) return;
    var a = $$('#client_paging span.pg');
    if (a.length <= 1) return;
    for (var i=0; i<a.length; i++) {
      if (a[i].className.indexOf('active') >= 0) {
        a[i].removeClassName('active');
        var d = a[i-1 < 0 ? a.length-1 : i-1];
        d.addClassName('active');
        clients.click({id: d.client_id*1, photos: clients.clients[d.client_id*1]}, d.innerHTML*1 - 1);
        return;
      }
    };
  },

  next: function() {
    if (effect_processing != 0) return;
    var a = $$('#client_paging span.pg');
    if (a.length <= 1) return;
    for (var i=0; i<a.length; i++) {
      if (a[i].className.indexOf('active') >= 0) {
        a[i].removeClassName('active');
        var d = a[i+1 >= a.length ? 0 : i+1];
        d.addClassName('active');
        clients.click({id: d.client_id*1, photos: clients.clients[d.client_id*1]}, d.innerHTML*1 - 1);
        return;
      }
    };
  }
};

/*
 * Information
 */
var information = {
  expanded: false,
  send_mail_shown: false,

  expand: function() {
    if (effect_processing != 0) return;
    if (send_mail.expanded) send_mail.change();
    if (information.expanded) return;
    queue.hide();
    portfolios.collapse();
    clients.collapse();
    //send_mail.collapse();
    //$('information').style.left = '0px';
    effect_processing+= 1;
    $('information').appear({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    //Effect.BlindDown('nav_send_mail', {duration: 0.2, afterFinish: decEffect});
    information.expanded = true;
    information.send_mail_shown = true;
    //
    $('send_mail_form').show();
    $('send_mail_form').setOpacity(1.0);
    $('contact_sent').hide();
  },

  collapse: function() {
    if (!information.expanded) return;
    effect_processing++;
    $('information').fade({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    if (information.send_mail_shown) {
      //effect_processing++;
      //Effect.BlindUp('nav_send_mail', {duration: 0.2, afterFinish: decEffect});
      information.send_mail_shown = false;
    }
    information.expanded = false;
  }
};

/*
 * SendMail
 */
var send_mail = {
  expanded: false,
  params: '',
  processing: false,

  expand: function() {
    if (send_mail.expanded) return;
    if (effect_processing != 0) return;
    var d = $('contact');
    $('send_mail_form').show();
    $('send_mail_form').setOpacity(1.0);
    $('contact_sent').hide();
    d.style.top = '-500px';
    effect_processing+= 4;
    new Effect.MoveBy('information', 0, 700, {duration: 0.7, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
    $('information').fade({duration: 1.0, afterFinish: decEffect});
    d.appear({duration:0.6, afterFinish: decEffect});
    new Effect.MoveBy('contact', 582, 0, {duration: 0.8, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
    send_mail.expanded = true;
    //
    if (information.send_mail_shown) {
      effect_processing++;
      Effect.BlindUp('nav_send_mail', {duration: 0.2, afterFinish: decEffect});
      information.send_mail_shown = false;
    }
  },

  change: function() {
    if (!send_mail.expanded) return;
    effect_processing+= 4;
    new Effect.MoveBy('information', 0, -700, {duration: 0.7, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
    $('information').appear({duration: 1.0, afterFinish: decEffect});
    $('contact').fade({duration: 0.6, afterFinish: decEffect});
    new Effect.MoveBy('contact', -500, 0, {duration: 0.8, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
    send_mail.expanded = false;
    //
    if (!information.send_mail_shown) {
      effect_processing++;
      Effect.BlindDown('nav_send_mail', {duration: 0.2, afterFinish: decEffect});
      information.send_mail_shown = true;
    }
  },

  collapse: function() {
    if (!send_mail.expanded) return;
    effect_processing+= 2;
    new Effect.MoveBy('information', 0, -700, {duration: 0.7, transition: Effect.Transitions.EaseFromTo, afterFinish: decEffect});
    $('contact').fade({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    send_mail.expanded = false;
  },

  send: function() {
    if (effect_processing != 0) return;
    if (send_mail.processing) return;
    send_mail.processing = true;
    var dom = $('send_mail_form');
    send_mail.params = Form.serialize(dom);
    effect_processing+= 1;
    dom.fade({duration: 0.2, to: 0.5, afterFinish: function() {
      new Ajax.Request('/models/model2', {
        method: 'post',
        parameters: send_mail.params,
        onSuccess: function() {
          decEffect();
          send_mail.processing = false;
          // look at model2/create.js.erb
        }
      });
    }});
  }
};

/*
 * Queue
 */
var queue = {
  current: 0,
  num: 0,
  loading_photo: null,

  show: function(disable_check_effect) {
    if (!disable_check_effect && effect_processing != 0) return;
    if (queue_photos.length < 1) return;
    portfolios.collapse();
    information.collapse();
    send_mail.collapse();
    clients.collapse();
    menu.disactiveMenu();
    //
    $('queue_photo_' + queue.num).innerHTML = "";
    var d = new Element('img');
    queue.loading_photo = queue_photos[queue.current];
    d.onload = function() {
      if (this.src != queue.loading_photo) return;
      effect_processing+= 1;
      this.parentNode.appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    };
    $('queue_photo_' + queue.num).appendChild(d);
    d.src = queue_photos[queue.current];
    queue.num = (queue.num + 1) & 1;
    queue.current+= 1;
    if (queue.current >= queue_photos.length) queue.current = 0;
    else preload([queue_photos[queue.current]]);
    effect_processing+= 2;
    $('queue_photo_' + queue.num).fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    $('queue_page').appear({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
  },

  hide: function() {
    if (queue_photos.length == 0) return;
    effect_processing+= 3;
    $('queue_page').fade({duration: PAGE_FA_SPEED, afterFinish: decEffect});
    $('queue_photo_0').fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
    $('queue_photo_1').fade({duration: PHOTO_FA_SPEED, afterFinish: decEffect});
  }
};

