jQuery.fn.center = function (fixed) {
	if(fixed == true){
		this.css("position","fixed");
		this.css("top", ( $(window).height() - this.outerHeight() ) / 2 + "px");
		this.css("left", ( $(window).width() - this.outerWidth() ) / 2 + "px");
	}else{
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.outerHeight() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.outerWidth() ) / 2+$(window).scrollLeft() + "px");
	}
    return this;
}

	var dateFormat		= "yyyy-mm-dd";
	var dateFormatShow	= "dd mmmm `yy";
	Date.format 		= dateFormat;
	var mouseoverTimer	= false;
	var clickedEl		= false;
	var clickFunctions	= new Array();
	var loggedIn		= '0';
	var bottomContentT	= false;

	$(document).ready(function() 
	{
		init();
			checkLogin();
			if(loggedIn != '0'){
				$(".loggedin").show();				
			}else{
				$("div#bottom_container div.specials").show();
			}
			bottomContentT = setTimeout(function(){
					$("div#bottom_container div.bottom_content:visible").slideUp(1000);
				},
				2000);
				
			$('.bt').each(function(){
				$(this).bt({
					  shrinkToFit: true,
					  padding: 10,
					  cornerRadius: 10,
					  contentSelector: "$(this).attr('title')",
					  positions: ['top'],
					  fill: '#cc0000',
					  strokeStyle: '#ffffff',
					  showTip: function(box){
						$(box).fadeIn(500);
					  },
					  hidetip: function(box, callback){
						$(box).fadeOut(500);
					  },
					  cssStyles: {color: '#ffffff'}				  
					});
			});

			if($("#egk_datumShow").length > 0){
				$("#egk_datumShow").datePicker(
					{
						closeOnSelect	: true,
						displayClose	: true,
						clickInput		: true,
						createButton	: false,
						renderCallback:function(td,thisDate,month,year){
							if(thisDate.isWeekend()){
								td.addClass('weekend');
							}
							if(in_array(thisDate.asString(dateFormat),dates)){
								td.addClass('selectable');
							}else{
								td.addClass('disabled');
							}
						}
					})
					.bind(
							'dpClosed',
							function(e,selected,prop){
								var value = ''
								if(typeof(selected[0]) != 'undefined'){
									value = selected[0];
									var dvalue = value.asString('mm/dd/yyyy');
									var d = new Date(dvalue);
									$("#egk_datum").val(d.asString(dateFormat));
									$("#egk_datumShow").val(d.asString(dateFormatShow));
								}
							}
					);	
			}
			$("li.bottom_label").each(function(){
				$(this).bind("mouseover",function(){
					clearTimeout(bottomContentT);
					var label = $(this).attr("rel");
					mouseoverTimer = setTimeout(function(){
							if($("div#"+label+"Content").is(":hidden")){
								$("div.bottom_content").hide();
								$("div#"+label+"Content").show();
							}
						},350);
				}).bind("mouseout",function(){
					clearTimeout(mouseoverTimer);
				});
			});
			$("img.bottom_cross").bind("click",function(){
				var label = $(this).parents("li:first").attr("rel");
				$("div#"+label+"Content").hide();
			});
			
	});

	function init()
	{
		if($("#egkformulier").length > 0 && egk_freecode > 0){
			$("#egk_actiecode").val('vervallen').parents('p:first').hide().after('<p class="red"><b>Geen administratiecode nodig<b/></p>').next().show();
		}
		menubloks();
		tb_remove();
		sfhover('nav');
		$('#footer ul li:last, div.left ul:eq(0) li:last, div.right ul:eq(0) li:last').addClass('last');
		$('#top ul.nav li a:first').addClass('first');
		
		/* replace rating-elements by images */
		$('span.rating').each(function() {
			var color = '';
			if($(this).hasClass('black')){
				color='black.';
			}
			var aRating = $(this).text().split(' ');
			$(this).html('<img src="/style/dagjeuit_default/img/web/rating/' + color + aRating[1] 
				+ '.gif" alt="Waardering ' + aRating[1] + ' op /5" title="Waardering ' + aRating[1] + '/5" />');
		});
		/* replace wheather-elements by images */
		$('span.wheather').each(function() {
			$(this).html('<img src="/style/dagjeuit_default/img/web/wheather/' + $(this).text() 
				+ '.gif" alt="' + $(this).text() + '" title="' + $(this).text() + '" />');
		});
		
		var search_wat = $('#search_wat').val();
		var search_waar = $('#search_waar').val();
		$('#search_wat').focus(function(){
			if($(this).val() == search_wat){
				$(this).val("");
			}
			}).blur(function(){
				if($(this).val() == ""){
					$(this).val(search_wat);
				}
			});
		$('#search_waar').focus(function(){
			if($(this).val() == search_waar){
				$(this).val("");
			}
			}).blur(function(){
				if($(this).val() == ""){
					$(this).val(search_waar);
				}
			});
		$('#front_search').bind('submit',function(){
			if($('#search_wat').val() == search_wat){
				$('#search_wat').val("");
			}
			if($('#search_waar').val() == search_waar){
				$('#search_waar').val("");
			}
		});
		$('form.search_watwaar').bind('submit',function(){
			if($('.search_wat').val() == search_wat){
				$('.search_wat').val("");
			}
			if($('.search_waar').val() == search_waar){
				$('.search_waar').val("");
			}
		});	
		$("#focus").focus();
		$("a.clickurl").click(function(){
			if(rel = $(this).attr("rel")){
				$(this).attr("href",rel);
			}
			if(analytics = $(this).attr("analytics")){
				pageTracker._trackPageview(analytics);
			}
			return true;
		});

		$("#verrasme").bind("click",function(){
			pageTracker._trackPageview('verrasme');
			window.open('/zoeken/verrasme.php','_self');
		});
		
		$("#content div:first h2:first").addClass("titel");
		
		$(".verplicht + td input,.verplicht + td textarea").css("border","1px solid red");

		$("input#egk_versturen").bind('click',function(){
			var datum 	= $("#egk_datum").val();
			var actcode	= $("#egk_actiecode").val();
			var naam	= $("#egk_naam").val();
			var plaats	= $("#egk_plaats").val();
			var email	= $("#egk_email").val();
			var jaar	= $("#egk_jaar").val();
			var maand	= $("#egk_maand").val();
			var dag		= $("#egk_dag").val();
			var nieuws	= $("#egk_nieuwsbrief:checked").val();
			if(nieuws == 1){
				nieuws = "F13CB85D-56CE-B081-8C84-42C9F3554595";
			}
			var geslacht= $("#egkformulier input:radio[name=geslacht]:checked").val();
			var guid	= $("#egk_guid").val();
			if(typeof(datum) == "undefined" || datum == ""){
				alertthis("Je bent vergeten een datum in te voeren");
				return false;
			}
			if(egk_freecode == 0){
				if(typeof(actcode) == "undefined" || actcode == ""){
					alertthis("Je bent vergeten een administratieve code in te voeren");
					return false;
				}
			}
			if(typeof(naam) == "undefined" || naam == ""){
				alertthis("Je dient je naam op te geven");
				return false;
			}
			if(typeof(plaats) == "undefined" || plaats == ""){
				alertthis("Je dient je woonplaats op te geven");
				return false;
			}
			if(typeof(email) == "undefined"  || email ==""){
				alertthis("Je dient je email adres op te geven");
				return false;
			}
			if(typeof(geslacht) == "undefined" || geslacht == ""){
				alertthis("Je bent vergeten je geslacht aan te geven");
				return false;
			}
			var rand = Math.floor(Math.random()*10001);
			$.post(
				"/egk.php",
				{
					action	: 'docheck',
					datum	: datum,
					actcode	: actcode,
					guid	: guid,
					jaar	: jaar,
					maand	: maand,
					dag		: dag,
					plaats	: plaats,
					geslacht: geslacht,
					email	: email,
					nieuws	: nieuws,
					naam	: naam,
					r		: rand
				},
				function(data){
					var typeResponse = $('result',data).text();
					var melding		 = $('response',data).text();
					if(typeResponse == 'error'){
						alertthis(melding,'Error');
						return false;
					}else{
						showEgkMelding(melding);
					}
				}
			
			);
			return false;
		});
	};


	function addToFavorites(url,title){
	   if (document.all)
		 window.external.AddFavorite(url, title);
	   else if (window.sidebar)
		 window.sidebar.addPanel(title, url, "");
	   else if (window.sidebar&&window.sidebar.addPanel)
		 window.sidebar.addPanel(title,url,"");

		return true;
	}

	var prevEditorId = 0;
	function toggleEditor(id, newWidth, newHeight) {
		if(document.getElementById(prevEditorId) && tinyMCE.get(prevEditorId)){
			$('#div_'+prevEditorId).css({display:'block'});
			tinyMCE.execCommand('mceRemoveControl', false, prevEditorId);
			$('#divtext_'+prevEditorId).html($('#'+prevEditorId).text());
			$('#'+prevEditorId).css({width:'1px',height:'1px'});
		}
		if (!tinyMCE.get(id)){
			$('#div_'+id).css('display','none');
			$('#'+id).css({width: newWidth,height: newHeight});
			tinyMCE.execCommand('mceAddControl', false, id);
			prevEditorId = id;
		}
	}

	function addLocalFavorite(url,naam){
		if(loggedIn != '0'){
			var message = "<h3>Eigen favorieten</h3><p>"+naam+" toevoegen aan je eigen lijstje van favorieten?</p>";
			if(typeof(impromptu) != "undefined"){
				type = "KeysiteInfo";
				$.prompt(
					message,
					{	
						callback: function(v,m,f){
								if(v){
									doAddLocalFavorite(url);
								}else{
									return false;
								}
						},
						buttons : {Ja : url, Nee : false},
						prefix: type,
						overlayspeed: 'fast'
					});

			}else{
				alert('het is op dit moment helaas niet mogelijk om deze pagina aan je lijst met favorieten toe te voegen. Probeer het later nogmaals en mocht dit blijven neem dan contact met ons op');
				return true;
			}
		}
	}

	function doAddLocalFavorite(url){
		$.get(url, function(data){
			alertthis(data,'Info');
		});
	}

	function copyGelegenheid(url,naam,type){
		if(loggedIn != '0'){
			if(typeof(type) != "undefined" && type == "K"){
				var buttons = {Kortingsbon : url+'&type=K', Annuleren : false};
				var message = "<h3>"+naam+"</h3><p>Gebruiken als basis voor een nieuwe kortingsactie?</p>";
			}else{
				var buttons = {Evenement : url+'&type=E', Gelegenheid : url+'&type=G', Annuleren : false};
				var message = "<h3>"+naam+"</h3><p>Gebruiken als basis voor een nieuw evenement of gelegenheid?</p>";
			}
			
			if(typeof(impromptu) != "undefined"){
				type = "KeysiteInfo";
				$.prompt(
					message,
					{	
						callback: function(v,m,f){
								if(v){
									window.open(url,'_self');
								}else{
									return false;
								}
						},
						buttons : buttons,
						prefix: type,
						overlayspeed: 'fast'
					});

			}else{
				alert('Het is op dit moment helaas niet mogelijk om de vermelding te dupliceren. Probeer het later nogmaals en mocht dit blijven neem dan contact met ons op');
				return true;
			}
			
		}
		
	}

		function openPage(url,target,tracker){
			pageTracker._trackPageview(tracker);
			window.open(url,target)
		}

		function switchView(view,hide){
			var toView = "box"+view;
			var toHide = "box"+hide;
			$("#"+toView).show();
			$("#"+toHide).hide();
		}

	function stopInlinePopup(name){
		var cookieName = 'inline_popup_'+name;
		$.cookie(cookieName,'true',{path : '/',expires: 60});
		tb_remove();
	}
	
	function showInlinePopup(title, url, width, height,name,extra){
		try {
			var cookieName = 'inline_popup_'+name;
			if($.cookie(cookieName) != null){
				return;
			}
			setTimeout(function(){
				tb_show(title,url+"&KeepThis=true&TB_iframe=true&width="+width+"&height="+height+"&"+extra);
				},500);
		}catch(err){}
	}
	
	function menubloks(){
		$("div.menublok_container").hide();
		$("#top ul.nav li").each(function(){
			var id = $(this).attr("id");
			id = id.replace("sitemenu_li_","");
			$(this).hover(
				function(){
					menublok(id,'show');
				},
				function(){
					menublok(id,'hide');
				}
			);
		});
		$("div.menublok_container").each(function(){
			var id = $(this).attr("id").replace("menublok_","");
			$(this).hover(
				function(){
					menublok(id,'keep');
				},
				function(){
					menublok(id,'hide');
				}
			);
		});
	}
	
	var menublok_timer;
	function menublok(id, action){
		if(action =='show'){
			clearTimeout(menublok_timer);
			$("div.menublok_container").hide();
			$("div#menublok_"+id).slideDown("50");
		}else if(action == 'keep'){
			clearTimeout(menublok_timer);
		}else{
			menublok_timer = setTimeout(function(){
							$("div#menublok_"+id).slideUp("50");
						},1000);
		}
		
	}
	
	function showEgkMelding(melding){
		alertthis(melding,'Info');
		$("#egk_actiecode").val("");
		$("#egk_nieuwsbrief").attr("checked",false);
	}
	
	function showAjaxLogin(obj){
		clickedEl = obj;
		if($("#ajax_login").length < 1){
			$.get("/live.php?action=ajaxShowLogin",function(data){
				$(data).appendTo("body");
				$("#ajax_noclick_layer").show();
				$("#ajax_login").center(true).hide().slideDown("slow");				
			});
		}else{
			$("#ajax_noclick_layer").show();
			$("#ajax_login").center(true).hide().slideDown("slow");				
		}
	}
	
	function ajaxLogout(){
		$.get("/beheer/login.php?action=logout");
		$("#profile img:first").attr("src","/style/dagjeuit_default/img/web/user/login.png");
		loggedIn = 0;
		checkLogin();
		$(".loggedin").hide();		
	}
	
	function hideAjaxLogin(){
		$("#ajax_noclick_layer").hide();
		$("#ajax_login").css("left",-1000);
	}
	
	function ajaxLoginVersturen(){
		$("#ajax_login_error").html("").hide();
		var email 	= $("#ajax_login_email").val();
		var pw 		= $("#ajax_login_pw").val();
		var callback= $("#ajax_login_callback").val();
		$("#ajax_login_pw").val("");
		$.post("/ajax",{email: email, pw: pw, callback: callback, action:"doAjaxLogin"},function(xml){
			var response = $(xml).find("response:first").text();
			if($(xml).find("error").length){
				$("#ajax_login_error").show().html(response);
			}else{
				loggedIn = 1;
				checkLogin();
				hideAjaxLogin();
				$(".loggedin").show();
				setTimeout(function(){
						if($(clickedEl).attr("onclick") || $(clickedEl).attr("click")){
							$(clickedEl).click();
						}else{
							window.location.href=$(clickedEl).attr("href");
						}
					},100);
			}
		},"xml");
		pw = false;
	}

	function checkLogin(){
		$(".checkLogin").each(function(){
			if(loggedIn == '0'){
				$(this).bind("click",function(){
					showAjaxLogin($(this));
					return false;
				});
			}else{
				$(this).unbind("click");
			}
		});
	}
	
	function showHoeZitDatForm(){
		showHoeZitDatFormulier();
	}
	function showHoeZitDatFormulier(){
		if(loggedIn != '0'){
			alert('ja nu kan ie getoont worden');
			return true;
		}
	}
var addthis_config = {
     ui_language: "nl",
	 ui_cobrand: "Dagjeuit.com",
	 services_compact: 'favorites, email, hyves, facebook, twitter, nujij, igoogle, digg, more',
	data_track_clickback: true
}
