var $convertFeaturedProductsFired = false;
var $convertTableFired = true;
var $convertProductFired = false;

function convertFeaturedProducts(){
	if(!$convertFeaturedProductsFired){
		$convertFeaturedProductsFired = true;
		// CONVERT PRODUCTS TABLE TO DIVs
		//var $productTable = jQuery('#div_articleid_71').prev().find('table:first').attr('id','product-table').css('display', 'none');
		/*
		var $productTable = jQuery('#main-column').find('table').find('table').find('table:first').attr('id','product-table').css('display', 'none');
		var $tdloop = 0;
		var $liloop = 0;
		var $productHTML;
		var $cartLink;
	
		jQuery('#product-placeholder').replaceWith('<ul id="product-list">');
	
		jQuery('#product-table tbody > tr').each(function(){
			if($tdloop == 0) // Add product wrapper
				$productHTML += '<div class="product">';
	
			if(($tdloop <5)&&($tdloop !=3)) // Add HTML but not from index 3
				$productHTML += jQuery(this).find('td:first').html();
	
			if($tdloop == 5) { // 6 Should be the last one we need
				$productHTML += '</div>'; // Close the div
				$tdloop = 0; // reset the loop
				if($liloop == 2) { //See if it is the last (3rd) product for this section
					jQuery('#product-list').append('<li>');
					jQuery('#product-list li:last').html($productHTML.replace('undefined',''));
					jQuery('#product-list li:last').find('.product').each(function(){
						$cartLink = jQuery(this).find('a.productnamecolor').attr('href').replace('ProductDetails','ShoppingCart');
						$newlink = document.createElement('a');
						$newlink.setAttribute('href', $cartLink);
						$newlink.setAttribute('rel', 'cartLink');
						$tn = document.createTextNode('Add Item');
						$newlink.appendChild($tn);
						jQuery(this).append($newlink);
					});
					$liloop = 0;
					$tdloop = 0;
					$productHTML = '';
				} else {
					$liloop++;
				}
	
			} else {
				$tdloop++;
			}
		});
		
		if($productHTML.length > 0){		
			jQuery('#product-list').append('<li>');
			jQuery('#product-list li:last').html($productHTML.replace('undefined',''));
			jQuery('#product-list li:last').find('.product').each(function(){
				$cartLink = jQuery(this).find('a.productnamecolor').attr('href').replace('ProductDetails','ShoppingCart');
				$newlink = document.createElement('a');
				$newlink.setAttribute('href', $cartLink);
				$newlink.setAttribute('rel', 'cartLink');
				$tn = document.createTextNode('Add Item');
				$newlink.appendChild($tn);
				jQuery(this).append($newlink);
			});
		};
		
		jQuery('#featured-products').append(jQuery('#product-list'));
		jQuery('a[rel*="cartLink"]').addClass('cartLink');
		jQuery('.product td[width*="64%"]').attr('width','100%');
		jQuery('.product td[width*="36%"]').remove();
		jQuery('.product img[src*="photos"]').parent().wrap('<div>');
	*/
		//WRAP MAIN TABLE
		jQuery('#content_area > table').wrap('<div id="main-content">');
		jQuery('#content_area').append(jQuery('#additional-content'));
		//jQuery('.product').find('table').css('display','inline');
	
		//SETUP SLIDERS
		if(jQuery('#slider1').length > 0){
			jQuery('#slider1').anythingSlider({
				width : 560,
				height: 203,
				buildArrows : false,
				delay:5000
			});
		};
		if(jQuery('#product-list').length > 0){
			jQuery('#product-list').anythingSlider({
				width : 560,
				height: 180,
				buildArrows : false,
				delay:6000
			});
		};
	} else {
		return false;
	}
}

