/**
 * De url die verwijst naar het phpscript dat wordt opgeroepen door facebook.  Indien men iets shared via de facebook sharer wordt de url die men meegeeft 
 * gechecked op metatags, daarmee dit script.
 * Moet dus aangepast worden 
 */
var FACEBOOKSHARERURL = 'http://jobs.aarixa.be/facebookShare2.php';

/**
 * De maximum lengte van de url is voor IE 2048 characters.  Dit is de kortste mogelijk.
 * 
 * Recommendations
	Extremely long URLs are usually a mistake. URLs over 2,000 characters will not work in the most popular web browser. Don't use them if you intend your site to work for the majority of Internet users.
	When you wish to submit a form containing many fields, which would otherwise produce a very long URL, the standard solution is to use the POST method rather than the GET method:
 * 
 * Microsoft Internet Explorer (Browser)
	Microsoft states that the maximum length of a URL in Internet Explorer is 2,083 characters, with no more than 2,048 characters in the path portion of the URL. In my tests, attempts to use URLs longer than this produced a clear error message in Internet Explorer.
 */
var MAXIMUM_URL_LENGTH = 2000;

/**
 * Referentie naar een object die de parameters van een vacature bijhoudt.  Dit vullen we op bij een click op de betreffende vacature div.
 */
var vacature = new Object();

var linkedInTo_email = "nick.bukers@aarixa.be";
var linkedInCompanyLogo = "http://jobs.aarixa.be/images/logo.jpg";
var linkedInCompany = "aariXa";

var showLinkedInButton = false;

function mailtoClickHandler(id) {
	var mailtoHref = "mailto:?subject=" + vacature.titleText + "&body=" + "Kijk eens naar deze vacature: http://jobs.aarixa.be/index.htm?id=" + id; //vacature.omschrijvingText;
	var windowMail = window.open(mailtoHref, "", "scrollbars=yes,resizable=yes,width=10,height=10");
	if(windowMail) {
		windowMail.close();
	}
	return false;
}

