var current = {};
current.Types =
{
	CONTENT_SHORT : 1,
	INTEREST_SHORT : 3,
	USER_SHORT : 2,
	LOCALE_US : 'en_US',
	LOCALE_UK : 'en_UK',
	LOCALE_IT : 'it_IT',
	SEARCH_QUERY_PARAM : 'q'
};

current.Constants = Class.create({
	initialize : function () {},
	setServerName : function (serverName)
	{
		this._serverName = serverName;
		this.setSecureServerName(serverName.replace(/http:/, 'https:'));
	},
	getServerName : function ()
	{
		return this._serverName;
	},
	setSecureServerName : function (serverName)
	{
		this._secureServerName = serverName;
	},
	getSecureServerName : function ()
	{
		return this._secureServerName;
	},
	setDatanodeUrl : function (datanodeUrl)
	{
		this._datanodeUrl = datanodeUrl	
	},
	getDatanodeUrl : function ()
	{
		return this._datanodeUrl;
	},
	setStaticContentUrl : function (staticContentUrl)
	{
		this._staticContentUrl  = staticContentUrl;
	},
	getStaticContentUrl : function ()
	{
		return this._staticContentUrl;
	},
	setBuildNumber : function(buildNumber)
	{
		this._buildNumber = buildNumber;
	},
	getBuildNumber : function()
	{
		return this._buildNumber;
	},
	setTrackingBucket : function (trackingBucket)
	{
		this._trackingBucket = trackingBucket;
	},
	getTrackingBucket : function()
	{
		return this._trackingBucket;
	},
	setTrackingAppPath : function (trackingAppPath)
	{
		this._trackingAppPath = trackingAppPath;
	},
	getTrackingAppPath : function()
	{
		return this._trackingAppPath;
	},
	setAdServerUrl : function (adServerUrl)
	{
		this._adServerUrl = adServerUrl;
	},
	getAdServerUrl : function()
	{
		return this._adServerUrl;
	},
	setAdSite : function (adSite)
	{
		this._adSite = adSite;
	},
	getAdSite : function()
	{
		return this._adSite;
	},
	setAdOrdinal : function (adOrdinal)
	{
		this._adOrdinal = adOrdinal;
	},
	getAdOrdinal : function()
	{
		return this._adOrdinal;
	},
	setFlixDomain : function (upload_domain)
	{
		this._upload_domain = upload_domain;
	},
	getFlixDomain : function()
	{
		return this._upload_domain;
	},
	setFlixPath : function (upload_path)
	{
		this._upload_path = upload_path;
	},
	getFlixPath : function()
	{
		return this._upload_path;
	},
	setFlixu : function (upload_username)
	{
		this._upload_username = upload_username;
	},
	getFlixu : function()
	{
		return this._upload_username;
	},
	setFlixx : function (upload_password)
	{
		this._upload_password = upload_password;
	},
	getFlixx : function()
	{
		return this._upload_password;
	},
	setClassID : function (classid)
	{
		this._classid = classid;
	},
	getClassID : function()
	{
		return this._classid;
	},
	setCabVersion : function (cab_version)
	{
		this._cab_version = cab_version;
	},
	getCabVersion : function()
	{
		return this._cab_version;
	},
	setXpiVersion : function (xpi_version)
	{
		this._xpi_version = xpi_version;
	},
	getXpiVersion : function()
	{
		return this._xpi_version;
	},
	setDmgVersion : function (dmg_version)
	{
		this._dmg_version = dmg_version;
	},
	getDmgVersion : function()
	{
		return this._dmg_version;
	},
	setFPControl : function (fp_control)
	{
		this._fp_control = fp_control;
	},
	getFPControl : function()
	{
		return this._fp_control;
	},	
	getLandingMsgReferrers : function()
	{
		this._landingMsgReferrers = ['aol.com','ask.com','buzz.yahoo.com','del.icio.us','digg.com','google.com','live.com','mixx.com','msn.com','reddit.com','sharethis.com','yahoo.com'];
		return this._landingMsgReferrers;
	},
	getProxyUrl : function ()
	{
		return this._proxyUrl;
	},
	setProxyUrl : function (proxyUrl)
	{
		proxyUrl = proxyUrl.replace(/\/broxy\.htm/, '');
		this._proxyUrl = proxyUrl;
	},
	getScriptName : function ()
	{
		return this._scriptName;
	},
	setScriptName : function (scriptName)
	{
		this._scriptName = scriptName;
	},
	getSwfDatanodeUrl : function ()
	{
		return this._swfDatanode;
	},
	setSwfDatanodeUrl : function (datanode)
	{
		this._swfDatanode = datanode;
	},
	getFacebookConfig : function ()
	{
		return this._fbConfig;
	},
	setFacebookConfig : function (config)
	{
		this._fbConfig = config;
	},
	getLocale : function ()
	{
		return this._locale;
	},
	setLocale : function (locale)
	{
		this._locale = locale;
	}
});
current.Constants.getInstance = function() {
	if (!document.__currentConstants__)
	{
		document.__currentConstants__ = new current.Constants();
	}
	return document.__currentConstants__;
};

