//---------
// GLOBALS
//---------

var UBER = {};

UBER.notifyTimeout = null;
UBER.shareItObj = null;
UBER.countdownInterval = null;

//-----------
// FUNCTIONS
//-----------

/*
* Adds a video to the page and loads it
*/
UBER.loadVideo = function(_url, _width, _height, _cover, _autostart, _quality) {			

	var flashvars = {};
	flashvars.file = _url;
	flashvars.cover = _cover;
	flashvars.quality = _quality;
	flashvars.width = _width;
	flashvars.height = _height;
	if(_autostart) flashvars.autostart = 'true';	

	var params = {};
	params.menu = 'false';
	params.allowfullscreen = 'true';
	params.allowscriptaccess = 'always';
	params.wmode = 'transparent';
	
	swfobject.embedSWF(SITE_URL + '_static/swf/player.swf', 'yt', _width, _height, '9.0.0', false, flashvars, params);
		
}

/*
* Displays a notification in newsletter subscription box
*/

UBER.subscriptionNotify = function(_message) {
	
	// show notification screen
	$('.newsletter .response').html(_message);
	$('.newsletter .response').fadeIn();
	$('.newsletter .body').fadeOut('fast');			

	// hide it after a few seconds
	UBER.notifyTimeout = setTimeout(function() {
		$('.newsletter .response').fadeOut('fast');
		$('.newsletter .body').fadeIn();	
		clearTimeout(UBER.notifyTimeout)
	}, 2000);	
}

/*
* Displays the countdown time
*/
var ubercount  = 0;
UBER.displayCountdown = function()
{
	var countdown = $('#countdown #digits');
		countdown.html('');
		
	var now = new Date((CURR_TIME + ubercount) * 1000);
	var releaseDate = new Date('August 11, 2010 01:00:00 PST');
	var diff = Math.floor((releaseDate.valueOf() - now.valueOf()) / 1000);

    if(diff > 0)
    {   
		ubercount++;
	
		var days  = Math.floor( diff / 86400);
		var hours = Math.floor( diff / 3600);
		var mins  = Math.floor( diff / 60);
		var secs  = diff;
	
		var dd = days;
			dd = (dd < 10) ? ('0' + dd) : dd;
		
		var hh = hours - days  * 24;
			hh = (hh < 10) ? ('0' + hh) : hh;
		
		var mm = mins  - hours * 60;
			mm = (mm < 10) ? ('0' + mm) : mm;
			
		var ss = secs  - mins  * 60;
			ss = (ss < 10) ? ('0' + ss) : ss;
	
		var digits = dd + 'd' + hh + 'd' + mm + 'd' + ss;
		var digitsArray = digits.split('');
	
		for(var i=0;i<digitsArray.length;i++)
		{
			countdown.append('<span class="d' + digitsArray[i] + '" style="margin-top:'+i+'px">' + digitsArray[i] + '</span>');
		}
    }
    else
    {
    	// stop counter
    	clearInterval(UBER.countdownInterval);
    	UBER.countdownInterval = null;
		
   		// show buy now
   		$('#countdown').fadeOut('fast', function(){
   			$('#digits', this).remove();
			$(this).addClass('buy-now');
   			$(this).fadeIn('slow');
   		});    	
    }
		
}


