var is_aol = navigator.userAgent.toLowerCase().indexOf("aol") != -1;
var is_mac = navigator.userAgent.indexOf('Mac') != -1;
var is_IE = navigator.userAgent.indexOf('MSIE') != -1;
// Start Channel 
function channel_init(){
}

// Header2.0
function p_o(o){return document.getElementById(o);}

function ai_posX(obj){
 var left=0;
 if(obj.offsetParent){
  while(obj.offsetParent){
   left+=obj.offsetLeft;
   obj=obj.offsetParent;
  }
 }else if(obj.x)left+=obj.x;
 return left;
}
function ai_posY(obj){
 var top=0;
 if(obj.offsetParent){
  while(obj.offsetParent){
   top+=obj.offsetTop;
   obj=obj.offsetParent;
  }
 } else if(obj.x)top+=obj.y;
 return top;
}

function togMore(e,state,oDivName){
 if ((oDivName == undefined) || (oDivName.length <= 0)) {
    oDivName = 'smore';
 }
 var oDiv=p_o(oDivName);
 if(state=='show'){
  posMore(e,oDivName);
  oDiv.style.zIndex=99999999;
  oDiv.style.display='block';
 } else {
  oDiv.style.display='none';
 }
 return false;
}
function posMore(e,oDivName){
    if ((oDivName == undefined) || (oDivName.length <= 0)) {
        oDivName = 'smore';
    }
    var oDiv=p_o(oDivName);
	var oLnk=p_o(oDivName+'a');

	posY = findMousePos(e);
	oDiv.style.left=(ai_posX(oLnk)+15)+'px';
	oDiv.style.top=(posY+oLnk.offsetHeight/2)+'px';
}

// Begin : Headers 2 code
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


// Shared functions

function findMousePos(e) {
	var posY = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return posY;
}

/* ____________________________________ openAuth - no dependencies */ 

