hasJavaScript();
var music, videos, share;

var videoLinks = [
	[ //Trailer
		//Defaults
		'rtmp://streams.paramount.com/flash/paramount/mp/titanic3d/Trl1/Titanic_Trl_1_960.flv',
		'http://downloads.paramount.com/mp/titanic3d/Trl1/Titanic_Trl_1_h264_640.mov',
		//HD Links
		'http://downloads.paramount.com/mp/titanic3d/Trl1/Titanic_Trl_1_h264_480.mov',
		'http://downloads.paramount.com/mp/titanic3d/Trl1/Titanic_Trl_1_h264_720.mov',
		'http://downloads.paramount.com/mp/titanic3d/Trl1/Titanic_Trl_1_h264_1080.mov'
	],
	[ //Featurette
		//Defaults
		'rtmp://streams.paramount.com/flash/paramount/mp/titanic3d/Featurette/Titanic_Featurette_REV_408Tall.flv',
		'http://downloads.paramount.com/mp/titanic3d/Featurette/Titanic_Featurette_REV_h264_640.mp4',
		//HD Links
		'http://downloads.paramount.com/mp/titanic3d/Featurette/Titanic_Featurette_REV_h264_480.mov',
		'http://downloads.paramount.com/mp/titanic3d/Featurette/Titanic_Featurette_REV_h264_720.mov',
		'http://downloads.paramount.com/mp/titanic3d/Featurette/Titanic_Featurette_REV_h264_1080.mov'
	]
];

var shareLinks = [
	[ //Trailer
		'http://www.facebook.com/sharer/sharer.php?u=http://www.youtube.com/watch?v=5d9ILag7mRA',
		'http://twitter.com/home?status=View+the+new+trailer+for+TITANIC+-+Experience+it+like+never+before+in+3D.+In+Theatres+on+April+4+http%3A%2F%2Fwww.youtube.com%2Fwatch?v=5d9ILag7mRA+via+@TitanicMovie'
	],
	[ //Featurette
		'http://www.facebook.com/sharer/sharer.php?u=http://youtu.be/DW2uyev0Wug',
		'http://twitter.com/home?status=Watch+this+TITANIC+video+-+Experience+it+like+never+before+in+3D.+In+Theatres+on+April+4+http%3A%2F%2Fyoutu.be%2FDW2uyev0Wug+via+%40TitanicMovie'
	]
];

addLoadEvent(function() {

	//Music player
	music = new musicPlayer();
		
	//Add tracks
	var tracks = $('music-tracks').getElementsByTagName('a');
	for(var x=0; x<tracks.length; x++)
		music.addTrack(tracks[x]);
			
	//Setup the small player
	setupSmallMusicPlayer();
	
	//Autoplay
	if(!music.isIOS)
		music.playTrack(0);
		
	//Add 'ended' action to music only for HTML5
	if(music.type == 'html5')
		music.player.addEventListener('ended', soundEnded, false);
	
	//Video player
	videos = new Player('player', true, false, true);
	videos.defaults['flash'] = videoLinks[0][0];
	videos.defaults['html5'] = videoLinks[0][1];
	videos.closeButton = $('video-player-close');
	videos.closeFunction = 'closeTrailer';
	videos.setupCloseButtonEvents();
	
	//Watch trailer button
	if($('trailer-play'))
		addListener($('trailer-play'), 'click', openTrailer, false);
		
	//Stop music if clicked on an outbound link
	var anch = document.body.getElementsByTagName('a');
	for(var x=0; x<anch.length; x++) {
		if(anch[x].target != '' && anch[x].target == '_blank') {
			addListener(anch[x], 'click', function() {
				if(music.status != 'paused')
					music.pauseTrack();
				if(videos.setVideoState != '')
					videos.videoState('pause');
			}, false);
		}
	}
	
	//Register pop-up
	addListener($('register'), 'click', function(e) {
		preventDefaults(e);
		window.open(this.href, '_blank', 'width=715,height=745,status=no,menubar=no,toolbar=no,location=no');
	}, false);
	
	//Video player 'featurette' and 'trailer' buttons
	var vbs = $('feature-buttons').getElementsByTagName('a');
	//Activate the first button
	vbs[0].style.visibility = 'visible';
	vbs[0].className = 'active';
	//Add events to them
	for(var x=0; x<vbs.length; x++)
		addListener(vbs[x], 'click', videoButtonClick, false);
		
	//FB Like Button
	FB.init({
		appId  : '302693276416807',
		status : true, 
		cookie : true, 
		xfbml  : true
	});
   
	FB.Event.subscribe('edge.create', function(responses) {
		//Open box
		var box = $('fb-open-container');
		box.style.visibility = 'visible';
		box.className = 'active';
		//Pause music
		if(music.status != 'paused')
			music.pauseTrack();
	});
	
	addListener($('fb-link'), 'click', function(e) {
		preventDefaults(e);
		//Open box
		var box = $('fb-open-container');
		box.style.visibility = 'visible';
		box.className = 'active';
		//Pause music
		if(music.status != 'paused')
			music.pauseTrack();
	}, false);
	
	addListener($('fb-close'), 'click', function(e) {
		preventDefaults(e);
		setTimeout(function() { $('fb-open-container').style.visibility = 'hidden'; }, 500);
		$('fb-open-container').className = '';
	}, false);
	
});

