var editindex = 0;
var wym;
var width = 0;

$(document).ready(function() {
	$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel

	/*HOMEPAGE*/
	$('#artwork_scroller').cycle({  fx: 'scrollHorz',
                                    next:   'a.scrollbtn.right', 
                                    prev:   'a.scrollbtn.left'});
    
    $('#artwork_scroller').cycle("pause");
    

    
	//$('.rounded').corner("8px");
	/*TEMPLATE.PHP UITGEBREID ZOEKEN*/
	$('#category_table').hide();
	$('#technique_table').hide();
	$('#discipline_header').toggle(
		function(){
			$('#discipline_table').slideUp("fast");
			$(this).find('img').attr('src', 'images/arr_up.gif');
		},
		function(){
			$('#discipline_table').slideDown("fast");
			$(this).find('img').attr('src', 'images/arr_down.gif');				
		}
	);

		
	$('#category_header').toggle(	
		function(){
			$('#category_table').slideDown("fast");
			$(this).find('img').attr('src', 'images/arr_down.gif');				
		},
		function(){
			$('#category_table').slideUp("fast");
			$(this).find('img').attr('src', 'images/arr_up.gif');
		}
	);

	$('#technique_header').toggle(	
		function(){
			$('#technique_table').slideDown("fast");
			$(this).find('img').attr('src', 'images/arr_down.gif');				
		},
		function(){
			$('#technique_table').slideUp("fast");
			$(this).find('img').attr('src', 'images/arr_up.gif');
		}
	);
	/*USER_PROFILE.PHP*/
	$('#bio').hide();
	$('#tentoonstellingen').hide();
	$('#profiel_header').css("background","#EEEEEE");
	$('#profiel_header').click(
		function(){
			$('#bio').hide();
			$('#bio_header').css("background","#FFFFFF");
			$('#tentoonstellingen').hide();
			$('#tentoonstellingen_header').css("background","#FFFFFF");
			$('#profiel').slideDown("fast");
			$(this).css("background","#EEEEEE");				
		}
	);

		
	$('#bio_header').click(	
		function(){
			$('#profiel').hide();
			$('#profiel_header').css("background","#FFFFFF");
			$('#tentoonstellingen').hide();
			$('#tentoonstellingen_header').css("background","#FFFFFF");
			$('#bio').slideDown("fast");
			$(this).css("background","#EEEEEE");			
		}
	);

	$('#tentoonstellingen_header').click(function(){	
			$('#bio').hide();
			$('#bio_header').css("background","#FFFFFF");
			$('#profiel').hide();
			$('#profiel_header').css("background","#FFFFFF");
			$('#tentoonstellingen').slideDown("fast");
			$(this).css("background","#EEEEEE");				
		}		

	);

	$("form#profiel_wijzigen").validate({
		errorClass: "invalid",
		rules: {
			form_voorletters: "required",
			form_voornaam : "required",
			form_achternaam : "required",
			form_nationaliteit: "required",			
			form_rekening: "required",
			form_bankplaats: "required",
			form_adres : "required",
			form_plaats : "required",
			form_postcode : "required",
			form_nummer : "required",
			form_telefoonnummer : "required",
			form_emailadres : {required : true, email: true}
		}
	});
	$("form#aanmelden_form").validate({
		errorClass: "invalid",
		rules: {	aanmelden_voorletters : "required",
					aanmelden_achternaam : "required",
					aanmelden_adres : "required",
					aanmelden_nummer : "required",
					aanmelden_postcode : "required",
					aanmelden_plaats : "required",
					aanmelden_telefoonnummer : "required",
					aanmelden_wachtwoord : "required",
					overeenkomst : "required" },
		messages : {overeenkomst : "U moet akkoord gaan met de overeenkomst" }
	});
	$("form#reg_form").validate({
		errorClass: "invalid",
		wrapper: "p",
		rules: {
			form_name : "required",
			form_adres : "required",
			form_postcode : "required",
			form_city : "required",
			form_email : {required : true, email: true}
		}
	});
	$("form#kunst_upload").validate({
		errorClass: "invalid",
		rules: {
			form_titel : "required",
			form_afmetingen_x : "required",
			form_afmetingen_y : "required",
			form_afmetingen_z : "required",
			form_jaartal : "required",
			zoek_disc : "required",
			form_prijs : "required",
			form_arttype : {required : true, min:1}
		},
		messages : {form_arttype : "Selecteer een kunstvorm",
					zoek_disc : "Selecteer een discipline" }
	});
	$('#kunst_upload').submit(function() { 
		
		if($("input[name='zoek_disc[]']:checked").length == 0) {
			alert("Je hebt nog geen discipline geselecteerd");
			return false;
		}

		if($("input[name='zoek_cat[]']:checked").length == 0) {
			alert("Je hebt nog geen categorie geselecteerd");
			return false;
		}

		if($("input[name='zoek_tech[]']:checked").length == 0) {
			alert("Je hebt nog geen techniek geselecteerd");
			return false;
		}

		if ($('#kunst_upload').valid()) {
			$.blockUI({
				message: $('#upload_box'),
				css: {
					border: 'none',
					backgroundColor: 'transparent'
				}
			});
			progress();
		}
	});

	$("#sitemap th a").mouseover(function(event){
		var thisTh = this;
		var thNr = -1
		$("#sitemap th a").each(function(i)
		{
			if(this == thisTh)
				thNr = i;
		});
		var liEl = $("#sitemap ul").get(thNr);
		$(liEl).find("li, a").css("color", "black");
		$(this).css("color", "black");
	});
	$("#sitemap th a").mouseout(function(event){
		var thisTh = this;
		var thNr = -1
		$("#sitemap th a").each(function(i)
		{
			if(this == thisTh)
				thNr = i;
		});
		var liEl = $("#sitemap ul").get(thNr);
		$(liEl).find("li, a").css("color", "#888");
		$(this).css("color", "#888");
	});



	$("#sitemap ul").mouseover(function(event){
		var thisTh = this;
		var thNr = -1
		$("#sitemap ul").each(function(i)
		{
			if(this == thisTh)
				thNr = i;
		});
		var thEl = $("#sitemap th a").get(thNr);
		$(thEl).css("color", "black");
		$(this).find("li, a").css("color", "black");
	});

	$("#sitemap ul").mouseout(function(event){
		var thisTh = this;
		var thNr = -1
		$("#sitemap ul").each(function(i)
		{
			if(this == thisTh)
				thNr = i;
		});
		var thEl = $("#sitemap th a").get(thNr);
		$(thEl).css("color", "#888");
		$(this).find("li, a").css("color", "#888");
	});

	$(".showEditors a").click(function(event){
		var templateFunctionsHtml = "<div class='functions_panel'><h2>Dynamische Inhoud</h2><ul>" + template_functions() + "</ul></div>";
		elements = $("div.editable");
		elements.each(
			function( intIndex ){
				editindex++;
				wym = $(this).wymeditor({
					html: '',
					logoHtml: '',
					stylesheet: 'wymstyle.css',
					containersItems: [
						{'name': 'H1', 'title': 'Heading_1', 'css': 'wym_containers_h1'},
						{'name': 'H2', 'title': 'Heading_2', 'css': 'wym_containers_h2'},
						{'name': 'P', 'title': 'Paragraph', 'css': 'wym_containers_p'}
					],

					skin: 'default',
					lang: 'nl',
					postInit: function(wym) {
						//render the containers box as a panel
						//and remove the span containing the '>'
						jQuery(wym._box).find(wym._options.containersSelector)
							.removeClass('wym_dropdown')
							.addClass('wym_panel')
							.find('h2 > span')
							.remove();
						//adjust the editor's height
						jQuery(wym._box).find(wym._options.iframeSelector)
							.css('height', '400px').css('marginTop', '10px');
						$.ajax({
							type: "GET",
							url: "include/get_teksten.php",
							data: "page1="+page1+"&page2="+page2+"&page3="+page3,
							success: function(msg){
								wym.html(msg);
							}
						});
						jQuery(wym._box).find(wym._options.classesSelector)
							.after("<input type='submit' class='submit' onclick='cancelWym()' style='color:#888; width:150px; margin-top:2px' value='Annuleren' />").after("<input type='submit' onclick='submitWym()' style='width:150px; margin-top:20px' class='submit' value='Opslaan' />").after(templateFunctionsHtml);
					}
				});
			}
		);
		if (editindex > 0)
		{
			$(this).parent().html("");
		}
	});

	if ($("div.editable").length == 1)
	{
		$(".showEditors").css('display', 'inline');
	}
});