current.stub = function(path){var c = window; path.split('.').each(function(s){if (typeof c[s] == 'undefined') {c[s] = {};} c = c[s];});};
current.uncache = function () {setSessionCookie(current.Cookies.CACHE_NAME, current.Cookies.CACHE_VALUE);}
current.loaded = [];
current.load = function(lib)
{
	if (current.loaded[lib] != null)
	{
		return;
	}
	current.loaded[lib] = true;
	var f = current.Constants.getInstance().getStaticContentUrl() + '/js/' + lib.split('.').join('/')  + '.js';
  	document.write('<script type="text/javascript" src="' + f + '"><\/script>');
}
document.onTabClick = function(node)
{
	var links = $$('li.tab a');
	if (links.length < 1 || $(node.rel) === null)
	{
		return;
	}
	links.each(
		function(link)
		{
			if (link.rel === null || $(link.rel) === null || (link.rel == node.rel))
			{
				return;
			}
			link.removeClassName('active');
			$(link.rel).hide();
		}
	);
	node.addClassName('active');
	$(node.rel).show();
	return false;
};
Position.GetWindowSize = function(w) {
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height];
};

onValidateComplete =  function (result, form)
{
	$$('.disableOnSubmit').each(function(node)
	{
		node.disabled = result;	
	});
};
function isUndefined(v)
{
	return (typeof v == 'undefined');	
}
function scaleToWidth(node, width)
{
	var size = Element.getDimensions(node);
	var nativeWidth = size.width;
	if (size.width > width)
	{
		node.width = width;
	}
	node.show();
}
function popWin(theURL, theWidth, theHeight, theBars)
{
	var width = theWidth;
	var height = theHeight;
	var bars = theBars;
	var winl = (screen.width - width) / 3;
	var wint = (screen.height - height) / 2;
	var theFeatures = "width=" + width + ",height=" + height +",top="+wint+",left="+winl+",scrollbars="+bars;
	var theWin = window.open(theURL,'popWin',theFeatures);
	theWin.focus();
}
function getURLParameter(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexString = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexString);
	var results = regex.exec(window.location.href);
	if(results === null)
	{
		return "";
	}
	else
	{
		return results[1];
	}
}
function highlightTerms(txt, q) {
	q = unescape(q);
	q = q.replace(/\+/, " ");
	var regexString = new RegExp(q, "gim");
	return txt.replace(regexString, function ($0) {return ("<em>"+$0+"</em>");});
}
var Events = Class.create({});
var now = new Date();

//Time functions
function calculateTime(secs, val1, val2, leadingString)
{
	var t = ((Math.floor(secs/val1))%val2).toString();
	if (t.length < 2) t = leadingString + t;
	return t;
}
function getDistanceOfTimeToNow(dateString)
{
	var endDate = new Date(dateString);
	var deltaDate = new Date(endDate - now);
	return Math.floor(deltaDate.valueOf()/1000);
}
function getDSTOffset(testDateString)
{
	// This method determines if it's currently DST in the US, and returns 0 for true and -1 for false
	// It assumes that the zone in question observes DST, not a very good one... 
	// But since we're only concerned with NY & LA, it should be ok. For now.
	var testDate	= new Date(testDateString);
	var startDST	= new Date(testDate.getFullYear(), 2, 1, 2); // 2nd Sunday in March, 2:00 AM
	var endDST		= new Date(testDate.getFullYear(), 10, 1, 2); // 1st Sunday in November, 2:00 AM
	endDST.setDate(1 + 7 - endDST.getDay());
	startDST.setDate(8 + 7 - startDST.getDay());
	if (startDST < testDate && testDate < endDST)
	{
		return 0;
	}
	return -1;
}
function getDateAndTimeFromUTC(utcDate, timezoneOffset, timezoneString, showMinutes, showSeconds, pattern)
{
	if (pattern == null || pattern == '') pattern = 'mmmm d, yyyy h:MM TT ';
	pattern				= (showMinutes) ? pattern : pattern.replace(':MM', ':00');
	pattern				= (showSeconds) ? pattern : pattern.replace(':ss', '');
	var dstOffset		= getDSTOffset(utcDate);
	timezoneString		= (dstOffset < 0) ? timezoneString : timezoneString.replace('S','D');
	timezoneOffset		+= dstOffset;
	var regionalDate	= new Date(utcDate);
	
	regionalDate.setHours(regionalDate.getHours() + timezoneOffset);
    return (dateFormat(regionalDate, pattern) + timezoneString); //dateFormat.masks.isoDateTime);
}