// CATEGORY PAGE (PRODUCT GRID) CONVERSION
function convertTable(){
	if((!$convertTableFired)&&(jQuery('a[href*="ProductDetails.asp"]').length > 0)){
		$convertTableFired = true;
		// CONVERT PRODUCTS TABLE TO UL
		var $productTable = jQuery('.colors_backgroundlight').parent().parent().parent().attr('id','product-table').css('display', 'none');
		jQuery('img[src*="clear1x1.gif"]').remove();
		jQuery('.smalltext > img').remove();
		jQuery('.smalltext').html('read more &raquo;');
		
		var $tdloop = 0;
		var $liloop = 0;
		var $listloop = 0;
		var $productHTML = '';
		var $cartLink;
		var $exit = false;
	
		jQuery('#product-placeholder').replaceWith('<ul id="product-list">');
	
		//BEGIN LOOPING THROUGH TABLE
		jQuery('#product-table tbody > tr').each(function(){
			if($tdloop == 0){ // ADD product wrapper
				$productHTML += '<div class="product">';
				jQuery(this).find('td').each(function(){
					if(!jQuery(this).hasClass('colors_backgroundlight')){ // CHECK for last row
						$productHTML += "<div class='product-stock' />";
						$productHTML += jQuery(this).html();
					} else {
						$actionHTML = '<div id="cart-actions">' + jQuery(this).html() + '</div>';
						$exit = true;
						return false; //exit each()
					};
				});
			} else if($tdloop == 1) {
				jQuery(this).find('td').each(function(index){
					if(index == 4){
						$productHTML += '<ul class="product-add"><li>' + jQuery(this).html() + '</li>';
					} else if(index == 5) {
						$productHTML += '<li>' + jQuery(this).html() + '</li></ul>';
					} else {
						$productHTML += jQuery(this).html();
					}
				});
			}
	
			if($exit){
				return false; // exit main each()
			}
	
			if($tdloop == 4) { // 4 Should be the last one we need
				$productHTML += '</div>'; // Close the div
				$tdloop = 0; // reset the loop
				if($liloop == 3) { //See if it is the last (4th) product for this section
					jQuery('#product-list').append('<li>');
					jQuery('#product-list li:last').html($productHTML.replace('undefined',''));
					$productHTML = '';
					$liloop = 0;
				} else {
					$liloop++;
					$listloop++;
				}
			} else {
				$tdloop++;
			}
		});
		
		if($productHTML.length > 100){
			jQuery('#product-list').append('<li>');
			jQuery('#product-list li:last').html($productHTML.replace('undefined',''));
		};
	
		jQuery(".product:empty").remove();
		
		jQuery('.product-stock').each(function(){
			if(jQuery(this).parent().find('.PageText_L490n').length > 0){
				jQuery(this).addClass('in-stock');
				jQuery(this).append(jQuery(this).parent().find('.PageText_L490n'));
			} else {
				jQuery(this).html('Call for stock');
			}
		});
	
		jQuery('.PageText_L337n').each(function(){
			var $targetElem = jQuery(this).parent().find('input:checkbox');
			$targetElem.attr('id', $targetElem.attr('value'));
		
			var $newLabel = document.createElement('label');
			$newLabel.setAttribute('for',$targetElem.attr('id'));
			if(($.browser.msie)&&(parseInt($.browser.version, 10) == 7)){
				$newLabel.setAttribute('class','ie7-add-item');
				$tn = document.createTextNode('Add Item');
				$newLabel.appendChild($tn);
				jQuery(this).after($newLabel);
			} else {
				jQuery(this).after($newLabel);
				jQuery($targetElem).button();
			}
			jQuery(this).remove();
		});
	
		jQuery('#product-table').replaceWith(jQuery('#product-list'));
		if(jQuery('#cart-actions').length <= 0)
			jQuery('#product-list').after($actionHTML);
		jQuery('#jmenuhide').parent().parent().parent().parent().parent().parent().attr('valign','top')
		jQuery('#jmenuhide').parent().parent().parent().parent().parent().addClass('product-nav');
		jQuery('.product-nav').parent().parent().parent().parent().css({'margin':'5px 0 10px', 'border-bottom':'1px solid #4A5164'});
		jQuery('.results_per_page_select').parent().append(jQuery('.previous_page_img:first'));
		jQuery('.results_per_page_select').parent().append(jQuery('.next_page_img:first'));
		jQuery('.product-add').wrap('<div class="product-add-wrap">');
		jQuery('.product-stock').wrap('<div>');
		//jQuery('.PageText_L483n').wrap('<div class="product-price">');
		jQuery('.product br').remove();
		jQuery("#product-list li:empty").remove();
	} else {
		return false;
	}
}

