/**
 * global script that is present on all pages
 */

var global = {};

global.func = function() {

	/**
	 * returns the scroll offsets for a page
	 *
	 * http://www.geekdaily.net/2007/07/04/javascript-cross-browser-window-size-and-centering/
	 */
	function get_scroll_offsets() {

		var offsets = {x:0, y:0};

		//IE
		if (!window.pageYOffset) {
			//strict mode
			if(!(document.documentElement.scrollTop == 0)) {
				offsets.y = document.documentElement.scrollTop;
				offsets.x = document.documentElement.scrollLeft;
			}
			//quirks mode
			else {
				offsets.y = document.body.scrollTop;
				offsets.x = document.body.scrollLeft;
			}
		}
		//w3c
		else {
			offsets.x = window.pageXOffset;
			offsets.y = window.pageYOffset;
		}

		return offsets;
	}


	/**
	 * getPageSize() by quirksmode.com
	 *
	 * @return Array Return an array with page width, height and window width, height
	 */
	function get_page_size()
	{

		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
			
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		var sizes = {
			page_width: pageWidth,
			page_height: pageHeight,
			window_width: windowWidth,
			window_height: windowHeight
		};
		
		return sizes;
	}

	
	function center( what )
	{

		// center panel in middle of browser
		var offsets = get_scroll_offsets();

		$('#'+what).css('top',  ($(window).height() / 2) - ($('#'+what).height() /2) + offsets.y );
		$('#'+what).css('left', ($(window).width() /2) - ($('#'+what).width() /2) );
	}  


	function setup_player_instructions()
	{

		$('.player_instructions_link').click(function(){		
			global.func.popup_player_instructions();
		});

		

		$('#player_instructions').click(function(){
			global.func.close_player_instructions();
		});
	}


	function popup_player_instructions()
	{
		center( 'player_instructions' );
		global.func.show_overlay( '#000', 0.7 );
		$('#player_instructions').show();
	}


	function close_player_instructions()
	{
		
		$('#player_instructions').hide();
		global.func.hide_overlay();
	}


	function display( div_id, bkgclr, opacity )
	{
		var page_sizes = get_page_size();

		if ( bkgclr == '' ) { bkgclr = '#000'; }
		if ( opacity == '' ) { opacity = 0.8; }

		$(div_id).css({
			backgroundColor: bkgclr,
			opacity: opacity,
			width: page_sizes.page_width,
			height: page_sizes.page_height
		}).show();

		// If window was resized, calculate the new overlay dimensions
		$(window).resize(function() {
			var page_sizes = get_page_size();
				
			// Style overlay and show it
			$(div_id).css({
				width: page_sizes.page_width,
				height: page_sizes.page_height
			});
		});
	}	
	

	return {

		setup_player_instructions : function() {
			setup_player_instructions();
		},

		popup_player_instructions : function() {
			popup_player_instructions();
		},

		close_player_instructions : function() {
			close_player_instructions();
		},

		center : function( dom_id ) {
			center( dom_id );
		},
			
		get_scroll_offsets : function() {
			get_scroll_offsets();
		},

		show_overlay : function( bkgclr, opacity ) {
			display( '#content_overlay', bkgclr, opacity );
		},

		hide_overlay : function() {
			$('#content_overlay').hide();
		}

	}
}();



/**
 * control for favourites and seen video functionality
 */
