$(document).ready(function() {

	/* main menu */
	$('ul.topLevel').superfish({
    	autoArrows : false,
    	dropShadows : false, // we'll manage this in CSS
		speed : 0
	});

	/* clients carousel */
	if ($('#FeaturedClients').length > 0) {	
	
		if ($('#FeaturedClients').parents('.home').length > 0) {
			$('#FeaturedClients').jcarousel({
				wrap : 'circular'
		    });		
		}
		else {
			$('#FeaturedClients').jcarousel({
				wrap : 'circular',
				itemFallbackDimension : 104 // items are hidden on load, so make sure this is set to the item width to avoid any issues.
		    });		
		}
    }

	/* cycle */
	if ($('#Feature ul').length > 0) {	
		$('#Feature ul').cycle({ 
			fx : 'fade', 
			speed : 500, 
			timeout : 8000,
			pause : true,
			next : '#Feature .btnNext', 
			prev : '#Feature .btnPrevious'
		});
	}
	
	if ($('#FastFact ul').length > 0) {
		$('#FastFact ul').cycle({ 
			fx : 'scrollRight', 
			speed : 500,
			timeout : 4000
		});	
	}
	
	/* client widget */
	if ($('.clientGallery').length > 0) {
	
		$.each($('.clientGallery'), function(){

			var $gallery = $(this),
				$pages = $gallery.find('.page'),
				total = $gallery.find('.logoList .logo').length;

			$gallery.find('.logoList ol').cycle({ 
				fx : 'fade', 
				speed : 500, 
				timeout : 0,
				next : $gallery.find('.next'),
				prev : $gallery.find('.previous'),
				after : function(previous, current) {
					
					// Get all slides
					var idx = $pages.index(current),
						page = idx + 1,
						start = 1,
						end = 6;
						
					if (page > 1) {
						start = ((page - 1) * end) + 1;
						end += start - 1;
					}
					
					if (end > total) {
						end = total;
					}					
					
					$gallery.find('.start').html(start);
					$gallery.find('.end').html(end);
				}
			});
		});	
	}
	
	$('.clientGallery .logoList .logo a').live('click', function(){
	
		var $client = $(this).parents('li'),
			$parent = $client.parents('.clientGallery'), // Get the parent
			$list = $parent.find('.logoList .logo'),
			$overview = $parent.find('.overview'),
			clickedIdx = $list.index($client); // Get the index of the clicked element

		// Update active class
		$list.filter('.chl2_active').removeClass('chl2_active');
		$client.addClass('chl2_active');

		$overview.find('li').stop(true, true); // Stop animation queue build up

		$overview.find('li:visible').fadeOut(300, function() {
			// Show the profile
			$overview.find('li:eq(' + clickedIdx + ')').fadeIn(300);
		
		});

		return false;
	});
	
	/* feature */
	
	$('#Feature .banner').bind('mouseenter', function(e){

		var $banner = $(this);

		$('#Feature img').stop(true, true);
		$('#Feature .controls').stop(true, true);

		$banner.find('.controls').show();
	});
	
	$('#Feature .banner').bind('mouseleave', function(e){
	
		var $banner = $(this);	
	
		$('#Feature img').stop(true, true);
		$('#Feature .controls').stop(true, true);
		
		// $banner.find('img').fadeTo(200, 1.0, function(){
			$banner.find('.controls').hide();		
		// });
		
	});
	
	toggleDefaultText($('#Search input'));

	/* expandable */
	if ($('.expandable').length > 0) {
	
		$.each($('.expandable .item'), function() {
			
			var item = $(this);
			
			if (!item.hasClass('open')) {
				
				item.find('.text').hide();
			}
		});
		
		$('.expandable .title').click(function() {
			toggleExpandable($(this));
		});	
	}
	
	/* select boxes */
	
	$('.uLogIn select').pseudoSelect();
	
	$('.uLogIn select').bind('change', function(){

		window.location.href = $(this).val();

	});	
	
	$('#SolutionFinder select').pseudoSelect();
	
	$('#SolutionFinder select').bind('change', function(){

		window.location.href = $(this).val();

	});
	
	$('.tagSearch select').pseudoSelect();
	
	/* sidebar navigation */
	
	manageSidebarNavigation();
	
	/* success story */
	
	// The client wants the image to be vertically centered. Unfortunately we can't do this with CSS because the image height and the text height are unkown.

	if ($('.home .caseStudy .logo img').length > 0) {
		
		var textH = $('.home .caseStudy .text').height(),
			imgH = $('.home .caseStudy .logo img').height();
				
		if (textH > imgH) {
			var difference = textH - imgH,
				offset = parseInt(difference / 2);
				
			$('.home .caseStudy .logo').css({'margin-top' : offset});
		}

		$('.home .caseStudy .logo').css({'visibility' : 'visible'});
	}
	
	/* tooltips */
	
	$('a').tooltip({
	    track: true, 
	    delay: 400, 
	    showURL: false,
	    top: -30,
	    left: -10,
		positionLeft: true
	});
	
	$('#submit_search').tooltip({
	    track: true, 
	    delay: 400, 
	    showURL: false,
	    top: -30,
	    left: -10,
		positionLeft: true
	});
});

function toggleExpandable(clicked) {

	var item = clicked.parents('.item');

    if (item != '#') {
        if ($(item).length > 0) {
            $(item).children('.text').animate({ height : 'toggle'});
            $(item).toggleClass('open');
        }
    }
}

function toggleDefaultText(input) {
	input.bind({
		'focus' : function() {
	        if (this.value == this.defaultValue) {
	        	this.value = '';
	    	}
	        if (this.value != this.defaultValue) {
		    	this.select();
	        }
		},
		'blur' : function() {
	        if (this.value == ''){
	        	this.value = (this.defaultValue ? this.defaultValue : '');
	    	}				
		}
	});
}

function manageSidebarNavigation() {
	
	$('#SubNavigation .open').show();
	
	// Hide children
	$('#SubNavigation .open').removeClass('open').addClass('closed').find('span').html('closed');
	
	var active = $('#SubNavigation .chl2_active'),
		children = active.children('ul'),
		top = $([]);

	if (active.hasClass('top')) {
		top = active;
	}
	else {
		top = active.parents('.top');
	}
	
	if (active.hasClass('top')) {
	
		if (children.length > 0) {
			children.show();
			active.find('.closed:first').removeClass('closed').addClass('open').find('span').html('open');
		}
	}
	else if (active.hasClass('second')) {
		top.find('ul').show();
		active.find('.closed').removeClass('closed').addClass('open').find('span').html('open');
		active.parents('.top').find('.closed:first').removeClass('closed').addClass('open').find('span').html('open');
		active.siblings('li').find('ul').hide();
	}
	else {
		top.find('ul').show();
		active.parents('.top').find('.closed:first').removeClass('closed').addClass('open').find('span').html('open');
		active.parents('.second').find('.closed:first').removeClass('closed').addClass('open').find('span').html('open');
		active.parents('.second').siblings('li').find('ul').hide();
	}
	
	$.each($('#SubNavigation .top'), function(){
		
		if ($(this).find('li').length == 0) {
		
			$(this).addClass('noChildren').find('span').remove();
		
		}
	}); 
}
