//jquery init
$(document).ready(function(){
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	$('a.external').click(function(evt){
		evt.preventDefault();
		window.open($(this).attr('href'));
	});
	$("iframe").each(function(){
		$(this).attr('src', $(this).attr('src'));
	});
	$("#form_contact_geboortedatum, #form_snoepshots_birthday, input#birthday").mask("99/99/9999");
	setSubgenres(true);
	setShottypes(true);
	$('#list-fiches li').mousemove(function(e){
		$('#tooltip').css('left', e.pageX - 6).css('top', e.pageY - $('#tooltip').height() - 5);
	});
	$('#list-fiches li').hover(function(e){
		$('#tooltip div.inner').html('');
		$('#tooltip').show();
		//console.log($(this).find('h2'));
		$('#tooltip div.inner').append($(this).find('h2').clone()).append($(this).find('h3').clone());
	}, function(){
		$('#tooltip').hide();
	});
	/*$('#youtube-form').submit(function(evt) {
		var allowed = $(this).find('#file').attr('accept').split(', ');
		var currentFile = $(this).find('#file').val();
		var currentExt = currentFile.substr(currentFile.lastIndexOf('.') + 1).toLowerCase();
		
		var found = false;
		for (var i = 0; i < allowed.length; i++) {
			if (allowed[i] == currentExt) {
				found = true;
			}
		}
		if (!found) {
			evt.preventDefault();
			alert('Het bestand dat u probeert op te laden wordt niet ondersteund door youtube: (*.' + currentExt + ')');
		}
		
		
		
		
	});*/
	$('div.checkbox').click(function(){
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');
		} else {
			$(this).addClass('active');
		}
		if (!$(this).hasClass('shots')) {
			setSubgenres(false);
		} else {
			setShottypes(false);
		}
	});
	var radius = 25;
	if ($.browser.msie && $.browser.version < 9) {
		radius = 17;
	}
	$('#column-center div#contentholder').corner("round " + radius + "px").parent().css("padding", "4px").corner("round " + radius + "px");
	
	radius = 12;
	$('#tooltip div.inner').corner("round " + radius + "px").parent().css("padding", "2px").corner("round " + radius + "px");
	$('#checkboxes div.inner').corner("round " + radius + "px").parent().css("padding", "4px").corner("round " + radius + "px");
	$('div.round').corner("round " + radius + "px").parent().css("padding", "4px").corner("round " + radius + "px");	
	
	//change logo in splash screen
	$('#splash a').hover(function(){
		$('#splash').find('img').attr('src', '/media/images/interface/logo_' + $(this).attr('id') + '.png');
	}, function(){
		$('#splash').find('img').attr('src', '/media/images/interface/logo_center.png');
	});
	
	//$('a.colorbox').colorbox();
	$('a.colorbox').click(function(evt){
		evt.preventDefault();
	});
	//embed snoepshots
	swfobject.embedSWF("/media/swf/snoepshots.swf?" + Math.random() * 99999, "snoepshots", "164", "570", "9.0.0","expressInstall.swf", {taal:taal, url:website, admin:admin, isshots:isshots}, {wmode: "transparent"}, {});
});
function setShottypes(init) 
{
	var arrChecked = [];
	$('div.checkbox').each(function() {
		if ($(this).hasClass('shots')) {
			if ($(this).hasClass('active')) {
				arrChecked.push($(this).attr('id'));
			}
		}
	});
	var arrShots = [];
	$('div#shots ul#list-fiches li').each(function(){
		for (var i = 0; i < arrChecked.length; i++) {
			if ($(this).hasClass(arrChecked[i])) {
				if (arrShots.indexOf($(this)) == -1) {
					arrShots.push($(this));
				}
			}
		}	
		$(this).hide();
	});
	$(arrShots).each(function(){
		$(this).show();
	});
	if (!init) {
		$.ajax( {
			type : "POST",
			url : "/ajax/set-shottypes/",
			data : "&types=" + arrChecked.join(";"),
			success : function(msg) {
			}
		});
	}
}

function setSubgenres(init) 
{
	var arrChecked = [];
	//push all checked boxes in an array
	$('div.checkbox').each(function(){
		if (!$(this).hasClass('shots')) {
			if ($(this).hasClass('active')) {
				arrChecked.push($(this).attr('id').split('subgenre_').join(''));
			}
		}
	})
	
	var arrProducts = [];
	$('div.products ul#list-fiches li').each(function(){
		for (var i = 0; i < arrChecked.length; i++) {
			if ($(this).hasClass('genre_' + arrChecked[i])) {
				if (arrProducts.indexOf($(this)) == -1) {
					arrProducts.push($(this));
				}
			}
		}	
		$(this).hide();
	});
	
	$(arrProducts).each(function(){
		$(this).show();
	});
	
	if (!init) {
		$.ajax( {
			type : "POST",
			url : "/ajax/set-genres/",
			data : "&genres=" + arrChecked.join(";"),
			success : function(msg) {
			}
		});
	}
}