function convertProduct(){
	if(!$convertProductFired){
		$convertProductFired = true;
		jQuery('img[src*="clear1x1.gif"]').remove();
		jQuery('#body-container br').remove();
		jQuery('.PageText_L489n').parent().parent().remove();
		
		jQuery('#vCSS_mainform').append('<div id="product"><div id="product-left-column" /><div id="product-right-column" />');
		jQuery('#product-right-column').append('<ul>');
		jQuery('#product-right-column > ul').append('<li>').append('<li>').append('<li>');
		jQuery('#product-right-column li:eq(0)').append(jQuery('a[href*="EmailaFriend.asp"]')).addClass('first');
		jQuery('#product-right-column li:eq(1)').append(jQuery('.vCSS_img_share_this').parent());
		jQuery('#product-right-column li:eq(2)').append(jQuery('input[name*="btnaddtowishlist"]'));

		$imageHTML = jQuery('#vCSS_mainform > table td:eq(1) td:eq(1)').html();
		$priceHTML = jQuery('.product_code_title').parent().parent().parent().html();
		$qtyField = jQuery('.PageText_L71n').parent().html();
		$addToCart = jQuery('.vCSS_input_addtocart').parent().html();
		
		// IS THERE QTY INFO IN THE HTML?
		var $isQty = $priceHTML.indexOf('PageText_L329n');

		if($isQty != -1){
			var $strQty = $priceHTML.substr($isQty - 13, $priceHTML.indexOf('<i>', $isQty) - $isQty);
			$priceHTML = $priceHTML.replace($strQty, '');
		}
		
		// BUILD PRODUCT DISPLAY
		jQuery('#product-left-column').html($imageHTML);
		jQuery('#product-right-column').prepend(jQuery('#ProductDetail_ProductDetails_div')).prepend(jQuery('.productnamecolorLARGE'));
		jQuery('#product-right-column').append($priceHTML);
		jQuery('#product-right-column').append('<div id="addtocart">');
		jQuery('#product-right-column #addtocart').append($qtyField).append($addToCart);
		jQuery('#product-right-column #addtocart').prepend('<div id="stock-status">');
		if($isQty != -1)
			jQuery('#stock-status').addClass('in-stock');
		jQuery('#product').append(jQuery('.PageText_L493n'));

		if(jQuery('.PageText_L483n').length <= 0){
			jQuery('.colors_text').wrapInner(function(){
				return '<span class="PageText_L483n"/>';
			});
			
			jQuery('.colors_productprice .colors_text span').removeClass('PageText_L483n');
			
			jQuery('.colors_productprice').wrapInner(function(){
				return '<span class="PageText_L483n"/>';
			});
			
			jQuery('.colors_productprice').css('display','inline');
		}
		
		jQuery('.PageText_L483n').addClass('dotted-line');
			
		jQuery('.PageText_L71n').html('Product Quantity');
		jQuery('.PageText_L492n').parent().parent().addClass('breadcrumb').parent().parent().parent().css('margin-top','20px');
		
		jQuery('#vCSS_mainform > table tr:gt(0)').remove();
		
		jQuery('#product').after('<div id="tabs"><ul><li><a href="#technical">Technical</a></li><li><a href="#global">Global Advantage</a></li><li><a href="#turnaround">Turnaround Times</a></li><li><a href="#repair">Repair Process</a></li><li><a href="#forms">Forms</a></li></ul><div id="technical"><h3>CERTIFIED & EXPERIENCED TECHNICIANS</h3><img alt="" src="/v/vspfiles/templates/GESCustom/images/technical-image1.jpg" border="0px" align="right"><ul><li>Numerous certifications & training from various OEM&rsquo;s</li><li>Generations of experience</li><li>Forefront of technology</li><li>Latest advancements test procedures & best practices </li><li>Speed and quality in all our repair services</li><li>Customized repair procedures</li><li>Warranty on all our repairs</li></ul><h3>THE LATEST TECHNOLOGY</h3><img alt="" src="/v/vspfiles/templates/GESCustom/images/technical-image2.jpg" border="0px" align="right"><ul><li>100% TRUE Load Testing</li><li>Dynamometers - provides full 	resistance testing to identify weak or faulty components on motors & drives</li><li>OEM Test Stations - allows full functional testing for most manufacturers</li><li>Servo Test Stations - provides precise repair & testing for servo drives, amplifiers, motors & more</li><li>Curve Tracer Signature Device - analyzes & detects problems with circuit boards</li></ul></div><div id="global"><ul><li>18 Month In-Service Warranty</li><li>1-5 Day Standard Turnaround</li><li>FREE 1-2 day Rush Service</li><li>Free Evaluations on All Items</li><img alt="" src="/v/vspfiles/templates/GESCustom/images/ges/navpromo_priceguarantee.png" border="0px" align="right"><li>The Global Price Guarantee - We&rsquo;ll Beat Any Competitor&rsquo;s Price by a Minimum of 10%!</li><li>24/7 Live Customer Support</li><li>Factory Certified Technicians</li><li>Industry Leading Test Equipment</li><li>Failure &amp; Testing Analysis</li><li>Diverse Inventory of Common Failure Components</li><li>On-line Repair Status &amp; Tracking</li><li>Most Complete Repair Pricing - Catalog and On-Line</li></ul></div><div id="turnaround"><h3>STANDARD</h3><p>1-5 Days</p><h3>RUSH</h3><p>1-2 Days</p><h3>EMERGENCY AFTER HOURS</h3><p>Available with an additional fee.  Additional fee is 25% of repair price or $200.00 whichever is greater.</p></div><div id="repair"><img src="/v/vspfiles/assets/images/dsc_0055.jpg" alt="" align="right" border="0" height="240" hspace="10" vspace="10" width="240"><ol><li>Ship us your repair and we receive, log-in, and assign a barcode to your unit. Next, a technician performs an evaluation and produces a list of parts necessary to complete the job. Within 24 hours a quote is then generated and sent to your for approval via fax, phone or email.<br><br></li><li>With approval of the quote, the repair process begins and a technician is assigned to acquire any necessary parts and begins the repairs.  After the item is repaired, it is tested under load conditions to ensure peak performance.  Further testing, quality checks and any preventative maintenance is performed.<br><br></li><li>Upon quality control approval, your repair is prepped and cleaned for return shipment.  Our shipping department carefully packages your item to ensure that it remains undamaged and ready to be installed.</li></ol></div><div id="forms"><img src="/v/vspfiles/assets/images/printables_350x350.jpg" height="200" width="200" hspace="10" align="left" /> Convenience at  your fingertips!  Sending a repair in is easy!  Print a packing slip and include it with your repair.<ul style="margin-left:210px;"><li><a href="http://www.gesrepair.com/v/vspfiles/assets/GESpackingslip.pdf">Packing Slips </a></li><li><a href="http://www.gesrepair.com/v/vspfiles/assets/GES_PackingSlip_ESP_R2.pdf">Packing Slip-Spanish</a></li><li><a href="http://www.gesrepair.com/v/vspfiles/assets/GESbrochure.pdf">Brochure</a></li><li><a href="http://www.gesrepair.com/v/vspfiles/assets/GES_Brochure6x11_1-10_Espanol.pdf">Folleto</a></li></ul></div></div>');

		jQuery('#product').append(jQuery('#guarantee-image'));
		
		$( "#tabs" ).tabs();

	} else {
		return false;
	}
}

