$(document).ready(function(){ 
	var deltaCounter = function(delta) {
		var count = parseInt($('.headertextgreen').text().split('/')[0]);
		count += delta;
		setPicCount(count);
	};
	
	var setPicCount = function(count) {
		var picCount = totalPics();
		$('.headertextgreen').text(count + '/' + picCount);
	};
	
	var totalPics = function() {
	  return $('#slider ul').children().length;
	}
	
	var setDescription = function(text) {
	  $('.description').text(text);
	}
	
	var sliderCallback = function() {
	  var itemWidth = parseInt($('#slider ul').css('width'), 10) / totalPics();
	  var margin = parseInt($('#slider ul').css('margin-left'), 10);
	  var itemIndex = -margin / itemWidth;
	  setPicCount(itemIndex + 1);
	  var item = $('#slider li:nth(' + itemIndex + ')');
	  setDescription($('input:hidden', item).val());
	  
	  $('.detail-link').hide();
	  $('#detail-link-' + itemIndex).show();
	};
	
	$("#slider").easySlider({
		prevText: 'Previous Slide',
		nextText: 'Next Slide',
		orientation: 'horizontal',
		callBack: sliderCallback
	});
	sliderCallback();
	
	function initMenus() {
   $('ul.menu ul').hide();
   
   $.each($('ul.menu'), function(){
     if (document.referrer === '') {
       $.cookie(this.id, null);
     } else {
       var cookie = $.cookie(this.id);
       if(cookie === null || String(cookie).length < 1) {
         $('#' + this.id + '.expandfirst ul:first').show();
       } else {
         $('#' + this.id + ' .' + cookie).next().show();
       }
     }
   });
 
   $('ul.menu li a').click(function() {
     var checkElement = $(this).next();
     var parent = this.parentNode.parentNode.id;
     if($('#' + parent).hasClass('noaccordion')) {
       if((String(parent).length > 0) && (String(this.className).length > 0)) {
         if($(this).next().is(':visible')) {
           $.cookie(parent, null);
         } else {
           $.cookie(parent, this.className);
         }
         $(this).next().slideToggle('normal');
       }
     }
     if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
       if($('#' + parent).hasClass('collapsible')) {
         $('#' + parent + ' ul:visible').slideUp('normal');
       }
       return false;
     }
     if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
       $('#' + parent + ' ul:visible').slideUp('normal');
       if((String(parent).length > 0) && (String(this.className).length > 0)) {
         $.cookie(parent, this.className);
       }
       checkElement.slideDown('normal');
       return false;
     }
   });
 }
 initMenus();
 
});

