/* Initialize any JQuery Accordions */
$(document).ready(function() { $(".menu").accordion({ collapsible: true, header: "dt", autoHeight: false, active: ".open"});});

var preloadimage = new Array();
function preloadImages(){
	for (x=0; x<preloadImages.arguments.length; x++){
		preloadimage[x] = new Image();
		preloadimage[x].src = preloadImages.arguments[x];
	}
}

/*
	Preload images when this script runs.
*/
preloadImages(
	"http://c0952732.cdn.cloudfiles.rackspacecloud.com/images/common/menu/menutab90.png",
	"http://c0952732.cdn.cloudfiles.rackspacecloud.com/images/common/menu/menutab100.png",
	"http://c0952732.cdn.cloudfiles.rackspacecloud.com/images/common/menu/menutab105.png",					
	"http://c0952732.cdn.cloudfiles.rackspacecloud.com/images/common/menu/menutab120.png"
);

function openProductDetail(productId){

	var height = 660;
	var width = 600;
	var left = Math.floor( (screen.width - width) / 2);
  	var top = Math.floor( (screen.height - height) / 2);
	 
	var detailWindow = window.open('','ProductImageLarge', 'top=' + top + ',left=' + left + ',width=' + width + ',height=' + height + ',status=no,menubar=no,location=no,toolbar=no,directories=no,scrollbars=no');
	
	detailWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">');
	detailWindow.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>DoulaShop.com: Product Image</title>');
	detailWindow.document.write('<link href="http://c0952732.cdn.cloudfiles.rackspacecloud.com/style/main.css" rel="stylesheet" type="text/css"></head>');
	detailWindow.document.write('<body class="enlargedimage"><img src="http://c0952732.cdn.cloudfiles.rackspacecloud.com/images/products/large/');
	detailWindow.document.write(productId);
	detailWindow.document.write('.jpg" width="550" height="600" alt=""><p><a href="javascript:self.close()">Close This Window</a></p></body></html>');
	detailWindow.document.close();
	
	detailWindow.focus();
}

function openPopup(height, width, url){

	var left = Math.floor( (screen.width - width) / 2);
  	var top = Math.floor( (screen.height - height) / 2);
	 
	var detailWindow = window.open(url, '', 'top=' + top + ',left=' + left + ',width=' + width + ',height=' + height +',status=no,menubar=no,location=no,toolbar=no,directories=no,scrollbars=no');
	
	detailWindow.focus();
	
}

function toggleDiv( layerId ) {
	
	var style;
	
	if ( document.getElementById( layerId ) ){
		style = document.getElementById( layerId ).style;
	} else if ( document.all ) {
		style = document.all[ layerId ].style;
	} else if (document.layers) {
		style = document.layers[ layerId ].style;
	}
	
	style.display = style.display ? "":"block";
}

function showDiv( layerId ) {

	var style;
	
	if ( document.getElementById( layerId ) ){
		style = document.getElementById( layerId ).style;
	} else if ( document.all ) {
		style = document.all[ layerId ].style;			
	} else if (document.layers) {
		style = document.layers[ layerId ].style;
	}
	
	style.display = "block";
}

function setSwatchView(srcUrl){
	if (document.images.swatchview){
		document.images.swatchview.src = srcUrl;
	}
}

function setSwatchViewFromThumbSwatch(thumbElement){

	if ( !thumbElement || !thumbElement.src){ return; }
	
	var thumbSrc = thumbElement.src;
	var imgSrc = thumbElement.src.replace ('-thumb','');
	
	if (document.images.swatchview){
		document.images.swatchview.src = imgSrc;
	}
}

function showBigSwatches() {
	showDiv('bigswatches');
}

function setBigSwatch( url ) {
	if (document.images.bigswatch){
		document.images.bigswatch.src = url;
	}
}

