/* Harry Potter, Deathly Hallows part 2 | Global jQuery | March 21 2011 */

var CONFIG ={
	
	DEBUG : false
		
}

$(function(){
	var change_country = $('#change-locale a.change-country');
	change_country.bind('click', function() { $('#change-locale ul').fadeToggle() });
});

$(document).ready(function() {
	SITE.GENERIC.assignSubMenuZIndex();
	SITE.GENERIC.centerContent('.centerMenu');
	var $countdown = $('#game-launch dl.numbers');
	HarryPotterCountdown.init({
		now: $countdown.parent().find('.now').text(),
		to: $countdown.attr('title'),
		dom: {
			days: $countdown.find('.days'),
			hours: $countdown.find('.hours'),
			minutes: $countdown.find('.minutes'),
			seconds: $countdown.find('.seconds')
		},
		callback: function() {
			$("#game-launch").hide();
		}
	});
	$(".selectLanguage").colorbox({html:'<div id="languageSelector"><h2>Choose your country below</h2></div>', onComplete:function(){
		if($('#languageSelector UL').length<1){
			$('#change-locale UL').clone().appendTo("#languageSelector").show();
		}
		return false;
	}});
	$(".launchSignUp, .utilities .subscribe").colorbox({href:$('.launchSignUp a').attr('href'), innerWidth:"460px", innerHeight:"443px", iframe:true, onComplete:function(){
	
    }});
	
    
    
});

// ----------
// Safe console
// ----------
if (typeof console === 'undefined') console = {};
if (typeof console.log === 'undefined') console.log = function(){};

// ----------
SITE = {};
// ----------

SITE.GENERIC = {
	assignSubMenuZIndex: function() {
		var valueZindex = 9999;
		if (isIE8 == 1) {
			$('#game-page .section-wrapper section #tabs-nav ul#primary-sub-nav li').each(function() {
				var theElement = $(this);
				if (theElement.attr('class').indexOf('current') >= 0) {
					theElement.css({
						'z-index':10000
					})
				} else {
					theElement.css({
						'z-index':valueZindex
					})
				}
				valueZindex--;
			});
		}
	},


	centerContent:function(classToFind) {
		$(classToFind).each(function() {
			var eachElement = $(this);
			eachElement.css({
				'float':'left'
			});
			var widthElement = eachElement.width();
			var widthParent = eachElement.parent().width();
			var paddingToAdd = (widthParent - widthElement)/2;
			eachElement.css({
				'padding-left':paddingToAdd + 'px',
				'visibility':'visible'
			});
		});
	}/*,
   
	centerContent:function(classToFind) {
		$(classToFind).each(function() {
			var eachElement = $(this);
			eachElement.css({
				'float':'left'
			});
			var widthElement = SITE.GENERIC.getTotalWidth(eachElement);
			var widthParent = SITE.GENERIC.getTotalWidth(eachElement.parent());
			var paddingToAdd = (widthParent - widthElement)/2;
			eachElement.css({
				'padding-left':paddingToAdd + 'px',
				'visibility':'visible'
			});
		});
	},
	
	getTotalWidth: function(theElement) {
		var totalWidth = theElement.width();
		totalWidth += parseInt(theElement.css('padding-left').replace('px', '')) + parseInt(theElement.css('padding-right').replace('px', ''));
		totalWidth += parseInt(theElement.css('margin-left').replace('px', '')) + parseInt(theElement.css('margin-right').replace('px', ''));
		if (!isNaN(parseInt(theElement.css('borderLeftWidth').replace('px', '')))) {
			totalWidth += parseInt(theElement.css('borderLeftWidth').replace('px', ''));
		}
		if (!isNaN(parseInt(theElement.css('borderRightWidth').replace('px', '')))) {
			totalWidth += parseInt(theElement.css('borderRightWidth').replace('px', ''));
		}
		return totalWidth;
	}
	*/


};

// *******************************************************************************************
var HarryPotterCountdown = {
	config: null,
	timeout: null,

	init: function(config) {
		this.config = config;
		this.config.now = new Date(this.config.now);
		this.config.to = new Date(this.config.to);
		this.updateTimer();
	},
	
	updateTimer: function() {
		
		this.config.now = new Date(this.config.now.valueOf() + 1000);		
		var difference = new Date(this.config.to - this.config.now);
		difference_seconds = Math.floor(difference / 1000);
		
		if (difference_seconds < 0) {
			this.config.callback(); return;
		}
		
		var days = Math.floor(difference_seconds / 86400);
		var hours = Math.floor((difference_seconds - (days * 86400)) / 3600);
		var minutes = Math.floor((difference_seconds - (days * 86400) - (hours * 3600)) / 60);
		var seconds = Math.floor((difference_seconds - (days * 86400) - (hours * 3600) - (minutes * 60)));
		
		this.config.dom.days.text(this.padZero(days));
		this.config.dom.hours.text(this.padZero(hours));
		this.config.dom.minutes.text(this.padZero(minutes));
		this.config.dom.seconds.text(this.padZero(seconds));
		
		clearTimeout(this.timeout);
		this.timeout = null;
		
		var self = this;
		this.timeout = setTimeout(function() {
			self.updateTimer();
		}, 1000);
	},
	
	padZero: function(number) {
		if (number.toString().length == 1) number = '0' + number;
		return number;
	}
}

