var MonthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x] == this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}  

Array.prototype.uniqueByTitle = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x].title == this[i].title)
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

String.prototype.trim = function () {
	var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function expandElementAnimated(element, newHeight, preserveWidth) {
	$(element).animate( {
				   height: newHeight,
				   width: preserveWidth ? $(element).width() + "px" : "auto"
				   	},
					800);
}

var adSelectorContainer = "#lightbox-ad-container";
function lightboxAd(adSelector, adDuration, adFrequency) {
	var windowHeight = $(window).height();
	$(adSelectorContainer).add(adSelector).height( (windowHeight > 830) ? windowHeight : 830 );
	if(adDuration == null) return
	$(adSelectorContainer).add(adSelector).show();
	adLightbox = $(adSelectorContainer).overlay({
		absolute: true,
		top: 0,
		left: 0,
		closeOnEsc: false,
		api: true
	}).load();
	function closeAdLightbox() {
		adLightbox.close();
		if($.browser.msie && $.browser.version == "6.0") $(adSelectorContainer).hide();
		//$(".ticker, .site-container, .wrapper").show();
		$(".ticker").show();
		setVisible($(".site-container, .wrapper"), true);		
		$(window).unbind("resize");
	}
	$(adSelectorContainer).click(function() {
		closeAdLightbox();
	});
	$(window).load(function() {
		setTimeout(closeAdLightbox, adDuration);
	});
	jQuery.cookie('MSG_BUD_VISITED', 'true', { path: '/', expires: adFrequency });
}
function showLightboxAd(adSelector, adDuration, adFrequency) {
	if (jQuery.cookie("MSG_BUD_VISITED")) {
		$(document).ready(function() {
			//$(".ticker, .site-container, .wrapper").show();
			$(".ticker").show();
			setVisible($(".site-container, .wrapper"), true);
		});
		return
	}
	$(document).bind("ready", function() {
		$(adSelectorContainer).html($(adSelector).get());
		lightboxAd(adSelector, adDuration, adFrequency)
	});
	$(window).bind("resize", function() {
		lightboxAd(adSelector, null, adFrequency);							  
	});
}

function showLightbox(divId, title)
{
	$(document).ready(function(){
		$("#lightboxWrapper .lightboxBody").html($("#" + divId).html());
		$("#lightboxWrapper .lightboxHeading H1").html(title);
		$("#" + divId).css("display", "none");
		
		$("#lightboxWrapper").overlay({ 
			top: "center",
			
		    expose: { 
		        color: '#333', 
		        loadSpeed: 200, 
		        opacity: 0.9 
		    }, 
		 
		    // disable this for modal dialog-type of overlays 
		    closeOnClick: false, 
		 
		    // we want to use the programming API 
		    api: true 
		 
		// load it immediately after the construction 
		}).load();
	});
}

function isIPad () 
{
	return (navigator.userAgent.match(/iPad/i));
}
 
function isIPod () 
{
	return (navigator.userAgent.match(/iPod/i));
}
 
function isIPhone () 
{
	return (navigator.userAgent.match(/iPhone/i));
}

function isIOS () 
{
	return this.isIPhone() || this.isIPod() || this.isIPad();
}
 
function setVisible (element, visible) {
	element.css("visibility", visible ? "visible" : "hidden");
	/*
	if (isIOS()) {
		element.css("display", visible ? "block" : "none");
	}
	else {
		element.css("visibility", visible ? "visible" : "hidden");
	}
	*/
}

/* DART handling */
var dart = new function() {
	this.ord = Math.random() * 10000000000000000 + "?";
	this.tileIndex = 1; //* this has to be sequential, doesn't have to be tied to specific size

	this.size = "";
	this.position = "";
	this.videoId = "";
	
	//* videoId will only be passed in when showing the video
	this.getUrl = function(size, position)
	{
		this.size = size;
		this.position = position;
		
		var showDcopt = (size == "960x50");	//* dcopt=ist should only happen once per page, 
											//* it controls "expandable" ads and there should only be one
	
		var dartCall = (size == "1000x1000") ? "pfadx" : "adj";
	
		var url = "http://ad.doubleclick.net/" + dartCall + "/msg.msg.oao/{zone1}{zone2};" +
			"s1={s1};" +
			"s2={s2};" +
			"url={url};" +
			"kw={searchStr};" +
			"test={test};" +
			"{vid}" +
			"msg=ad;" +
			"pos={pos};" +
			"tile=" + this.tileIndex + ";" + 
			"sz={size};" +
			(showDcopt ? "dcopt=ist;" : "") +
			"ord=" + this.ord;
		
		this.tileIndex++;

		var searchStr = ""; //* TODO ($("#searchBoxInput").length > 0) ? $("#searchBoxInput")[0].value.replaceAll("\"", "") : "";

		//* this allows Ad team test certain ads only in dev/stage environments
		var test = "";
		if (location.href.indexOf("test=on") != -1)
		{
			test = "on";
			searchStr = "test";
		}
	
		var obj = this.getValues();
		//* for testing purposes
		if (test == "on") dbg(obj);
	
		//* this is a hack: we had to introduce "postion" param for this function, but in order not to change the signature we also used it for videoId when showing videos
		var vid = (size == "1000x1000" && typeof position != "undefined") ? "vid=" + position + ";" : "";
		if ( typeof( window[ '_doubleClickS2' ] ) != "undefined" ) {
			
		} else {
			_doubleClickS2 = "";
		}
		url = url.replace(/{zone1}/gi, obj.zone1)
			.replace("{zone2}", obj.zone2)
			.replace(/{s1}/gi, obj.s1)
			.replace("{s2}", _doubleClickS2 == "" ? obj.s2 : _doubleClickS2)
			.replace("{url}", obj.url)
			.replace("{test}", test)
			.replace("{vid}", vid)
			.replace("{pos}", obj.pos)
			.replace("{size}", size)
			.replace("{searchStr}", searchStr);

		//* for testing purposes
		if (test == "on") dbg("DART: " + url)
	
		return url;
	}
	

	this.getValues = function()
	{
		//* g doesn't exist under video.msg
		var pageIsArticle = (typeof g != "undefined" && typeof g.pageType != "undefined" && g.pageType == "article");
		
		var pathname = location.pathname.toLowerCase();
		var pathArr = pathname.split("/");

		var obj = {
				zone1 : "",
				zone2 : "",
				s1 : "",
				s2 : "",
				url : pathname.substring(1, location.pathname.length)
			};

		//* home
		if (pathname == "/")
		{
			obj.zone1 = "home";
			obj.zone2 = "";
			obj.s1 = "home";
			obj.s2 = "";
			obj.url = "";
		}
		else
		{
			//* need to catch articles placed under root or under zone1 (/our-teams/{article})
			
			//* article in the root
			if (pageIsArticle && pathArr.length == 2)
			{
				obj.zone1 = "articles";
				obj.zone2 = "";
				obj.s1 = "articles";
				obj.s2 = "";
			}
			
			//* article under zone 1
			else if (pageIsArticle && pathArr.length == 3)
			{
				obj.zone1 = this.cleanString(pathArr[1]);
				obj.s1 = obj.zone1;
				obj.zone2 = "/articles";
				obj.s2 = "articles";
			}
			else
			{
			 
				obj.zone1 = this.cleanString(pathArr[1]);
				obj.s1 = obj.zone1;
				obj.s2 = (pathArr.length > 2) ? this.cleanString(pathArr[2]) : "landing";
				obj.zone2 = "/" + obj.s2; 
				
				if (location.hostname.indexOf("video.msg.com") != -1)
				{
					obj.zone1 = "videos";
					obj.s1 = "videos";
					obj.s2 = "";
					switch (pathArr[1])
					{
						case "home":
							obj.zone2 = "/videos";
							break;
	
						case "categories":
							obj.zone2 = "/categories";
							break;
	
						case "search":
							obj.zone1 = "/search";
							break;
					}
				}
				else if (obj.zone1 == "page_not_found")
				{
					obj.zone1 = "other"
					obj.zone2 = ""
					obj.s1 = "other"
					obj.s2 = "";
					obj.url = "page-not-found";
				}
				else if (obj.zone1 == "photos" && obj.s2 != "landing")
				{
					//* specific gallery, special rules
					obj.url = obj.s2;
		
					obj.zone2 = "";
					obj.s2 = "";
				}
			}
		}

		obj.url = this.cleanString(obj.url);
	
		obj.pos = "top";
		if (this.size == "1000x1000")
		{
			obj.pos = "video"
		}
		else if (typeof this.position != "undefined" && this.position != "")
		{
			obj.pos = this.position;
		}
		
		return obj;
	}

	//* DART prohibits most chars and requires to replace with underscores
	this.cleanString = function(str)
	{
		return str.replace(/[^a-zA-Z 0-9]+/g,'_');
	}
}
/*/DART handling */

function dbg(str)
{
	if (typeof(window.console) != "undefined")
	{
		console.log(str);
	}
}

if ($.browser.msie) {
	$(function() {
		var fblink = document.getElementsByName('fb_share')[0];
		if (fblink) {
			fblink.attachEvent("onclick", function(event) {
				event.cancelBubble = true;
				event.returnValue = false;
			});
		}
	});	
}
