if(typeof UPICKDAILY == "undefined" || !UPICKDAILY) var UPICKDAILY = {};

UPICKDAILY.namespace = function() {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i++) {
        d = a[i].split(".");
        o = UPICKDAILY;

        // UPICKDAILY is implied, so it is ignored if it is included
        for (j = (d[0] == "UPICKDAILY") ? 1 : 0; j < d.length; j++) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

UPICKDAILY.namespace("utils");
UPICKDAILY.namespace("utils.loader");
UPICKDAILY.namespace("utils.message");
UPICKDAILY.namespace("utils.url");

UPICKDAILY.utils.doLog = function() {
	if (!DO_DEBUG) return; // flag should already verify window && window.console. Fixes an odd bug

	if (typeof window.console.debug != "undefined") {
		// firebug error console
		window.console.debug.apply(window.console, arguments);
	} else if (typeof window.console.log != "undefined") {
		// safari error console
		window.console.log.apply(window.console, arguments);
	}
}

/*
	Utility to submit xml into the GDC without incurring an html redirect to a "successURL"
	Shouldn't need to wrap this in xml. Inquire about a fix.
*/
UPICKDAILY.utils.getGdcXml = function(id, obj) {
	var gdc = "<answers collectionID=\""+id+"\">";
	for(var key in obj) {
		gdc += "<answer tag=\""+ key +"\"><![CDATA["+ obj[key] +"]]></answer>";
	}
	gdc += "</answers>";
	return gdc;
}

UPICKDAILY.utils.getDomain = function() {
	var domain = window.location.hostname;
	return domain.indexOf('nick.com') > 0 || domain.indexOf('mtvi.com') > 0 ? "" : "http://www.nick-d.mtvi.com";	
}

UPICKDAILY.utils.includeJs = function(filename) {
	var headTag = document.getElementsByTagName('head')[0];
	var scriptTag = document.createElement('script');
	scriptTag.src = filename;
	scriptTag.type = 'text/javascript';
	headTag.appendChild(scriptTag)
}

UPICKDAILY.utils.isEmptyString = function(obj) {
	return obj == null || trim(obj) == "" || obj == "undefined";
}

UPICKDAILY.utils.toggleButton = function(id, toggle) {
	$('#'+id).attr("disabled", !toggle);
}

UPICKDAILY.utils.trim = function(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

UPICKDAILY.utils.validateEmail = function(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	} else {
		return true;
	}
}

UPICKDAILY.utils.loader.init = function() {
	if (typeof UPICKDAILY.utils.loader.wait == 'undefined') {
		UPICKDAILY.utils.loader.wait = new YAHOO.widget.Panel("wait", {
			width: "240px",
			fixedcenter: true,
			close: false,
			draggable: false,
			zindex: 125,
			modal: true,
			visible: false
		});
		
		UPICKDAILY.utils.loader.wait.setHeader("Loading, please wait...");
		UPICKDAILY.utils.loader.wait.setBody("<img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\"/>");
		UPICKDAILY.utils.loader.wait.render(document.body);
	}
}

UPICKDAILY.utils.loader.show = function() {
	UPICKDAILY.utils.loader.init();
	UPICKDAILY.utils.loader.wait.show();
}

UPICKDAILY.utils.loader.hide = function() {
	UPICKDAILY.utils.loader.init();
	UPICKDAILY.utils.loader.wait.hide();
}

UPICKDAILY.utils.message.init = function() {
	if (typeof UPICKDAILY.utils.message.window == 'undefined') {
		UPICKDAILY.utils.message.window = new YAHOO.widget.Panel("message", {
			width: "240px",
			fixedcenter: true,
			close: true,
			draggable: false,
			zindex: 125,
			modal: true,
			visible: false
		});
		
		UPICKDAILY.utils.message.window.setHeader("");
		UPICKDAILY.utils.message.window.setBody("");
		UPICKDAILY.utils.message.window.render(document.body);
	}
}

UPICKDAILY.utils.message.show = function(title, message) {
	UPICKDAILY.utils.message.init();
	UPICKDAILY.utils.message.window.setHeader(title);
	UPICKDAILY.utils.message.window.setBody(message);
	UPICKDAILY.utils.message.window.show();
}

UPICKDAILY.utils.message.hide = function() {
	UPICKDAILY.utils.message.init();
	UPICKDAILY.utils.message.window.setHeader();
	UPICKDAILY.utils.message.window.setBody();
	UPICKDAILY.utils.message.window.hide();
}

// tweaked from: /common/javascript/window/launchwin.js
UPICKDAILY.utils.openBumper = function(wBumper, advertiser, onair, customBumper, customHeight, newWindow, isInHouse) {
	var bumperUrl = "/common/bumpers/bumperFrameset.jhtml?";
	var fullPageBumperUrl = "/upickdaily/common/bumpers/fullpage/index.jhtml?";
	var bumperParam = "wBumper";
	var targetUrlParameter = "advertiser";
	onair = onair != null && onair;
	isInHouse = isInHouse == undefined ? true : isInHouse;

	switch(wBumper) {
		case "external" : {
			bumperUrl = "/common/bumpers/go.jhtml?";
			bumperUrl += customBumper == null ? "" : ("bumper=" + escape(customBumper) + "&");
			bumperUrl += customHeight == null ? "" : ("bumperHeight=" + customHeight + "&");
			bumperUrl += isInHouse == undefined ? "" : ("isInHouse=" + isInHouse + "&");
			targetUrlParameter = "destination";
			break;
		} case "fullPage" : {
			bumperUrl = fullPageBumperUrl;
			targetUrlParameter = "destination";
			break;
		} case "paysite" : {
			break;
		} case "grown-ups" : {
			break;
		} case "launchpad" : {
			break;
		} case "sponsor" : {
			break;
		} default : {
			wBumper = "sponsor";
			break;
		}
	}

	var bumperHref = bumperUrl +
					 bumperParam +"="+ wBumper +"&"+
					 targetUrlParameter +"="+ escape(advertiser);

	if(onair) bumperHref += "&onair_landing=true";
	if(onair && wBumper == "external") {
		bumperHref = fullPageBumperUrl + targetUrlParameter +"="+ escape(bumperHref);
	}

	if(newWindow) {
		window.open(bumperHref, 'bumper', 'resizable=1,menubar=1,height=600,width=800');
	} else {
		window.location.href = bumperHref;
	}
}

UPICKDAILY.utils.openNewBumper = function(wBumper, advertiser, onair, customBumper, customHeight) {
	doLog('openNewBumper: '+advertiser);
	UPICKDAILY.utils.openBumper(wBumper, advertiser, onair, customBumper, customHeight, false);
}

UPICKDAILY.utils.url.getUrlPath = function(url) {
	var urlInfo = url.replace('//', '/').split('/');
	var domain = urlInfo.splice(0, 2).join("/");
	var docInfo = urlInfo.join("/");
	//alert("getUrlPath: "+domain+" | "+docInfo);
	return docInfo;
}

UPICKDAILY.utils.url.getContextPath = function(url, context, removeExtension, removeTrailingIndex) {
	var path = UPICKDAILY.utils.url.getUrlPath(url);
	
	// remove the file extension if a page is specified: file.jhtml
	if(removeExtension && path.lastIndexOf(".") >= 0) {
		path = path.substring(0, path.lastIndexOf("."));
	}
	
	// remove assigned context from url. context = "games" > url = /games/spongebob/game.jhtml > spongebob/game
	context = context == null || context == "" ? "" : context;
	var contextMatch = new RegExp("^"+context+"\/", "i")
	path = path.replace(contextMatch, "");;

	// remove trailing "/index" - to combine - "/some/url/index" & "/some/url/"
	if(removeTrailingIndex) path = path.replace(/\/index$/i, "");

	// remove trailing forward slash
	path = path.replace(/\/$/, "");
	
	if(path.indexOf("/") >= 0){
		path = path.replace(/\/$/, "");
	}

	return path;
}

UPICKDAILY.utils.emailValidate = function(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(address) == false) {
        return false;
	}else{
		return true;
	}

}

