$(document).ready(function () {

	
	
	imagePreview();

	
	if ( $(location).attr('href').toString().replace('http://','').replace('willcowells.com/','').replace('www.','') != '' ){//''http://willcowells.pro1.nl/' ){
	
		$(document).scrollTop( $('#container').offset().top );
	
	
	}



	$(".Jradio input").click(function(){					
		if($(this).is(':checked')) {
			$(this).parent().siblings('.active').removeClass('active');
			$(this).parent().addClass('active');
		}
	});


  	// + Reset Font Size + //
 	var originalFontSize = $('body').css('font-size');
    
	//alert( $.cookie("fontsize") );
	
	if ( $.cookie("fontsize") ){
		var test = $.cookie("fontsize");
		$('body').css('font-size', parseInt(test) );	
	}
	
	// Decrease Font Size
	$(".smallFont").click(function(){
		var newFontSize = 12;
		$('body').css('font-size', newFontSize);
		$('#bodyContent').css('font-size', newFontSize);		
		$.cookie("fontsize", newFontSize, {  path: '/',expires: 7 });	
		return false;
	});
  
	$(".mediumFont").click(function(){
		$('body').css('font-size', originalFontSize);
		$('#bodyContent').css('font-size', 13);
		$.cookie("fontsize", originalFontSize, {  path: '/',expires: 7 });
		return false;
	});
  
	// Increase Font Size
	$(".largeFont").click(function(){
		var newFontSize = 15;
		$('body').css('font-size', newFontSize);
		$('#bodyContent').css('font-size', newFontSize);
		
		$.cookie("fontsize", newFontSize, {  path: '/',expires: 7 });
		return false;
	});
  


	// - Reset Font Size - //




	$(".toggle_container").hide();

	$("p.trigger").click(function(){
		$(this).toggleClass("active").next().slideToggle("slow");
		return false;
	});



// FOCUS: focus on the first element on the page with class "focus"
	$('.jFocus:first').focus();

	
// CONFIRM: asked to confirm before leaving to href url
	$(".jConfirm").click(function(event) {  
		
		event.preventDefault();  // prevent the anchor tag from sending the user off to the link   
		
		var url = this.href; 
		var title = this.title;
		
		
		if ( this.rel == 'agree' && $('#agree').is(':checked') == false ){
	
			
			yousure = false;	
			alert( $('#agree').attr("title") );
				
						
		} else {
		
			var yousure = confirm(title);

		}
		
		if (yousure == true) {
			document.location = url;
		} else {
			// Do nothing
		}		
	
	});


	$("a[rel^='photoBox']").prettyPhoto( {theme: 'light_rounded' });
			
	$("#livesearch").hide();

	// + Make boxes clickable + //
	$(".clickBox").each(function () {
								  
		$(this).click(function(){

			var href = $(this).find("a.clickEvent").attr("href");
			
			if (href){
				var target = $(this).find("a.clickEvent").attr("target");
				
				if (target == "_blank"){
					window.open(href);
				} else {
					document.location = href;
				}
			}
		});
		$(this).hover(function() { 
			$(this).addClass('active');   
		  }, function() {   
			$(this).removeClass('active');
		});
	});
	// - Make boxes clickable - //
	
$('#searchInput').keyup(function(e) {
										
		switch(e.keyCode) { 
			// User pressed "up" arrow
			case 38:
				navigate('up'); 
			break;
			// User pressed "down" arrow
			case 40:
				navigate('down');
			break;
			// User pressed "enter"
			case 13:
				if(currentUrl != '') {
					
					window.location = currentUrl;
					return false;
				}
				default:showResult(this.value,this.lang);
			break;
		}
	});
	
}); // end doc







function submitCart(nr){
	
	//alert( $(this).attr("value") );


	 $('#amountForm_'+nr).submit();



}


// Add data to let the hover know which index they have
for(var i = 0; i < $("#livesearch .searchBox").size(); i++) {
	$("#livesearch .searchBox").eq(i).data("number", i);
}




function navigate(direction) {

		// Check if any of the menu items is selected
		if($("#livesearch .itemhover").size() == 0) {
			currentSelection = -1;
		}
		
		if(direction == 'up' && currentSelection != -1) {
			if(currentSelection != 0) {
				currentSelection--;
			}
		} else if (direction == 'down') {
			if(currentSelection != $("#livesearch .searchBox").size() -1) {
				currentSelection++;
			}
		}
		setSelected(currentSelection);
}

function setSelected(menuitem) {

		$("#livesearch .searchBox").removeClass("itemhover");
		$("#livesearch .searchBox").eq(menuitem).addClass("itemhover");
		currentUrl = $("#livesearch .searchBox a").eq(menuitem).attr("href");
}



$("#livesearch .searchBox").hover(
							
	function () {
		currentSelection = $(this).data("number");
		setSelected(currentSelection);
	}, function() {
		$("#livesearch .searchBox a").removeClass("itemhover");
		currentUrl = '';
	}
);
	

	
	

var xmlhttp;

function clearLiveSearch(inp){
	
	if (inp)
		$("#livesearch").fadeOut("slow");
	else 
		$("#livesearch").fadeIn("slow");

}

function showResult(str,language)
{
	


	
	if ( str.length==0 ){
		document.getElementById("livesearch").innerHTML="";
	  	document.getElementById("livesearch").style.border="0px";
	  	return;
	}
	
	xmlhttp=GetXmlHttpObject()
	
	if ( xmlhttp==null ) {
	 	 alert ("Your browser does not support XML HTTP Request");
	 	 return;
	 }
	 
	var url="/"+language+"/search/";
	url=url+"?keyword="+str+"&ajax=true";

	xmlhttp.onreadystatechange=stateChanged ;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged()
{
	if ( xmlhttp.readyState==4 ){
		document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
		clearLiveSearch(0);
		
		//nu();
		
	}
}

function GetXmlHttpObject() 
{
	if ( window.XMLHttpRequest ) {
		return new XMLHttpRequest();// code for IE7+, Firefox, Chrome, Opera, Safari
	}
	if ( window.ActiveXObject ){
		return new ActiveXObject("Microsoft.XMLHTTP");// code for IE6, IE5
	}
	
	return null;
}

