google.setOnLoadCallback(function(){$(document).ready(GlobalInit);});
/*
$(document).ready(function() {
	GlobalInit();
});
*/

function GlobalInit()
{
	NAVIGATION = new Navigation();
	NAVIGATION.init();
	PAGES = new Pages();
	PAGES.contentHeight();
	SLIDER = new Slider();
	SLIDER.init();
	SEARCH = new Search();
	SEARCH.init();
}

function Navigation()
{
	this.init = init; function init()
	{
		// This removes the CSS 'falback'
		$("#tgNavMainOptions ul.child").removeClass("child");
		
		$("#tgNavMainOptions li").hover(function(){
			//Add the class of 'current'
			$(this).addClass("current").next().children('a').addClass('nobkg');	
			//Check for child 'ul' element and slidedown
			if ($(this).has('ul')) {
				$(this).children("ul").slideDown();
			}
		}, function() {
			// Remove the class of 'current'
			// Stop any sub-menu animation and set its display to none
			$(this).removeClass("current").next().children('a').removeClass('nobkg');
			
			if ($(this).has('ul')) {
				$(this).children("ul").stop(true, true).css("display", "none");
			}
		});
		
		// LFW
		$("#lfwSelectorList ul.child").removeClass("child");
		
		$("#lfwSelectorList li").hover(function(){
			$(this).addClass("current").next().children('a').addClass('nobkg');	
			if ($(this).has('ul')) {
				$(this).children("ul").slideDown();
			}
		}, function() {
			$(this).removeClass("current").next().children('a').removeClass('nobkg');
			if ($(this).has('ul')) {
				$(this).children("ul").stop(true, true).css("display", "none");
			}
		});
	}
}

function Search()
{
	this.init = init; function init()
	{
		$('#searchFormDefault input[name=keywords]').bind('focus', function(){
			$(this).unbind('focus').val('');
			});
		//$("#searchFormDefault input, #searchFormSalon input").focus(function(){if(this.value == this.defaultValue){ this.select(); }});
		$('#showMoreArticles').click( function() { showMoreArticles(); return false; });
		
		function showMoreArticles()
		{
			$('#salonSearchOthers ul li.sfHide').show().animate({'height':'60px'}, 'slow', function() { $(this).css({'height':'100%'}); });
			$('#salonDidntFind').html('<p>Didn\'t find what you were looking for? Try the <a href="/salon/finder"><u>Salon Finder</u></a></p>');
		}
	}
}

function Pages()
{
	var tickerData;
	var tickerWidth = 700;
	var tickerSpeed = 10000;
	var tickerData;
	var tickerInc = 0;
			
	this.init = init; function init()
	{
		var h = $('#pagesContentHold').innerHeight();
		$('#pagesContentHold').css('margin-top', -h);
		$('#pagesContentBg').height($('#pagesContentHold').innerHeight());
	};
			
	this.tickerInit = tickerInit; function tickerInit()
	{
		var pagesTicker = document.getElementById('pagesTicker');
		
		if(!ShinobiUtils.isEmpty(pagesTicker))
		{
			$.get('/gateway/latest-news', {}, function(data) { tickerData = data; tickerRotate(); }, 'json');
		}
	};
			
	function tickerRotate()
	{
		$('#pagesTicker .ticker').show().html(nextArticle());
		$('#pagesTicker .tickerReveal').css({'width':tickerWidth, 'margin-left':-tickerWidth}).animate({'width':'0px', 'margin-left':'0px'}, tickerSpeed, 'linear', tickerFadeOut);
	};

	function tickerFadeOut()
	{
		$('#pagesTicker .ticker').fadeOut('slow', tickerRotate);
	};
		
	function nextArticle()
	{
		t = tickerData[tickerInc];
		if(ShinobiUtils.isEmpty(t['tickertitle']))
		{
			t['tickertitle'] = t['title'];
		}
			
		article = '<a href="/pages/article/' + t['id'] + '">' + t['tickertitle'] + '</a>';
		tickerInc++;
		if(tickerInc == tickerData.length) tickerInc = 0;
		return article;
	};
	
	this.contentHeight = contentHeight; function contentHeight()
	{
		var content_height = $('#pagesContent').height();
		
		var pageId = $('body').prop('id');
		
		if (content_height > 600 && pageIdChecker(pageId)) {
			$('#pagesCategory').height(content_height);
		}
		
		function pageIdChecker(pageId) {	
			if (pageId != 'style-finder' || 'fashion-week' || 'salon') {
				return false;
			} else {
				return true;
			}
		}
	};
}