function formCode(form)
{
	if (form == "form_newsletter") {
		$("#form_newsletter_name").hide();
		$("#form_newsletter_email").hide();
		$("#form_newsletter_submit").hide();
		
		$("#form_newsletter_firstname").click(function(){
			$("#form_newsletter_name").show();
			$("#form_newsletter_email").show();
			$("#form_newsletter_submit").show();
		});
		
		$("#form_newsletter input").click(function(){
			$(this).val("");
		})
		
		$("#" + form + "_submit").click(function(){
			var firstname = $("#form_newsletter_firstname").val();
			firstname = firstname.split("voornaam").join("");
			$("#form_newsletter_firstname").val(firstname);
		
			var name = $("#form_newsletter_name").val();
			name = name.split("naam").join("");
			$("#form_newsletter_name").val(name);
			
			var email = $("#form_newsletter_email").val();
			email = email.split("email").join("");
			$("#form_newsletter_email").val(email);
			
			validate_form(form);
			return false;
		});
	} else {
		$("#" + form + "_submit").click(function(){
			validate_form(form)
			return false;
		})
	}
}


//form validation
var validate = true;
function validate_form(form) {
	if (form == "form_contact") {
		var optin = $("#form_contact_optin").attr('checked');
		if (!optin) {
			$("#form-item-optin").addClass("error");
			return false;
		}
	}
	//functie die automatisch een formulier valideert via ajax, je moet hier niets veranderen
	$("#form-item-optin").removeClass("error");
	$("#" + form + "_submit").hide();
	$("#snoepshot_foto_submit-nl").hide();
	$("#snoepshot_foto_submit-fr").hide();
	$("#contest_submit-nl").hide();
	$("#contest_submit-fr").hide();
	var validate = true;
	
	if (form == "form_reserve") {
		form = "form_abo";
		var r1Obj = $("#form_abo_r1_id");
		var r2Obj = $("#form_abo_r2_id");
		if (r1Obj.val() == r2Obj.val()) {
			r1Obj.attr("class", "error");
			r2Obj.attr("class", "error");
			validate = false;
		}
	}

	if (validate) {
		var params = $("#" + form).serialize();
		params += "&form_id=" + form;
		params += "&id=" + id;

		if (params.indexOf("form_abo_voornaam1") == -1) {
			var aantal = $("#form_abo_aantal");
			
			for (var i = 0; i < aantal; i++) {
				params += "&form_abo_voornaam" + (i+1) + "=" + $("form_abo_voornaam" + (i+1));
			}
		}
		
		
		$("#submit_form").attr("disabled", true);
		$("#submit_form").hide();
		
		
		$.ajax( {
			type : "POST",
			url : "/ajax/validate-form/",
			data : params,
			success : function(msg) {
				if (form != "form_event_surfer") {
					if (form != "form_snoepshots" && form != "form_contact" && form != "form_contest") {
						$("#" + form + "_submit").show();
					}
					$("#snoepshot_foto_submit-nl").show();
					$("#snoepshot_foto_submit-fr").show();
					$("#contact_submit-nl").show();
					$("#contact_submit-fr").show();
					$("#contest_submit-nl").show();
					$("#contest_submit-fr").show();
				} else {
					$("#submit_form").show();
				}
				
				validate = true;
				
				$("#submit_form").removeAttr("disabled");
			
				msg = eval(msg);
	
				$("input, textarea, select").each( function() {
					if (this.id.indexOf(form) != -1) {
			
						$(this).removeClass("error");
						$(this).removeClass("error_text");
						$(this).addClass($(this).attr("type"));
					}
				});
	
				if (msg) {
					var goNext = true;
					

					if (form == "form_newsletter") {
						if (!msg[0]) {
							if ($("#form_newsletter_email").val() != $("#form_newsletter_email2").val()) {
								goNext = false;
								$("#form_newsletter_email").focus();
								$("#form_newsletter_email").select();
								$("#form_newsletter_email").attr("class", "error");
								$("#form_newsletter_email2").attr("class", "error");
								$("#submit_form").show();
							}	
						}
					}
					
					
					if (form == "form_abo") {
						goNext = false;						
					}
					
					if (msg[0]) {
						
						$("#submit_form").show();
					
						for ( var i = 0; i < msg[1].length; i++) {
							var obj = $("#" + msg[1][i]);
							
							if (!obj.attr("id")) {
								$('input').each( function() {
									if (this.id.indexOf(msg[1][i])!=-1) {	
										
										obj = $("#" + this.id);
									}
								});
								
							}
								
							if (i == 0) {
								obj.focus();
								obj.select();
							}
							
							var error = "error";
							var type = obj.attr("type");
							
							if (type == "text" || type == "password") {
								error += "_" + type;
							}
	
							if (obj.attr("type") == "checkbox") {
								obj.parent().attr("class", error);			
							}
							
							obj.attr("class", error);
						}
					} else if (goNext) {
						if (form == "form_contact") {
							//contactSubmit();
							window.location = "/" + taal + "/contact/thankyou";
						}else if (form == "form_person") {
							repetitionSubmit();
						}else if (form == "form_contest") {
							window.location = "/" + taal + "/contest/thankyou";
						} else {
							//window.location = "/surfers/succes";
							window.location = "/" + taal + "/shots/form/thankyou";
						}
					} else {
						window.location = msg[1];						
					}
		
				}
			}
	});
	}
	return false;
}