openAuth = {
    init : function() {
       
    	this.token = null;
        this.loggedIn = false;
        this.userId = null;
        this.referer = "";
        this.getToken();
        
    },
    createTokenCookie : function (name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    },
    readTokenCookie : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    eraseTokenCookie : function(name) {
        this.createTokenCookie(name,"",-1);
    },
    getToken : function() {    
        if (this.readTokenCookie('__s03t19m01_')) {
            this.token = this.readTokenCookie('__s03t19m01_');
            /*start streampad*/
			try {
  				streampadPlayer.OpenAuth.setToken("ao1Y6FaONj-lZJAj", openAuth.token);
  			}
			catch(err) {}
			/*end streampad*/
            if (this.token) {
                this.getInfo();
            }
        }
        else {
            requestUrl = "https://api.screenname.aol.com/auth/getToken?devId="+openAuth.devId+"&f=json&c=openAuth.gotToken";
            tokenJSONScript = new scriptJSON(requestUrl);
            tokenJSONScript.buildScriptTag();
            tokenJSONScript.addScriptTag();    
        }    
    },
    gotToken : function(objJSON) {
        if (objJSON.response.statusCode != 200) {
            this.loggedIn = false;
            tokenJSONScript.removeScriptTag();
            this.makeLoginLink();
        } else {
            this.loggedIn = true;
            this.token = objJSON.response.data.token.a;
            /*start streampad*/
			try {
  				streampadPlayer.OpenAuth.setToken("ao1Y6FaONj-lZJAj", openAuth.token);
  			}
			catch(err) {}
			/*end streampad*/
            tokenJSONScript.removeScriptTag();            
            if (this.token) {
                this.getInfo();
            }
        }
    },
    getInfo : function() {
        requestUrl = "https://api.screenname.aol.com/auth/getInfo?a="+openAuth.token+"&devId="+openAuth.devId+"&referer="+openAuth.referer+"&f=json&c=openAuth.gotInfo";
        infoJSONScript = new scriptJSON(requestUrl);
        infoJSONScript.buildScriptTag();
        infoJSONScript.addScriptTag();    
    },
    gotInfo : function(objJSONB){
        if (objJSONB.response.statusCode !== 200) {
            this.eraseTokenCookie('__s03t19m01_');
            
            this.makeLoginLink();
        } else {
            if (!this.readTokenCookie('__s03t19m01_')) {
                this.createTokenCookie('__s03t19m01_', this.token, 1);    
            }        
            this.userId = objJSONB.response.data.userData.loginId;           
			/*start streampad*/
			try {
				streampadPlayer.vars.sn = openAuth.userId;
			} catch (e) {}
			/*end streampad*/
            
            this.makeLogOutLink();
        }
    },
    makeLoginLink : function() {
        
	var self = this;
	jQuery("#GH_").trigger('setAuthState',{
        	doAuth : true,
        	authenticated: false,
        	unauthState: '<li class="GH_hat_LI"><a class="jSignIn" title="Sign In / Register">Sign In / Register</a></li>'
   	 	});
	
	jQuery('#GH_ .jSignIn').live('click', function(e) { 
       		 e.preventDefault(); 
        	self.openAuthWindow(e); 
   		 });    
    	},
    	
    openAuthWindow : function (){
            var loginUrl = "http://api.screenname.aol.com/auth/login?devId="+openAuth.devId+"&f=qs&succUrl="+openAuth.loginSuccURL;    
            var x = (screen.width - 515)/2; var y = (screen.height -490)/2;                                 
            window.open(loginUrl,'name','resizable=yes,width=515,height=490,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);
    },
    
    makeLogOutLink : function() {
        
        if (this.userId && (!(document.getElementById("logoutLink")))) {
            
             jQuery("#GH_").trigger('setAuthState',{
            doAuth : true,
            authenticated: true,
            authState: "<li class='GH_hat_LI'><b class='GH_hat_links_A'>Hi, " + this.userId + "</b></li><li class='GH_hat_LI'><a class='jSignOut GH_hat_more' title='Sign Out'>Sign Out</a></li>"
        });
        
        jQuery('#GH_ .jSignOut').live('click', function(e) { 
            e.preventDefault(); 
            
            var logoutUrl = "https://api.screenname.aol.com/auth/logout?a="+openAuth.token+"&devId="+openAuth.devId+"&doSNSLogout=1&f=qs&succUrl="+openAuth.logoutSuccURL;
            /*AIM.transactions.endSession(true);*/ /*sitesocial*/   
             
            var x = (screen.width - 528)/2; var y = (screen.height -530)/2;                                 
            window.open(logoutUrl,'name','resizable=yes,width=528,height=530,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);    
        });
    }

    },

    /*this function calls Blogsmith's sns javasvript to populate Blogsmith's comment's module fields*/
    sns : function () {
        if (document.getElementById("sns")) {
            sns.calls.getToken();
        }
    },
    /*The function de-populates Blogsmith's comment's module fields*/
    clearForm: function(){
        if (document.getElementById('cmtuinfo_sns')){
            var snsInput = getElementsByClassName(document, "input", "formtext");
            for(a=0; a<snsInput.length; a++) {
                snsInput[a].value = '';
            }
        }
    }

}
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

/* end openauth */



/*______________________________________________search_______________________________*/
function GlobalFooterSearch(options) {
    this.options = options;
    this.searchTextBox = options.searchTextBox;
    this.searchButton = options.searchButton;
    this.formName = options.formName;
    this.searchDefaultText = options.searchDefaultText;
    this.searchTextBox.value = this.searchDefaultText;
    this.keyPresssed = false;
    this.attachSearchTextEvents();
    this.attachSearchActions();
};   
GlobalFooterSearch.prototype = {   
    constructor : GlobalFooterSearch,
    bind : function(fnMethod){
        var objSelf = this;
        return(
            function(){
                return(fnMethod.apply(objSelf, arguments));
            }
        );
    },   
   
    crossBrowserEvnt : function (e) {
		if (!e) {var e = window.event;}
    	if (e.target) {targ = e.target;}
   	 	else if (e.srcElement) {targ = e.srcElement;}
    	if (targ.nodeType == 3) {targ = targ.parentNode;}        
		return targ;
	},
    attachSearchTextEvents : function(){
         this.searchTextBox.onclick = this.bind(function(e){
            if(this.keyPresssed != true){
				this.searchTextBox.value = "";
            }
         });
         this.searchTextBox.onblur = this.bind(function(e){
             if(this.searchTextBox.value == ""){
				this.searchTextBox.value = this.searchDefaultText;
				this.keyPresssed = false;
            }
        });
        this.searchTextBox.onkeypress = this.bind(function(e){
			this.keyPresssed = true;
         });
    },      
    attachSearchActions : function () {
        this.searchButton.onclick = this.bind(function(e){        
			if(this.keyPresssed == true){		
				this.searchQuery = this.searchTextBox.value;          
				this.formName.onsubmit();
	            }
        });         
        this.formName.onsubmit = this.bind(function(e){              
           
			if (!e) {			
				e = window.event;
			}
            if (e.preventDefault) {			    
				e.preventDefault();
            }
            else if (window.event && window.event.returnValue) {
				window.eventReturnValue = false;
            } else { e.returnValue = false;}            
           
		   if(this.keyPresssed == true && this.searchTextBox.value != ""){           
            this.searchQuery = this.searchTextBox.value;
			document.location.href = this.doSearch();}
		   
        });
    }, 
    doSearch : function () {
        var searchQuery = this.searchQuery.toLowerCase();
        var searchUrl = "/search/?q=" + encodeURIComponent(this.searchQuery);
        return searchUrl;
    }
};