global.video = function() {


	function response_set_seen( res )
	{

		if ( res.result != 'success' ) {
			return;
		}
		

		if ( $('#seen_ctrl__' + res.media_id).val() == 'unmark' ) {
		
			$('#seen_button__' + res.media_id).html( 'mark as seen' );
			$('#seen_ctrl__' + res.media_id).val( 'mark' );
		}
		else {
			$('#seen_button__' + res.media_id).html( 'mark as unseen' );
			$('#seen_ctrl__' + res.media_id).val( 'unmark' );			
		}
	}
	

	function set_seen( media_id )
	{

		var data = 'S:' + media_id + ':' + $('#seen_ctrl__' + media_id).val();
			
		$('#rpc_form input[@name=data]').val( data );


		$('#rpc_form').ajaxSubmit({
			url: 'main.php?page=player&out=no&cmd=send&from=ajax',
			dataType: 'json',
			success: global.video.response_set_seen
		});
	}


	function response_set_favourite( res )
	{

		if ( res.result != 'success' ) {
			return;
		}
		

		if ( $('#favourite_ctrl__' + res.media_id).val() == 'unmark' ) {
		
			$('#fav_button__' + res.media_id).html( 'add to faves' );
			$('#favourite_ctrl__' + res.media_id).val( 'mark' );
		}
		else {
			$('#fav_button__' + res.media_id).html( 'remove from faves' );
			$('#favourite_ctrl__' + res.media_id).val( 'unmark' );
		}
	}

	
	function set_favourite( media_id )
	{

		var data = 'F:' + media_id + ':' + $('#favourite_ctrl__' + media_id).val();
			
		$('#rpc_form input[@name=data]').val( data );


		$('#rpc_form').ajaxSubmit({
			url: 'main.php?page=player&out=no&cmd=send&from=ajax',
			dataType: 'json',
			success: global.video.response_set_favourite
		});
	}

	
	return {

		response_set_seen : function(res) {
			response_set_seen( res );
		},

		response_set_favourite : function(res) {
			response_set_favourite( res );
		},

		set_seen : function( media_id ) {
			set_seen( media_id );
		},

		set_favourite : function( media_id ) {
			set_favourite( media_id );
		}
	}
}();


/**
 * popup floating flash player
 */
global.player = function() {

	var player_media_id = false;

	return {

		show : function( media_id, artist_id ) {

			$('#floating_player').removeClass('floating_player_43');
			$('#floating_player').removeClass('floating_player_169');
			$('#floating_player').addClass('floating_player');
			$('#floating_player_iframe').attr( 'height', '370' );

			global.func.center('floating_player');

			global.func.show_overlay( '#000', 0.7 );
			
			// load up video in iframe
			var url = '/public/main.php?page=flash_player&out=bkg&media_id='
					+ media_id + '&artist_id=' + artist_id;

			$('#floating_player_iframe').attr( 'src', url );

			$('#floating_player').show();

			player_media_id = media_id;

		},

		hide : function() {

			$('#floating_player').hide();
			global.func.hide_overlay();

			$('#floating_player_iframe').attr( 'src', '' );
		},

		show_freebie : function(video, width, height) {

			if ( (width/height) > 1.4 ) {
				// 16:9
				$('#floating_player').removeClass('floating_player_43');
				$('#floating_player').addClass('floating_player_169');
			}
			else {
				// 4:3  = 1.333333
				$('#floating_player').removeClass('floating_player_169');
				$('#floating_player').addClass('floating_player_43');
			}

			$('#floating_player_iframe').attr( 'height', height );


			global.func.center('floating_player');

			global.func.show_overlay( '#000', 0.7 );
			
			// load up video in iframe
			var url = '/public/main.php?page=guest_player&out=no'
				+ '&video=' + video
				+ '&width=' + width
				+ '&height=' + height;
			
			$('#floating_player_iframe').attr( 'src', url );

			$('#floating_player').show();
		}

	}
}();


/**
 * guest, home page center flash player
 */
global.guest_player = function() {
	return {

		show : function( video ) {

			$('#showreel_player_iframe').attr('src','/public/main.php?page=guest_player&out=no&autoplay=yes&height=270&width=480&video='+video);
		}
	}
}();


global.redirect = function() {

	return {
		join_page : function() {
			parent.global.redirect.url( '/public/main.php?page=join' );
		},

		login_page : function() {
			parent.global.redirect.url( '/public/main.php?page=login' );
		},

		url : function( url ) {
			document.location = url;
		}
	}
}();


/**
 * CONSTRUCT
 */
$(document).ready(function(){

	global.func.setup_player_instructions();

});
