jQuery.fn.single_resizeElements = function(){
	//Get the screen height and width
	var windowH = $(document).height();
	var windowW = $(window).width();	
	//Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':windowW,'height':windowH});
	//Set the popup window to center
	
	 windowH = $(window).height();
	 windowW = $(window).width();
	
	$('#dialog_single').css('top',  windowH/2-$('#dialog_single').height()/2);
	$('#dialog_single').css('left', windowW/2-$('#dialog_single').width()/2);
} 

$(window).resize(function() {
	jQuery.fn.single_resizeElements();
});


jQuery.fn.single_loadVideo = function(movieUrl, tracking_text){
	jQuery.fn.single_resizeElements();
	//transition effect			
	$('#mask').fadeTo(500,0.9, function(){
		//transition effect
		$('#dialog_single').fadeIn(500, function(){
			$('#contentheader').hide();
			$('.banner-teaser').hide();
			$('#trailer').val("hallo");
			jQuery.fn.single_resizeElements();
				var params = {
						menu: "false",
						allowScriptAccess:"always",
						quality: "best",
						allowFullScreen:"true",
						bgcolor:"#FFFFFF",
						wmode:"transparent"
				};
			var attributes = {};
			$("#dialog_single").append("<div id=\"trailer\"></div>")
			
			var flashvars = {
				movieUrl:movieUrl
			}             
			if(pageTracker){
				pageTracker._trackEvent('Video_tutorial', tracking_text);
			}
			swfobject.embedSWF("./swf/telekursid_loader_single.swf", "trailer", "746", "605", "9", "", flashvars, params, attributes);
			$('html, body').animate({scrollTop:0}, 0);
		}); 
	});
}

$(document).ready(function() {	

	//select all the a tag with name equal to modal
	$('a[name=modal_single]').click(function(e) {
		//Cancel the link behavior     
 		
		var movieUrl = this.href.replace('&', '!?!');
		
		var image = $('a[name=modal_single]')[0].children[0];   
		var alt_desc = image.alt;
		var tracking_text = alt_desc.slice(6, alt_desc.length);  
        
		var rautePosition = movieUrl.indexOf('#') + 1;
	    movieUrl = window.location + movieUrl.slice(rautePosition, movieUrl.length);   
		e.preventDefault();
		jQuery.fn.single_loadVideo(movieUrl, tracking_text);
	});	

	//if mask is clicked
	$('#mask').click(function () {
		jQuery.fn.single_closeTrailer();
	});			
});

//if mask is clicked
jQuery.fn.single_closeTrailer = function(){
	$('.window_single').fadeOut(500, function(){
		$('#contentheader').show();
		$('.banner-teaser').show();
		$('#mask').fadeOut(500, function(){
			swfobject.removeSWF("trailer");
		});
	});
}