function runSlideShow() {
	if (document.all) {
		document.images.SlideShow.style.filter="blendTrans(duration=2)";
		document.images.SlideShow.filters.blendTrans.Apply();
	}
	document.images.SlideShow.src = preLoad[j].src;
	if (document.all) {
		document.images.SlideShow.filters.blendTrans.Play();
	}
	
	j = j + 1;
	if (j > (p - 1)) j = 0;
	setTimeout('runSlideShow()', 5000);
}


function pause(millis) {
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); } while(curDate-date < millis);
} 

function changeToBeta(theElement){
	
	var beginningText = theElement.href.substring(0,11);
	
	if ( beginningText == "http://www." ){
		//theElement.href = "http://beta." + theElement.href.substring(11);
	}
}

function showCheapShipping(weight){
	if (weight < 1){ toggleDiv('cheapshipping'); }
}

function showUPC(upcText){
	if (upcText){ toggleDiv('upc'); }
}

function putSpanAroundCentsOld(priceSpan){
	
	var priceText = priceSpan.innerText + "";
	
	var dotspot = priceText.indexOf(".");
	
	if (dotspot == -1){ return; }
	
	priceSpan.innerText = priceText.substring(0,dotspot);
	
	spannode = document.createElement("span"); 
	spannode.setAttribute("class", "cents");
	spannode.innerText = priceText.substring(dotspot,dotspot + 3);
	
	priceSpan.appendChild( spannode );
}

function putSupAroundCents(priceSpan){
	
	var priceText = priceSpan.innerText + "";
	
	var dotspot = priceText.indexOf(".");
	
	if (dotspot == -1){ return; }
	
	priceSpan.innerText = priceText.substring(0,dotspot);
	
	spannode = document.createElement("sup");
	spannode.innerText = priceText.substring(dotspot,dotspot + 3);
	
	priceSpan.appendChild( spannode );
}

function putSpanAroundCents(priceSpan, hideDoubleZeros){
	
	var priceText = priceSpan.innerText + "";
	
	var dotspot = priceText.indexOf(".");
	
	if (dotspot == -1){ return; }
	
	if ( hideDoubleZeros ){
		
		priceSpan.innerText = priceText.substring(0,dotspot);
	
	} else {
		
		priceSpan.innerText = priceText.substring(0,dotspot);
		
		spannode = document.createElement("span"); 
		spannode.setAttribute("class", "cents");
		spannode.innerText = priceText.substring(dotspot,dotspot + 3);
		
		priceSpan.appendChild( spannode );
	}
}

function getCents( value ){
	
	value = Math.round( value*100 )/100;
	
	if ( value == Math.floor(value) ) {
		return value + '.00';
	} else {
		if ( value * 10 == Math.floor(value * 10) ){
			return value + '0';
		} else {
			return value ;
		}
	}	
}

function setYouSave( basePrice, salePrice ){
	
	if (basePrice.length < 1) { return; }
	if (isNaN(basePrice) || isNaN(salePrice)) { return; }

	basePrice -= 0;
	salePrice -= 0;
	
	var discount = basePrice - salePrice;
	
	var discountPercent = discount / basePrice * 100;
		
	var youSaveAmountDiv = document.getElementById('youSaveAmount');
	
	if (youSaveAmountDiv){
		youSaveAmountDiv.innerHTML = "<span class=\"price\">$" + getCents(discount) + " (" + Math.round(discountPercent) + "%)</span>";
	}
}

function setStockStatus( yahooText ){
	
	if ((yahooText == "Usually ships the same business day")||(yahooText == "Usually ships the next business day")){	
		$("#stockDisplayText").append( "<div class=\"shipstoday\"><div class=\"ui-icon ui-icon-check\"></div> In Stock, Ships Today.</div>");
	} else {
		$("#stockDisplayText").append( "<div class=\"note\"> <label>Stock Status:</label>&nbsp;" + yahooText + "</div>" );
	}
}

var embeddedPlayer = null;
var enlargedPlayer = null;
 
