function viewportHeight() {
	var viewportheight;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerHeight != 'undefined'){ viewportheight = window.innerHeight; }
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0)
	{ viewportheight = document.documentElement.clientHeight; }
	// older versions of IE
	else {	viewportheight = document.getElementsByTagName('body')[0].clientHeight;	 }
	return viewportheight;
}
function viewportWidth() {
	var viewportwidth;
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined'){ viewportwidth = window.innerWidth; }
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{ viewportwidth = document.documentElement.clientWidth; }
	 // older versions of IE
	 else { viewportwidth = document.getElementsByTagName('body')[0].clientWidth; }
	 return viewportwidth;	
}

/* 
 	Messaging popups 
*/
function messageService(msgTitle,msgTxt,msgLen)
{
	// create msg containers
	var curDate = new Date();
	var msgID = "msg-"+curDate.getTime();		
	
	$("body").append("<div id=\""+msgID+"\" class=\"message-service\" style=\"display: none;\"></div>");
	$("#"+msgID).append("<div class=\"message-content\"><a href=\"#\" class=\"close\">close</a><div class=\"inner-msg clearfix\"></div></div>");
	
	// assign close button action
	$("#"+msgID+" .message-content a.close").click(function() {
		$("#"+msgID).remove();
		return false;
	});
	
	// write msg
	$("#"+msgID+" .message-content .inner-msg").html("<h3>"+msgTitle+"</h3><p>"+msgTxt+"</p>");
	
	// center the message box
	var msgHeight = $("#"+msgID).height();
	$("#"+msgID).css({
		position: "fixed",
		top: (viewportHeight()/2)-(msgHeight/2)
	});
	
	// animate
	$("#"+msgID).fadeTo("fast", 0, function() { $("#"+msgID).css("display","block"); });
	$("#"+msgID).fadeTo("slow", 1);
	//$("#"+msgID+" .message-content .inner-msg").animate({opacity: 1});
	
	// remove item
	if (parseInt(msgLen) != 0) {
		var temp = setTimeout("$('#"+msgID+"').remove()", msgLen);
	}
}

/*
 TEXT resizing slider
 */
function init_textSlider(){
	var START_SIZE = 12;
	var INITIAL_SIZE = 12;
				
	if ($.cookie('fontsize')){
		START_SIZE = $.cookie('fontsize');
	}
	var bods = document.getElementsByTagName('body');
	bods[0].style.fontSize = START_SIZE+"px";
				
	$('#textResizerSlider').slider({
		stepping: 1,
		min: 12,
		max: 15,
		startValue: 12,
		axis: 'horizontal',
		change: function(e,ui) {
			
			var myVal = $('#textResizerSlider').slider('value',0);
			var bods = document.getElementsByTagName('body');
			bods[0].style.fontSize = myVal+"px";
			
			$.cookie('fontsize', myVal, { path: '/'});
		} 
	});
			
	if ($.cookie('fontsize')){
		$('#textResizerSlider').slider("moveTo",START_SIZE,0);
	}
}

$(document).ready(function(){
	// init text-size slider
	init_textSlider();
	
	// primary nav drop downs
	$("#primaryNav-container ul#primaryNav li:has(ul)").mouseover(function() {
		$("select").css("visibility","hidden");
		$(this).addClass("over");
	});
	$("#primaryNav-container ul#primaryNav li:has(ul)").mouseout(function() {
		$("select").css("visibility","visible");
		$(this).removeClass("over");
	});
	// remove functions from children in top nav
	$("#primaryNav-container ul#primaryNav li ul li:has(ul)").unbind("mouseover");
	$("#primaryNav-container ul#primaryNav li ul li:has(ul)").unbind("mouseout");
});

	

/*
	AddThis Product Social-Network Sharing Widget
*/ 
addthis_pub  = 'HarrietCarter'; 
addthis_logo ='http://www.harrietcarter.com/resources/harrietCarter/images/layout/logo.jpg'; 
addthis_brand = 'Harriet Carter';
addthis_image ='http://www.harrietcarter.com/resources/harrietCarter/images/layout/logo.jpg'; 



/* Product rotator */

