$j( function() {

	if($j('body').hasClass('cms-index-index')) {
		$j('.col-main').parent('.pageWidth').removeClass('pageWidth');
		$j('#home-cycle').addClass('pageWidth');
	}

	positionCarousel();
	PositionCycleImages();

	$j('#carousel_ul').jcarousel({
		wrap: 'circular'
	});
	
	/*$j('#home-cycle img ').each(function() {
		var imageWidth = $j(this).width();
		var screenWidth = $j('html').width();
		var newImagePosition = ((imageWidth - screenWidth)/2) * (-1);
		$j(this).css({'position':'relative','left':newImagePosition});
	});*/
	
	
	
	function PositionCycleImages() {
		$j('#home-cycle img').each(function() {
			var imageWidth = $j(this).width();
			var screenWidth = $j('html').width();
			var newImagePosition = (screenWidth/2) - (imageWidth/2);
			if(imageWidth > screenWidth) {
				$j(this).css({'left': newImagePosition});
			} else {
				$j(this).css({'left': 'auto'});
			}
		});
	}
	

	$j(window).resize(positionCarousel);
	$j(window).resize(PositionCycleImages);
	$j(window).scroll(positionCarousel);

	
	var initialCarouselHeight = $j('#carousel_container').height();
	var initialGridHeight = $j('#imageBox_container').height();
	var isDoingTheMotions = false;
	

	$j( "#switchLink" ).click( function() {

		if(isDoingTheMotions == false) {

			if(document.getElementById("carousel_container").style.display != "none") {
				$j('#switchLink').slideUp();
				$j('#carousel_container').animate({
					height: initialGridHeight
				}, 1000);
				isDoingTheMotions = true;
				$j('#carousel_container').fadeOut("1000", function() {
					$j('#imageBox_container').fadeIn();
					$j('#carousel_container').css({
						height: initialCarouselHeight
					});
					$j('#switchLink').css({
						height: "24px",
						background: "url('/skin/frontend/giogoi/AW11/images/home/slider/controls/grid-2.png')"
					});

					$j('#switchLink').slideDown();
					isDoingTheMotions = false;
				});
			}
			else if(document.getElementById("imageBox_container").style.display != "none") {
				$j('#switchLink').slideUp();
				$j('#imageBox_container').animate({
					height: initialCarouselHeight
				}, 1000);
				isDoingTheMotions = true;
				$j('#imageBox_container').fadeOut("1000", function() {
					$j('#carousel_container').fadeIn();
					$j('#imageBox_container').css({
						height: initialGridHeight
					});

					$j('#switchLink').css({
						height: "40px",
						background: "url('/skin/frontend/giogoi/AW11/images/home/slider/controls/grid-1.png')"
					});

					$j('#switchLink').slideDown();
					isDoingTheMotions = false;
				});
			}
		}

	});	
	
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var ua = navigator.userAgent;
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	
			if (re.exec(ua) != null) {
		
				rv = parseFloat(RegExp.$1);
		
			}
		}
	
	if (navigator.appName == 'Microsoft Internet Explorer' && rv == 8) {
		
		$j('.bg-panel a').hover( function() {
			var index = $j('.bg-panel a').index(this);
			$j(this).find('.linkInfo').css({
				marginTop: "-122px"
			});
		}, function() {
			var index = $j('.bg-panel').index(this);
			$j(this).find('.linkInfo').css({
				marginTop: "28px"
			});
		});
	} else {
		
		$j('.bg-panel a').hover( function() {
			var index = $j('.bg-panel a').index(this);
			$j(this).find('.linkInfo').stop().animate({
				marginTop: "-122px"
			}, 250, 'swing');
		}, function() {
			var index = $j('.bg-panel').index(this);
			$j(this).find('.linkInfo').animate({
				marginTop: "28px"
			}, 250, 'swing');
		});
	
	}
	
	$j('#home-cycle')
	.after('<div id="cycle-nav">')
	.cycle({
		fx: 'fade',
		//speedIn: 2400,
		//speedOut: 2400,
		pager:  '#cycle-nav',
		timeout: 3800,
		before: initBeforeCallback
		//after: initAfterCallback
	});
	
		$j(window).keydown(function (e) {
	
			// var isDoingTheMotions is defined above.
			// Left Keypress
			if(e.keyCode == 37 && document.getElementById('carousel_container').style.display != 'none' && isDoingTheMotions == false) {
				
				$j('.jcarousel-prev').trigger('click');
			}
			// Right Keypress
			if(e.keyCode == 39 && document.getElementById('carousel_container').style.display != 'none' && isDoingTheMotions == false) {
				
				$j('.jcarousel-next').trigger('click');
			}
		
		});
	
	EasterIsHere("tiesto",".tiestoFancyBox","click");
	EasterIsHere("logmein","div#topLogin .title","click","email");

});

	
function EasterIsHere(word,elementToTrigger,trigger,elementToFocus) {
	var secretWord = word
	var easterEggCounter = 0;
	var EasterEggArray = new Array();
    var concatenatedEasterEgg = "";
	
	$j(window).keydown(function (e) {
	
		EasterEggArray[easterEggCounter] = String.fromCharCode(e.keyCode);
		
		//if(easterEggCounter >= secretWord.length) {
			var yeah = easterEggCounter-secretWord.length;
			for(i=yeah;i<=easterEggCounter;i++) {
		    	concatenatedEasterEgg += EasterEggArray[i];
			}
			EasterEggStringTest = concatenatedEasterEgg.substring(concatenatedEasterEgg.length-secretWord.length)
			
			if(EasterEggStringTest.toUpperCase(EasterEggStringTest) == secretWord.toUpperCase(secretWord)) {
					$j(elementToTrigger).trigger(trigger);
			}
			
		//}
		easterEggCounter += 1;
		
	});
	
	if(elementToFocus != null) {
		$j(elementToFocus).focus();
	}
	
}

