$(document).ready(function() {
	if($('#newsListHome').length > 0) $('#newsListHome').innerfade({ animationtype: 'fade', speed: 750, timeout: 6000, type: 'sequence', containerheight: '80px' });
	if($('#ytVideos').length > 0) loadYtVideos();
	if($('#footerContainer').length > 0){
		var footerHeight = $('div#footerContainer').outerHeight(true);
		$("<div />").attr("id", "clearfooter").css({ 'height': footerHeight }).appendTo("div#outerContainer");	
		
		if($.browser.msie && $.browser.version.substr(0,1) < 7){
			$("div#outerContainer").css({ 'height': '100%' });						   
		}else{
			$("div#outerContainer").css({ 'min-height': '100%' });						   
		}
		$("div#outerContainer").css({ 'margin-bottom': -footerHeight + "px" });
	}
	if($('.showCaseContainer').length > 0) $.groupShowCase();
	if($('.smallCarouselWrapperHome').length > 0){
		var totalCols = Math.ceil($('.smallCarouselList').children('li').size() / 4);
		$('.smallCarouselList').easyListSplitter({ 
			colNumber: totalCols,
			maxItemsPerCol: 4,
			direction: 'horizontal'
		});
		$(".listContainer1").carouFredSel();
	}
	if($('.smallCarouselWrapper').length > 0){
		var totalCols = Math.ceil($('.smallCarouselList').children('li').size() / 10);
		$('.smallCarouselList').easyListSplitter({ 
			colNumber: totalCols,
			maxItemsPerCol: 10,
			direction: 'horizontal'
		});
		$(".listContainer1").carouFredSel();
	}
});

function setFrameHeight(){
	if(document.all){
		document.getElementById("formFrame").height = formFrame.document.getElementById("mainDiv").offsetHeight;
	}else{
		document.getElementById("formFrame").height = formFrame.document.getElementById("mainDiv").offsetHeight + 20;
	}
}

function submitSearchForm(strLanguage){
	switch(strLanguage){
		case "nl":{
			if(document.getElementById("search_term").value=="" || document.getElementById("search_term").value=="Zoeken..." || document.getElementById("search_term").value=="Search..."){
				return false;
			}
			break;
		}
		case "uk":{
			if(document.getElementById("search_term").value=="" || document.getElementById("search_term").value=="Zoeken..." || document.getElementById("search_term").value=="Search..."){
				return false;
			}
			break;
		}
	}
	document.getElementById("searchForm").submit();
}

function submitContactForm(strLanguage){
	switch(strLanguage){
		case "nl":{
			if(document.getElementById("Emailadres").value==""){
				alert("E-mailadres is een verplicht veld.");
				document.getElementById("Emailadres").focus();
				return false;
			}
			break;
		}
		case "uk":{
			if(document.getElementById("Emailadres").value==""){
				alert("E-mail address is a required field.");
				document.getElementById("Emailadres").focus();
				return false;
			}
			break;
		}
	}
	document.getElementById("contactForm").submit();
}

/** newsletter subscription **************************************************************************************************************/

function SendNewsletterSubscribtion(iLan) {
	var emailInput = $("#NewsLetterSubscriptionEmail").val();
	
    if ($(".newsletter_form p.error").length > 0) {
        $(".newsletter_form p.error").remove();
    }
    if ($(".newsletter_form p.success").length > 0) {
        $(".newsletter_form p.success").remove();
    }
	if(emailInput==""||emailInput=="Vul uw e-mailadres in..."||!checkEmail(emailInput)){
		if(iLan==1) { $("<p>").attr("class", "error").text("Er is geen geldig e-mailadres opgegeven.").appendTo(".newsletter_form"); }
		if(iLan==2) { $("<p>").attr("class", "error").text("No valid e-mailaddress supplied.").appendTo(".newsletter_form"); }
		return false;
	}
	
	$.ajax({
        type: "POST",
        url: getBaseURL() + "webfront/forms/newsletter/newsletter_subscription.asp?ac=send&email=" + emailInput,
		success: function(data, textStatus, XMLHttpRequest){
			OnSubscribtionSucceeded(data, textStatus, XMLHttpRequest, iLan)
		},
        error: function(XMLHttpRequest, textStatus, errorThrown){
			OnSubscribtionFailed(XMLHttpRequest, textStatus, errorThrown, iLan)
		}
    });
};

