var coloroptionswinContent = "";
coloroptionswinContent += '<a class="close" href="#" onclick="closeLightbox();">CLOSE X</a>';
coloroptionswinContent += '<div class="toplightboxedge top485x"><div class="bottomlightboxedge bottom485x">';
coloroptionswinContent += '<div class="inside">';
coloroptionswinContent += '</div>';
coloroptionswinContent += '</div></div>';
var coloroptionswinW = 485; var coloroptionswinH = 80;
var coloroptionswinSrc = "coloroptions.php";
var coloroptionswinItemFindID = "colorgrid";
var coloroptionswinItemFindType = "div";
var coloroptionswinSrcParams = "";
var coloroptionswinRowHeight = 113;
var coloroptionswinElemsMax = 12;


var addtobagwinContent = "";
addtobagwinContent += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="95" height="105" id="addtobaganim" align="middle">';
addtobagwinContent += '<param name="allowScriptAccess" value="always" />';
addtobagwinContent += '<param name="movie" value="swf/addtobag.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="swf/addtobag.swf" quality="high" bgcolor="#ffffff" width="95" height="105" name="addtobaganim" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
addtobagwinContent += '</object>';
var addtobagwinW = 115; var addtobagwinH = 105;
var addtobagParams = "";


var mybagwinContent = "";
mybagwinContent += '<a class="close" href="#" onclick="closeLightbox();">CONTINUE SHOPPING X</a>';
mybagwinContent += '<div id="ledge" class="toplightboxedge top585x"><div id="redge" class="bottomlightboxedge bottom585x">';
mybagwinContent += '<div class="inside">';
mybagwinContent += '</div>';
mybagwinContent += '</div></div>';
var mybagwinW = 585; var mybagwinH = 216;
var mybagwinSrc = "mybag.php";
var mybagwinItemFindID = "bagtb";
var mybagwinItemFindType = "tr";
var mybagwinSrcParams = "";
var mybagwinElemsMax = 6;
var mybagwinRowHeight = 80;
var itemsInMyBag = 6;



/* ###################### EXTENDED OBJECT FUNCTIONALITY ####################*/
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,t) {
	   	element = $(element);
    	element.style.left = t +"px";
	},	
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});



/* ###################### LIGHTBOX FUNCTIONALITY ####################*/
var arrayPageSize = new Array();
var arrayPageScroll = new Array();
var objOverlay;
var objBody;
var objLightbox;
var insideBox;
var objLightboxTemp;


function mybag() {
	createLightboxContainer('mybagwin');
}
function coloroptions(prodID) {
	coloroptionswinSrcParams = "prodID="+prodID;
	createLightboxContainer('coloroptionswin');
}
function addtobag(prodID) {	
	addtobagwinParams = "prodID="+prodID;
	createLightboxContainer('addtobagwin');
}

function createLightboxContainer(boxName) {
	basicContent = eval(boxName + "Content");
	objBody = document.getElementsByTagName("body").item(0);
	if (!objOverlay && !objLightbox) {
		objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);
		
		objLightbox = document.createElement("div");		
		objLightbox.style.display = 'none';
		objLightbox.className = "lightbox";
		objBody.appendChild(objLightbox);
	}
	objLightbox.setAttribute('id',boxName);
	objLightbox.innerHTML = basicContent;

	activateOverlay(boxName);
}

function activateOverlay(boxName) {
	hideSelectBoxes();
	arrayPageSize = getPageSize();
	Element.setHeight('overlay', arrayPageSize[1]);
	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8, afterFinish: function() {
		lightboxPosition(objLightbox,boxName,eval(boxName+"W"),eval(boxName+"H"),true);
	} });
}

function lightboxLoading(boxName) {
	if (boxName != "addtobagwin") {
		insideBox = objLightbox.getElementsByTagName("div")[2];
		insideBox.innerHTML = "";
		Element.setHeight(insideBox, 100);
		insideBox.style.backgroundImage = "url(\"images/basicelements/ajaxrotation.gif\")";		
		insideBox.style.backgroundPosition = "center center";
		insideBox.style.backgroundRepeat = "no-repeat";
		new Effect.Appear(objLightbox, { duration: 0.3, fps: 28, from: 0.0, to: 1, afterFinish: function() {
			lightboxPopulate(boxName);
		}});
	} else {
		Element.show(objLightbox);
	}
}

