
/*
	This js has the functions to display a compare "pop up" using layers and arrays to maintain the items selected
	One layer to put the page disable, another layer to display the compare table creating a pop up efect.
	The items properties come from a HTML tag called "item" wich is in the NS item cell template
*/

var items = new Array(); 
var phantom = new Array();
var stock = 0;
var HTML;

function compare() {
	// preload images
	imgClose=new Image; 
	imgClose.src=(document.all)?
			'http://shopping.netsuite.com/core/media/media.nl?id=3928&c=494083&h=c24ebb1f93abec74ab05':
			'http://www.contractorsdirect.com/site/templates/close.gif';
	
	imgRemove=new Image; 
	imgRemove.src=(document.all)?
			'http://shopping.netsuite.com/core/media/media.nl?id=3927&c=494083&h=8eb085bbf6b504c67553':
			'http://www.contractorsdirect.com/site/templates/remove.gif';

	makeTable();
	displayTable();
	Page(0);		
}

//This function is the basic structure of the DIV and table to show the compare items (is the FRAMEWORK!!!)
//Can be modified putting new NORMAL HTML with the new design
//Need to include the call for function "page(1)" to close the framework (in some link, or object event)
//in this part **<tr><td>Product</td></tr><tr><td>Condition</td></tr><tr><td>Page Yield</td></tr><tr><td>Color</td></tr>**
//is where the especs of the product to show is defined   --- width:728px; 
function makeTable() {
	HTML='<div id="divCompare" style="position:absolute;height:500px;left:165px;top:120px;z-index:10000;background-color:#FFFFFF;border:2px solid #CCC; padding:10px;"><div style="vertical-align:top;text-align:right;background-color:#FFFFFF;"><a href="javascript:void(0)" onClick="Page(1);" title="exit compare" ><img src="'+imgClose.src+'" border="0" alt="exit compare" hspace="5" vspace="5" /></a><table class="temptxt" align="center" border="0" cellspacing="0" cellpadding="3"><tr><td><!-- row for the remove item gifs --></td><td></td></tr><tr><td><!-- row for the thumbnails --></td><td></td></tr><tr><td valign="bottom" class="comparetitle" nowrap><!-- row for item name --></td><td></td></tr><tr><td valign="bottom" class="comparetitle" nowrap id="list_label"><!-- row for list --></td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Price</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Amps</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Voltage</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>RPM</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Arbor Size</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Compaction Force</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Frequency</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Motor/Engine</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Motor</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Engine</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Horsepower</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Length of Cut</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Diagonally Cuts</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Centrifugal Force</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Exciter Speed</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Batch Output</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Pre-mix Bags (88 lb)</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Impact Energy</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Amps/Flow Rate</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>BPM</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Speed</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Bit Type</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Tilt</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Discharge Height</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Bit Capacity</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Weight</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Plunge Feature</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Blade Capacity</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Depth of Cut</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Shipping</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Towable</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Working Width</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Drum Width</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Travel Speed</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Water Tank Capacity</td><td></td></tr><tr><td valign="top" class="comparetitle" nowrap>Base Type</td><td></td></tr></table><br><br></div>';
}

//function to remove an item from the compare framework
//CAN'T BE MODIFIED//
function popItem(event) {
	if (stock>1) {
		event = event || window.event; 
		var a = event.target || event.srcElement; 

		var _id = a.getAttribute("rel");
		var trs = document.getElementById("div-C").getElementsByTagName("tr");

		for (var i=0; i<trs.length; i++){
			trs[i].removeChild(document.getElementById("tr" + i.toString() +"_"+ _id));	
		}
		stock--;

		// resize divisions
		document.getElementById("divCompare").style.width = (71 + 132*stock) + "px";
		document.getElementById("div-C").style.width = (71 + 132*stock) + "px";
		
		if (stock<2) Page(1);
	}
}

//Function to put or remove the item ID in the ARRAY of items to compare.
//This function evaluate if remove or put the item-id, depending on the value of the checkBox who trigger the event.
//This function is called trought the checkbox onclick event in the item cell template
//CAN'T BE MODIFIED//
function setItem(event){	
	event = event || window.event; 						//this is for IE bug.
	var chk = event.target || event.srcElement; 

	if (chk.checked == true)
		items[items.length]={id:chk.id};			
	else
		popItemX_ID(chk.id)		
}

// Function to remove the item of the array. It is the helper of the setItem func.
// --CAN'T BE MODIFIED !!--
function popItemX_ID(parid) {
	for(var i=0;i<items.length;i++) {
		if (items[i].id==parid) 
		    items.splice(i,1);
	}
}


