$(function() {
	
	// Override default parameters onload
	$.fn.hoverscroll.params = $.extend($.fn.hoverscroll.params, {
		vertical : false,
		width: '100%',
		height: 400,
		arrows: false
	});

	
	// Generate hoverscroll with overridden default parameters
	$('#myscroll').livequery(function() {$('#myscroll').hoverscroll();});
	
	// Regenerate hoverscroll on window resize /////////////////
	$(window).resize(function() {
		
		if(currentPage == "agency-employment") {
			$('#content').load("agency-employment/agency-employment.php","sid="+Math.random()); // Load the employment footer
			initializeCarousel();
		}
		
		$('#myscroll').livequery(function() {$('#myscroll').hoverscroll();});
		
		if($('#myscroll').size()) { extendRightContainer(); }
		
		if($('#visuals-detail').size()) { moveRightArrow(); }
	});
	
	Shadowbox.init({
		enableKeys: false,
		autoDimensions: true
	});

	// ############# Visuals ###############################################################################################################//
	// Extend the width of the div container for the visuals hover scroll
	$('#visuals-right').livequery(function() {
		extendRightContainer();
	});
	
	function extendRightContainer() {
		var extraSpace = (($(window).width() - $('#content').width()) / 2);	//This is the left and right extra space on the browser
		var newRightContainerWidth = (944 - 250) + extraSpace; //The new width of the right hand side container
		$('#visuals-right').css('width',newRightContainerWidth); //Set the width 	
	}
	
	// Place the arrow for results against teh right hand side of window for individuals visuals
	$('#visuals-detail').livequery(function() {
		moveRightArrow();
	});
	
	function moveRightArrow() {
		var extraSpace = ((($(window).width() - $('#content').width()) / 2)+1);	//Extra space on the browser +1px in case of odd number size
		$('#arrow-right-portfolio').css('right',-extraSpace); //Set the new right arrow position	
	}
	
	
	// Rollover states for the site
	$('#follow-newsletters, #follow-twitter, #follow-wordpress, #follow-flickr, #doors-expand, #doors-generate, #doors-improve, #doors-create, #doors-increase, #doors-measure, #doors-integrate').livequery(function(){ 
        var rolloverID = $(this).get(0).id + "-text";
		$(this).hover(function() { $('#'+rolloverID).css('display','block'); }, function() { $('#'+rolloverID).css('display','none'); }); 
    }, function() { 
        $(this).unbind('mouseover').unbind('mouseout');  // unbind the mouseover and mouseout events 
    });
    
    // Rollover states for the boxes on each door page
	$('.box3-top-visibility, .box3-top-interaction, .box2-mid-clientstory, .box2-mid-interaction, .box2-mid-conversion, .box2-mid-visibility, .box2-mid-visuals, .box-bot-clientstory, .box-bot-thinking').livequery(function(){ 
        var rolloverClass = $(this).attr('class').split(' ').slice(-1) + "-text";
		$(this).hover(function() { $('.'+rolloverClass).css('display','block'); }, function() { $('.'+rolloverClass).css('display','none'); }); 
    }, function() { 
        $(this).unbind('mouseover').unbind('mouseout');  // unbind the mouseover and mouseout events 
    }); 
	
	$('#newsSectionSitemap').livequery(function() {
		$('#newsSectionSitemap').css('display','none');
	});
	
	$('#agency-start').livequery(function() {
		$(this).children('li').hover(function(){
			$(this).siblings().children('a').addClass('inactive');
			$(this).children('a').addClass('active');
		}, function(){
			$(this).siblings().children('a').removeClass('inactive');
			$(this).children('a').removeClass('active');
		});
	});
	
	$('#results-choices').livequery(function() {
		$(this).children('li').hover(function(){
			$(this).siblings().children('a').addClass('inactive');
			$(this).children('a').addClass('active');
		}, function(){
			$(this).siblings().children('a').removeClass('inactive');
			$(this).children('a').removeClass('active');
		});
	});

	$('.capalink').livequery(function() {
		$(this).hover(function(){
			$(this).addClass('currentCapaLink');
			$('.capalink:not(.currentCapaLink)').addClass('dimmedCapaLink');
		}, function(){
			$('.capalink').removeClass('dimmedCapaLink currentCapaLink');
		});
	});
	
	$("#polaroidLiteCarouselContainer").livequery(function() {
		setTimeout(function() {initializeCarousel();},1000);
	});
	
	function initializeCarousel() {
		var imageWidth = 97;
		var imagePadding = 8;
		var numberOfImagesOnScreen = Math.floor($('#polaroids').width()/(imageWidth + (imagePadding*2)));
		
		$("#polaroidLiteCarouselContainer").jCarouselLite({
			btnNext: "#arrow-right",
			btnPrev: "#arrow-left",
			visible: numberOfImagesOnScreen
		});
	}
	
	$('#article').livequery(function() {
		$('#article').jScrollPane({setSpecificWidth: 941, showArrows:true, scrollbarWidth: 15, arrowSize: 16});
	});
	
	$('#newsbank').livequery(function() {
		$('#newsbank').jScrollPane({setSpecificHeight: 312, setSpecificWidth: 358, wheelSpeed: 68, showArrows:true, scrollbarWidth: 15, arrowSize: 16});
	});
	//visualsSavedForHistory
	
	//Pre load an image 
	$.preLoaderArray = function() {
		for (var i = 0; i<arguments.length; i++) {
			img = new Image();
			img.src = 'img/layout/'+arguments[i];
		}
	}
	$.preLoaderArray("avatar-menu-open.png","bucket-open-bg.png","bucket-open-middle-bg.png","follow-newsletters.png","bucket-open-middle-bg.png","bucket-open-bg.png","bucket-open-middle-bg.png","door-01.png","door-02.png","door-03.png","door-04.png","door-05.png","door-06.png","door-07.png","capabilities.png");

	
	/*for ( var i=0; i<preloaderLen; ++i ){
		preLoaderImageLoadedArray[i] = $('<img />').attr('src', 'img/layout/'+preLoaderArray[i]);
		if(i == (preloaderLen-1)) {
			alert(preLoaderImageLoadedArray[i].width());
		}
	}*/
	
});

function limitCharCountTextArea(element,count) {
	if(element.value.length > count) {
		var string = element.value;
		element.value = string.substr(0,count);
		alert("Character limit of " + count + " reached!");
	}
}