/* REBUILD MANUFACTURER MENU */
function convertMenu(){
}

/* SITE WIDE jQuery CONVERSIONS */
jQuery(document).ready(function(){
	jQuery('table.nav').attr('cellspacing','4');
	if(jQuery('.PageText_L517n').length > 0)
		jQuery('.PageText_L517n').parent().parent().addClass('breadcrumb').html(jQuery('.PageText_L517n').parent().parent().html().replace(/&gt;/gi,''));

	if(jQuery('.vCSS_breadcrumb_td').length > 0)
		jQuery('.vCSS_breadcrumb_td').addClass('breadcrumb').html(jQuery('.vCSS_breadcrumb_td').html().replace(/&gt;/gi,''));

	jQuery('img[src*="SearchResults_SubCat_Angle.gif"]').css('display','none');
	//jQuery('table[class!="nav"]').attr({cellpadding:'0', cellspacing:'0'});
	
	jQuery('#display_menu_3 div').addClass('left-nav');
	jQuery('#display_menu_3 div:first').addClass('left-nav-main');
	jQuery('#display_menu_2 div').addClass('top-nav');
	jQuery('#display_menu_2 div:first').removeClass('top-nav');
	jQuery('.v65-productDisplay:first').addClass('prod-search-result-table');

	jQuery('a.colors_productname').after('<img src="http://b.cdn.searchspring.net/ajax_search/sites/t7sw8a/img/missing.jpg" style="height:33px;width:50px;">');
	//convertMenu('#menu8');
	var $current_href = top.location.href;
	
	if(($current_href.indexOf('product-p')!=-1)&&($current_href.indexOf('.htm')!=-1)){
		convertProduct();
	}
	
	if(jQuery('table.prod-search-result-table').length > 0){
		convertSearchResults();
	}
});

