/*
	Javascript for Werkwebsite

	version 0.1 - 10-09-2009

*/

function clickCallback(did) {

	var loc = 'Download/' + did+'/'+Math.random();

	var b = document.getElementsByTagName('base');
	if (b && b[0] && b[0].href) {
		if (b[0].href.substr(b[0].href.length-1) == '/' && loc.charAt(0) == '/') {
			loc = loc.substr(1);
		}
		loc = b[0].href + loc;
	}

	jQuery.ajax({
		url: loc,
		async:   false,
		timeout: 5000
	});
}

/* using a sync request so that the code will be executed. If there is an asynchronious request, the browser might move away without counting the click */

var lastMouse = '';
var lastDid = '';

function testMouseDown(did) {
	lastMouse = 'down';
	lastDid = did;
}
function testMouseUp(did) {
	if (lastMouse == 'down') {
		if (lastDid == did) {
			// event was fired on the same event as mousedown was.
			clickCallback(did);
		}
	}
}

function tagUrls() {
	var aLinks = document.getElementsByTagName('a');

	var regexp_domain="werkwebsite";
	var regexp_ishttp=/(http(.)*:\/\/)/;


	for (var i=0;i<aLinks.length;i++) {

		var eachLink = aLinks[i];

		//Check if the link is valid and is an external url

		if((eachLink.href != null) && (eachLink.href.match(regexp_domain) == null) && eachLink.href.match(regexp_ishttp) != null) {
			
			//eachLink.onclick = function() { clickCallback(); }
			//eachLink.onclick = function(did) { clickCallback(did); }(eachLink.id)
			/*$(eachLink).click(function() {
				window.alert("click~");
			}*/
		}
	}
}


function setupSearch() {

	if (document.getElementById('header_search_input')) {
		document.getElementById('header_search_input').ondrop = function() { if (document.getElementById('header_search_input').value == 'Type hier je zoekopdracht in') { document.getElementById('header_search_input').value = ''; } };
		// note: ondrop works only in IE untill html 5
		document.getElementById('header_search_input').onfocus = function() { if (document.getElementById('header_search_input').value == 'Type hier je zoekopdracht in') { document.getElementById('header_search_input').value = ''; } document.getElementById('header_search_input').onfocus = null };
	}
}



function admin_editLinkCat(elemid, categoryId, categoryText, categoryWeight) {

	if (categoryText == null) { return false; }
	if (categoryId == null) { return false; }
	if (categoryWeight == null) { return false; }

	var loc = '';

	var b = document.getElementsByTagName('base');
	if (b && b[0] && b[0].href) {
		if (b[0].href.substr(b[0].href.length-1) == '/' && loc.charAt(0) == '/') {
			loc = loc.substr(1);
		}
		loc = b[0].href + loc;
	}
	
	$.post(	loc+'admin.do.php', 
			{ sys_typepage: 'editlinkcat', categoryid: categoryId, catname: categoryText, catweight: categoryWeight },
			function(data){
			
				//document.getElementById(elemid).innerHTML = categoryText;

				
				$('#links_container').load(loc+'admin.links.php?action=display&rand='+Math.random());
			} 
	);

}



function admin_addDownload() {

	var selectDrop = document.getElementById('allDownloadsList');
	/*var splitValue = selectDrop.options[selectDrop.selectedIndex].text.split(": ");
	var anchor = splitValue[0];
	var url = splitValue[1];
	document.getElementById('entry_downloadids').value += selectDrop.options[selectDrop.selectedIndex].value+',';*/

	// meteen ajax-event doen en updaten?
	// anders moet ik misschien lastig doen met weight..

	//admin_downloadsforpage

	var iPageid = document.getElementById('entry_entryid').value;
	var iWeight = document.getElementById('admin_downloadweight_new').value;

	
	if (isNaN(iWeight)) { return false; } // weight
	if (selectDrop.options[selectDrop.selectedIndex].value == null) { return false; } 


	var loc = '';

	var b = document.getElementsByTagName('base');
	if (b && b[0] && b[0].href) {
		if (b[0].href.substr(b[0].href.length-1) == '/' && loc.charAt(0) == '/') {
			loc = loc.substr(1);
		}
		loc = b[0].href + loc;
	}

	
	$.post(	loc+'admin.do.php', 
			{ sys_typepage: 'downloadtopage', weight: iWeight, downloadid: selectDrop.options[selectDrop.selectedIndex].value, pageid: iPageid },
			function(data){
			
				//document.getElementById(elemid).innerHTML = categoryText;

				
				$('#admin_downloadsforpage').load(loc+'admin.list_downloads.php?downloadpageid='+iPageid+'&rand='+Math.random());
			} 
	);
}

