$(document).ready(function(){

	headHeight = $('header').height();
	
	//On Hover Out
	function sexyIn(){
		$('header').stop().animate({
			height: '95'			 
		},{ duration: "slow", easing: "easeInOutExpo"})
		.css({
			borderBottom: '1px solid #333'
		});
		$('.sexyness').stop().delay('500').fadeTo('slow', 1).show();

	}
	
	//On Hover Out
	function sexyOut(){
		$('.sexyness').stop().fadeTo('slow', 0, function() { //Fade to 0 opactiy
			$('.sexyness').hide();  //after fading, hide it
			$('header').stop().animate({
				height: headHeight			 
			}, 'slow')
			.css({
				borderBottom: 'none'
			})
		});

	}
	
	//Set custom configurations
	var config = {
		 sensitivity: 5, // number = sensitivity threshold (must be 1 or higher)
		 interval: 20, // number = milliseconds for onMouseOver polling interval
		 over: sexyIn, // function = onMouseOver callback (REQUIRED)
		 timeout: 800, // number = milliseconds delay before onMouseOut
		 out: sexyOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("header li.collection").hoverIntent(config);


	if ( $('#lookbook').length ) {
		//Paging  and Slider Function
		$.fn.rotatecarousel = function(){
			if ('.loader:hidden') {
				$('ul.rotator a').removeClass('active');
				$(this).addClass('active');	
				var imgSrc = $(this).attr('href'); //Find Image Name
				$('<img src="' + imgSrc +  '" class="imnext" />').prependTo('.fadein');
			}
			$('.loader').fadeIn();
			$('img.imnext').load(function(){
				$('.loader').fadeOut(function(){						  
					$('.fadein img:last-child').fadeOut(function(){
						$(this).remove();
					});
				});
			});
				
		}; 
		
		rotatecarouselswitch = function(){
			play = setInterval(function(){
				$active = $('a.active').parent().next().find('a'); //Move to the next paging
				if ( $active.length === 0) { //If paging reaches the end...
					$active = $('ul.rotator a:first'); //go back to first
				}
				$active.rotatecarousel();
			}, 5000);
		};
	
		rotatecarouselswitch();
		
		//Lookbook scroller
		$('#lookbook ul.rotator a').click(function() {
			$(this).rotatecarousel();
			clearInterval(play);
			rotatecarouselswitch();
			return false;		
		});
	}
						   
	
	//Homepage slider
	if ( $('body.home, .page-template-pageblog-php').length ) {
		
		//Paging  and Slider Function
		$.fn.rotate = function(){
			if ($(this).hasClass('prev')) {
				$('.fadein').find('img:visible').fadeOut().end().find('img:last').fadeIn().prependTo('.fadein');			
			} else {
				$('.fadein img:visible').fadeOut().next('img').fadeIn().end().appendTo('.fadein');	
			}
		}; 
		
		rotateSwitch = function(){
			play = setInterval(function(){
				$('a.next').rotate();
			}, 5000);
		};
		
		$('.fadein img:gt(0)').hide();
		rotateSwitch();
		
		$('.control a').click(function(){
			$(this).rotate();
			clearInterval(play);
			rotateSwitch();
			return false;
		});

	}
	
	$('ul.thumbs a').click(function() {
		$('ul.thumbs a').removeClass('active');
		$(this).addClass('active');
		var mainImage = $(this).attr('href'); //Find Image Name
		$('#main_image').attr({ src: mainImage });
		return false;		
	});
	
	$('ul.thumbs li:only-child').parent().hide();
	
	if ( $('fieldset.add option:only-child').length ) {
		$('fieldset.add option:only-child').parent().hide();
		$('label').text($('fieldset.add option:only-child').parent().attr('id')).hide();
	}

	$('ul.rotator a').click(function() {
		$('ul.rotator a').removeClass('active');
		$(this).addClass('active');
		var videoSrc = $(this).attr('href'); //Find Image Name
		$('#video iframe').attr({ src: videoSrc });
		return false;		
	});
	$('ul.rotator a:first, ul.thumbs a:first').addClass('active');
	
	if( $('a[rel^=prettyPhoto], a[href*=.jpg], #quickview a[href*=.gif], #quickview a[href*=.png]').length > 0 ) {
		$('a[rel^=prettyPhoto], a[href*=.jpg], #quickview a[href*=.gif], #quickview a[href*=.png]').prettyPhoto();
	}
	
	
	var	teasID = 0;
	
	$.fn.showhide = function(){ 
		fullText = $(this).html(); //Get full HTML of .showhide_block
		$('body').append('<textarea id="count">' + fullText +  '</textarea>');
		if ( $('#count').val().length >= 120 ) {
			teasText = fullText.substring(0,120) + "..."; //Cut down html .showhide_block to 100 characters (teaser version)
			$(this).html(teasText); //Replace .showhide_block with new teaser version
			$('<a href="#" class="showhide">View More</a>').insertAfter($('.showhide_block'));
		}
		
	};//end function
	
	$('.showhide_block').showhide();
	
	$("a.showhide").click(function() { //On click...					   
		if ($(this).text() == 'View Less') { //If text link is 'View Less' ...
			$(this).text("View More").prev().html(teasText); //Switch text to 'View More' then change HTML to teaser 
		} else { //If not...
			$(this).text("View Less").prev().html(fullText); //Switch text to 'View Less' then show full HTML
		}
		return false;
	});


//*********************************************************************
//FILTER - GAMES
//*********************************************************************


	//Filter Fixed Top
	

	if( $('.filter').length > 0 ) {
			$('.filter a:first').addClass('filtered');
			
			function filterScroll(){		
				var top = 321;
				$(window).scroll(function (event) {
					var winHeight = $(window).height() - 151;
					var filterHeight = $('.filter').height();
	
					if ( filterHeight >= winHeight ) {
						
					} else {
						var y = $(this).scrollTop();
						if (y >= top) {
							$('.filter').addClass('fixed');
						} else {
							$('.filter').removeClass('fixed');
						}
					};
				});
			}
	
			$(window).load(function(){
				retailersHeight = $('ul.retailers li').outerHeight();
				retailHeight = Math.ceil((( $('ul.retailers li').size() ) / 3)) * retailersHeight;
				$('ul.retailers').css({'height' : retailHeight});
				if( $.browser.safari != true ){
					filterScroll();
				}
			});
			
			$('.filter a').click(function(){
				$('.filter a').removeClass('filtered');
				$(this).addClass('filtered');
				var filterby = $(this).attr('rel');
				
				if ( $(this).attr('rel') == 'all' ) {
					retailHeight = Math.ceil((( $('ul.retailers li').size() ) / 3)) * retailersHeight;
					$('ul.retailers').fadeOut('slow', function(){
						$('ul.retailers li').show();
						$('ul.retailers').fadeIn('slow').animate({
							height: retailHeight
						});
					});
				} else {	
					retailHeight = Math.ceil((( $('ul.retailers li.' + filterby).size() ) / 3)) * retailersHeight;
					$('ul.retailers').fadeOut('slow', function(){
						$('ul.retailers li').hide().each(function(){
							if ( $(this).hasClass(filterby) ) {
								$(this).show();
							}								 
						}).parent().fadeIn('slow').animate({
							height: retailHeight
						});
					});
				}
				
				$('html, body').animate({
					scrollTop: 0
				} , {
					duration: 'slow'
				});
				
				return false;
			});
	
		}

	




	/******************************************** 
		 #Carousel
	********************************************/
	/////Carousel Function + Smart Columns
	$.fn.carousel = function(){ 
		
		var rotatorLi = $(this).find('ul.rotator li').width();
		var rotatorSum = $(this).find('ul.rotator li').size();
		var carousel = $('.carousel').width();
	
		var rotatorMaxLi = Math.floor(carousel / rotatorLi); 
		var rotatorSlideSum =  Math.ceil(rotatorSum / rotatorMaxLi); 
	
		var adjustLi = (carousel / rotatorMaxLi); 
		var adjustRotator = (adjustLi * rotatorSum); 
		
		var lastSlideSum = Math.floor(adjustRotator / carousel); 
		var lastSlide = (carousel * lastSlideSum) - adjustRotator; 
	
		$(this).find('ul.rotator li').css({ 'width' : adjustLi}); 
		$(this).find('ul.rotator').css({ 'width' : adjustRotator}); 
	
		$('input, textarea').focus(function () {if (this.value == this.defaultValue) {$(this).attr({value: ''});}$(this).addClass('focused');}).blur(function () {if (this.value === '') {this.value = this.defaultValue;$(this).removeClass('focused');}});
			
		var sum = 0;
				
		if (carousel < adjustRotator) { //If the list is bigger than the carousel viewport
			$(this).parent().find('a.right-scroll').click(function() { 				   
				if(sum < (rotatorSlideSum-1)) {	
					sum++;
					$(this).parent().find('a.left-scroll').removeClass('deactive');
					switch(sum){
						case rotatorSlideSum: //on the last slide...
							$(this).parent().addClass('deactive');
							break;
						case rotatorSlideSum-1: //second to last slide...
							$(this).parent().find('a.right-scroll').addClass('deactive');
							if (lastSlide < -1) {
								$(this).parent().find('ul.rotator').animate({ marginLeft: '+=' + lastSlide },{ duration: "slow", easing: "easeInOutExpo"});
							}
							else {
								$(this).parent().find('ul.rotator').animate({ marginLeft: '-=' + carousel },{ duration: "slow", easing: "easeInOutExpo"});
							}
							break;
						default: //on click else
							$(this).parent().find('ul.rotator').animate({ marginLeft: '-=' + carousel },{ duration: "slow", easing: "easeInOutExpo"});
							break;
					}
				}
				$(this).parent().find('.reelpaging').html('Page ' + (sum + 1) + ' of ' + rotatorSlideSum);
				return false;
			}); //end switch case
		
			$(this).parent().find('a.left-scroll').addClass('deactive');
			$(this).parent().find('a.left-scroll').click(function() { 
				if(sum > 0) {	
					sum--;
					$(this).parent().find('a.right-scroll').removeClass('deactive');
					switch(sum){
						case 0: //if back to original slide...
							if (lastSlide < -1 && rotatorSlideSum == 2 ) { 
								$(this).parent().find('ul.rotator').animate({ marginLeft: '-=' + lastSlide },{ duration: "slow", easing: "easeInOutExpo"});
							}
							else {
								$(this).parent().find('ul.rotator').animate({ marginLeft: '+=' + carousel },{ duration: "slow", easing: "easeInOutExpo"});
							}
							$(this).parent().find('a.left-scroll').addClass('deactive');
							break;
						case rotatorSlideSum-2: //1st click back from the last slide...
							if (lastSlide < -1) {
								$(this).parent().find('ul.rotator').animate({ marginLeft: '-=' + lastSlide },{ duration: "slow", easing: "easeInOutExpo"});
							}
							else {
								$(this).parent().find('ul.rotator').animate({ marginLeft: '+=' + carousel },{ duration: "slow", easing: "easeInOutExpo"});
							}
							break;
						default:
							$(this).parent().find('ul.rotator').animate({ marginLeft: '+=' + carousel },{ duration: "slow", easing: "easeInOutExpo"});
							break;
					}
					$(this).parent().find('.reelpaging').html('Page ' + (sum + 1) + ' of ' + rotatorSlideSum);
				}
				return false;
			}); //end switch case
		} else { //if there is only one slide...
			$(this).find('a.right-scroll, a.left-scroll').addClass('deactive');
		}//end if carasouel statement
		$(this).parent().find('.reelpaging').html('Page ' + (sum + 1) + ' of ' + rotatorSlideSum);
	};//end function


	////Trigger Carousel + Component Functions
	$('div[class^="carousel"]').carousel();	//run the carousel function on that ID	




	
});	