function getEasternTimezoneString()
{
	var edt_2008 = new Date("March 9, 2008 02:00:00 EST");
	var est_2008 = new Date("November 2, 2008 02:00:00 EDT");
	var edt_2009 = new Date("March 8, 2009 02:00:00 EST");
	var est_2009 = new Date("November 1, 2009 02:00:00 EDT");
	var edt_2010 = new Date("March 14, 2010 02:00:00 EST");
	var est_2010 = new Date("November 7, 2010 02:00:00 EDT");
	
	var localTime = new Date();
	var TimezoneOffset = -4;
	var ms = localTime.getTime() 
             + (localTime.getTimezoneOffset() * 60000)
             + TimezoneOffset * 3600000;
	var rightNowInNY = new Date(ms);
	switch (parseInt(rightNowInNY.getFullYear()))
	{
		case 2007:
			if (rightNowInNY > edt_2007 && rightNowInNY < est_2007) return ' EDT';
			else return ' EST';
		case 2008:
			if (rightNowInNY > edt_2008 && rightNowInNY < est_2008) return ' EDT';
			else return ' EST';
		case 2009:
			if (rightNowInNY > edt_2009 && rightNowInNY < est_2009) return ' EDT';
			else return ' EST';
		case 2010:
			if (rightNowInNY > edt_2010 && rightNowInNY < est_2010) return ' EDT';
			else return ' EST';
		default: 
			return ' EDT';
	}
}
//DWR Error Handling;
function dwrErrorBlackHole()
{
	$A(arguments).each(function (arg)
	{
	});
}

//TODO - DWR //
//if (window['dwr'] || document['dwr'])
//{
//	if (window['DWREngine'] != null)
//	{
//		//var a = current.Authorize.getInstance();
//		//DWREngine.setPreHook(a.auth.bind(a));
//	}
//	dwr.engine.setErrorHandler(dwrErrorBlackHole);
//}

if (!window['console'])
{
	var console = {}
	console.log = function() {};
}
var trace = console.log;
function getSwfByName(swfName)
{
	if (Prototype.Browser.IE) 
	{
		return window[swfName];
	}
	else
	{
		return document[swfName];	
	}
}
var HintUtils = {
	resetHint  : function (el)
	{
		if (el.value === '' && el.title !== null)
		{
			el.setStyle({color: '#999'});
			el.value = el.title;
		} else if (el.value === '')
		{
			el.value = '';
		}
		return el;
	}
};
Element.addMethods(HintUtils);
Object.extend(String.prototype,
{
	introspect : function ()
	{
		return this.replace(/_/,'-').toLowerCase().camelize();
	},
	truncateDots : function (len, ignoreSpaces)
	{
		if (this.length <= len)
		{
			return this;
		}
		var endIndex = len;
		if (!ignoreSpaces)
		{
			endIndex = this.lastIndexOf(' ', len);
			if (endIndex == -1)
			{
				endIndex = len;
			}
		}
		this.slice(0, endIndex - 3) + '...';
		return this;
	},
	dumbify : function()
	{
 		s = this.replace( /\u2018/g, "'" ).replace( /\u2019/g, "'" ).replace( /\u201c/g, '"' ).replace( /\u201d/g, '"' ).replace( /\u2013/g, '-' ).replace( /\u2014/g, '--' );		
		return s;
	}
});

function truncateText(text, len, truncate_string, truncate_lastspace)
{
	text = typeof(text) != 'undefined' ? text : '';
	len = typeof(len) != 'undefined' ? len : 30;
	truncate_string = typeof(truncate_string) != 'undefined' ? truncate_string : '...';
	truncate_lastspace = typeof(truncate_lastspace) != 'undefined' ? truncate_lastspace : false;
	
	if (text == '')
	{
		return '';
	}

	if (text.length > len)
	{
		text = text.substring(0, len);
		if (truncate_lastspace)
		{
			var newLen = len;
			for (var i = newLen-1; text.charAt(i) != ' '; i--) {
				newLen--;
			}
			newLen--;
			if (newLen == 0) newLen = len;
			text = text.substr(0, newLen);
			//returnText = returnText.replace('/\s+?(\S+)?$/', '');
		}
		return text+truncate_string;
	}
	else
	{
		return text;
	}
}

function getFlashMovieObject(movieName)
{
	if (window.document[movieName]) 
 	{
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName]; 
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}

function adjustRows (clipTextArea, cutoff)
{
	var clipRows = clipTextArea.rows;
	if (Prototype.Browser.IE)
	{
		var ieAdjust = false;
		while ((clipTextArea.scrollHeight > clipTextArea.clientHeight) && (clipTextArea.rows < 30))
		{
			clipTextArea.rows += 3;
			ieAdjust = true;
		}
		if (ieAdjust)
		{
			clipTextArea.scrollTop = 0;
			redraw(clipTextArea);
			setTimeout(function() { clipTextArea.focus(); },1);
		}
	} else {
		var splitRows = clipTextArea.value.split("\n");
		var splitCount = 1;
		for (var i=0; i<splitRows.length; i++)
		{
			if (splitRows[i].length > 80)
			{
				splitCount += Math.ceil(splitRows[i].length/80);
			}
		}
		splitCount += splitRows.length;
		if (splitCount > cutoff)
		{
			clipTextArea.rows = Math.min(splitCount,30);
		}
	}
}

function redraw(_a)
{
	if (_a && _a.style)
	{
		_a.style.display = "none";
		_a.style.display = "";
	}
}