function delDFP(requesturl, iPageid) {

	// delete download from page

	if (!confirm("Weet je zeker dat je deze download van deze pagina wilt loskoppelen?")) { return false; }
	
	var loc = '';

	var b = document.getElementsByTagName('base');
	if (b && b[0] && b[0].href) {
		if (b[0].href.substr(b[0].href.length-1) == '/' && loc.charAt(0) == '/') {
			loc = loc.substr(1);
		}
		loc = b[0].href + loc;
	}


	$.get(requesturl, function(data){

		//document.getElementById('admin_downloadsforpage').innerHTML = $.get(loc+'admin.list_downloads.php?downloadpageid='+iPageid+'&rand='+Math.random());
		$('#admin_downloadsforpage').load(loc+'admin.list_downloads.php?downloadpageid='+iPageid+'&rand='+Math.random());
	});
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300,left = 390,top = 412');");
	}

//$(document).ready(tagUrls);
$(document).ready(setupSearch);

$(document).ready(function(){
	
	$("#entry_name").blur(function(){
		var value = $(this).val();
		
		if($("#catId").val() == 2){
				var newTitle = value + ' ? Lees alles over ' + value + ' | Werkwebsite';
		}
		if($("#catId").val() == 3){
			var newTitle = value + ' ? Lees alles over opleidingen | Werkwebsite';
		}
		
		$("#entry_meta_pagetitle").val(newTitle);
		
		var value = value.replace(/ /g,"-");
		var value = value.toLowerCase();
		var shorttag = value + ".html";
		$("#shorttag").val(shorttag);
		
	});
	
	$("form[name=respond]").submit(function(){
		var error = 0;
		var output = "";
		if($("form[name=respond] input[name=naam]").val() == ""){
			error = 1;
			output += "- Vul uw naam in<br />";
		}
		if($("form[name=respond] input[name=email]").val() == ""){
			error = 1;
			output += "- Vul uw e-mail adres in<br />";
		}
		if($("form[name=respond] textarea[name=ervaring]").val() == ""){
			error = 1;
			output += "- Vul uw ervaring in<br />";
		}
		
		if(error == 1){
			$("#error").html(output);
			$("#error").show();
			
			return false;
		}
		else{
			$("#formRespond").hide();
			$("#success").show();
			
			$.post("ajax/postRespond.php",$("#respond").serialize(),function(data){
				$("#responds").html(data);
			});
			return false;
		}
	});
	var sURL = escape(location.href);
	 	
	var sTitle = escape(document.title);
		
	var sSnippet = $("#dividerLeft2").text();
		sSnippet = sSnippet.substring(0,100);
		sSnippet = escape(sSnippet);
		//alert(sSnippet);
	$(".printF").attr("href","http://www.printfriendly.com/print?url="+sURL+"&partner=sociable");
	$(".digg").attr("href","http://digg.com/submit?phase=2&amp;url="+sURL+"&amp;title="+sTitle+"&amp;bodytext=");
	$(".delicious").attr("href","http://delicious.com/post?url="+sURL+"&amp;title="+sTitle+"");
	$(".facebook").attr("href","http://www.facebook.com/share.php?u="+sURL+"&amp;t="+sTitle);
	$(".google").attr("href","http://www.google.com/bookmarks/mark?op=edit&amp;bkmk="+sURL+"&amp;title="+sTitle+"&amp;annotation="+sSnippet);
	$(".mailTo").attr("href","mailto:?subject="+sTitle+"&body="+sURL);
	$(".hyves").attr("href","http://www.hyves.nl/profilemanage/add/tips/?name="+sTitle+"&amp;text="+sURL+"&amp;rating=5");
	$(".nuJIJ").attr("href","http://nujij.nl/jij.lynkx?t="+sTitle+"&amp;u="+sURL+"&amp;b="+sSnippet);
	$(".twitter").attr("href","http://twitter.com/home?status="+sURL+" "+sTitle);
	
	$("form[name=newsLetter]").submit(function(){
		
		var error = 0;
		var errormessage = "";
		if($("form[name=newsLetter] input[name=name]").val() == ""){
			error = 1;
			errormessage = errormessage + "- Vul uw naam in a.u.b.<br />";
		}
		if($("form[name=newsLetter] select[name=gender]").val() == ""){
			error = 1;
			errormessage = errormessage + "- Vul uw geslacht in a.u.b.<br />";
		}
		if($("form[name=newsLetter] input[name=email]").val() == ""){
			error = 1;
			errormessage = errormessage + "- Vul uw e-mailadres in a.u.b.<br />";
		}
		if($("form[name=newsLetter] input[name=conditions]").val() == ""){
			error = 1;
			errormessage = errormessage + "- U dient akkoord te gaan met de voorwaarden.<br />";
		}
		if(error == 1){
			$(".error").show();
			$(".error").html(errormessage);
			
		}
		else{
			$.post("ajax/newsLetter.php",$("form[name=newsLetter]").serialize(),function(data){
				$(".success").show();
				$(".success").html(data);
				$(".error").hide();
				$("form[name=newsLetter]").hide();
			});
			
		}
		
		return false;
		
	});
	
	$(".uploadedImage").live("click",function(){
	var src = $(this).attr("src");
	var brokenstring=src.split("/");
	var count = brokenstring.length - 1;
	
	var newImage = "http://www.werkwebsite.nl/afbeeldingen/big_"+ brokenstring[count];
	$("body").append("<div class='popPicture'><img src='leeg' id='newImg'></div>");
	$(".popPicture").css({
	
		"position":"fixed",
		"background":"#000",
		
		"padding":"10px",
		"left":"50%", 
		"top":"50%", 
		"margin-left":"-300px",
		"margin-top":"-240px",
		
		"border":"1px solid #232c4a",
		"z-index":"255",
		"cursor":"pointer"
	
		});
	
	$("#newImg").attr("src",newImage)
	
	var height = $(".popPicture").height();
	var width  = $(".popPicture").width();
	
	height = height / 2 + 10;
	width  = width  / 2 + 10;
	
	$(".popPicture").css({
		"margin-left":"-"+width+"px",
		"margin-top":"-"+height+"px"
	});
	
	});
	
	
	$(".popPicture").live("click",function(){
		$(this).remove();
	});
	
	$(".imageUpload").css({"cursor":"pointer"});
	
	$("#form_opleiding").submit(function(){
		var error = 0;
		var notice = "";
		if($("input[name=name]").val().length < 3){
			notice += "- Vul de naam van de opleiding in<br />";
			error = 1;
		}
		if($("input[name=niveau]").val().length < 3){
			notice += "- Vul het niveau van de opleiding in<br />";
			error = 1;
		}
		if($("input[name=instelling]").val().length < 3){
			notice += "- Vul de naam van de instelling in<br />";
			error = 1;
		}
		if($("input[name=plaats]").val().length < 3){
			notice += "- Vul de plaats van de opleiding in<br />";
			error = 1;
		}
		if($("textarea[name=verwachting]").val().length < 10){
			notice += "- Vul de verwachting van de opleiding in<br />";
			error = 1;
		}
		if($("textarea[name=voormij]").val().length < 10){
			notice += "- Vul in waarom de opleiding iets voor mij is<br />";
			error = 1;
		}
		if($("textarea[name=behaald]").val().length < 10){
			notice += "- Vul de verwachting na het behalen van het diploma in<br />";
			error = 1;
		}
		if($("textarea[name=mogelijkheden]").val().length < 10){
			notice += "- Vul de caričremogelijkheden van de opleidinge in<br />";
			error = 1;
		}
		if($("textarea[name=duur]").val().length < 10){
			notice += "- Vul de duut van de opleiding in<br />";
			error = 1;
		}
		if($("textarea[name=eisen]").val().length < 10){
			notice += "- Vul de eisen van de opleiding in<br />";
			error = 1;
		}
		if(error == 1){
			$("#error").html(notice);
			$("#error").show();
			return false;
		}
		else{
			$("#success").html('Hartelijk dank voor uw medewerking');
			$("#success").show();
		}
			
	});
	$("input[name=name]").live("keyup",function(){
		var value=$(this).val();
		$(".opleiding").html(value);
			
	});
	
	$("object").live("click",function(){
		var sURL = $(this).attr("id");
		window.open(sURL);
	});
	$("embed").live("click",function(){
		var sURL = $(this).attr("id");
		window.open(sURL);
	});
	
	$(".sImage").each(function(){
		var sUrl = $(this).html();
		var sImage = $(this).attr("id");
		
		$(this).html('<a href="'+sUrl+'" target="_blank"><img src="/afbeeldingen/'+ sImage +'"></a>');
	});
	$("#toolname").live("keyup",function(){
								
		var value = $(this).val();						 
		var value = value.replace(/ /g,"-");
		var value = value.toLowerCase();
		var shorttag = value + ".html";
		$("input[name=shorttag]").val(shorttag);
	 });
	
	
	$("#uploadCV").submit(function(){
		$(".uploadCV").show();
		$(".uploadCV").load(function(){
			var iUniqueId = $(this.contentDocument).find('span').html();
			$("#uploadCV").hide();
			$(".uploadCV").hide();
			$("#uniqueId").html("<p><strong>Jouw id</strong><br />Je unieke id is "+iUniqueId+". Met dit nummer kun je eenvoudig jouw geplaatste document terugvinden.</p>");
	    });
	});
});