function Slider() 
{
	var slider = $('#slider');
	var sliderInfo = $('#sliderInfo');
	var sliderThumbs = $('#sliderThumbs');
	var styleInfo = $('#styleInfo');
	var thumbControl = $('#thumb_control');
	var thumbsHeight = sliderThumbs.height();
	
	this.init = function() 
	{	
		//Set the counter and max number of slides
		var i = 0;
		var max = $("li", slider).length -1;
		
		//Show slider controls
		$(slider).hover(function() {
			$('.sliderNav', this).stop(true,true).fadeIn('slow');
		}, function() {
			$('.sliderNav', this).stop(true,true).fadeOut('slow');
		});
		
		//Position the image vertically center
		slider.find('img').each(function() {
			if ($(this).height() < 709){
				var maginTop = (709-$(this).height())/2;
				$(this).css({
					'marginTop' : maginTop
				});
			}
		});
		
		//Add class to first slide
		sliderThumbs.find('li:first-child a').addClass('currentSlide');
		
		//Function for previous slide
		function slidePrev(){
            $("li", slider).eq(i).filter(':not(:animated)').animate({
        		"opacity" : 0
        		}, 400, function(){
        			(i == 0) ? i=max : i--;
        	    	$("li", slider).eq(i).animate({
        	    		"opacity" : 1
        	    	}, 400, function() {
        	    		var slideText = $('li', slider).eq(i).find('img').attr('alt');
        	    		if (!slideText == '') {
        	    			$('p', sliderInfo).text(slideText);
        	    		} else {
        	    			$('p', sliderInfo).text('No description available');
        	    		}
        	    	});
        	});
 			sliderThumbs.find('li a.currentSlide').removeClass('currentSlide');
 			sliderThumbs.find('li').eq(i - 1).find('a').addClass('currentSlide');
		};
		
		//Function for next slide
		function slideNext(){
            $("li", slider).eq(i).filter(':not(:animated)').animate({
        		"opacity" : 0
        		}, 400, function(){
      				(i == max) ? i=0 : i++;
        	    	$("li", slider).eq(i).animate({
        	    		"opacity" : 1
        	    	}, 400, function() {
        	    		var slideText = $('li', slider).eq(i).find('img').attr('alt');
        	    		if (!slideText == '') {
        	    			$('p', sliderInfo).text(slideText);
        	    		} else {
        	    			$('p', sliderInfo).text('No description available');
        	    		}
        	    	});
        	});
 			sliderThumbs.find('li a.currentSlide').removeClass('currentSlide');
 			sliderThumbs.find('li').eq(i + 1).find('a').addClass('currentSlide');
		};
		
		//Click handler for next slide
		$('#nextSlide').bind('click', function() {
			slideNext();
			return false;
		});
		
		//Click handler for previous slide		
		$('#prevSlide').bind('click', function() {
			slidePrev();
			return false;
		});
		
		//Click handler for opening the style caption box
		$('#styleOpen').bind('click', function() {
			$(this).fadeOut(400, function() {
				$(sliderInfo).slideDown(400, function() {
					$('#styleClose').fadeIn(400);
				});				
			});
			return false;
		});	
		
		//Click handler for closing the style caption box
		$('#styleClose').bind('click', function() {
			$(this).fadeOut(400, function() {
				$(sliderInfo).slideUp(400, function() {
					$('#styleOpen').fadeIn(400);
				});
			});
			return false;
		});	
		
		//Click handler for slide thumbnails
		$('a', sliderThumbs).bind('click', function() {
			var index = $(this).parent('li').index();
			if (index != i) {
				$("li", slider).eq(i).filter(':not(:animated)').animate({
        			"opacity" : 0
        			}, 400, function(){
      					i = index;
        		    	$("li", slider).eq(i).animate({
        		    		"opacity" : 1
        		    	}, 400, function() {
        		    		var slideText = $('li', slider).eq(i).find('img').attr('alt');
        		    		if (!slideText == '') {
        		    			$('p', sliderInfo).text(slideText);
        		    		} else {
        		    			$('p', sliderInfo).text('No description available');
        		    		}
        		    	});
        		});
        		//Search all sliderThumbs li's and remove class of currentSlide then add class to clicked item.
        		sliderThumbs.find('li a.currentSlide').removeClass('currentSlide');
        		$(this).addClass('currentSlide');
        	}
			return false;
		});
		
		//Thumbnail carousel
		$(".thumbcontainer").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			scroll: 1,
			circular: false,
			visible: 6
		});
		
		$(".archives").jCarouselLite({
			btnNext: ".lfwNext",
			btnPrev: ".lfwPrev",
			scroll: 1,
			circular: false,
			visible: 4
		});
		
		//Show thumbnail controls if more than 6 slides on style finder
		var numThumbs = $('li', sliderThumbs).length;
		if (numThumbs > 6) {
			$(".prev, .next, #thumb_control").fadeIn();
		}
		
		//Show thumbnail controls if more than 4 slides on fashion week
		var archiveThumbs = $('li', '#showArchive').length;
		if (archiveThumbs > 4) {
			$(".lfwPrev, .lfwNext,", ".archives").fadeIn();
		}	
		
		//Show/Hide all thumbnails		
		sliderThumbs.height('90px').removeClass('invisible');
		thumbControl.bind('click',function() {
			if ($(this).hasClass('showall')){
				sliderThumbs.animate({
					height: 90
				}, function() {
					$(thumbControl).removeClass('showall');
					sliderThumbs.removeClass('full');
					$('.next, .prev').fadeIn();
				});
				
			} else {
				$('.next, .prev').hide();
				sliderThumbs.slideUp('slow', function(){
					$(thumbControl).addClass('showall');
					sliderThumbs.addClass('full');
					sliderThumbs.css({
						'height': (numThumbs % 6) * 100
					});
					sliderThumbs.slideDown();
				});
			} 
			return false;
		});
	};
}