function OnSubscribtionSucceeded(data, textStatus, XMLHttpRequest, iLan) {
    if ($(".newsletter_form p.error").length > 0) {
        $(".newsletter_form p.error").remove();
    }
    if ($(".newsletter_form p.success").length > 0) {
        $(".newsletter_form p.success").remove();
    }
	if(iLan==1) { $("<p>").attr("class", "success").text("Bedankt voor uw aanmelding.").appendTo(".newsletter_form"); }
	if(iLan==2) { $("<p>").attr("class", "success").text("Thanks for signing up.").appendTo(".newsletter_form"); }
}

function OnSubscribtionFailed(XMLHttpRequest, textStatus, errorThrown, iLan) {
    if ($(".newsletter_form p.error").length == 0) {
		if(iLan==1) { $("<p>").attr("class", "error").text("Er is iets fout gegaan, probeer het opnieuw.").appendTo(".newsletter_form"); }
		if(iLan==2) { $("<p>").attr("class", "error").text("Something went wrong, try again.").appendTo(".newsletter_form"); }
    }
}

function checkEmail(emailaddress) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(emailaddress)) {
		return false;
	}else{
		return true;	
	}
}

/** yt videos ****************************************************************************************************************************/

function loadYtVideos(){
	$.ajax({
		type: "GET",
		url: "http://gdata.youtube.com/feeds/users/ExpansionBV/uploads?alt=json-in-script&format=5",
		cache: false,
		dataType: "jsonp",
		success: parseXml,
		error: parseError
	});
}

function parseXml(data, textStatus, XMLHttpRequest){
	var feed = data.feed;
	var entries = feed.entry || [];
	var html = ['<ul id="videos">'];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		var title = entry.title.$t;
		var description = entry.media$group.media$description.$t;
		var thumbUrl = entry.media$group.media$thumbnail[0].url;
		var movieUrl = entry.media$group.media$player[0].url;
		html.push('<li onclick="loadVideo(\'', movieUrl, '\',\'', thumbUrl, '\',\'', title, '\',\'', description, '\', true)">',
				  '<p class=\'bold green\'>', title, '</p>',
				  '<img src="', thumbUrl, '" width="90" height="65" align=\'left\'/>', 
				  '<p>', description.substr(0, 80), '...</p>',
				  '</li>');
	}
	html.push('</ul>');
	document.getElementById('ytVideos').innerHTML = html.join('');
	if (entries.length > 0) {
		$('#crumbMenu').after("<div id='movieContent'></div>");
		loadVideo(entries[0].media$group.media$player[0].url, entries[0].media$group.media$thumbnail[0].url, entries[0].title.$t, entries[0].media$group.media$description.$t, false);
	}
}

function parseError(XMLHttpRequest, textStatus, errorThrown) { /*alert("error :" + XMLHttpRequest.responseText);*/ }

function loadVideo(movieUrl, thumbUrl, movieTitle, movieDescription, autoplay) {
	jwplayer("jwPlayer").setup({
		file: movieUrl,
		image: thumbUrl,
		width: 635,
		height: 340,
		stretching: "fill",
		controlbar: "bottom",
		modes: [
			{ type: 'flash', src: getBaseURL() + 'webfront/jwplayer/player.swf' },
			{ type: 'html5' },
			{ type: 'download' }
		]
	});
	
	var movieContent = '<h1>' + movieTitle + '</h1><p>' + movieDescription + '</p>';
	$('#movieContent').html(movieContent);
}

/** retrieve baseurl *********************************************************************************************************************/

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}

