dboard = new Object();

var hostUrl = "http://www.adultswim.com/";

var MsgURL = "http://fancollective.adultswim.com/Community/memberservices/regwall/Status";
var gotoMsgURL = "http://fc.adultswim.com/Community/memberservices/regwall/Message";
var gotoFriendReqURL = "http://fc.adultswim.com/Community/memberservices/regwall/InviteFriend?action=listInvites";

dboard.init=function(){ 

	this.textcolor = "000000";
	this.linkcolor = "0000ff";
	this.linkrollcolor = "0000ff";
	this.fbgcolor = "ffffff";
	this.COOKIE_DOMAIN="adultswim.com";
	this.COOKIE_STATIC=" expires=Sunday, 21-Mar-2010 23:59:59 GMT;";
	this.COOKIE_DELETE=" expires=Fri, 02-Jan-1970 00:00:00 GMT;";
	this.lHref = window.location.href;
	
	if(this.isLoggedIn()){
		this.fvUsername = this.getCookie("displayname");
		this.fvLoggedin = "true";
		this.fvMsgURL = escape(MsgURL); 
	}else{
		this.fvUsername = "null";
		this.fvLoggedin = "null";
		this.fvMsgURL = "null";
	}
	
}

/// ----- cookie code -----------
dboard.getPropVal=function(sString,sProp,cDelim,cEnd){
	var match = (sString == null) ? null : sString.match(sProp + cDelim + "[^" +cEnd + "]*");
	return (match == null) ? null :match[0].split(cDelim)[1];
}
dboard.getCookie=function(sCookie){
	return this.getPropVal(document.cookie,sCookie,"=",";");
}
dboard.setCookie=function(sCookieName, sVal, sCookieType){
	sCookieType = (sCookieType == null) ? "" : sCookieType;
	document.cookie = sCookieName+"="+sVal+ ";" + sCookieType+" path=/; domain=."+this.COOKIE_DOMAIN;
}
dboard.deleteCookie=function(sCookieName){
	this.setCookie(sCookieName, "", this.COOKIE_DELETE);
}

dboard.isLoggedIn=function(){
	return (this.getCookie("authid") != null); 
}
dboard.logOut=function(){ 
	this.deleteCookie("authid");
	this.deleteCookie("authpass");
	this.deleteCookie("displayname");
	this.deleteCookie("LiSESSIONID");

	if(this.lHref.indexOf("fancollective.adultswim")!= -1 || this.lHref.indexOf("login.adultswim")!= -1 || this.lHref.indexOf("tenint1")!= -1 || this.lHref.indexOf("tendev-l2")!= -1){
		window.location.href = hostUrl + 'index2.html';
	}else{
		window.location.reload();
	}

}

dboard.goMsgURL=function(){
	window.location.href = gotoMsgURL;
}

dboard.goFriendRequest=function(){
	window.location.href = gotoFriendReqURL;
}

dboard.getParams=function(){ 
	var s="";
	s+="fvUsername=" + this.fvUsername; 
	s+="&fvMsgURL=" + this.fvMsgURL; 
	s+="&fvTextcolor=" + this.textcolor;
	s+="&fvLinkcolor=" + this.linkcolor;
	s+="&fvLinkRollcolor=" + this.linkrollcolor;
	
	s+="&";	
	
	return s;
}

dboard.init(); 


/***BEGIN NAV***/

$(document).ready(function(){
	SiteNav.init();
});

