$(document).ready(function() {
	
	/* VIDEO */		
	if (ieVersion >= 9.0) {
		
		var video = document.getElementById("stijlroyal-video");
		$("#fallback").remove();
		
		if (isIpad || isIphone) {
			$("#video-mute img").attr("src", main_url + '2012/media/final/ij.png');
		}
		
		if (isIphone) {
			$(video).remove();
		} else {
			// Button
			function toggleButton() {
			    var a = video.paused ? 'play' : 'pause';
			    $("#video-play img").attr("src", main_url + '2012/media/final/' + a + '.png');
			}
			
			// Video-Steuerung
			function toggleVideo() {
    		    video.paused ? video.play() : video.pause();
    		}
    		
    		// Video-Steuerung
			function toggleMute() {
    		    video.muted = !video.muted;
    		}
    		
    		// Video zurückspulen
    		function firstFrame() {
    			video.play();
    			video.pause();
    		}
    		
    		// Event für Button 
    		$("#video-play a").click(function() {
    			toggleVideo();
    			return false;
    		});
    		
    		// Event für Button 
    		$("#video-mute a").click(function() {
    			toggleMute();
    			return false;
    		});
			
			// Events zur Buttonanzeige
			$(video).bind("play", toggleButton);
			$(video).bind("pause", toggleButton);
			$(video).bind("ended", toggleButton);
			$(video).bind("ended", firstFrame);
			
		}
		
		// Anpassung der Größe
		function resizeMedia() {
		    var w_width = $(window).width(),
		        w_height = $(window).height(),
		        w_ratio = Math.max(w_width / 720, w_height / 400),
		        c_width = (720 * w_ratio),
		        c_height = (400 * w_ratio),
		        w_offset = 0.5 * (w_width - c_width);
		    		    	    
		    $("#background").css({
		        left: w_offset,
		        width: c_width,
		        height: c_height
		    });		    
		    
		    $("#stijlroyal-media").css({
		        width: $(window).width(),
		        height: $(window).height()
		    });
		    
		    /*
		    $("#video-play").css({
		    	left: Math.floor(0.5 * (c_width - 453)),
		    	bottom: Math.floor(0.5 * ($(window).height() - 310))
		    });
		    */
		      
		}
		
		// Media Anpassungen
		if (isIpad || isIphone || isAndroid) {
			$(window).bind("orientationchange", resizeMedia);
		} else {
			$(window).resize(resizeMedia);
		}
		
		resizeMedia();
		toggleVideo();
		
	} else {
		$("#video-mute").hide();
		$("#video-play").hide();
		
		var stijlroyalPlayer = $f("fallback", {
		    src: main_url + "2012/media/flowplayer-3.2.7.swf",
		    wmode: "opaque"
		    }, {
		    	clip: {
		    		autoPlay: true,
		    		autoBuffering: true,
		    		url: main_url + "2012/media/" + video_for_flash,
		    		onBeforeFinish: function(clip) {return false;}
		    	},
		    	plugins: {
		    		controls: {
		    			url: main_url + "2012/media/flowplayer.controls-3.2.5.swf",
		    			all: false,
		    			play: true,
		    			mute: true,
		    			backgroundColor: "transparent",
						backgroundGradient: "none"		    			
		    		}
		    	}
		    }
		);
		
		function resizeMediaIE() {
		    var w_width = $(window).width(),
		        w_height = $(window).height(),
		        w_ratio = Math.max(w_width / 720, w_height / 400),
		        c_width = Math.floor(720 * w_ratio),
		        c_height = Math.floor(400 * w_ratio),
		        w_offset = 0.5 * (w_width - c_width);
		    
		    $("object").attr("width", c_width).attr("height", c_height);
		    
		    $("#background").css({
		        left: w_offset,
		        width: c_width,
		        height: c_height
		    });
		    
		    $("#stijlroyal-media").css({
		        width: jQuery(window).width(),
		        height: jQuery(window).height()
		    });  
		}
		
		$(window).resize(resizeMediaIE);
		resizeMediaIE();
	}
	
});
