var searchBoxDirty = false;
var productContactTimer;
var searchActive = false;
var searchHandle;

function init(controller,action)
{
	/* ALWAYS RUN HERE DOWN */
	if(ie6())
	{
		$('input[type="text"]').addClass('typeText');
	}

	/* Change pointer on anchors with no href attribute */
	$('.clink').hover(function(){ this.style.cursor='pointer'; });
	$('.closebutton').hover(function(){ this.style.cursor='pointer'; });
	$('#fullsearch').click(function() { $('#ProductSearchForm').submit(); });
	
	$('.livehelp').click(function() { 
		if(navigator.userAgent.toLowerCase().indexOf('opera') != -1 && window.event.preventDefault) 
			window.event.preventDefault();
		this.newWindow = window.open('/webim/client.php?locale=en&amp;url='+escape(document.location.href)+'&amp;referrer='+escape(document.referrer), 'webim', 'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1');
		this.newWindow.focus();
		this.newWindow.opener=window;
		return false; 
	});

	$('#flashmessageclose').click(function() { $('#flashmessagecontainer').fadeOut(300); });	
	setTimeout(function() { $('#flashmessagecontainer').fadeOut(300); },'10000'); 

	if(!ie6())
	{
		$('#searchclose').click(function() { $('#dynamicresults').fadeOut(300); });	
		$('#ProductKeywords').focus(function(){ 
				if($('#ProductKeywords').val() == 'Enter search term(s)')
				{
					$('#ProductKeywords').val('');
					searchBoxDirty = false;
				}
	
				if(searchBoxDirty) $('#dynamicresults').fadeIn(300); 
			}
		);
		
		$('#ProductSearchForm').submit(function() {
			if($('#ProductKeywords').val() =='Enter search term(s)' || $('#ProductKeywords').val() =='')
			{
				alert('Search keyword required.'); 
				return false; 
			}
			else
				searchHandle.abort();
		});
		
		$('#ProductKeywords').blur(function() {
				if($('#ProductKeywords').val() == '')
				{
					$('#ProductKeywords').val('Enter search term(s)');
					searchBoxDirty = false;
					$('#dynamicresults').fadeOut(300); 
				}
			}
		);
	
		$('#ProductKeywords').keyup(function() { 
			if($('#ProductKeywords').val().length > 3)
			{
				if(searchActive == true) searchHandle.abort();
	
				searchHandle = $.ajax({
					method: "get",url: "/products/liveresults?keywords="+encodeURIComponent($('#ProductKeywords').val()),
					beforeSend: function() {
						searchActive = true;
						$('#dynamicresults').fadeIn(200); 
						$('#searchcontent').html('');
						$('#busyspinner').css('display','block');
					},
					success: function(html){ //so, if data is retrieved, store it in html
						searchActive = false;
						$('#busyspinner').css('display','none');
						$("#searchcontent").html(html); //show the html inside .content div
						searchBoxDirty = true;
					}
				}); //close $.ajax(
			}
			else
			{
				$("#searchcontent").html('');
				$('#dynamicresults').css('display','none'); 
			}
		});
	}
}

function ie6()
{
	return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}


function leadtimes()
{
	alert('Orders for items which are not in stock will take longer than stated time if amount ordered from the brand does not meet minimum billing that the manufacturers have established.');
}

function closewindow(div)
{
	$('#'+div).fadeOut(500);
}

function productflag(productnumber)
{
	$.ajax({
		url: "/products/flag/"+productnumber,
		success: function(){
			$('#productflag').css('display','block');
			setTimeout("$('#productflag').fadeOut(600)",6000);
		}
	}); //close $.ajax(
}

function productcontact()
{
	$('#productcontacterror').css('display','none');
	$('#productcontactsuccess').css('display','none');
	$('#productcontactform').css('display','block');
	clearTimeout(productContactTimer);
	$('#productcontact').fadeIn(500);
}

function sendproductcontact(productnumber)
{

	if($('#email').val() !== undefined && $('#email').val().length == 0)
	{
		alert('You must enter an email address.');
		return false;
	}

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test( $('#email').val()) == false) 
	{
		alert('You must enter a valid email address.');
		return false;
	}
	
	if($('#message').val().length == 0)
	{
		alert('You must enter a message.');
		return false;
	}

	if($('#email').val() == undefined ) dstruct={ message: $('#message').val(), product: $('#product').val() }
	else dstruct={ email: $('#email').val(), message: $('#message').val(), product: $('#product').val() }

	$.ajax({
		type: "post",
		url: "/products/contact",
		data: dstruct,
		success: function(status){
			$('#productcontactform').css('display','none');
			if(status == 1)
				$('#productcontacterror').css('display','block');
			else
				$('#productcontactsuccess').css('display','block');
				
			productContactTimer = setTimeout("$('#productcontact').fadeOut(600)",6000);
			
			$('#email').val('');
			$('#message').val('');
		},
		error: function(){
			$('#productcontactform').css('display','none');
			$('#productcontacterror').css('display','block');
			productContactTimer = setTimeout("$('#productcontact').fadeOut(600)",6000);		
		}
	}); //close $.ajax(
}