var SiteNav = {
	init: function() {
		SiteNav.setupRollovers();
		SiteNav.setupPositioning();
		SiteNav.setupSearch();
		
		$(window).bind("resize", function(){
			SiteNav.setupPositioning();
		});
	},
	
	setupSearch: function() {
		$('#search-field').focus(function() {
			$("#search-anchor").css('background-position', '0 0')
			$(this).css('background-position', '0 0')
		});
		
		$('#search-anchor').click(function() {
			document.search.submit();
		})
	},
	
	setupRollovers: function() {
		$("#video-primary-nav, #shows-primary-nav").hover(
			function() {
				var id = $(this).attr('id').replace('-primary-nav', '');
				$('#' + id + '-dropdown-nav').removeClass('off');
				$(this).find('.arrow-nav').html('&#9662;');
			}, 
			function() {
				var id = $(this).attr('id').replace('-primary-nav', '');
				$('#' + id + '-dropdown-nav').addClass('off');
				$(this).find('.arrow-nav').html('&#9656;');
			}
		)
		
		$("#shows-dropdown-nav, #video-dropdown-nav").hover(
			function() {
				var id = $(this).attr('id').replace('-dropdown-nav', '');
				$(this).toggleClass('off');
				$("#" + id + "-primary-nav").find('.arrow-nav').html('&#9662;');
				
			}, 
			function() {
				var id = $(this).attr('id').replace('-dropdown-nav', '');
				$(this).toggleClass('off');
				$("#" + id + "-primary-nav").find('.arrow-nav').html('&#9656;');
			}
		)
	},
	
	setupPositioning: function() {
		try{
			$('#shows-dropdown-nav').css('left', $('#shows-primary-nav').position().left);
			$('#video-dropdown-nav').css('left', $('#video-primary-nav').position().left);
		} catch(e){};
	}
};



/***END NAV***/

var Utilities = {
	log: function(str){
		try {
			console.log(str);
		} 
		catch (e) {
		}
	},
	
	debug: function(str){
		try {
			console.debug(str);
		} 
		catch (e) {
		}
	},
	
	trace : function(str) {
		try {
			console.trace(str)
		} 
		catch (e) {
		}
	},
	
	error: function(str){
		try {
			console.error(str);
		} 
		catch (e) {
		}
	},
	
	info : function(str) {
		try {
			console.info(str)
		} 
		catch (e) {
		}
	},
	
	warn : function(str) {
		try {
			console.warn(str)
		} 
		catch (e) {
		}
	},
	
	commafy: function(str) { 
		var regex = /(\d+)(\d{3})/; 
		while (regex.test(str)) {
			str += "";
			str = str.replace(regex, '$1' + ',' + '$2');
		}
		return str;
	},
	
	uncommafy: function(str) { 
		var regex = /,/g; 
		while (regex.test(str)) {
			str += "";
			str = str.replace(regex, '');
		}
		return str;
	},
	
	periodfy: function(str) { 
		var regex = /(\d)(\d)/; 
		while (regex.test(str)) {
			str = str.replace(regex, '$1' + '.' + '$2');
		}
		return str;
	},
	
	unperiodfy: function(str) { 
		var regex = /\./g; 
		while (regex.test(str)) {
			str = str.replace(regex, '');
		}
		return str;
	},
	
	addZero: function(str) {
		if (str.length == 1) {
			str += '0';
		}
		return str;
	},
	
	removeZero: function(str) {
		var regex = /(\d)\.0/g; 
		while (regex.test(str)) {
			str = str.replace(regex, '$1');
		}
		return str;
	},
	
	lengthify: function(str) {
		if (str.length >= 4) {
			str = str.substring(0,3);
		}
		return str;
	}
};

//iPad detection 
/*
if (navigator.userAgent.match(/iPad/i)) {
	if (document.cookie.indexOf("mobileRedirect=false") == -1) {
		var x = Utilities.readCookie('mobileRedirect');
		if (x) {
		
		}
		else {
			var referrer;
			
			try {
				referrer = escape(location);
			} catch (oErr) {
				referrer = 'http://www.adultswim.com';
			}
			window.location = "http://www.adultswim.com/mobile/ipad.html?referrer=" + referrer;
		}
	}
}
*/

//iPhone detection
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
	var x = Utilities.readCookie('mobileRedirect');
	if (x) {
		
	}
	else {
		var referrer;
	
		try {
			referrer = escape(location);
		} catch(oErr) {
			referrer = 'http://www.adultswim.com';
		}
		window.location = "http://www.adultswim.com/mobile/iphone.html?referrer=" + referrer;
	}
}
