var spacing;
var footerSpacing;
var windowSize = $(window);
var bgRatio = 5/3;
var share;
var bWidth = $(window).width();
var bHeight = $(window).height() -2;
var linksHidden = false;
var meRef;
var player;
var isiPad;
var isNative;
var mediaFileMP4 = 'http://pdl.warnerbros.com/wbmovies/thehobbit/trailer1/THE_HOBBIT_TRL_1_2D_IMAX_480.mov';
var mediaFileWEBM = '';
var mediaFileOGV = '';


$(document).ready(function(){
	ipadCheck();

	$("#skip-button").click(function(){
		// Skip button clicked
		removeVideo();
	});
	
	// Downloads popup

	$("#download-button").click(function(e){
		$("#download-window").lightbox_me({
			centered:true
		});
		e.preventDefault();
	});
	
	$("#wallpaper-hit").hover(
		function(){
			$(".download-popup").fadeIn(300);
		},
		function(){
			$(".download-popup").fadeOut(300);
		}
	);

	$('#view_options ').click(function(){
		$('#viewing-options').fadeIn(500);
	});

	$('#viewing-options span').click(function(){
		$('#viewing-options').fadeOut(300);
	});
	
});

function ipadCheck(){
	isiPad = navigator.userAgent.match(/iPad/i) != null;
	
	if(isiPad){
		removeVideo();
		$('#view_options').hide(0);
		$('#full_screen').hide(0);
	}
	else{
		$('#video-wrap #mp4').attr('src', mediaFileMP4);
	/*	$('#video-wrap #webm').attr('src', mediaFileWEBM );
		$('#video-wrap #ogv').attr('src', mediaFileOGV); */
		initVideo();
 	}
}

function removeVideo() {
	if(player != null){player[0].player.pause();}
	$("#video-wrap").hide();
	initSite();
}

function initSite() {
	$("#skip-button").hide();
	
	$('#content').show();
 	$('#footer_wrap').show();

	$("html").css({"overflow-y": "auto"});

	$('#player #play').click(function(){
		if(isiPad){$(this).attr('href', mediaFileMP4); }
		else{
			$("#video-wrap").show();
			$("#skip-button").show();
			$('#content').hide();
		 	$('#footer_wrap').hide();
		 	$("html").css({"overflow-y": "hidden"});

			player[0].player.setCurrentTime(0);
			player[0].player.setCurrentRail();
			player[0].player.play();
			videoResize();
		}
	});
	
	bgResize();
	footerRepos();
	contentAniResize();

	share = new Share('share');
	share.url = 'http://www.thehobbit.com/index.html';
	share.title = 'THE HOBBIT: AN UNEXPECTED JOURNEY';
	share.description = 'THE HOBBIT: AN UNEXPECTED JOURNEY, in theaters December 14, 2012';
	share.tweet = 'THE HOBBIT: AN UNEXPECTED JOURNEY, in theaters December 14, 2012 www.thehobbit.com';
	

	$('#footer').delay(1400).fadeIn(900);


	$('.main').delay(200).fadeIn(1400, 'easeInOutQuad', function(){

	});
}


function contentResize(){
	spacing = ((windowSize.height())/16) - 20;
	$('#date.content_box').css({"padding-top": spacing + "px", "padding-bottom": spacing + "px"});
}

function contentAniResize(){
	spacing = ((windowSize.height())/16) - 20;
	$('#date.content_box').animate({"padding-bottom": spacing + "px", "padding-top": spacing + "px", "opacity": "1"}, 1400, 'easeInOutSine');
	$('#content_wrap .content_box').animate({"opacity": "1"}, 1400, 'easeInOutSine');
}

function footerRepos(){
	if(($(window).height() - 740) > 90){
		$('#footer_wrap').css({"height": $(window).height() - 740 + "px"});
	}
	else{$('#footer_wrap').css({"height": "90px"});}
}


function bgResize(){
	if(windowSize.width() < 1000){
		$('.main').css({"width":"1000px"});

		if(windowSize.height() < 600){
			$('.main').css({"height":"600px"});
		}
		
		return;
	}
	if((windowSize.width() / windowSize.height()) < bgRatio ){
		$('.main').css({"width":"100%"});
		$('.main').css({"height": (1/bgRatio) * windowSize.width() + "px"});
	}

	else{
		$('.main').css({"width":"100%"});
	}
}


