var hostname = window.location.hostname;

$(document).ready(function (){
		
	Cufon.replace('.button_newstyle',{ hover: true});
	//Cufon.replace('h2');
		
	if(window.location.hash){
		var id = window.location.hash;
		if(id != '#overview'){
			item_lightbox(id);
		}
	}
	
	$('.lightbox').bind('click',function(){
		var id = $(this).attr("href");
		item_lightbox(id);
	});
		
	var timer
	$(".captchaimg").mouseover(function(e) {
		    timer = setTimeout( function(){
			$(".captcha_explain").fadeIn("slow");
		}, 1300 )}).mouseout(function(){
		  clearTimeout( timer );
		  $(".captcha_explain").fadeOut("slow");
		});
	
	
	$(".recaptcha").click(function(){
		ko = new Date();
		ji = ko.getTime();		
		var url = "<img src=\"http://" + hostname + "/home/captcha/" + ji + "\">";
		$(".captchaimg").html(url);
		return false;
	});
	
	
	

	$('.onhold').css({'opacity' : '0.4'});
	
	  //make sold items non-transparent
   $('.onhold').bind('mouseenter',function (){
      $(this).animate({opacity: 1},{queue:false, duration:500 } );
      //return false;
   });

   //make sold items transparent
   $('.onhold').bind('mouseleave',function (){
      $(this).animate({opacity: 0.4},{ queue:false, duration:500 });
      //return false;
   });

	
	$("#imageorder, #contactform").submit(function(){	
		var errors = 0;
		$("#" + this.getAttribute("id") + " .required").each(function(){
			var bla = $(this).val();
			if(isEmpty(bla)) {
				$(this).animate({ backgroundColor: "#FF4455", color: "white" }, 1000);
				errors++;
			}else{
				$(this).animate({ backgroundColor: "white", color: "black" }, 1000);
			}
	 	});
		
		$("#" + this.getAttribute("id") + " .email").each(function(){
			var bla = $(this).val();
			if(!isEmail(bla)) {
				$(this).animate({ backgroundColor: "#FF4455", color: "white" }, 1000);
				errors++;
			}else{
				$(this).animate({ backgroundColor: "white", color: "black" }, 1000);
			}
		});
		
		
		$("#" + this.getAttribute("id") + " .checkbox").each(function(){
			var bla = $(this).val();
			if($(this).is(':checked')) {
				$("span.voorwaarden").animate({ backgroundColor: "white", color: "black" }, 1000);		
			}else{
				$(".voorwaarden").animate({ backgroundColor: "#FF4455", color: "white" }, 1000);
				errors++;
			}
		});
		
		var parameters = {
			captcha: this.captcha.value
	    };
	
	    var url = "/home/checkcaptcha";

		$.post(url,parameters,onComplete1);
		working = false;
	
		function onComplete1(response) {
			if(response == 'false'){
				$(".captchainput").animate({ backgroundColor: "#FF4455", color: "white" }, 1000);
				ko = new Date();
				ji = ko.getTime();		
				var url = "<img src=\"http://www.joli.nl/home/captcha/" + ji + "\">";
				$(".captchaimg").html(url);
			}
			else if(response == 'empty') {
				$(".captchainput").animate({ backgroundColor: "#FF4455", color: "white" }, 1000);
			}
			else {
				$(".captchainput").animate({ backgroundColor: "white", color: "black" }, 1000);
			}
		}
		
		if(errors == 0){
			//return false;
		}else{
			return false;
		}
	});		
});

function isEmail(email) {
	if (/^\w+([\.-\\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)) return true;
	else return false;
}

function isEmpty(value) {
	if (value.length > 0) return false;
	else return true;
}

function isNumeric(value) {
	if (value.length > 0) return false;
	else return true;
}	

function item_lightbox(id){
		
	var id = parseInt( id.substring( 1 ) );
	
	var url = window.location.hostname;
	var url = 'http://' + url + window.location.pathname;

	

	// check if url ends on "/"
	var lastchar = url.charAt( url.length -1 );

	if(lastchar != '/'){
		var url = url + '/';
	}
	// end check + manipulation url

	var url = url + id;

	var html = $.ajax({
	  url: url,
	  async: false
	 }).responseText;

	var newStr = "<div id=\"lb\"></div><div id=\"lbimg\"><a href=\"#\" title=\"sluit item\" id=\"lbclose\">&nbsp;</a>" + html + "</div>";

	$("body").append(newStr);
	
	////////
	
	$("#lbimgimg").bind('load', function() {
		imgwidth = $("#lbimgimg").width();
		$("#imageinfo").css( "width", imgwidth );
		$("#imageinfo").fadeIn("slow");
		$("#lb").css("width",$(document).width());
		$("#lb").css("height",$(document).height());
		$("#container").css("height",$(document).height());
	});
	
	$("#lb").css("width",$(document).width());
	$("#lb").css("height",$(document).height());
	$("#container").css("height",$(document).height());
	$("#lb").fadeIn("slow");
	
	$("#lb").animate({ 
        opacity: 0.8
      }, 1500 );
	
	$("#lbimg").css( "height", $(document).height() );
	$("#lbimg").fadeIn("slow");
			
	$("#lbimg,#lbimgimg").bind('click',function (){
		$("#lb").fadeOut("slow");
		$("#lbimg").fadeOut("fast");
		$("#lb").remove();
		$("#lbimg").remove();
		$("#lbimgimg").remove();
		$("#container").css("height",$(document).height());
		//remove hashcode
		window.location.hash = '#overview';
		return false;
	});
		
	$(".order").bind('click',function (){
			//window.location.hash = '';
			var url2 = 'http://' + window.location.hostname + '/shop/';
			
			var lastchar = url2.charAt( url2.length -1 );

			if(lastchar != '/'){
				var url2 = url2 + '/';
			}
			
			var url2 = url2 + 'order/' + $(this).attr("id");
			
			location.href = url2;
			return false;
		});
	
	///////

	return false;
}