//////////////////////////////////////////////////////
//
//	The purpose of this file is to
//	apply a hover class on mouseover
//	on the <li> elements in the nav.
//	This is because IE doesn't put
//	the pseudo class :hover on <li> elements
//
//	This is code that uses the
//	jquery javascript library (http://jquery.com/)
//
//////////////////////////////////////////////////////

$(document).ready(function() {
	//comment this out if you have no png images
    $('img[@src$=.png]').ifixpng(); 
});

$(window).load(function() {
	
	//animated cream logo
	$('#cream a').hover(function(){
			$('#cream').animate({backgroundColor: '#eb9e29'}, 'slow');
		}, function(){
			$('#cream').animate({backgroundColor: '#bebebe'}, 'slow');
	});
	
	//homepage slideshow
	if($('#slideShow').length > 0)
	{
		$('#slideShow').serialScroll({items: 'div.slide', next:  'a.next', prev: 'a.prev', target: '#boogie', constant: false, duration: 300, easing: 'swing'});
	
		$('#slideShow a.next,#slideShow a.prev').hover(
			function(){ 
				$(this).dequeue();
				$(this).animate({width: "22px"}, "fast");
			},
			function(){
				$(this).animate({width: "16px"}, "fast");
			}
		);
	}
	
	if($('#c1').length > 0)
	{
		var divs = $('#c1 > div');
		var n = divs.length;
		if(n > 0) 
			$(divs[Math.floor(n*Math.random())]).css({display: 'block'});

	}
	
	if($('#c3').length > 0)
	{
		var ps = $('#c3 p');
		var n = ps.length;
		if(n > 0)
			$(ps[Math.floor(n*Math.random())]).css({display: 'block'});

	}
	
	if($('#gallery').length > 0)
	{
		var defaultDescription = "";
		defaultDescription = $('#titleDesc .description').html();
		$('#gallery li a').each(function(){
			var screen = $('<div class="screen"></div>').appendTo(this);
			$(this).children('.screen').animate({opacity: 0.5}, '1');
			$(this).hover(
				function(){
					if($(this).parent().hasClass('selected'))
						return;
					$(this).children('.screen').dequeue();
					$(this).children('.screen').animate({opacity: 0}, 'fast');
					$('#titleDesc .description').html($(this).siblings('span.description').html());
				}, 
				function(){
					if($(this).parent().hasClass('selected'))
						return;
					$(this).children('.screen').animate({opacity: 0.5}, 'fast');
					$(this).children('.screen').dequeue();
					$('#titleDesc .description').html(defaultDescription);
			});
		});
		
		/*var scrollWidth = Math.ceil($('#gallery ul li').length / 6 ) * (882);*/
		var scrollWidth = Math.ceil($('#gallery ul li').length / 6 ) * (882);
		scrollWidth = scrollWidth + 1;
		var excludeValue = $('#gallery ul li').length - 1;
		var previous_href = '';
		var current_href = '';
		var href_list = '';
		var href = '';
		var temp_href = '';
		$('#gallery ul').width(scrollWidth + 'px');
		$('#gallery').serialScroll({items: 'li', next:  'a.next', prev: 'a.prev', target: '#slider', constant: true, duration: 1000, easing: 'easeOutQuart', step: 6, cycle: true, lock: false, exclude: 5});
	
		$('#gallery ul li.selected a .screen').animate({opacity: 0}, 'fast');
		$('#gallery a.prev').focus(function(){
			this.blur();
		});
		$('#gallery a.next').focus(function(){
			this.blur();
		});
		$('#gallery ul li a').focus(function(){
			this.blur();
		});
		$('#gallery ul li a').click(function(){
			var mrA = this;
			if($(this).parent().hasClass('selected'))
			{
				return false;
			}
			defaultDescription = $(this).siblings('span.description').html();
			$('#gallery ul li.selected').removeClass('selected').find('.screen').animate({opacity: 0.5}, 'fast');
			$(this).parent().addClass('selected');
			$('#galleryContent').animate({opacity: 0},'slow', function(){
				/*$('#galleryContent #target').css({display: "none"});*/
				$('#galleryContent #target').load(mrA.href + ' #galleryContent div' , function(){
					/*$('#galleryContent #target').css({display: "block"});*/
					$('#galleryContent').animate({opacity: 1},'slow');
					previous_href = current_href;
					current_href = mrA.href;
					href_list = GetCookie('href_list');
					if (href_list != '') href_list += '|';
					href_list += previous_href;
					SetCookie('href_list', href_list);
				}); 
			});
			return false;
		});
		$('#back a').click(function(){
			var mrB = this;
			defaultDescription = $(this).siblings('span.description').html();
			$('#gallery ul li.selected').removeClass('selected').find('.screen').animate({opacity: 0.5}, 'fast');
			$(this).parent().addClass('selected');
			$('#galleryContent').animate({opacity: 0},'slow', function(){
				href_list = GetCookie('href_list');
				if (!href_list) {
					href = document.location.href;
				}
				else if (href_list == '') {
					href = document.location.href;
				}
				else {
					var ahref_list = href_list.split("|");
					if (!ahref_list[ahref_list.length - 1]) {
						href = document.location.href;
					}
					else {
						href = ahref_list[ahref_list.length - 1];
					}
					href_list = '';
					for (var i = 0; i < ahref_list.length - 1; i++) {
						if (href_list != '') href_list += '|';
						href_list += ahref_list[i];
					}
					SetCookie('href_list', href_list);
				} 
				$('#galleryContent #target').load(href + ' #galleryContent div' , function(){
					$('#galleryContent').animate({opacity: 1},'slow');
				}); 
			});
			return false;
		});
		
		
		
		$('#gallery a.next,#gallery a.prev').hover(
			function(){ 
				$(this).dequeue();
				$(this).animate({width: "22px"}, "fast");
			},
			function(){
				$(this).animate({width: "16px"}, "fast");
			}
		);
	}
});

function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue);
  // Expires the cookie in one month
  var date = new Date();
  date.setMonth(date.getMonth()+1);
  document.cookie += ("; expires=" + date.toUTCString()); 
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}