function positionCarousel() {
	var offset = $j(window).scrollTop();
	var viewportHeight = $j(window).height();
	var padding = 40;
	var diff = 916 - (viewportHeight + offset);
	
	
	if(diff > 0) {
		var position = 330 + diff;
	} else {
		var position = 330;
	}

	$j('#scrollerboxContainer').css({
		'bottom' : position + padding
	});
}
	
function initBeforeCallback() {
	
	if($j(this).hasClass('whiteBackground')) {
		$j(".cms-home").addClass("background-grey");
		$j(".cms-home").removeClass("background-black");
	}
	else {
		$j(".cms-home").addClass("background-black");
		$j(".cms-home").removeClass("background-grey");
	}
	
	//ZoomIn();
}
/*function initAfterCallback() {
	ZoomOut();
}*/
function ZoomIn() {
	
	var backgroundColour = "";
	if($j('body').hasClass('background-black')) {
		 backgroundColour = "#000000";
	} else if($j('body').hasClass('background-grey')) {
		 backgroundColour = "#383A3A";
	}

	var cycleWidth = $j('#home-cycle').width() + 10;
	var cycleHeight = $j('#home-cycle').height();
	var hCOffsetTop = $j('#home-cycle').offset().top;
	var hCOffsetBottom = $j('#home-cycle').offset().top + cycleHeight;
	var hCOffsetLeft = $j('#home-cycle').offset().left;
	var hCOffsetRight = $j('#home-cycle').offset().left + cycleWidth;
	
	if($j('.leftShutter').length == 0) {
		var html = '<div class="leftShutter"></div><div class="rightShutter"></div><div class="topShutter"></div><div class="bottomShutter"></div>';
		$j(html).insertAfter('body');
	}
	
	$j('.leftShutter').offset({
		left: hCOffsetLeft,
		top:  hCOffsetTop
	});
	$j('.rightShutter').offset({
		left: hCOffsetLeft + cycleWidth,
		top:  hCOffsetTop
	});
	$j('.topShutter').offset({
		left: hCOffsetLeft,
		top:  hCOffsetTop
	});
	$j('.bottomShutter').offset({
		left: hCOffsetLeft,
		top:  hCOffsetBottom
	});
	
	$j('.leftShutter').css({
		'width': '0',
		'height': cycleHeight,
		'background-color':backgroundColour,
		'position':'absolute',
		'z-index': '100'
	});
	$j('.rightShutter').css({
		'width': '0',
		'height': cycleHeight,
		'background-color':backgroundColour,
		'position':'absolute',
		'z-index': '101'
	});
	$j('.topShutter').css({
		'width': cycleWidth,
		'height': '0',
		'background-color':backgroundColour,
		'position':'absolute',
		'z-index': '102'
	});
	$j('.bottomShutter').css({
		'width': cycleWidth,
		'height': '0',
		'background-color':backgroundColour,
		'position':'absolute',
		'z-index': '103'
	});
	
	$j('.leftShutter').animate({
		'width':(cycleWidth / 2)
	},1000);
	$j('.rightShutter').animate({
		'width':(cycleWidth / 2),
		'left': hCOffsetLeft + (cycleWidth/2)
	},1000);
	$j('.topShutter').animate({
		'height':(cycleHeight/2)
	},1000);
	$j('.bottomShutter').animate({
		'height':(cycleHeight/2),
		'top':  hCOffsetBottom - (cycleHeight/2) 
	},1000);
	
	ZoomOut();

}

function ZoomOut() {

	var cycleWidth = $j('#home-cycle').width();
	var cycleHeight = $j('#home-cycle').height();
	var hCOffsetTop = $j('#home-cycle').offset().top;
	var hCOffsetBottom = $j('#home-cycle').offset().top + cycleHeight;
	var hCOffsetLeft = $j('#home-cycle').offset().left;
	var hCOffsetRight = $j('#home-cycle').offset().left + cycleWidth;
	
	$j('.leftShutter').animate({
		'width':0,
		'left': hCOffsetLeft
	},1000);
	$j('.rightShutter').animate({
		'width':0,
		'left': hCOffsetLeft + cycleWidth
	},1000);
	$j('.topShutter').animate({
		'height':0,
	},1000);
	$j('.bottomShutter').animate({
		'height':0,
		'top':  hCOffsetBottom
	},1000);
}