//function to show the compare table with the items loaded.
function displayTable() {
	
	if (items.length>1)	{																// are there more than one item to compare
		var div = document.createElement("div");										// create the framework div object 
		div.id="div-C";
		div.innerHTML = HTML; 															// put the framework code in the page

		var trs = div.getElementsByTagName("table")[0].getElementsByTagName("tr");		// get an array of the framework TRs	
		stock = items.length;															// how many items to show? 
		
		for (var i=0;i<items.length;i++){	

			var td_count = 0;

			// get the "item" tag with the item info - this tag is in the Item cell template
			// replace the name of the checkbox with the name of the item tag.
			var tip = document.getElementById(items[i].id.replace("chk_","tip_"));
					
			// Create the variable to display HTML of the first TD, has HTML to remove the item
			var html='<a href="javascript:void(0);" onclick="popItem(event)" title="remove item">'
					+'<img src="'+imgRemove.src+'" rel="td_'+i.toString()+'" border="0" alt="remove item"/></a>';
			displayTD(trs,html,i,td_count++);

			// Create de html to display link to product page
			html='<a href="'+tip.getAttribute("itm_href")+'"><img src="'+tip.getAttribute("img_src")+'" border="0" width="95" /></a>';
			displayTD(trs,html,i,td_count++);
			
			html='<a href="'+tip.getAttribute("itm_href")+'" ><strong>'+tip.getAttribute("itm_name")+'</strong></a>';
			displayTD(trs,html,i,td_count++);

			// Display list for matrix items
			displayProperty(tip,trs,'itm_listvals',	i,td_count++);
			var listLabel =  tip.getAttribute('itm_listlbl');

			// Display price list if there is one, else display regular price
			if (tip.getAttribute('itm_pricelist')!="")
				displayProperty(tip,trs,'itm_pricelist', i,td_count++);	
			else
				displayProperty(tip,trs,'itm_price', i,td_count++);	
	
			// Diplay each one of the specs for the item in a column
			displayProperty(tip,trs,'itm_amps',		i,td_count++);
			displayProperty(tip,trs,'itm_volt',		i,td_count++);
			displayProperty(tip,trs,'itm_rpm',		i,td_count++);
			displayProperty(tip,trs,'itm_asize',	i,td_count++);
			displayProperty(tip,trs,'itm_comf',		i,td_count++);
			displayProperty(tip,trs,'itm_freq',		i,td_count++);
			displayProperty(tip,trs,'itm_mtreng',	i,td_count++);
			displayProperty(tip,trs,'itm_motor',	i,td_count++);
			displayProperty(tip,trs,'itm_eng',		i,td_count++);
			displayProperty(tip,trs,'itm_hpwr',		i,td_count++);
			displayProperty(tip,trs,'itm_lcut',		i,td_count++);
			displayProperty(tip,trs,'itm_diacut',	i,td_count++);			
			displayProperty(tip,trs,'itm_cenf',		i,td_count++);
			displayProperty(tip,trs,'itm_exspd',	i,td_count++);
			displayProperty(tip,trs,'itm_bout',		i,td_count++);
			displayProperty(tip,trs,'itm_prembgs',	i,td_count++);			
			displayProperty(tip,trs,'itm_ienerg',	i,td_count++);
			displayProperty(tip,trs,'itm_afr',		i,td_count++);
			displayProperty(tip,trs,'itm_bpm',		i,td_count++);
			displayProperty(tip,trs,'itm_speed',	i,td_count++);
			displayProperty(tip,trs,'itm_bittype',	i,td_count++);			
			displayProperty(tip,trs,'itm_tilt',		i,td_count++);
			displayProperty(tip,trs,'itm_dish',		i,td_count++);
			displayProperty(tip,trs,'itm_bcap',		i,td_count++);
			displayProperty(tip,trs,'itm_wt',		i,td_count++);			
			displayProperty(tip,trs,'itm_plfeat',	i,td_count++);
			displayProperty(tip,trs,'itm_blcap',	i,td_count++);
			displayProperty(tip,trs,'itm_depcut',	i,td_count++);
			displayProperty(tip,trs,'itm_shipp',	i,td_count++);
			displayProperty(tip,trs,'itm_towabl',	i,td_count++);
			displayProperty(tip,trs,'itm_workw',	i,td_count++);
			displayProperty(tip,trs,'itm_drumw',	i,td_count++);
			displayProperty(tip,trs,'itm_travsp',	i,td_count++);
			displayProperty(tip,trs,'itm_wtcp',		i,td_count++);
			displayProperty(tip,trs,'itm_basetype',	i,td_count++);
			
		}	
		
		hideEmptyTRs(trs);																		// hide TRs with no data
		document.getElementsByTagName("body")[0].appendChild(div);								// Put framework in the page
		document.getElementById('list_label').innerHTML = listLabel;							// display correct list label
				
		//Select the framework position depending on the screen size
		var posleft = (screen.width > 960)?((screen.width-960)/2+120):120;
		document.getElementById("divCompare").style.left = posleft+'px';
		document.getElementById("divCompare").style.top = '165px';
		
		//Look here to set the width *NC*
		//you can sync the two divs  here only make a reference to div-C*NC*
		// document.getElementById("divCompare").style.width = (202 + 132*stock) + "px";
		// resize divisions
		document.getElementById("divCompare").style.width = (71 + 132*stock) + "px";
		document.getElementById("div-C").style.width = (71 + 132*stock) + "px";

	}
}