$(document).ready(function() {
	// global vars for rotator script
	var speed = 5000;
	var pauseTime = 1000000;
	var startpos = 0;
	var timer = 0;
	var rotatorCount = $("#side-container .vProduct-thumbnails-container .vProduct-thumbnails-productsContainer").length - 1;
	var containerWrap = '';
	var containerLen = 0;
	var counter = '';

/* 	for (rotatorIndex=0;rotatorIndex<=rotatorCount;rotatorIndex++) { */
	
		//Image Rotator on Homepage
		containerWrap = $("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer");
/* 		$(containerWrap).("div.row:first"). */
		containerLen = $("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row").length;
		counter = $('<ul class="co-nav co-nav-0"></ul>');
			//Check for Multiple Callouts
			if (containerLen > 1) {
				//Insert UL.co-nav Before Callout Div
				$(containerWrap).before(counter);
				//Populate UL.co-nav
				for (i=1;i<=containerLen;i++) {
					$(counter).append('<li><a class="rotator-index" href="#">' + i + '</a></li>');
				};
				/* $(counter).append('<li><a class="rotator-control" href="#" title="Pause the current image."></a></li>'); */
			};
			//Label First and Last Classes of UL.co-nav
			$("ul.co-nav-0 li:first").addClass("first");
			$("ul.co-nav-0 li:last").addClass("last");

		containerWrap = $("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer");
		containerLen = $("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row").length;
		counter = $('<ul class="co-nav co-nav-1"></ul>');
			//Check for Multiple Callouts
			if (containerLen > 1) {
				//Insert UL.co-nav Before Callout Div
				$(containerWrap).before(counter);
				//Populate UL.co-nav
				for (i=1;i<=containerLen;i++) {
					$(counter).append('<li><a class="rotator-index" href="#">' + i + '</a></li>');
				};
				/* $(counter).append('<li><a class="rotator-control" href="#" title="Pause the current image."></a></li>'); */
			};
			//Label First and Last Classes of UL.co-nav
			$("ul.co-nav-1 li:first").addClass("first");
			$("ul.co-nav-1 li:last").addClass("last");

/* 	}; */
	
	// init the buttons for the main callout switcher
	$("ul.co-nav-0 li a.rotator-index").mouseover(function() {
		var matchIndex = $("ul.co-nav-0 li a.rotator-index").index(this);
		
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row").hide();
		$("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row:eq("+matchIndex+")").show();
		
		// set active anchor
		$("ul.co-nav-0 li a").removeClass("active");
		$(this).addClass("active");
		/* $(".rotator-control").removeClass("rotator-play"); */
	});
	$("ul.co-nav-0 li a.rotator-index").mouseout(function() {
		var matchIndex = $("ul.co-nav-0 li a").index(this);
		//rotateCallouts(matchIndex);
		timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
	});

	$("ul.co-nav-1 li a.rotator-index").mouseover(function() {
		var matchIndex = $("ul.co-nav-1 li a.rotator-index").index(this);
		
		// kill timout
		clearTimeout(timer);
		
		// hide all, then show matching callout img
		$("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row").hide();
		$("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row:eq("+matchIndex+")").show();
		
		// set active anchor
		$("ul.co-nav-1 li a").removeClass("active");
		$(this).addClass("active");
		/* $(".rotator-control").removeClass("rotator-play"); */
	});
	$("ul.co-nav-1 li a.rotator-index").mouseout(function() {
		var matchIndex = $("ul.co-nav-1 li a").index(this);
		//rotateCallouts(matchIndex);
		timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
	});

	// play/pause on click
/*
	$(".rotator-control").click(function() {
		var buttonCurrent = $(".rotator-control").attr("class");
		if ( buttonCurrent == 'rotator-control' )
			{
				clearTimeout(timer);
				var matchIndex = $("ul.co-nav li").index($("ul.co-nav > li:has(a.active)"));
				timer = setTimeout("rotateKeepAlive("+matchIndex+")",pauseTime);
				$(".rotator-control").addClass("rotator-play");
			}
		else
			{
				clearTimeout(timer);
				var matchIndex = $("ul.co-nav li").index($("ul.co-nav > li:has(a.active)"));
				timer = setTimeout("rotateKeepAlive("+matchIndex+")",speed);
				$(".rotator-control").removeClass("rotator-play");
			};
		return false;
	});
*/

	$("ul.co-nav li a").click(function() {
		return false;
	});
	
	// rotater function
	rotateCallouts = function(current)
	{
		var containerLen = 0;
		var numLen = 0;
		var newPos = current + 1;

		containerLen = $("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row").length;
		numLen = $("ul.co-nav-0 li a").length;
		newPos = current + 1;
		
		// hide all
		$("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row").hide();
		$("ul.co-nav-0 li a").removeClass("active");
		//alert(numLen);

		// rotate		
		if( newPos == containerLen || newPos == numLen )
		{
			//alert("restarted. current: "+current+" | newPos: "+newPos+" | img-num: "+containerLen+numLen)
			newPos = startpos;
			$("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row:eq(0)").show();
			$("ul.co-nav-0 li:eq(0) a").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$("#side-container .vProduct-thumbnails-container:eq(0) .vProduct-thumbnails-productsContainer div.row:eq("+newPos+")").show();
			$("ul.co-nav-0 li:eq("+newPos+") a").addClass("active");
		}


		containerLen = $("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row").length;
		numLen = $("ul.co-nav-1 li a").length;
		newPos = current + 1;
		
		// hide all
		$("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row").hide();
		$("ul.co-nav-1 li a").removeClass("active");
		//alert(numLen);

		// rotate		
		if( newPos == containerLen || newPos == numLen )
		{
			//alert("restarted. current: "+current+" | newPos: "+newPos+" | img-num: "+containerLen+numLen)
			newPos = startpos;
			$("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row:eq(0)").show();
			$("ul.co-nav-1 li:eq(0) a").addClass("active");
		}
		else
		{
			//alert("continuing: "+newPos);
			$("#side-container .vProduct-thumbnails-container:eq(1) .vProduct-thumbnails-productsContainer div.row:eq("+newPos+")").show();
			$("ul.co-nav-1 li:eq("+newPos+") a").addClass("active");
		}


		timer = setTimeout("rotateKeepAlive("+newPos+")",speed);
	}
	
	rotateKeepAlive = function(lastPos)
	{
		// kill timout
		clearTimeout(timer);
		
		// restart
		rotateCallouts(lastPos);
	}
	
	// init the rotator
	rotateCallouts(startpos - 1);
	
});