/*-------------------
  FUNCTIONS
  -------------------
*/
function videoButtonClick(e) {
	preventDefaults(e);
	//Activations
	var vbs = $('feature-buttons').getElementsByTagName('a');
	for(var x=0; x<vbs.length; x++) {
		if(this.id != vbs[x].id) {
			vbs[x].style.visibility = 'visible';
			vbs[x].className = 'active';
		}
		else {
			vbs[x].style.visibility = 'hidden';
			vbs[x].className = '';
		}
	}
	//Now do stuff
	var hd = $('video-hd-links').getElementsByTagName('a');
	var sh = $('share-video').getElementsByTagName('a');
	switch(this.id) {
		case 'button_featurette':
			videos.defaults['flash'] = videoLinks[1][0];
			videos.defaults['html5'] = videoLinks[1][1];
			for(var x=0; x<hd.length; x++)
				hd[x].href = videoLinks[1][x+2];
			for(var x=0; x<sh.length; x++)
				sh[x].href = shareLinks[1][x];
			break;
		case 'button_trailer':
			videos.defaults['flash'] = videoLinks[0][0];
			videos.defaults['html5'] = videoLinks[0][1];
			for(var x=0; x<hd.length; x++)
				hd[x].href = videoLinks[0][x+2];
			for(var x=0; x<sh.length; x++)
				sh[x].href = shareLinks[0][x];
			break;
	}
	videos.playVideo();
}

function soundEnded() {
	music.nextTrack();
}

function closeTrailer() {
	$('video-player').className = '';
	videos.videoState('pause');
}

function openTrailer(e) {
	preventDefaults(e);
	$('video-player').className = 'active';
	//Always play Trailer when opened
	var hd = $('video-hd-links').getElementsByTagName('a');
	var sh = $('share-video').getElementsByTagName('a');
	videos.defaults['flash'] = videoLinks[0][0];
	videos.defaults['html5'] = videoLinks[0][1];
	for(var x=0; x<hd.length; x++)
		hd[x].href = videoLinks[0][x+2];
	for(var x=0; x<sh.length; x++)
		sh[x].href = shareLinks[0][x];
	$('button_trailer').style.visibility = 'hidden';
	$('button_trailer').className = '';
	$('button_featurette').style.visibility = 'visible';
	$('button_featurette').className = 'active';
	//Play video
	videos.setFlashScaling('false');
	videos.playVideo();
	if(music.status != 'paused')
		music.pauseTrack();
}

function setupSmallMusicPlayer() {
	addListener($('small-player-track'), 'click', function(e) {
		preventDefaults(e);
		$('music-player-open').className = 'active';
	}, false);
	
	addListener($('music-player-open-close'), 'click', function(e) {
		preventDefaults(e);
		$('music-player-open').className = '';
	}, false);
	
	addListener($('small-player-play'), 'click', function(e) {
		preventDefaults(e);
		smallPlayerPlay('', false);	
	}, false);
	
	music.extraPlayAction = function() {
		smallPlayerPlay('play', true);
		var track = music.tracks[music.current]['title'];
		var limit = 40;
		if(track.length > limit)
			track = track.substring(0, limit) + '...';
		$('small-player-track').innerHTML = track;
	}
	
	music.extraPauseAction = function() {
		smallPlayerPlay('pause', true);
	}
	
	addListener($('small-player-prev'), 'click', function(e) {
		preventDefaults(e);
		music.previousTrack();
	}, false);
	
	addListener($('small-player-next'), 'click', function(e) {
		preventDefaults(e);
		music.nextTrack();
	}, false);
}

function smallPlayerPlay(type, onlyDisplay) {
	var type, onlyDisplay;
	var play = $('small-player-play');
	if(type == 'pause' || (type == '' && play.className.indexOf('active') !== -1)) {
		play.className = play.className.replace(' active','');
		if(onlyDisplay != true)
			music.pauseTrack();
	}
	else if(type == 'play' || (type == '' && play.className.indexOf('active') === -1)) {
		play.className = play.className.replace(' active','') + ' active';
		if(onlyDisplay != true)
			music.playTrack();
	}
}

function $(elem) {
	return document.getElementById(elem);
}

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

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

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
		window.onload = func;
	else {
		window.onload = function() {
			if(oldonload)
				oldonload();
			func();
		}
	}
}

function hasJavaScript(msg) {
	var msg = (msg == null || msg == '' ? '#no-js, #no-js-bg' : msg);
	//"No Javascript" message
	var head = document.getElementsByTagName('head')[0];
	var njsStyle = document.createElement('style');
		njsStyle.setAttribute('type','text/css');
	var rule = msg+' {display: none} ';
	if(head){
		if(njsStyle.styleSheet)
			njsStyle.styleSheet.cssText = rule;
		else
			njsStyle.appendChild(document.createTextNode(rule));
		head.appendChild(njsStyle);
	}
}

//SHARE CLASS
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();
}
