$(document).ready(function() {

	$('#projectToggle').each(function() {
		
		var projectToggleClickState = $.cookieJar('projectToggleClickState', {
			cookie: {
				path: '/', 
				expires: 1
			}
		});
		//projectToggleClickState.destroy();
		
		if(projectToggleClickState.cookieObject){
			$(projectToggleClickState.cookieObject).each(function(i, value){
				jQuery.each(value, function(name, val) {
					var theId = name;
					var theState = val.state;
					var $theObject = $('#'+theId);
					if(theState === "open"){
						$theObject.find(".list-simple").hide();
						$theObject.find(".toggleClick").addClass('open');
						$theObject.find(".list-detail").addClass('open');
					}
				});
			});
		}
		
		$(".block a.toggleClick").click(function() {
			
			var $this = $(this);
			var $blockParent = $this.parents('.block');
			var $blockParentId = $blockParent.attr('id');
			var $listSimple = $blockParent.find('.list-simple');
			var $listDetail = $blockParent.find('.list-detail');
			
			$this.toggleClass('open');
			if($listDetail.hasClass('open')){
				$listDetail.slideUp().removeClass('open');
				$listSimple.slideDown();
				projectToggleClickState.set($blockParentId,{
					"id" : $blockParentId,
					"state" : "closed"
				});
			} else {
				$listDetail.slideDown().addClass('open');
				$listSimple.slideUp();
				projectToggleClickState.set($blockParentId,{
					"id" : $blockParentId,
					"state" : "open"
				});
			}
			
			return false;
			
		});
		
	}); // END $('#projectToggle')
	
	// fancy pics
	$("a.group").fancybox({
		'hideOnContentClick': false
	});
	
	// paginate 
	$(".hpager a").click(
		function () {
			$("div.page").hide();
			showpage = $(this).attr('rel');

 			$('#'+showpage).show();
		}
	);
	

	// paginate
	$("ul.paginate li").quickpaginate({ perpage: 10, showcounter: true, pager : $(".paginate-nav") });
	$("div.download").quickpaginate({ perpage: 10, showcounter: true, pager : $(".paginate-nav") });
	$("div.ovc").quickpaginate({ perpage: 10, showcounter: true, pager : $(".paginate-nav") });
	
	//mainMenu preparation
	var $navLiMain = $('#header ul.nav li.main');
	var $navUlChild = $navLiMain.find('ul').not('.subsub');
	$navUlChild.height(0).css({'overflow':'hidden'}).show();
	
	//main nav hover
	$('#header ul.nav li.main').hover(function(e) {
		var $that = $(this);
		//hover in
		var $ulChild = $(this).children('ul');
		$ulChild.css({'overflow':'visible'});
		$that.css({'overflow':'visible'});
		
		var $li = $ulChild.find('li');
		var liHeight = 0; //hard coded!
		$li.each(function() { liHeight += parseInt($(this).height())});
		if($li.length !== 0){
			$(this).height(parseInt(liHeight + 10));
		}	
		$ulChild.stop().animate({height : parseInt(liHeight + 10)}, 300, function() {
			//animate callback
			$that.css({'overflow':'visible'});
		});
	},function(e) {
		var $that = $(this);
		//hover out
		var $ulChild = $(this).children('ul');
		var $li = $ulChild.find('li');
		$(this).height(parseInt(23));
		$ulChild.stop().animate({height : 0}, 300, function() {
			//animate callback
			$that.css({'overflow':'hidden'});
		});
	});
	
	$('li.main li.sub', '#header ul.nav').hover(function(e) {
		var $ulChild = $(this).children('ul');
		$ulChild.toggle();
	}, function(e) {
		var $ulChild = $(this).children('ul');
		$ulChild.toggle();
	});
	
	//slider
	$('.slider').each(function() {
		var $this = $(this);
		var $container = $this.find('.pageContainer');
		var $pages = $this.find('.page');
		var $pagesAmount = $pages.length;
		var $pageWidth = $this.find('.page').width();
		var $pageHeight = $this.find('.page').outerHeight();
		var $controls = $this.find('a.controls');
		var $controlsContainer = $controls.parent();
		var $goLeft = $controls.filter('.goLeft');
		var $goRight = $controls.filter('.goRight');
		var loop = true;
		
		//hide controls container
		$controlsContainer.hide();
		
		if($this.hasClass('noLoop')){
			loop = false;
		}

		//set the width of the pageContainer and add only if javascript exists css
		var tempPageHeight = 50;
		
		$pages.each(function() {
			var height = parseInt($(this).outerHeight());
			if(height >= tempPageHeight){
				tempPageHeight = height;
			}
		});
		
		$container.width($pageWidth).css({
			'position':'relative',
			'height':tempPageHeight
		});
		
		$pages.css({
			'position':'absolute',
			'display':'none'
		});
		
		//do check for active state of current month
		var indexActive = 0;
		//$('.page').eq('2').addClass('active');
		$pages.each(function(i) {
				
			if($(this).hasClass('active')){
				indexActive = i;
			} else {
				$(this).hide();
			}
		});
		
		$('.page').eq(indexActive).show();
		
		if($pages.length >= 2){
			$controlsContainer.show();
			$controlsContainer.children().show();
		}
		
		$goLeft.attr('rel', 0 + parseInt(indexActive));
		$goRight.attr('rel', 1 + parseInt(indexActive));
		
		$goLeft.click(function(){
			var $this = $(this);
			var rel = parseInt($this.attr('rel'));
			if(rel >= 1) {
				$pages.eq(rel).slideUp();
				$goRight.attr('rel', rel);
				rel = rel-1;
				$pages.eq(rel).slideDown();
				$goLeft.attr('rel', rel);
			} else if(loop) {
				$pages.eq($pagesAmount-1).slideDown();
				$pages.eq(rel).slideUp();
				$goLeft.attr('rel', $pagesAmount-1);
				$goRight.attr('rel', $pagesAmount);
				
			} else {
				//nothing
			};
			return false;
		});
		
		$goRight.click(function(){
			var $this = $(this);
			var rel = parseInt($this.attr('rel'));
			if(rel <= $pagesAmount-1) {
				$pages.eq(rel).slideDown();
				$pages.eq(rel-1).slideUp();
				$goLeft.attr('rel', rel);
				rel = rel+1;
				$goRight.attr('rel', rel);
			} else if(loop) {
				$pages.eq(rel-$pagesAmount).slideDown();
				$pages.eq(rel-1).slideUp();
				$goLeft.attr('rel', '0');
				$goRight.attr('rel', '1');
			} else {
				//nothing
			}
			return false;
		});
			
	});
	
});	
