/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
Modified filesearchhover.js Feb 2007
*/

function gE(id){return document.getElementById(id);}


var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 300;	// maximum image size.


function gettrailobj(){return document.getElementById("trailimageid").style}
function gettrailobjnostyle(){return document.getElementById("trailimageid")}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}





function showtrail(itemNo){

	var itemData = catalogueData[itemNo];
    document.onmousemove = followmouse;
	
		
	// determine Promotion type
	var promoText = "";
    var webDisplayCode = itemData[fieldNames['WebDisplayCode']];
    if (webDisplayCode == "SP") {promoText = "ON SPECIAL"}
    else if (webDisplayCode == "NEW") {promoText = "NEW ARRIVAL"}
    else if (webDisplayCode == "FLOOR") {promoText = "FLOOR MODEL SALE"}
		
	
   // determine dimensions
    var dimensionString ="";
    if (itemData[fieldNames["W"]] != ""){
		dimensionString = '<div><span id="popupItemDimensions">';
        dimensionString += itemData[fieldNames["W"]] + "&quot;&nbsp;W&nbsp;&nbsp;";
    if (itemData[fieldNames["D"]] != ""){dimensionString += "x&nbsp;&nbsp;"+ itemData[fieldNames["D"]] + "&quot;&nbsp;D&nbsp;&nbsp;"}
    if (itemData[fieldNames["H"]] != ""){dimensionString += "x&nbsp;&nbsp;"+ itemData[fieldNames["H"]] + "&quot;&nbsp;H"}
	dimensionString +='</span></div>';		   
    }

	
	
	// determine price if on special
	var priceString = ""
	   
	    priceString ='<div id="popupItemPrice">'

    if ((webDisplayCode == "SP" || webDisplayCode == "FLOOR") && itemData[fieldNames['SpecialPrice']] != "") {
	    if (itemData[fieldNames['Retail']]!="") {
	        priceString += 'Reg. <del>'
		    priceString += formatCurrency(itemData[fieldNames['Retail']], true) + '</del>';
		}
		if (itemData[fieldNames['Retail']] != "" && itemData[fieldNames['SpecialPrice']] != "") {
		    priceString += ', '
		}
		if (itemData[fieldNames['SpecialPrice']] != "") {
		    priceString += 'Now <b>'
		    priceString += formatCurrency(itemData[fieldNames['SpecialPrice']], true);
		    priceString += '</b>'
		}
    }
    else {
		if (itemData[fieldNames['RegularPrice']] != "") {
	        priceString += 'Reg. <del>'
		    priceString += formatCurrency(itemData[fieldNames['RegularPrice']], true) + '</del>';
		}
		if (itemData[fieldNames['NowPrice']] != "") {
		    priceString += ', Now <b>'
		    priceString += formatCurrency(itemData[fieldNames['NowPrice']], true);
		    priceString += '</b>'
		}

	}
		
			priceString += '</div>'
	
	// assemble popup innerHTML	
		
	var popupContent = ''
	popupContent += '<div class="popup">'
    popupContent += '<img id="popupImage" class="popupImg" src="/collections/_dbimages/'+itemData[fieldNames["ImageURL"]]+'.jpg"  alt="[image unavailable]" title="" />'
    popupContent += '<span class="logoBLVD">BLVD</span>'
    popupContent += '<span class="promo" id="popupPromo">'+promoText+'</span><br />'
    popupContent += '<div class="logoINTERIORS">INTERIORS</div>'
    popupContent += '<div class="text_container">'
    popupContent += '<div class="itemName" id="popupItemName">'+itemData[fieldNames["ItemName"]]+'</div>'
    popupContent += dimensionString;
    popupContent += '<div id="popupItemFinish">'+itemData[fieldNames["Description"]]+'</div>'
    popupContent += priceString;
    popupContent += '<div class="itemNo">Item# <span id="popupItemNumber">'+itemNo+'</span></div>'
    popupContent += '</div><!-- end of text_container -->'
    popupContent += '</div><!--end of popup-->'
    		
	
			
	gettrailobjnostyle().innerHTML = popupContent;
	
	gettrailobj().display = "inline";	
}



function formatCurrency(num, showCents) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
    if (showCents)
        return ((sign)?'':'-') + '$' + num + '.' + cents;
    else
        return ((sign)?'':'-') + '$' + num;
}


function hidetrail(){
	gettrailobjnostyle().innerHTML = " ";
	gettrailobj().display="none";
	document.onmousemove="";
	gettrailobj().left="-500px";
}




function followmouse(e){

	var xcoord=offsetfrommouse[0];
	var ycoord=offsetfrommouse[1];

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight);

	//if (document.all){
	//	gettrailobjnostyle().innerHTML = 'A = ' + truebody().scrollHeight + '<br>B = ' + truebody().clientHeight;
	//} else {
	//	gettrailobjnostyle().innerHTML = 'C = ' + document.body.offsetHeight + '<br>D = ' + window.innerHeight;
	//}

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < 380){
			xcoord = e.pageX - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + 110)){
			ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < 380){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - 400; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
		if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}
