$(document).ready(function(){
	linkNewWindow();
	doFlashHeader();
	doPlayer();
	addSocial();
})

function linkNewWindow(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = obj.className + " external";
	};	
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};		
};

function doFilter(){
	var strType = $('#filter-type').val();
	var strKitchen = $('#filter-kitchen').val();
	
	if (strType != "" || strKitchen != "") {
		if (strType == "")
			strType = "ALL";
		
		if (strKitchen == "")
			strKitchen = "ALL";
	
		document.location.href = $('#basepath').val() + strType + '/' + strKitchen + '/';
	}
}

function doSearch(){
	var strText = $('#search-text').val();
	
	if (strText != "") {
		document.location.href = $('#basepath').val() + 'search/' + strText + '/';
	}
}

function doFlashHeader() {
	$('.header-flash').flash(
	    {	src: '/assets/swf/flash_header.swf',
			width: 1000,
			height: 165,
			menu: false,
			wmode: 'transparent'
		 }, 
		 
	    { version: '8' }
	);
}




function doPlayer() {
	$('.player').flash(
	    {	src: '/assets/swf/player.swf',
			width: 300,
			height: 166,
			menu: false,
			flashvars: { 	file: '/assets/swf/commercial.flv',
							image: '/assets/images/commercial.png'
						} 
		 }, 
	    { version: '8' }
	);
	$('.player-nl').flash(
	    {	src: '/assets/swf/player.swf',
			width: 300,
			height: 168,
			menu: false,
			flashvars: { 	file: '/assets/swf/commercial_nl.flv',
							image: '/assets/images/commercial-be.png'
						} 
		 }, 
	    { version: '8' }
	);
	$('.player-fr').flash(
	    {	src: '/assets/swf/player.swf',
			width: 300,
			height: 168,
			menu: false,
			flashvars: { 	file: '/assets/swf/commercial_fr.flv',
							image: '/assets/images/commercial-be.png'
						} 
		 }, 
	    { version: '8' }
	);
}


/*
 * Transforms static social media provider links to dynamic.	
 *
 * Select the first get parameter which contains a hard coded
 * link to the home of website, and replaces it with the uri
 * of current page.
 *
 * Opens social media links in pop up if javascript is
 * available.
 */

function addSocial() {
	var socialMedia = $('#socialMedia');
	$(socialMedia).find('a').each(function(){
	        // Link replacement
	        var link = $(this).attr('href');
	
	        var newLink = link.replace(/(.*?\?.*?=)([^&]*)(&?.*)/gi, '$1');
	        var additonalParams = link.replace(/(.*?\?.*?=)([^&]*)(&?.*)/gi, '$3');
	
	        newLink = newLink + encodeURIComponent(location.href) + additonalParams;
	
	        $(this).attr('href', newLink);
	
	        // Pop up
	        $(this).click(function(){
	                popUp = window.open($(this).attr('href'), "socialMediaPopUp", "width=775,height=640,status=no,scrollbars=yes,resizable=no");
	                popUp.focus();
	                return false;
	        });
	
	        // Google Link
	        if ($(this).hasClass('google')) {
	                var additionalTags = '&title=' + encodeURIComponent($('title').eq(0).text());
	                metaKeywords = $('meta[name="keywords"]').eq(0).attr('content');
	                if (metaKeywords != '' && metaKeywords != undefined) {
	                        additionalTags += '&labels=' + encodeURIComponent(metaKeywords);
	                }
	                $(this).attr('href', $(this).attr('href') + additionalTags);
	        }
	});
}

