$(document).ready(function(){
	
	$("#homeGalleryList li .homeGalleryItem").hover(function() {
		$(this).css({'z-index' : '10'});	
		$(this).addClass("hover").stop()
			.animate({
				marginTop: '-103px', 
				marginLeft: '-137px', 
				top: '50%', 
				left: '50%', 
				width: '275px', 
				height: '207px',
				fontSize: '18px' 
			}, 400);
		
		} , function() {
		$(this).css({'z-index' : '0'});
		$(this).removeClass("hover").stop()
			.animate({
				marginTop: '0', 
				marginLeft: '0',
				top: '0', 
				left: '0', 
				width: '169px', 
				height: '127px', 
				fontSize: '12px' 
			}, 600);
	});
	
	$("#mainMenu ul ul").hide();
	$("#mainMenu > ul > li").wrapInner('<div class="menuItemWrap" />')
	$("#mainMenu > ul > li .menuItemWrap").prepend("<span></span>"); //Throws an empty span tag right before the a tag
	
	$("#mainMenu > ul > li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the a tag
		$(this).find("span").show().html(linkText); //Add the text in the span tag
	}); 
	
	$("#mainMenu > ul > li").hover(function() {	//On hover...
		$(this).find("span").stop().animate({ 
			marginTop: "-75" //Find the span tag and move it up 40 pixels
		}, 250);
		$(this).find("ul").fadeIn(750);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0" //Move the span back to its original state (0px)
		}, 250);
		$(this).find("ul").fadeOut(250);
	});
	
	
	$('.item').hover(
		function(){
			var $this = $(this);
			expand($this);
		},
		function(){
			var $this = $(this);
			collapse($this);
		}
	);
	
	
	var rotation = function ($elem){
   	$elem.rotate({
    	  angle:360, 
    	  animateTo:0, 
    	  easing: function (x,t,b,c,d){        // t: current time, b: begInnIng value, c: change In value, d: duration
    	      return c*(t/d)-b;
	      }
	   });	
	}
	
	var backrotation = function ($elem){
	$elem.rotate({
    	  angle:0, 
    	  animateTo:360, 
    	  callback: rotation,
    	  easing: function (x,t,b,c,d){        // t: current time, b: begInnIng value, c: change In value, d: duration
    	      return c*(t/d)+b;
	      }
	   });
	}

	function expand($elem){		
		rotation($('.link',$elem));
		$elem.stop().animate({width:'268px'}, 1000).find('.item_content').fadeIn(400);
	}
	function collapse($elem){
		backrotation($('.link',$elem));
		$elem.stop().animate({width:'46px'}, 1000).find('.item_content').stop(true,true).fadeOut();
	}
	
	$(".accordion h3:not('.open')").next('.formtab').hide();
	$('.accordion h3').click(function() {
		if( $(this).is('.open') ) { 			
			$(this).next('.formtab').slideToggle();
			$(this).removeClass('open');
		} else {
			$(".accordion h3.open").next('.formtab').slideToggle();
			$(".accordion h3.open").removeClass('open');		
			$(this).next().slideToggle();
			$(this).addClass('open');
		}		
		return false;
	});
	
	
	$("#pThumbs").prepend('<ul id="galleryListUl">');
	
	var slideshow = $('#pSlideshow').cycle({
		fx:      'fade',
		timeout:  2000,
		prev:    '#pSlideshowPrev',
		next:    '#pSlideshowNext',
		pager:   '#galleryListUl',
		pagerAnchorBuilder: function(idx, slide){
			var classname = ""; 			
			if(idx%3==2){ classname = ' class="last"'; }
			return '<li' + classname + '><a href="#"><img src="' + $('img',slide).attr('src') + '" width="100" height="75" alt="" /></a></li>'; 
		}        	
	});
	
	slideshow.cycle('pause', true);
	
	$('#pSlideshowPlay').click(function() {
		var paused = slideshow.is(':paused');
		if(paused){
			$('#pSlideshowPlay span.on').hide();
			$('#pSlideshowPlay span.off').show();		
		} else {
			$('#pSlideshowPlay span.off').hide();
			$('#pSlideshowPlay span.on').show();		
		}	
		slideshow.cycle(paused ? 'resume' : 'pause', true);
		return false;
	});
	
});
