$(document).ready(function(){
	$('td.text').parent().addClass('plain');
	$('#categorylist table tr:odd').addClass('odd');

	// Adds a class on mouseover (to solve IE6 :hover issue)
	$('#nav_tabs li').mouseover( function(){ $(this).addClass('hover').siblings().removeClass('hover'); }).mouseout (function() {
		$(this).removeClass('hover');
	});
});

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

$(document).ready(function(){
	
	/* if customer already logged in hide the login link and display the logout one */
	
	//var customerLoggedIn = ("<%=getCurrentAttribute('customer','email','')%>".indexOf('@') > -1)?true:false;
	if(customerLoggedIn){
		$('.log-link').each(function(){$(this).css('display','none');});
		$('.logout-link').each(function(){$(this).css('display','block');});
	}

	/* remove spacer images set by NS on the left and right columns */	
	function removeStretch(){
		$('#left_secure img, #right_secure img').each(function(){
			if($(this).attr('src').indexOf('stretch.gif') > -1)
				$(this).remove();
		});		
	}
	removeStretch();
	/* do it again after 4 seconds after page load, to wait for NS to finish loading content */
	$(window).bind('load', function(){ window.setTimeout(removeStretch, 4000, true); });

	currentTab = "<%=getCurrentAttribute('site','currenttablabel')%>".toString().toLowerCase();
	$('ul#nav img').each(function(){
		if (currentTab.indexOf($(this).attr('alt').toString().toLowerCase()) > -1){
			$(this).addClass('activetab');	
		}  
	});  
	
	/* set a class for each row of the dynamic left nav */
	$('#left_content tr').each(function(){
		var cellsNumber = $(this).children('td').length;
		$(this).addClass('tds'+cellsNumber);
	});
	
	/* copies trigger script for live chat popup into the links of class livehelp: theres one on the gallery image and another on the footer */
	$('a.livehelp').each(function(){
		$(this).attr('href',$('#VolusionLiveChat a').attr('href'));
	});

	/* cart page: table buttons need to be set to auto */
	$('#tbl_checkout').parents('table').filter(':first').css({'width' : 'auto','float' : 'right'});
	
	if($("img[src$='/images/nav/prev_dim.gif']").length > 0){
		$("img[src$='/images/nav/prev_dim.gif']").each(function(){
			$(this).parents('table').filter(':first').css('display','none');
		});
	}
});

/* cart page: copy the secure icons from footer into the previous content */
$(window).load(function(){
	if(document.getElementById('carttable') && document.getElementById('bottomcontentitem') && document.getElementById('carticons')){	
		$('#bottomcontentitem').empty();
		$('#carticons').children('div').each(function(){
			$(this).detach().appendTo('#bottomcontentitem');
		});
	}
	if(document.getElementById('itemtemplate') && document.getElementById('productsecurelogos') && document.getElementById('carticons')){	
		$('#productsecurelogos').empty();
		$('#carticons').children('div').each(function(){
			$(this).detach().appendTo('#productsecurelogos');
		});
	}
	
	
});

/* remove NS resizing options from an image object */
function imgRemoveResize(imgObj){
	var src = imgObj.attr('src');
	if (typeof src != 'undefined' && src.indexOf('resizeid') > -1){
		imgObj.attr('src',src.split('resizeid')[0]);
	}	
}

function poplowestprice(){
    	testwindow = window.open('/site/obb-files/lowest-price-promise.html','lowestprice','width=650,height=500,toolbar=no,location=0,directories=0,status=0,menubar=0,scrollbars=1,copyhistory=0,resizable=1');
	    testwindow.moveTo(0, 0);
	}
	