function submitWym()
{
	cnf = confirm("Weet u zeker dat u alle wijzigingen wilt opslaan?");
	if(cnf)
	{

		myContent = escape($.wymeditors(0).xhtml());

		$.ajax({
			type: "POST",
			url: "include/submit_cms.php",
			data: "page1="+page1+"&page2="+page2+"&page3="+page3+"&content="+myContent,
			success: function(msg){
				location.reload();
			}
		});
	}
	else
	{
		return false;
	}
}


function cancelWym()
{
	cnf = confirm("Weet u zeker dat u alle wijzigingen wilt verwerpen?");
	if(cnf)
	{
		location.reload();
	}
	else
	{
		return false;
	}
}

function template_functions()
{
	var content = "";
	functions = [
		{'naam': 'Aanmelden Nieuwsbrief', 'code': '{{NIEUWSBRIEF AANMELDEN}}'},
		{'naam': 'Kunstenaars A-Z', 'code': '{{KUNSTENAARSAZ}}'}
	]

	for (i = 0; i < functions.length; i++)
	{
		content += "<li><a href='' onclick='$.wymeditors(0).insert(\""+ functions[i]['code'] +"\"); return false'>" + functions[i]['naam'] + "</a></li>";
	}

	return content;
	
}

function progress(){
	if(width > 200){
		width = 0;
		$('#progress_bar').css('width', width);
		progress();
	}
	else if(width != 220){
		
		setTimeout("progress()", 350);
		$('#progress_bar').css('width', width)
		width += 10;
	}		
}

