// Register NICK overlay Namespace
if(typeof NICK == "undefined" || !NICK) var NICK = {};
NICK.overlay = {};
//NICK.namespace("overlay");

(function($) {
		  
//if(!$) {
//	return;
//}

NICK.overlay.isLoading = false;
NICK.overlay.isOpen = false;
NICK.overlay.isIE6 = false;

/**
 * Create the HTML template schema based on clients browser
 */
NICK.overlay.create = function() {
	// Create Overlay Template
	$('<div class="odop_popup_content_loader"><!-- Content Loader --></div>' +
	  '<div class="odop_popup">' +
			'<div class="odop_popup_box">' +
				'<div class="odop_popup_wrapper">' +
					'<div class="odop_popup_header">' +
						'<h2 class="odop_popup_title"></h2>' +
						'<a class="odop_popup_close">Close</a>' +
						'<span class="odop_popup_loader">Loading...</span>' +
					'</div>' +
					'<div class="odop_popup_content"></div>' +
				'</div>' +
			'</div>' +
		'</div>').appendTo( $("body") );

	$(".odop_popup_close").click(NICK.overlay.close);

	var userAgent = navigator.userAgent.toLowerCase();
	var isIE = /msie/.test( userAgent );
	var bVersion = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([0-9]+)/ ) || [0,'0'])[1];

	if ( isIE ) {
		if ( bVersion == 6 ) {
			this.isIE6 = true;

			$(".odop_popup").addClass("odop_popup_ie6");
			$("<div />").addClass("odop_popup_square_borders").prependTo( $(".odop_popup_wrapper") );

			$(window).bind("scroll", function() {
				if ( NICK.overlay.isOpen ) {
					$(".odop_popup").css("top", $(window).scrollTop() + 50);
				}
			});
		} else {
			$(".odop_popup").addClass("odop_popup_ie7");
			$('<div class="odop_popup_vs"></div>' +
			  '<div class="odop_popup_hs"></div>' +
			  '<div class="odop_popup_tl"></div>' +
			  '<div class="odop_popup_tr"></div>' +
			  '<div class="odop_popup_br"></div>' +
			  '<div class="odop_popup_bl"></div>').prependTo( $(".odop_popup_wrapper") );
		}
	} else {
		$(".odop_popup").addClass("odop_popup_rb");
	}

	// Apply the init to all elements with the 'nick-overlay' class
	$(".nick-overlay").click(function(e) {
		var title = $(this).attr("title");
		var url = $(this).attr("href");
		NICK.overlay.open(title, url, { method: "ajax" });
		return false;
	});
}

/*
 * Replace the close button temporarily with a loading icon
 */
NICK.overlay.loadingToggle = function() {
	if ( this.isLoading == true ) {
		this.isLoading = false;
		$(".odop_popup_loader").hide();
		$(".odop_popup_close").show();
	} else {
		this.isLoading = true;
		$(".odop_popup_loader").show();
		$(".odop_popup_close").hide();
	}
}

/**
 * Close overlay
 */
NICK.overlay.close = function() {
	$(".odop_popup_box").fadeOut("fast", function() {	$(".odop_popup").hide(); } );
	$("#nick-overlay-modal-dop").remove();
	//NICK.utils.showSwfs();
	NICK.overlay.isOpen = false;
}

/**
 * Open overlay
 */
NICK.overlay.open = function(title, url, options) {
	var defaults = {
		title: null,
		modal: true,
		method: "ajax"
	};
	var options = $.extend(defaults, options);
	NICK.overlay.isOpen = true;

	// Hide flash objects
	//NICK.utils.hideSwfs();

	// Set overlay title
	$(".odop_popup_title").text(title || options.title);

	// Disable and fade out contents if a modal
	if ( options.modal == true ) {
		$("#nick-overlay-modal-dop").remove();
		$("body").append($("<div />")
			.attr("id", "nick-overlay-modal-dop")
			.css({opacity: 0.6, height: $(document).height()})
		);
	}

	// Open a loading overlay
	$(".odop_popup_content").html("Loading");
	$(".odop_popup_box").css("width", 250).show();
	$(".odop_popup").show().addClass("odop_popup_loading")

	// Dynamically place the overlay 50px from current top in IE6 since 'static' doesnt work
	if ( this.isIE6 ) {
		$(".odop_popup").css("top", $(window).scrollTop() + 50);	
	}

	// Load content based on given method
	/*switch( options.method ) {
		case "ajax":
			$(".odop_popup_content_loader").load(url, function() {
				var w = $(this).width() + 40;
				var h = $(this).height() + 40;
				var html = $(this).html();

				$(this).empty();
				NICK.overlay.loaded(options, html, w, h);
			});
			break;
	}*/
	NICK.overlay.loaded(options, "", 580, 546);
	
	
}

/*
 * Final settings once overlay data has been initialized
 */
NICK.overlay.loaded = function(options, html, w, h) {
	$(".odop_popup").removeClass("odop_popup_loading");

	$(".odop_popup_box").css({
		width: w,
		height: h
	});

	$(".odop_popup_content").html("<img src='http://www.nick.com/dayofplay/overlay-assets/roadblock3.jpg'>");
}

NICK.overlay.checkDOPRoadblock = function(coast) {
	$.get("http://www.nick.com/dynamo/data/dayofplay/dayofplay-poll.jhtml", {coast: coast}, function(data){
 		if (data.showRoadblock == "true") {	  
			NICK.overlay.open("WORLDWIDE DAY OF PLAY", "http://www.nick.com/dayofplay/roadblock.jhtml");
		}		
	}, "jsonp");
}

NICK.overlay.getRoadblockTimezone = function() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	return std_time_offset;
}

NICK.overlay.getCoastTimezone = function() {
	if (NICK.overlay.getRoadblockTimezone() == -5)
		return "east";
	else return "west";
}

// Create the overlay template on document ready
$(document).ready(function() { NICK.overlay.create(); });

})(typeof jQuery === "undefined" ? $ : jQuery);


// JavaScript Document