function lightboxPosition(elem,boxName,wVar,hVar,contAction) {
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
	
	lightboxTop = Math.round((arrayPageScroll[1] + (arrayPageSize[3] / 2)));
	lightboxLeft = Math.round((arrayPageSize[2] / 2));
		lightboxLeft = (lightboxLeft-(wVar/2)) + 200;
		lightboxTop = (lightboxTop-(hVar/2));	
		if (lightboxTop <= 0) lightboxTop=0;
	Element.setLeft(elem, lightboxLeft);
	Element.setTop(elem, lightboxTop);	
	if (contAction) lightboxLoading(boxName);
}

function lightboxPopulate(boxName) {
	ajaxUrl = eval(boxName+"Src");
	ajaxParams = eval(boxName+"SrcParams");
	baseH = eval(boxName+"H");
	rowHeight = eval(boxName+"RowHeight");	

	// ajax to grab content
	var url = ajaxUrl;
	var pars = 'rand=' + Math.floor(Math.random() * 999);
		if (ajaxParams!='') pars += '&' + ajaxParams;
	var myAjax = new Ajax.Request( 
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: function(response) { resizePopulateLightbox(response,boxName,baseH,rowHeight); },
			onFailure: function() { insideBox.innerHTML = "Error receiving content. Please refresh"; }
		}
	);
}

function resizePopulateLightbox(response,boxName,baseH,rowHeight) {
	insideBox.style.backgroundImage = "none";
	
	itemFindID = eval(boxName+"ItemFindID");
	itemFindType = eval(boxName+"ItemFindType");
		
	numElems = lightboxTemporaryCont(response,itemFindID,itemFindType);
	
	//insideBox.innerHTML = response.responseText;
		if (numElems > eval(boxName+"ElemsMax")) numElems = eval(boxName+"ElemsMax");
		
	if (boxName == "coloroptionswin") {
		rws = Math.ceil(numElems/3);
		newH = baseH + (rws*rowHeight);		
	} else {
		newH = baseH + (numElems*rowHeight);
	}
	percH = 100*((newH-35)/insideBox.offsetHeight);
	lightboxPosition(objLightbox,boxName,eval(boxName+"W"),newH,false);
	
	new Effect.Scale(insideBox, percH, {scaleX: false, scaleY:true, scaleFromCenter: true, scaleContent: false, delay: .1, duration: .5, scaleMode: { originalHeight: insideBox.offsetHeight, originalWidth: insideBox.offsetWidth }, afterFinish: function() {
		insideBox.innerHTML = response.responseText;
	} } );
}

function lightboxTemporaryCont(response,contID,elemType) {
	objLightboxTemp = document.createElement("div");
	objLightboxTemp.setAttribute('id','lightboxtemp');
	objLightboxTemp.style.display = 'none';
	objLightbox.appendChild(objLightboxTemp);
	objLightboxTemp.innerHTML = response.responseText;	
	elemNum = document.getElementById(contID).getElementsByTagName(elemType).length;
	objLightbox.removeChild(objLightboxTemp);
	return elemNum;
}

function closeLightbox() {
	Element.hide(objLightbox);
	Element.hide(objOverlay);
	showSelectBoxes();
}


/* FLASH INTEGRATION FUNCTIONS */
var addtoBagObj;
function getAddToBagID(swfID) {
	if (navigator.appName.indexOf("Microsoft") > -1) {
		addtoBagObj = window[swfID];
	} else {
		addtoBagObj = document[swfID];
	}
}

function addToBagEnd(str) {
	closeLightbox();
}