$(document).ready(function () {
	//centerPopupDiv();

	var vacatureUrl = window.location.href;
	var parameters = getUrlParameters();
	var vacatureUrlId = parameters["id"];
	
	$(".vac_titel").click(function () {
		// lets try the jquery popup instead..
		//enablePopup();
		$(".a_cv").show();
		
		// alle vacature specifieke waarden ophalen.
		vacature.id = $(this).attr("id");
		vacature.titleHtml = $(this).html();
		vacature.titleText = $(this).text();
		vacature.omschrijvingHtml = $(this).next().next().html();
		vacature.omschrijvingText = $(this).next().next().text();
		vacature.vacatureHtml = $(this).next().next().next().next().html();
		
		// indien er nog geen id parameter aan de url hangt of de id die aan de url hangt is niet gelijk aan de id van de geselecteerde vacature voegen we die toe,
		// kwestie dat deze ook gebookmarked kan worden.
		var vacatureUrlId = parameters["id"];
		if(vacatureUrlId == undefined || vacatureUrlId != vacature.id) {
			vacatureUrl = window.location.href;
			if(vacatureUrl.indexOf("?") > -1) 
				vacatureUrl = vacatureUrl.substring(0, vacatureUrl.indexOf("?"));
			vacatureUrl += "?id=" + vacature.id;
		}
		
		/*
		 * SEND CV LINK
		 */
		var sendCvHref = $(this).parent().next().children().first().attr("href");
		$("#sendCvButton").attr("href", sendCvHref);
		
		/*
		 *  APPLY WITH LINKEDIN
		 */
		var linkedInJobId = sendCvHref.substring(20);
		
		var linkedInApplyButton = GenerateLinkedInApplyButton(vacature.titleText, linkedInJobId, true);
		
		//$("#LinkedInScript").attr("data-jobTitle", "FREAK");
		$("#popUpLinkedInApply").html(linkedInApplyButton);
		IN.init();	
		
		
		/*
		 * TWITTER SHARE BUTTON
		 */
		/*
		example
		<a href=”http://twitter.com/home?status=Currently reading http://www.test.com/post-url” title=”Click to share this post on Twitter”>Share on Twitter</a>
		*/
		// twitter share link, href zetten..  http://twitter.com/home?status=Currently reading " + vacatureUrl + "
		$("#twitterShareLink").attr("href", "http://twitter.com/home?status=Currently reading " + vacatureUrl);
						
		/*
		 * LINKED IN SHARE BUTTON
		 * Bij deze kunnen we mooi de te sharen url, titel en omschrijving meegeven aan de url.
		 */
		// linkedin share link, href zetten
		$("#linkedInShareLink").attr("href", "http://www.linkedin.com/shareArticle?mini=true&url=" + vacatureUrl + "&title=" + vacature.titleText + "&summary=" + vacature.omschrijvingText);
		
		/*
		 * FACEBOOK SHARE BUTTON
		 * na een tal van pogingen waarbij bepaalde parameters niet meer werken langs facebook hun kan lijkt deze de beste
		 * oplossing (facebook had een andere manier waarbij je een url met parameters t (title) en d (description) kon 
		 * meegeven maar dit zetten ze stop.  
		 * daarbij haalt facebook de metatags op van de pagina (open graph) op bepaalde parameters te tonen 
		 * (image, title, description..). daarvoor geven we een url mee van de url van een php pagina mee aan de share link
		 * die de parameters bevat waarbij de metatags worden ingevuld. tedious maar FB faseert de andere mogelijkheden 
		 * binnenkort uit dus we go with this..
		 */
 
		/*
		example hoe het aanvankelijk werd gedaan.
		function fbs_click() {
			u=location.href; t=document.title; 		
			window.open(‘http://www.facebook.com/sharer.php?u=’ + encodeURIComponent(u) + ’&t=’ + encodeURIComponent(t), ’sharer’, 'toolbar=0, status=0, width=626, height=436'); 
			return false;
		}
		*/
		/*
		DEPRECATED..
		*/
		/*content += "        	<a target='_blank' onclick='return fbs_click() '" + 
			"href='http://www.facebook.com/sharer/sharer.php?u=http://www.aarixa.be/anton/website5/facebookShare.php?" + "url=" + vacatureUrl + " &description=" + vacature.titleText + "&title=" + vacature.titleText + "&site_name=aariXa-vacancies" + "&type=article" + 	"&image=images/logo.jpg" + "&'><img src='images/facebook.png' alt='Facebook' width='20' height='20' border='0' /></a>";
		*/
		
		/*
		DEPRECATED..
		*/
		//content += "			<fb:share-button class='meta'><meta name='medium' content='mult'/><meta name='title' content='name of fan page'/><meta name='description' content='description of fan page'/><link rel='target_url' href='http://facebook.com/Anti.Social.Development'/></fb:share-button>";
								
		// we kunnen enkel de url meegeven via sharer.php.  de rest van de info wordt via de metatags opgehaald.
		// hier maken we de url zonder parameters.  wat we hier doen is een url aanmaken (die verwijst naar een php file) 
		// en de parameters meegeven.  de parameters vullen dan eerst de metatags in via het phpscript, facebook gebruikt deze dan.
		// tedious but works.  een van de weinige manieren om het proper te doen eenmaal je met een dynamische javascript site zit.
		var urlLength = window.location.href.indexOf('?') > -1 ? window.location.href.indexOf('?') : window.location.href.length;
		var siteUrl = window.location.href.slice(0, urlLength);
		// about to create facebookurl without the description and measure the length.
		var facebookurl = createFacebookUrl({vacature:vacature, siteUrl:siteUrl, omschrijving:""});
		var facebookUrlLength = createFacebookUrl({vacature:vacature, siteUrl:siteUrl, omschrijving:""}).length;
		var omschrijving = encodeURIComponent(vacature.omschrijvingText).length + facebookurl.length > MAXIMUM_URL_LENGTH ? 
			(vacature.omschrijvingText.substring(0, MAXIMUM_URL_LENGTH - facebookurl.length - 3) + "...") : vacature.omschrijvingText;
		// misschien moeten we eerder iets standaard als omschrijving meegeven, stijl 'zie hier deze ongelooflijke vacature' ?
		var facebookurl = createFacebookUrl({vacature:vacature, siteUrl:siteUrl, omschrijving:omschrijving});
		$("#facebookShareLink").attr("href", facebookurl);
		
		// sharen op skype?
		//content += "        	<a href='#'><img src='images/skype.png' alt='Skype width='20' height='20' border='0'' /></a>";
		
		// nog te voorzien, sharen via rss
		//content += "        	<a href='#'><img src='images/feed-icon-28x28.png' alt='RSS' width='20' height='20' border='0' /></a>";
		
		$('#mailtoLink').attr('onclick', 'mailtoClickHandler('+vacature.id+'); return false;');
		
		$("#vacatureTitle").text(vacature.titleText); 
		$("#popUpOmschrijving").html(vacature.omschrijvingHtml);
		$("#popUpVacature").html(vacature.vacatureHtml);
		
		
		$.colorbox({width:"870px", inline:false, opacity:0.75, html:$('#vacatureContent').html(), onCleanup: cleanup(), height:"100%", maxHeight:"500px"}); 
		
		//setTimeout(showLinkedIn, 1000 );
	
		//return false;
	});
	
	function cleanup() {
		$(".a_cv").hide();	
	}
	
	function showLinkedIn() {
		var sendButtonPosition = $("#sendCvButton").offset();
		
		//alert(sendButtonPosition.left + " " + $("#popUpLinkedInApply").width());
		
		$("#popUpLinkedInApply").css({
			"position": "absolute", 
			"left": sendButtonPosition.left - $("#popUpLinkedInApply").width() - 30,
			"top": sendButtonPosition.top,
			"z-index": 1000,
			"height": "50px",
			"width": "200px"
		});		
	}
	
	$(window).resize(function() {	
		showLinkedIn();
	});
	
	setInterval(showHideLinkedInButton, 10);
	
	function showHideLinkedInButton() {
		var display = "none";
		
		showLinkedInButton = true;
	
		if ($("#colorbox").css("display") == "none") {
			showLinkedInButton = false;
		}
		
		if (showLinkedInButton) {
			display = "block";
		}
		
		$("#popUpLinkedInApply").css({
			"display": display
		});		
		
		showLinkedIn();
	}
	
	function createFacebookUrl (data) {
		if(data==undefined || data.vacature==undefined)
			return "";
		
		var urlparams = "" + "vacatureId=" + data.vacature.id + "&description=" + escapeOmissionStabbing(data.omschrijving) + "&title=" + data.vacature.titleText + "&site_name=aariXa-vacancies" +"&type=article" + "&image=images/logo.jpg" + "&url=" + data.siteUrl; // + "&";
		
		urlparams = encodeURIComponent(urlparams);
		var facebookurl = "http://www.facebook.com/sharer/sharer.php?u=" + FACEBOOKSHARERURL + "?" + urlparams;
		//console.log("about to return facebookurl returnvalue=" + facebookurl);
		
		return facebookurl;
	}
	
	function escapeOmissionStabbing(string) {
		return string;
	
		var stringbuffer = new Stringbuffer();
		for(var i = 0; i < string.length; i++) {
			var char = string[i];
			if(char == "'") {
				stringbuffer.append("&#39;");
			}
			/*else if (char == ' ') {
				stringbuffer.append('');
			}*/
			else {
				stringbuffer.append(char);
			}
		}
		return stringbuffer.toString();
	}
	
	function getUrlParameters() {
		// url parameters ophalen
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
	
	
	// indien de vacature id parameter is meegegeven aan de url, onmiddellijk naar de detail popup gaan.
	if(vacatureUrlId != undefined) {
		$("#" + vacatureUrlId).click();
	}
			
});