var message = "This function has been disabled for security purposes."; 

function convertSearchResults(){
	//jQuery('.v65-productDisplay-cell + td').addClass('rightBorder');
	
	jQuery('td[background="/v/vspfiles/templates/GESCustom/images/Grid_Single_Divider_Vert.gif"]').addClass('rightBorder').attr('background','');
	jQuery('a.productnamecolor + img').addClass('noProductImage');

	var $imgsrc = 'http://b.cdn.searchspring.net/ajax_search/sites/t7sw8a/img/missing.jpg';

	jQuery('table.v65-productDisplay-inner tr').each(function(index){
		if(jQuery(this).find('td.v65-productDisplay-cell').length > 0){
			jQuery(this).find('.v65-productDisplay-cell > a').addClass('productImage');

			if(jQuery(this).find('img[src*="nophoto-0.gif"]').length > 0){
				$imgsrc = 'http://b.cdn.searchspring.net/ajax_search/sites/t7sw8a/img/missing.jpg';
			} else {
				$imgsrc = jQuery(this).find('img').attr('src');
			}
		}
		
		if(jQuery(this).find('a.productnamecolor + img').length > 0){
			jQuery(this).find('a.productnamecolor + img').attr('src', $imgsrc);
		}
	});
	
	try{
		if(clearance)
			jQuery('a.smalltext.colors_text').append('&nbsp;<span style="color:#FFEE77;font-weight:bold">CLEARANCE</span>')
	} catch(err) {
		
	}
}

function rtclickcheck(keyp){
	if (navigator.appName == "Netscape" && keyp.which == 3){
		alert(message);
		return false; 
	} 
	if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2) { 	
		alert(message); 	
		return false; 
	} 
} 

document.onmousedown = rtclickcheck;
