/*

 * imageFunctions.js - miscellaneous Macromedia functions to handle images

 */



function MM_findObj(n, d) { //v3.0

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;

}



function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}



function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments;

	document.MM_sr=new Array;

	for(i=0;i<(a.length-2);i+=4)

		if ((x=MM_findObj(a[i]))!=null){

			document.MM_sr[j++]=x;

			if(!x.oSrc) x.oSrc=x.src;

			x.src=a[i+2];

			if (a[i+3]) x.alt=a[i+3];

		}

}



function createObject(FinishID, DFAImage, BeautyImage, FinishImage, FinishName, InTab,isNew){

	this.FinishID = FinishID;

	this.DFAImage = DFAImage;

	this.BeautyImage = BeautyImage;

	this.FinishImage = FinishImage;

	this.FinishName = FinishName;

	this.InTab = InTab;

	this.isNew = isNew;

}



function SetFinish(finish) {

	//window.scrollTo(0, 120);

	var myTitle = myBrandDoorStyleName + " " + mySpeciesName + " " + aFinish[finish].FinishName;

	if(aFinish[finish].isNew &&	document.getElementById("shownew") != null){
		document.getElementById("shownew").style.display = "block";
	}
	else if(document.getElementById("shownew") != null){
		document.getElementById("shownew").style.display = "none";
	}


	var myPattern = new RegExp("beauty\/","i");

	var myBeautyURL = new String(aFinish[finish].BeautyImage);

	var myBeautyURLLarge = myBeautyURL.replace(myPattern, "beauty/large/");


	MM_swapImage('beauty','',myBeautyURL);

	if (document.getElementById("bshotPhotoZoom")) {

		document.getElementById("bshotPhotoZoom").href = myBeautyURLLarge;

		document.getElementById("bshotPhotoZoom").title = myTitle;

	}


	var myPattern = new RegExp("dfas\/","i");

	var myDfaURL = new String(aFinish[finish].DFAImage);

	var myDfaURLMed = myDfaURL.replace(myPattern, "dfas/medium/");

	var myDfaURLLarge = myDfaURL.replace(myPattern, "dfas/large/");


	MM_swapImage('dfa','',myDfaURLMed);

	if (document.getElementById("dfaPhotoZoom")) {

		document.getElementById("dfaPhotoZoom").href = myDfaURLLarge;

		document.getElementById("dfaPhotoZoom").title = myTitle;

	}


	MM_swapImage('finish','',aFinish[finish].FinishImage,aFinish[finish].FinishName);

	// set id of folio form

	if (document.forms['Folio.ItemAdd'])

		if (document.forms['Folio.ItemAdd'].elements['Folio.ItemAdd.data_item_id'])

			document.forms['Folio.ItemAdd'].elements['Folio.ItemAdd.data_item_id'].value = aFinish[finish].FinishID;

	// set javascript variable to keep track of current finish

	currentFinish = finish;

	//Call function to set finishswatch class as selected

	//Function call goes here

	if (currentFinishNode == null) {

		currentFinishNode = document.getElementById("finish"+finish);

		currentFinishNode.className = "selectedFinish";

	} else {

		currentFinishNode.className = "";

		currentFinishNode = document.getElementById("finish"+finish);

		currentFinishNode.className = "selectedFinish";

	}

	// set save/remove state of current finish
	if (aFinish[currentFinish].InTab == true) {
		if (document.getElementById("Folio.ItemAdd"))
		{
			var tempImg = document.createElement("IMG");
			tempImg.src = "/images/icon-remove.gif";
			var temp = document.getElementById("Folio.ItemAdd");
			temp.className = 'removelink';
			temp.firstChild.src = tempImg.src;
			temp.replaceChild(document.createTextNode('Remove'), temp.childNodes[1]);
		}
	} else {
		if (document.getElementById("Folio.ItemAdd"))
		{
			var tempImg = document.createElement("IMG");
			tempImg.src = "/images/icon-save.gif";
			var temp = document.getElementById("Folio.ItemAdd");
			temp.className = 'savelink';
			temp.firstChild.src = tempImg.src;
			temp.replaceChild(document.createTextNode('Save'), temp.childNodes[1]);
		}
	}
	
	// MER5114 - product Rating
	showTop10Div(aFinish[currentFinish].FinishID);

	swapTxt("fnsh","getCurrent");
}

// MER5114 - product Rating
function showTop10Div(prodId){
    var divPrefix = "top10DoorFinish_";   

    for(i=1; i<aFinish.length; i++){
        div = $(divPrefix + aFinish[i].FinishID);
        aFinish[i].FinishID==prodId ? div.style.display = "block" : div.style.display = "none";
    }
    
//    aFinish.each(
//        function(finish){
//            alert(finish);
//        }
//     );

}

function getCurrentFinishName() {

	//Retrieve current finish name from array

	var sFinishName = aFinish[currentFinish].FinishName;

	return sFinishName;

}

//Increment/Decrement the counter for saved items on community pages
function updateCount(productType, productId, idName) {
    var sURL = "/communityselections/topSavedCount.aspx?productType=" + productType
                                                    + "&productId=" + productId;
		                             
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        httpReq = new XMLHttpRequest();
        httpReq.open("GET", sURL, false);
        httpReq.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        httpReq = new ActiveXObject("Microsoft.XMLHTTP");
        if (httpReq) {
            httpReq.open("GET", sURL, false);
            httpReq.send();
        }
    }
    
    var response = httpReq.responseText;
    
    if (!isNaN(response)) {
        document.getElementById(idName).innerHTML = response;
    }
    
    return; 
}