/* ------------- SavedBikeModelsFrontend_290509 ------------------- */
function SavedBikeModels()	{
	
	var customer = null;
	var suitelet = '/app/site/hosting/scriptlet.nl?script=31&deploy=1';
	
	this.customer = function( customerId )	{
		if( customerId ) customer = new SavedBikeModelsCustomer( customerId );
	}
	this.suitelet = function()	{
		return suitelet;
	}
	
	// GET CURRENT CUSTOMER MODELS
	this.getCustomerModels = function()	{
		if( customer )	{
			document.getElementById( 'saved-models' ).style.display = 'block';
			customer.getModels();
			var list = document.getElementById( 'saved-models-list' );
			if( list.getElementsByTagName( 'p' ).length == 0 )	{
				list.innerHTML = '<p id=saved-models-loading class=saved-model>Loading...</p>';
				list.innerHTML += '<p id=saved-models-noresults class=saved-model style=display:none>Search for a bike model and add it to your Saved Bike Models.</p>';
			}
		}
	}
	this.whilegettingmodels = function()	{
	}
	this.allmodelsgotten = function( models )	{
		var loading = document.getElementById( 'saved-models-loading' );
		var noresults = document.getElementById( 'saved-models-noresults' );
		var list = document.getElementById( 'saved-models-list' );
		var ps = list.getElementsByTagName( 'p' );
		loading.style.display = 'none';
		noresults.style.display = ( models.length == 0 ) ? 'block' : 'none';
		for( var i = 0; i < models.length; i++ )	{
			var p = document.createElement( 'p' );
			p.id = 'saved-model-' + models[i].id();
			p.className = 'saved-model';
			p.innerHTML = '<a class=remove href=javascript:_savedModels.deleteCustomerModel(' + models[i].id() + ')>Remove</a><a class=name href=' + models[i].url() + '>' + models[i].name() + '</a>';
			var add = true;
			for( var j = 0; j < ps.length; j++ )	{
				if( ps[j].innerHTML == p.innerHTML ) add = false;
			}
			if( add )	{
				list.appendChild( p );
			}
		}
	}
	
	// SAVES A MODEL TO THE CUSTOMER
	this.saveCustomerModel = function( name, url )	{
		// REQUIRE NAME AND URL
		if( !name || !url ) return false;
		var bikeModel = new SavedBikeModelsModel( null, url, name );
		customer.addModel( bikeModel );
		document.getElementById( 'save-model-link' ).href = 'javascript:void(0)';
		document.getElementById( 'save-model-link' ).innerHTML = 'Saving...';
		document.getElementById( 'save-model-link' ).className = 'saving';
	}
	this.whilesavingmodel = function()	{
	}
	this.onmodelsaved = function()	{
		document.getElementById( 'save-model-link' ).style.display = 'none';
		customer.getModels();
	}
	
	// DELETES A MODEL FROM THE CUSTOMER
	this.deleteCustomerModel = function( modelId )	{
		// REQUIRE MODEL ID AND PROMPT
		var goon = confirm( "Are you sure you want to remove this model from your list?" );
		if( !modelId || !goon ) return false;
		customer.removeModel( modelId );
		var noresults = document.getElementById( 'saved-models-noresults' );
		var alink = document.getElementById( 'saved-model-' + modelId );
		var list = document.getElementById( 'saved-models-list' );
		var ps = list.getElementsByTagName( 'p' );
		alink.parentNode.removeChild( alink );
		if( ps.length == 2 ) noresults.style.display = 'block';
	}
	this.whiledeletingmodel = function( modelId )	{
	}
	this.onmodeldeleted = function( modelId )	{
	}
	
	// WRITES ADD TO MODELS LINK
	this.addToModelsLink = function( url )	{
		// REQUIRE CUSTOMER
		if( !customer ) return false;
		document.write( '<a id=save-model-link href=javascript:saveModel()></a>' );
		var ajax = null;
		try	{
			ajax = new XMLHttpRequest();
		}
		catch(e)	{
			try	{
				ajax = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)	{
				try	{
					ajax = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(e)	{
				}
			}
		}
		var rand = ( ( Math.random() * 1000 ) + "" ).substring( 0, 3 );
		var url = _savedModels.suitelet() + '&a=hm&ci=' + customer.id() + '&u=' + url + '&rand' + rand;
		ajax.open( 'GET', url, true );
		ajax.onreadystatechange = function()	{
			if( this.readyState == 4 )	{
				var alink = document.getElementById( 'save-model-link' );
				if( this.responseText == 'Has Item' )	{
					alink.parentNode.removeChild( alink );
				}
				else	{
					alink.innerHTML = 'Add this to My Saved Models';
				}
			}
		};
		ajax.send( null );
	}
	
	return this;
	
}
function SavedBikeModelsCustomer( customerId )	{
	
	this.id = function()	{
		return customerId;
	}
	
	this.getModels = function()	{
		models = [];
		var ajax = null;
		try	{
			ajax = new XMLHttpRequest();
		}
		catch(e)	{
			try	{
				ajax = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)	{
				try	{
					ajax = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(e)	{
				}
			}
		}
		var rand = ( ( Math.random() * 1000 ) + "" ).substring( 0, 3 );
		var url = _savedModels.suitelet() + '&a=g&ci=' + customerId + '&rand' + rand;
		var loadingFunction = _savedModels.whilegettingmodels;
		var finishFunction = _savedModels.allmodelsgotten;
		ajax.open( 'GET', url, true );
		ajax.onreadystatechange = function()	{
			if( this.readyState == 1 )	{
				loadingFunction();
			}
			else if( this.readyState == 4 )	{
				var results = this.responseText.split( '#MODEL#' );
				for( var i = 1; i < results.length; i+=2 )	{
					var atts = results[i].split( '#ATTRIBUTE#' );
					models.push( new SavedBikeModelsModel( atts[0], atts[1], atts[2] ) );
				}
				finishFunction( models );
			}
		};
		ajax.send( null );
	}
	
	this.addModel = function( bikeModel )	{
		bikeModel.saveModel( customerId );
	}
	
	this.removeModel = function( modelId )	{
		var bikeModel = new SavedBikeModelsModel( modelId, null, null );
		bikeModel.deleteModel( customerId );
	}
	
	var models = [];
	
	return this;
	
}
function SavedBikeModelsModel( id, url, name )	{
		
	this.id = function()	{
		return id;
	}
	this.url = function()	{
		return unescape( url );
	}
	this.name = function()	{
		return unescape( name );
	}
	
	this.saveModel = function( customerId )	{
		var ajax = null;
		try	{
			ajax = new XMLHttpRequest();
		}
		catch(e)	{
			try	{
				ajax = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)	{
				try	{
					ajax = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(e)	{
				}
			}
		}
		var rand = ( ( Math.random() * 1000 ) + "" ).substring( 0, 3 );
		var ajaxurl = _savedModels.suitelet() + '&a=s&ci=' + customerId + '&n=' + name + '&u=' + url + '&rand' + rand;
		var loadingFunction = _savedModels.whilesavingmodel;
		var finishFunction = _savedModels.onmodelsaved;
		ajax.open( 'GET', ajaxurl, true );
		ajax.onreadystatechange = function()	{
			if( this.readyState == 1 )	{
				loadingFunction();
			}
			else if( this.readyState == 4 )	{
				finishFunction();
			}
		};
		ajax.send( null );
	}
	
	this.deleteModel = function( customerId )	{
		var ajax = null;
		try	{
			ajax = new XMLHttpRequest();
		}
		catch(e)	{
			try	{
				ajax = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch(e)	{
				try	{
					ajax = new ActiveXObject('Msxml2.XMLHTTP');
				}
				catch(e)	{
				}
			}
		}
		var rand = ( ( Math.random() * 1000 ) + "" ).substring( 0, 3 );
		var ajaxurl = _savedModels.suitelet() + '&a=d&ci=' + customerId + '&i=' + id + '&rand' + rand;
		var loadingFunction = _savedModels.whiledeletingmodel;
		var finishFunction = _savedModels.onmodeldeleted;
		ajax.open( 'GET', ajaxurl, true );
		ajax.onreadystatechange = function()	{
			if( this.readyState == 1 )	{
				loadingFunction( id );
			}
			else if( this.readyState == 4 )	{
				finishFunction( id );
			}
		};
		ajax.send( null );
	}
	
	return this;
	
}
var _savedModels = new SavedBikeModels();