/*-----------------------------------------------------------
 function to display an item property in a selected TD
 parameters: 
			tip = object with the item properties 
			trs = array with the framework TDs.
			prop = the name of the property to be display  
			i= index of the array with the items ids
			j= number of the TD to use
-----------------------------------------------------------*/
function displayProperty(tip,trs,prop,i,j)
{
	//here we create a new TD and put in the TDs Array 
	var td = document.createElement("td");												// create a new TD
	td.id="tr"+j.toString()+"_td_"+i.toString(); 										// assign ID, allows to remove it later 
    td.className = "tdDataCompare";														// assign a class for CSS styles
	td.innerHTML = tip.getAttribute(prop); 												// write the value inside the TD
	trs[j].appendChild(td); 															// put the new td in the TDS array
}

/*-----------------------------------------------------------
 function to display html code  in a selected TD of the 
 framework parameters: 
			trs = array with the framework TDs.
			html = html code to write inside the TD.
			i= index of the array with the items ids
			j= number of the TD to use
-----------------------------------------------------------*/
function displayTD(trs,html,i,j)
{
	var td = document.createElement("td");												// create a new TD
	td.id="tr"+j.toString()+"_td_"+i.toString();										// assign ID, allows to remove it later 
	td.className = "tdDataCompare";														// assign a class for CSS styles
	td.innerHTML = html;																// write the HTML  inside the TD
	trs[j].appendChild(td);																// put the new td in the TDS array
}

//---------------------------------------------------------------------
// function to go trought all the TRs in the table and hide those empty
// parameters: 
//			trs = array with the framework TDs.
//
// rows - "r" start at row 3 (id 2), where spec values begin to display
// cells - "c" start at column 2 (id 1), to ignore spec titles

function hideEmptyTRs(trs){
	var count = 0; 
	var tds, empty, content;

	for (var r=2; r<trs.length; r++) {
		tds = trs[r].getElementsByTagName("td");
		empty = true;

		for (var c=1; c<tds.length; c++)
			if (tds[c].innerHTML!="") 
				empty=false;

		if (empty)
			trs[r].style.display = 'none';
		else
		    trs[r].className = (count++%2==0)?"white":"grey";							// assign a class for CSS styles
	}
}

//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// Blur functions follow:
// Master function to show or hide the blur and the compare table with the items selected.
// the use of the parameter OP is for evaluate if hidden or shown the framework.
// --CAN'T BE MODIFIED !!--
function Page(op){
	
  //op==1 remove the div-C and blur from the page, make the framework hidden
  if (op==1)  {	
	if (phantom.length > 0){
		for (var i=0;i<phantom.length;i++)
				phantom[i].style.visibility = "visible";
	}
	if (document.getElementById("Blur")) {
		 popOBJ(document.getElementById("Blur"));					// here the blur is removed 

	}
	if (document.getElementById("Blur2")) {
		 popOBJ(document.getElementById("Blur2"));					// here the blur is removed 
	}
 	popOBJ(document.getElementById("div-C"));						// here the framework is removed 
	popOBJ(document.getElementById("divCompare"))
 }
 else {
 	//show black background
	if (items.length>1)
	{
		if (!document.getElementById("Blur2")) 						// check if the black background exist
		{
			var n = navigator.userAgent;
			if (/MSIE/.test(n))
			{
				phantom = document.getElementsByTagName("select");	// deprecate don't erase
				for (var i=0;i<phantom.length;i++){ 
					phantom[i].style.visibility = "hidden";
				}
			}
			//create the blur element
			div=document.createElement("div");
			div.id="Blur";

			var h=document.body.offsetHeight;						// calculate the size of the blur	
			var h_a=document.body.scrollHeight;
			var w=document.body.offsetWidth;
			h = (h>h_a)?h:h_a;										// evaluate the height to use 
			
			// determine the style of the blur (the color, opacity,etc).
			//the only thing to maintain is the "z-index:9999" prop. to put the blur behind the compare framework
			//This portion of code CAN BE MODIFIED//
			div.innerHTML= "<div id=\"Blur2\" style=\"position:absolute;background:#000;opacity: 0.40;-moz-opacity: .4;filter: alpha(opacity=40);width:"+w+"px;height:"+h+"px;top:0;left:0;right:0;bottom:0;z-index:9999;border:0 \"> </div>";
			
			//more settings to put the blur in the right position
	 		if (document.all){
				div.style.width=(document.body.offsetWidth-20)+"px";
			}
		
			var tt=document.createTextNode(" ");
			div.appendChild(tt);
			document.getElementsByTagName("body")[0].appendChild(div)		
			window.scroll(0,0)
		}
	}
  }
}
	
//function the remove an object from the page
function popOBJ(pObj){
	if (pObj){
		pObj.parentNode.removeChild(pObj);
	}
}