function onTemplateLoaded(experienceID) {
 
  	var bcExp = brightcove.getExperience(experienceID);
 
 	if (experienceID == 'embeddedPlayer'){
    	embeddedPlayer = bcExp.getModule(APIModules.VIDEO_PLAYER);
	}
	
	if (experienceID == 'enlargedPlayer'){
    	enlargedPlayer = bcExp.getModule(APIModules.VIDEO_PLAYER);
		
		//make sure the correct video is set, each time it's loaded.
		if (currentVideoSelected){
			enlargedPlayer.loadVideo(currentVideoSelected);
		}
	}
}


var enlargedPlayerShowing = false;
var currentVideoSelected = null;

function initializeCurrentVideoSelected(id){
	currentVideoSelected = id;
}

function loadEnlargedVideo(){

	enlargedPlayerShowing = true;
	
	embeddedPlayer.pause();
	
	//try to set the player. Often fails because player is slow to load.
	if (enlargedPlayer && enlargedPlayer.isPlayerDefined() && currentVideoSelected){
		enlargedPlayer.loadVideo(currentVideoSelected);
	}
}

function unloadEnlargedVideo(){

	enlargedPlayerShowing = false;
	
	enlargedPlayer.pause();
}

function setVideo(assetId, currentElement){
	
	currentVideoSelected = assetId;
	
	// Set Embedded Video	
	embeddedPlayer.cueVideo(assetId);
	//try to set the player. Often fails because player is slow to load.
	if (enlargedPlayer && enlargedPlayer.isPlayerDefined()){
		enlargedPlayer.cueVideo(currentVideoSelected);
	}
	
	// Set Transcript
	var transcriptHTML = document.getElementById('transcript-' + assetId);
	var activeTranscriptDiv = document.getElementById('activetranscript');
	activeTranscriptDiv.innerHTML = '<legend>Transcript</legend>' + transcriptHTML.innerHTML;
	
	// If the enlarged video is open, set and start it. Else start the embedded player
	if (enlargedPlayerShowing){
		enlargedPlayer.play();
	} else {
		embeddedPlayer.play();
	}
	
	//remove all .selected
	$('.othervideo').removeClass('selected')
	
	//add selected to the current one
	$(currentElement).addClass("selected");
}


function showTranscript(){
	$("#activetranscript").slideToggle();
	//Alternate Method: $("#activetranscript").animate({"height": "toggle"}, { duration: 250 });
}


function displayCents ( value ){
	value = Math.round( value*100 )/100;
	return ( value == Math.floor( value )) ? value + '.00' : ( (value * 10 == Math.floor( value * 10 )) ? value + '0' : value);
}

function writeYouSave( basePrice, salePrice ){
	
	if (basePrice.length < 1) { return; }
	if (isNaN(basePrice) || isNaN(salePrice)) { return; }

	basePrice -= 0;
	salePrice -= 0;
	
	var discount = basePrice - salePrice;
	
	var discountPercent = discount / basePrice * 100;
	
	document.write("<span class=\"price\">$" + displayCents(discount) + " (" + Math.round(discountPercent) + "%)</span>");
}

function writeStockStatus( yahooText ){

	if ((yahooText == "Usually ships the same business day")||(yahooText == "Usually ships the next business day")){
		document.write("In Stock, Ships Today");
	} else {
		document.write(yahooText);
	}
}

function initializeBoldChat() {

	try {
		var pageViewer = _bcvm.getPageViewer("7746757330145273614"); pageViewer.setParameter("WebsiteDefID", "1955238913888664788");
		pageViewer.setParameter("ChatWindowDefID", "4279435195046390697");
		pageViewer.setParameter("ChatWidth", "640");
		pageViewer.setParameter("ChatHeight", "480");
		pageViewer.setParameter("CallWidth", "480");
		pageViewer.setParameter("CallHeight", "360");
		pageViewer.setParameter("CallWindowDefID", "3738474055364478798");
		pageViewer.setParameter("InvitationDefID", "7558489404639720602");
		pageViewer.setParameter("WindowScheme", "https");
		pageViewer.pageViewed();
	} catch(error) {}
}