/*Function for ad free switch of header  */
UPICKDAILY.utils.doBumper = function(whatLink, bumpered) {
	if(bumpered == "true") {
		openBumper("fullPage", whatLink);
	} else {
		document.location.href = whatLink;	}
}

UPICKDAILY.utils.makehomepage = function() {
	var title = "Nick.com";
	var url = "http://www.nick.com/";
	if(window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) {
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	} else if(document.all) {
		// add fav in IE
		if(document.body.style && document.body.style.behavior) {
			document.body.style.behavior = 'url(#default#homepage)';
			document.body.setHomePage(url);
		} else if(window.external) {
			window.external.AddFavorite(url , title);
		}
	}
}

/* temporarily expose functions outside of namespace */
doLog = UPICKDAILY.utils.doLog;
getGdcXml = UPICKDAILY.utils.getGdcXml;
getDomain = UPICKDAILY.utils.getDomain;
includeJs = UPICKDAILY.utils.includeJs;
isEmptyString = UPICKDAILY.utils.isEmptyString;
toggleButton = UPICKDAILY.utils.toggleButton;
trim = UPICKDAILY.utils.trim;
validateEmail = UPICKDAILY.utils.validateEmail;
doBumper = UPICKDAILY.utils.doBumper;
openBumper = UPICKDAILY.utils.openBumper;
openNewBumper = UPICKDAILY.utils.openNewBumper;
makehomepage = UPICKDAILY.utils.makehomepage;