/* ###################### MAIN AJAX FUNCTIONALITY ####################*/
function mainBoxContentSwitch(flName,loadingParam) {
	window.location = flName;
	/*
	flName = flName + "&ajax=true&";
	if (loadingParam == undefined || loadingParam == '') {
		loadingText = "loading...";
	} else {
		loadingText = loadingParam;
	}
	if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
		new Effect.Fade("mainContentContainer", { from: 1, to: .1, duration: .1, afterFinish: function() {
			document.getElementById("mainContentContainer").innerHTML = '<div class="mainLoading"><img src="images/ajaxrotation.gif" alt="" width="16" height="16" \/> ' + loadingText + '</div>';
			new Effect.Fade("mainContentContainer", { from: .1, to: 1, duration: 1.4, afterFinish: function() { 
				// ajax to grab content
				ajaxMainSwitch(flName);
			} } );
		} } );
	} else {
		// temporary safari fix due to known float: left and animation problems
		document.getElementById("mainContentContainer").innerHTML = '<div class="mainLoading"><img src="images/ajaxrotation.gif" alt="" width="16" height="16" \/> ' + loadingText + '</div>';
		setTimeout("ajaxMainSwitch('"+flName+"')", 450);
	}*/
}

function ajaxMainSwitch(flName) {
	var url = flName;
	var pars = 'rand=' + Math.floor(Math.random() * 999);
	var myAjax = new Ajax.Request( 
		url,
		{
			method: 'get',
			parameters: pars,
			onComplete: mainBoxContentLoad,
			onFailure: function() { document.getElementById("mainContentContainer").innerHTML = "Error receiving content. Please refresh"; }
		}
   );
}

function mainBoxContentLoad(response) {
	if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {	// safari fading animation bug
		new Effect.Fade("mainContentContainer", { from: 1, to: .1, duration: .35, afterFinish: function() { 
			document.getElementById("mainContentContainer").innerHTML = response.responseText;
			new Effect.Fade("mainContentContainer", { from: .1, to: 1, duration: .25, afterFinish: function() {
			} } );
		} } );		
	} else {
		document.getElementById("mainContentContainer").innerHTML = response.responseText;
		Element.show("mainContentContainer");
	}
}


/* ###################### SEARCH FUNCTIONALITY ####################*/
var qVal = "";
function searchSub(frmObj) {
	qVal = document.getElementById("q").value;
	if (qVal != 'search' && qVal != '') {
		mainBoxContentSwitch('searchexternal.html','searching for <b>'+qVal+'</b>');
	} else {
		searchError(frmObj,'on');
	}
}

var objSearchError;
function searchError(frmObj,status) {
	if (!objSearchError) {
		objBody = document.getElementsByTagName("body").item(0);

		objSearchError = document.createElement("div");
		objSearchError.setAttribute('id','searchError');
		objSearchError.style.display = 'none';
		objBody.appendChild(objSearchError);
		objSearchError.innerHTML = "Please enter a search term";

		q = document.getElementById("q");
		qWrap = document.getElementById("inputwrapper");

		Element.setTop(objSearchError,findPosY(q));
		Element.setLeft(objSearchError,findPosX(q) + Element.getWidth(qWrap) );
	}
	if (status=='on') {
		new Effect.Appear(objSearchError, { duration: .25, afterFinish: function() {
			setTimeout("searchError('"+frmObj+"','off')",850);
		}});
	} else {
		new Effect.Fade(objSearchError, { duration: .25, from: 1.0, to: 0 });
	}
}


/* ###################### PRODUCT DETAIL COLOR SWITCHER ####################*/
var defaultColor = ""; var defaultColorTxt = ""; var defaultItemTxt = ""; var defaultColorBox = "";
var shoecolorpath = "images/productdetails/";
function colorSwitchOn(elem,newSrc,colortxt) {
	if (defaultColor=="") {
		defaultColor = document.getElementById("shoemedium").src;
			defaultColor = defaultColor.substring(defaultColor.lastIndexOf('/')+1,defaultColor.length-4);
		defaultColorTxt = document.getElementById("colortxt").innerHTML;
		defaultItemTxt = document.getElementById("itemID").innerHTML;
		defaultColorBox = "boxcolor1";
	}	
	document.getElementById("shoemedium").src = shoecolorpath + newSrc + ".jpg";
	document.getElementById("colortxt").innerHTML = colortxt;
	document.getElementById("itemID").innerHTML = newSrc;
	
}
function colorSwitchOff() {
	document.getElementById("shoemedium").src = shoecolorpath + defaultColor + ".jpg";
	document.getElementById("colortxt").innerHTML = defaultColorTxt;
	document.getElementById("itemID").innerHTML = defaultItemTxt;
}
function colorSwitch(elem,newSrc,colortxt) {
	document.getElementById(defaultColorBox).className = "";
	document.getElementById("shoemedium").src = shoecolorpath + newSrc + ".jpg";
	document.getElementById("colortxt").innerHTML = colortxt;
	document.getElementById("itemID").innerHTML = newSrc;
	document.getElementById(elem.id).className = "focused";
	defaultColor = newSrc;
	defaultColorBox = elem.id
	defaultColorTxt = colortxt;
	defaultItemTxt = newSrc;	
	document.getElementById("chosencolor").value = newSrc;
}


