jQuery.fn.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').css('top',  windowH/2-$('#dialog').height()/2);
	$('#dialog').css('left', windowW/2-$('#dialog').width()/2);
} 

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


jQuery.fn.loadVideo = function(chapter){
	jQuery.fn.resizeElements();
	//transition effect			
	$('#mask').fadeTo(500,0.9, function(){
		//transition effect
		$('#dialog').fadeIn(500, function(){
			$('#contentheader').hide();
			$('#trailer').val("hallo");
			jQuery.fn.resizeElements();
				var params = {
						menu: "false",
						allowScriptAccess:"always",
						quality: "best",
						allowFullScreen:"true",
						bgcolor:"#FFFFFF",
						wmode:"transparent"
				};
			var attributes = {};
			$("#dialog").append("<div id=\"trailer\"></div>")
			
			var flashvars = {
				chapter:chapter
			}
			if(pageTracker){
				pageTracker._trackEvent('Video', 'chapter '+chapter);
			}
			swfobject.embedSWF("./swf/telekursid_loader.swf", "trailer", "536", "479", "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]').click(function(e) {
		//Cancel the link behavior
		
		var chapter = "";
		if(this.href.search("chapter1") != -1){
			chapter  = 0;
		}
		else if(this.href.search("chapter2") != -1){
			chapter = 1;
		}
		else if(this.href.search("chapter3") != -1){
			chapter = 2;
		}
		else if(this.href.search("chapter4") != -1){
			chapter = 3;
		}
		else if(this.href.search("chapter5") != -1){
			chapter = 4;
		}
		else if(this.href.search("chapter6") != -1){
			chapter = 5;
		}	
		e.preventDefault();
		jQuery.fn.loadVideo(chapter);
	});	

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

//if mask is clicked
jQuery.fn.closeTrailer = function(){
	$('.window').fadeOut(500, function(){
		$('#contentheader').show();
		$('#mask').fadeOut(500, function(){
			swfobject.removeSWF("trailer");
		});
	});
}