ciu={};
ciu.debug = false;

ciu.setupGallery = function() {
  var $=jQuery;
  ciu.bindGalleryLink($('#g_num_nav a'));
  
  function checkHash() {
    if (window.location.hash == "") {
      $('.fade').show();
      window.location.hash = ciu.gObj[ciu.active-1].url;
    } else {
      for (var i=0;i<ciu.gObj.length;i++) {
        if (window.location.hash == "#"+ciu.gObj[i].url) {
          if (i==0) {
            ciu.active=-1;
          }
            $('#link'+(i+1)).click();
          break;
        }
      }
    }
  }
  checkHash();
}

ciu.bindGalleryLink = function($link) {
  var $=jQuery;
  var count=ciu.gObj.length;
  $link.click(function(e) {
    e.preventDefault();
    this.blur();
    if (this.id == 'linkNext') {
      if (ciu.active==count) {goTo=1;} else {goTo=ciu.active+1};
      $('#link'+goTo).click();
    } else if (this.id == 'linkPrev') {
      if (ciu.active==1) {goTo=count;} else {goTo=ciu.active-1};
      $('#link'+goTo).click();
    } else {
      var clicked = parseInt((this.id).substr(4)),i=clicked-1;
      window.location.hash = ciu.gObj[i].url;
      if (clicked != ciu.active) {
        $('#link'+ciu.active).removeClass('active');
        $('#copy'+ciu.active).fadeTo(0,0);
        $('#img'+ciu.active).fadeTo(600,0);
        
        if (!$('#copy'+clicked)[0] && !ciu.gObj[i].started) {
          ciu.gObj[i].started = true;
          $('#desc').prepend(ciu.gObj[i].desc);
          $('#content').append(ciu.gObj[i].img);
        }
        
        $('#copy'+clicked).css('opacity',0).show().fadeTo(0,1);
        $('#img'+clicked).css('opacity',0).show().fadeTo(600,1);
          
        $('#link'+clicked).addClass('active');
        
        if (typeof pageTracker != "undefined") {
          pageTracker._trackPageview($('#link'+clicked).attr('href'));
        }
        
        ciu.active = clicked;
      }
    }
  });
}

ciu.swapApproach = function(targ) {
  jQuery('.app_desc div:visible').hide();
  jQuery('#'+targ).show();
  
}

ciu.brandIdentity = function() {
  var $=jQuery;
  var active=0;
  var goTo=0;
  var total=biArr.length-1;
  
  $('#bi_next').click(function(e) {
    e.preventDefault();
    this.blur();
    if (active==total) {goTo=0;} else {goTo=active+1;}
    swap();
  });
  
  $('#bi_prev').click(function(e) {
    e.preventDefault();
    this.blur();
    if (active==0) {goTo=total;} else {goTo=active-1;}
    swap();
  });
  
  function swap() {
    $('#bi_img_'+active).fadeTo('500',0);
    $('#bi_img_'+goTo).css({'opacity':'0','display':'block'}).fadeTo('500',1);
    
    if (typeof pageTracker != "undefined") {
      pageTracker._trackPageview('/gallery/brand-identity'+$('#bi_img_'+goTo+' img').attr('src'));
    }
    
    active = goTo;
  }
  
  $(function(){
    for (i=1;i<total+1;i++) {
      $('#bi_img_'+i).append(biArr[i]);
    }
  });
}

ciu.setupLegacy = function() {
  var $=jQuery;
  
  $('#carousel').jCarouselLite({
    btnNext:'#leg_next',
    btnPrev:'#leg_prev',
    visible:5,
    circular:false,
    beforeStart:function(a) {
      window.focus();
    }
  })
  
  $('#carousel img.leg_thumb')
    .bind('mouseover', function(e) {
      var over = parseInt((this.id).substr(3));
      $('#ar_'+over).show();
      $('#copy_'+over).show();
    })
    .bind('mouseout', function(e) {
      var over = parseInt((this.id).substr(3));
      $('.leg_arrow:visible').hide();
      $('.leg_copy:visible').hide();
    });
  
}

ciu.run = function(runMe) {
	if( jQuery.browser.safari ){
		if (jQuery('.x_chk').width() > 1 || jQuery('.x_chk').width() == null) {
			setTimeout(function(){ciu.run(runMe);runMe=null;},10);
      return false;
		} 
	};
  if (typeof runMe == 'string') {
    eval(runMe);
  } else {
    runMe();
  }
}

ciu.checkCookie = function() {
  var isSet='false';
  var nameEQ = "ciu_intro=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) isSet = c.substring(nameEQ.length,c.length);
	}
  
  if(isSet=='false') {
    document.cookie = "ciu_intro=played;path=/";
    return 'play';
  }
  return 'skip';
}

ciu.eraseCookie = function() {
  document.cookie = "ciu_intro=false;path=/";
}

ciu.logoHover = function(state) {
  if (state == 'show') {
    $('#replay_tab').show().stop().animate({'left':'-20'});
  } else {
    $('#replay_tab').stop().animate({'left':'0'});
  }
}

trace = function(msg) {
  if (ciu.debug == true) {
    if (jQuery('#trace_wrap')[0] == undefined) {
      jQuery('body').append("<div id='trace_wrap' style='position:fixed;bottom:0;margin-top:75px;width:100%;'>"+
      "<a id='trace_close' style='position:absolute;top:-22px;right:5px;padding:3px 5px;background:#fff;border:2px solid #000;color:#000;cursor:pointer;'>(close)</a>"+
      "<div id='trace_msg' style='padding:10px;border-top:4px ridge #F3F3EE;background:#FFF;height:100px;overflow:auto;position:relative;'>"+
      "</div></div>");
      jQuery('#trace_close').click(function() {
        jQuery('#trace_msg').slideToggle('fast');
      });
    }
    jQuery('#trace_msg').append(msg+'<br />');
  }
}