/* ###################### MY BAG ####################*/
/* MY BAG FUNCTIONALITY */
function removeFromBag(prodID) {
	// ajax functionality to remove from the bag
	/*
	var url = '';
	var pars = '';
	var myAjax = new Ajax.Request( 
		url,
		{ 
			method: 'get',
			parameters: pars,
			onComplete: function() { removeBagRow(prodID); },
			onFailure: function() { alert("Could Not Clear From Tracker. Please Refresh and Try Again"); }
		}
	);	
	*/
	removeBagRow(prodID);
}

function removeBagRow(blockID) {
	element = blockID.parentNode.parentNode;
	new Effect.Parallel(
		[ new Effect.BlindUp(element,
			{ sync: true }), 
		new Effect.Opacity(element, 
			{ sync: true, to: 0.0, from: 1.0 } ) ],
		{ duration: 0.3, 
			afterFinish: function(effect)
				{ 
					blockID.parentNode.parentNode.parentNode.removeChild(blockID.parentNode.parentNode);
					itemsInMyBag--;
					removeItemResizeLightbox();					
				} 
		}
	);
}

function removeItemResizeLightbox() {
	lObj = insideBox;
	bagObj = document.getElementById("bagtb");
	ledge = document.getElementById("ledge");
	redge = document.getElementById("redge");	
	ogHeight = lObj.offsetHeight;
	if (itemsInMyBag < 6) {
		bagsperc = 100 * ((bagObj.offsetHeight-77) / bagObj.offsetHeight);
		lsperc = 100 * ((lObj.offsetHeight-67) / lObj.offsetHeight);
		
		new Effect.Parallel(
			[ new Effect.Scale(bagObj, bagsperc,
				{ sync: true, scaleX: false, scaleY:true, scaleFromCenter: false, scaleContent: false, scaleMode: { originalHeight: bagObj.offsetHeight, originalWidth: 465 } }), 
			new Effect.Scale(lObj, lsperc,
				{ sync: true, scaleX: false, scaleY:true, scaleFromCenter: false, scaleContent: false, scaleMode: { originalHeight: lObj.offsetHeight, originalWidth: 465 } } ) ],
			{ duration: 0.3	}
		);
	}
}


function bagUpdateQty(qID) {
/*
	rID = qID.parentNode.parentNode.id;
	oldQty = document.getElementById("oldqty"+rID).value;
	newQty = document.getElementById("qty"+rID).value;
	pPrice = document.getElementById("price_"+rID).value;
	var pminus = false;
	qtyDiff = 0;
	if (newQty < oldQty) {
		pminus = true;
		qtyDiff = oldQty - newQty;
	}

	currentSubTotal = document.getElementById("subtotalPrice");

	if (pminus) {
		currentSubTotal.innerHTML = parseInt(currentSubTotal.innerHTML) - (qtyDiff*pPrice);
	} else {
		currentSubTotal.innerHTML = parseInt(currentSubTotal.innerHTML) + (qtyDiff*pPrice);
	}
	document.getElementById("oldqty"+rID).value = newQty;
*/
}


/* ###################### EXTRA PROTOTYPES AND FUNCTIONS ####################*/


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}





/* ONLOAD FUNCTIONALITY */
window.onLoad = function() {
	getAddToBagID('addtobaganim');
}