function videoResize(){
		isNative = (player[0].player.media.pluginType == "native");
		
		bWidth = $(window).width();
		bHeight = $(window).height() - 2;
		
		if(isNative){
		//	alert(bWidth);
			$('#player2').width(bWidth);
			$('#player2').height(bHeight);
		//	alert($('#player2').width());
    	
		//	$('#mep_0').attr('style', "width: "+bWidth+"; height: "+bHeight+";");	
//			$('#mep_0').attr('height', bHeight);

			$('.mejs-layer').width(bWidth);	
			$('.mejs-layer').height(bHeight);
    
			player[0].player.setControlsSize();
		}
	 	else {
	 		player[0].player.options.videoWidth = bWidth;
	 		player[0].player.options.videoHeight = bHeight;
     
	 		meRef.setVideoSize(bWidth,bHeight);
	 		player[0].player.setControlsSize();
	 	} 
}

$(window).resize(function(){
	bgResize();
	contentResize();
	footerRepos();
	videoResize();
	
});




function addListener(obj, evt, fnc, useCapture) {
	if(!useCapture) useCapture = false;
	if(obj.addEventListener)
		obj.addEventListener(evt, fnc, useCapture);
	else 
		obj['on'+evt] = fnc;
}

function preventDefaults(e) {
	var evt = e || window.event;
	if(evt.preventDefault)
		evt.preventDefault();
	else {
		evt.returnValue = false;
		evt.cancelBubble = true;
	}
}

function Share(par) {
	//PRIVATE
	var par;
	var self = this;

	//PUBLIC
	this.url = '';
	this.title = '';
	this.description = '';
	this.tweet = '';
	
	//METHODS
	this.init = function() {
		if(typeof par == 'string')
			par = document.getElementById(par);
		if(typeof par == 'object') {
			//Loop through share links and listen for events
			var items = par.getElementsByTagName('a');
			for(var x=0; x<items.length; x++) {
				addListener(items[x], 'click', function(e) {
					preventDefaults(e);
					self.sendShare(this);
				}, false);
			}
		}
	}
	
	this.sendShare = function(what) {
		var what;
		if(typeof what == 'object') {
			var toSend = '';
			switch(what.getAttribute('rel')) {
				case 'facebook':
					toSend = 'http://www.facebook.com/share.php?u='+encodeURIComponent(this.url);
					break;
				case 'twitter':
					toSend = 'http://twitter.com/home?status='+encodeURIComponent(this.tweet);
					break;
				case 'digg':
					toSend = 'http://www.digg.com/submit?phase=2&url='+encodeURIComponent(this.url)+'&title='+encodeURIComponent(this.description);
					break;
				case 'delicious':
					toSend = 'http://del.icio.us/post?url='+encodeURIComponent(this.url)+'&title='+encodeURIComponent(this.description);
					break;
				case 'reddit':
					toSend = 'http://www.reddit.com/submit?url='+encodeURIComponent(this.url)+'&title='+encodeURIComponent(this.description);
					break;
				case 'stumbleupon':
					toSend = 'http://www.stumbleupon.com/submit?url='+encodeURIComponent(this.url)+'&title='+encodeURIComponent(this.description);
					break;
			}
			window.open(toSend, '_blank');
		}
	}
	
	//START
	this.init();
}

function initVideo(){
	// Initialize video player
	player = $('audio,video').mediaelementplayer({
			videoWidth: bWidth,
			videoHeight: bHeight,	
			features: ['playpause','current','progress','duration','tracks','volume'],
			enableAutosize: true,
			success:function(me, domO){
				meRef = me;
				me.play();
				
			//	$("#skip-button").show();

				me.addEventListener('play', function(e){
			     player[0].player.setControlsSize();
			    }, false);
			    
			    me.addEventListener('ended', function(e){
			     removeVideo();
			    }, false);
				
			},
			error: function(){
				alert('video error');
				removeVideo();
			}
	});
	
	if(($.browser.safari)){$('.mejs-overlay-button').hide(0);}
}