var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=500,height=550,scrollbars=1';

function raw_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  }
  if (isUndefined(target)) {
    target = '_blank';
  }
  var theWindow =
    window.open(url, target, features);
  theWindow.focus();
  return theWindow;
}

function link_popup(src, features) {
    raw_popup(src.getAttribute('href'), features);
	return false;
}

function open_reserveren_form(src){
	features = 'location=0,statusbar=0,menubar=0,width=800,height=700,scrollbars=1';
	raw_popup(src.getAttribute('href'), '_blank', features);
}

function isUndefined(v) {
    var undef;
    return v===undef;
}

function checkPasswords() {
	var password1 = $("#aanmelden_wachtwoord1").attr("value");
	var password2 = $("#aanmelden_wachtwoord2").attr("value");
	if(password1==password2)
	{
		$("#wachtwoorden_p").css("display", "none");
		$("#aanmelden_submit").removeAttr("disabled");
	}
	else
	{
		$("#wachtwoorden_p").css("display", "block");
		$("#aanmelden_submit").attr("disabled", "1");
	}
}

function submitStatusForm(e) {
	if(confirm("Weet u zeker dat u de status van dit kunstwerk wilt aanpassen?"))
	{
		var el = $("#statusForm_"+e);
		el.submit();
	}
	else
	{
		return false;
	}
}

function voegRijToe(arg)
{	
	if (arg != null) {
		$(".inputs"+arg+":last").after("<tr class=\"multiple_row inputs"+arg+"\">" + $(".inputs"+arg+":last").html() + "</tr>");
		$(".inputs"+arg+":last").find("input").attr("value", "");
	}
	else{
		$(".inputs:last").after("<tr class=\"multiple_row inputs\">" + $(".inputs:last").html() + "</tr>");
		$(".inputs:last").find("input").attr("value", "");
	}
}


function deleteRij(element,arg)
{
	if(arg!=null){
		aantalRijen = $(".inputs"+arg+"").length;
	} else {
		aantalRijen = $(".inputs").length;
	}
	if(aantalRijen >1)
		$(element).parent().parent().remove();
		
	else
	{
		$(element).parent().parent().find("input").attr("value", "");
	}

}