$(document).ready(function() {

	
	//-------------------------
	// DECORATIVE & FUNCTIONAL
	//-------------------------
		
	/*
	 * let CSS know that JS is enabled
	 */
	
	$('div#container').removeClass('noJS');

	/*
	 * Add corners to modules, buttons
	 */

	$('div#navMain > ul > li > a').append('<span class="left"></span><span class="right"></span>');
	$('div#navMain > ul > li').append('<div class="marker"></div>');
	$('div#navSection > ul > li > a').append('<span class="left"></span><span class="right"></span>');
	$('div#navSubSection > ul > li > a').append('<span class="left"></span><span class="right"></span>');	
	$('div.module:not(.highlight, .newsletter) div.wrapper').append('<div class="fold"></div>');
	$('div.module.twitter').append('<div class="bird"></div>');
	$('div.related div.header').append('<div class="fold"></div>');
	$('div#accordion div.header').append('<div class="fold"></div>');
					
	// add countdown to header
	$('div#navMain').append('<div id="countdown"><div id="digits"></div></div>');
	$('div#countdown').click(function()
	{
		window.open('http://www.xbox.com/en-US/games/m/mondaynightcombatxbla/');
		return false;
	});
	
	/*
	* Newsletter Sign Up input field hint
	*/

	var email = '.module.newsletter #email';

	// hide when focus is on	
	$(email).focus(function(){
		$('label', $(this).parent()).css('display', 'none');
	});

	// show when blur
	$(email).blur(function(){
		$('label', $(this).parent()).css('display', ($(this).val() == '') ? 'block' : 'none');
	});	

	// hide if there is any text in the username field
	if($(email).val() != '') {
		$('label', $(email).parent()).css('display', 'none');	
	}

	/*
	 * Add .last and .first to last item of the group
	 */
	$('ul li:last-child').addClass('last');
	$('ul li:first-child').addClass('first');
	$('#content .related:last-child').addClass('last');
	$('.job:last-child').addClass('last');
		
	/*
	 * Wrap HR with div
	 */
	$('hr').wrap('<div class="hr"></div>');

	//-------------	
	// INTERACTIVE
	//-------------

	/*
	 * Sponsors slideshow
	 */
	
	if($('#sponsors').length > 0) {	
		$('#sponsors .content').cycle({
			fx: 'scrollDown',
			easeIn:  'easeInBounce', 	
			speedIn: 800,
			speedOut: 800, 		 			
			timeout: 10000,
			pause: true,
			sync:true
		});	
	}
	
	/*
	 * Characters slideshow
	 */
	
	if($('.slideshow').length > 0) {

		$('.slideshow .content').cycle({
			fx: 'fade',
			timeout: 5000,
			pager: '.slideshow .controls ol',
			timeout:0,
	    	pagerAnchorBuilder: function(id, slide) {
	    	   return '<li' + ($(slide).hasClass('embeddedVideo') ? ' class="video"' : '') + '><a href="#">' + (id + 1) + '</a></li>';
	    	},
			pause: true
		});
	
		$('.slideshow .controls a').click(function(){
			$(this).blur();
		});
	
		$('.slideshow .controls a.playback').click(function(){
			if($(this).hasClass('pause')) {
				$('.slideshow .content').cycle('resume');
				$(this).removeClass('pause');						
			}
			else {
				$('.slideshow .content').cycle('pause');		
				$(this).addClass('pause');					
			}
			return false;	
		});

		// hide if there is no content
		if($('.slideshow .content').children().length <= 1) {
			$('.slideshow .content').css('border-bottom', '1px solid #000');			
			$('.slideshow .controls').hide();
		}
	
		// center slideshow controls
		var cntrl = $('.slideshow .controls');
		var cntrlx = $('.slideshow .controls div');
		//cntrlx.css("left", ((cntrl.width()/2) - (cntrlx.width() / 2) - 20));  
		cntrlx.css("left", ((cntrl.width()/2) - (cntrlx.width() / 2)));  		
	}
	
	// center image gallery pager
	var cntrlb = $('#imageGallery .options');
	var cntrlxb = $('#imageGallery .options ol');
	cntrlxb.css("left", ((cntrlb.width()/2) - (cntrlxb.width() / 2)));

	// image galleyr

	// add last-col to image gallery li
	$('#imageGallery ul li').each(function(i) 
	{
		i += 1;
		
		if(i != 0 && i % 5 == 0) {
			$(this).addClass('last-col')
			$(this).removeClass('last');
		}
	});

	/*
	* Load home page featured video
	*/
	
	if($('#yt.home').length > 0) {
		
		// get video location, clear default contents
		var videoCover = $('img', $('#yt')).attr('src');
		var videoUrl = $('a', $('#yt')).attr('href');
		
		// load home video
		UBER.loadVideo(videoUrl, 640, 390, videoCover, false, 'hd');	
	}
	

	/*
	* Load video in video gallery
	*/
	
	if($('#yt.filmstrip').length > 0) {
		
		// get video location, clear default contents
		var videoCover = $('#yt img').attr('src');
		var videoUrl = $('#yt a').attr('href'); 
		var videoTitle = $('#yt img').attr('alt');
		
		// set title
		$('.description .videoTitle').html(videoTitle);
					
		// load video in gallery			
		UBER.loadVideo(videoUrl, 920, 548, videoCover, false, 'hd');	
	}
	
	/*
	 * Enable customized SharedThis button
	*/

	if($('#shareThis').length > 0) {
	    UBER.shareItObj = SHARETHIS.addEntry({},{button:false});
	    UBER.shareItObj.attachButton(document.getElementById('shareThis'));	
	}
	
	/*
	* Setup subscription Form
	*/

	if($('#subscribeForm').length > 0) {	
		
		$('#subscribeForm').ajaxForm({
			dataType: 'json',
			beforeSubmit: function(formData, jqForm, options) {
				// validate
				if(!(/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/.test($('#subscribeForm #email').attr('value')))) {
					// show error
					UBER.subscriptionNotify('Please enter a valid email address.');				
					return false;
				}			
			},
			success: function(_response) {
			
				if(_response.success == true) {
					// success, reset the email field
					$('.newsletter label').css('display', 'block');				
					$('.newsletter #email').attr('value', '');
				}
				else {
					$('.newsletter #email').focus();
				}

				UBER.subscriptionNotify(unescape(_response.message));
			}
		});
	}
	
	
	/*
	* Enable clicks in navSubSections LI to take user to Character's page
	*/
	
	$('#navSubSection li').click(function() {
		window.location.href = $('a', $(this)).attr('href');
	});
	

	/**
	* Enable twitter feeds
	*/
	if($('.tweet').length > 0)
	{
	    $('.tweet').tweet({
	      username: "UberEnt",
	      avatar_size: 48,
	      count: 1,
	      loading_text: "loading tweets..."
	    });			
	}

	
	/*
	* Enable accordion 
	*/
	if($('#accordion').length > 0)
	{
		$("#accordion").accordion({collapsible:true});
	}


	/*
	* Enable static menu 
	*/
	if($('#content.floating').length > 0)
	{
		var menu = $('#content.floating');
		var position = menu.offset();

		$(window).scroll(function()
		{

			if($(this).scrollTop() > 268 && !menu.hasClass('static'))
			{
				menu.fadeOut('fast').fadeIn('fast').addClass('static');
			}
			else if($(this).scrollTop() <= position.top && menu.hasClass('static'))
			{
				menu.fadeOut('fast').fadeIn('fast').removeClass('static');
			}

			
		});
	}
	
	
	/*
	* Countdown
	*/
	
	UBER.countdownInterval = setInterval('UBER.displayCountdown()', 1000);
	UBER.displayCountdown();	
		
	//----------
	// CLEAN UP
	//----------
	
		
});


