

/**
 * Vyew.com site js functions
 *	upports Top login/logout buttons from the html-header template
 */
 
var origLoginHTML;
var userEmail;
$( checkLogin );

function checkLogin(){
	if(typeof vyew != "undefined" && vyew.checkLogin){
		vyew.checkLogin(function(res)
		{	
			if(typeof res.em != "undefined"){
				updateView(res.em);
			}else{
				updateView(); }
	
			try{	
				if( typeof(onLoginInfo)=="function" ){
					onLoginInfo(res);
				}
			}catch(e){}
		},this);	
	}
}
    


function updateView(em)
{
    if(!origLoginHTML) origLoginHTML = $("#user-login").html(); 
    if(em && em.length > 2){
		var isGuest=(em.indexOf("@")==-1) ? true : false;
		var lg="Hello, ";
		if(isGuest) lg+="Guest";
		else lg+=em;
		if(urlSegment(0)=="vyewmypc") lg+=" | <a href='/go/desktop'>Share My Desktop</a>"
		
		if(isGuest) lg+=" | <a href='/go/start'>My Guest VyewBook</a>"
		else lg+=" |  <a href='/go'>My VyewBooks</a>"
		
		lg+="  | <a href='#logout' onclick='doLogout();'>Logout</a>";

		$("#user-login").html(lg);
    }else{
		$("#user-login").html( origLoginHTML );
    }
	//sign in to ee, if not yet
	if(getCookie("sid") && getCookie('em')){
		var expuh=getCookie("exp_userhash");
		var expui=getCookie("exp_uniqueid");
		if(!(expuh.length > 5) || !(expui.length > 5)){
			$.post('/site/EESingleSignonAjax.php', {f:'sso'}, 
			function(ret)
			{
				ret=eval(ret);
				if(ret && ret.exp_uniqueid && ret.exp_userhash){
					if(getCookie('keepLoggedIn')=='1') expir="forever";
					else expir="session";
					setCookie('exp_uniqueid',ret.exp_uniqueid,expir);
					setCookie('exp_userhash',ret.exp_userhash,expir);
					//if in forum, then refresh once
					dfr=getCookie('didForumRefresh');
					dfr=(typeof dfr=="undefined" || dfr=="undefined")?0:dfr;
					if ( urlSegment(1)=="forum" && dfr<=5 ){
						dfr++;
						setCookie('didForumRefresh',dfr,'session');
						top.document.location.reload(true);
					}
				}
			});
		}
	}//if 
}
function dbg(x){
       if(typeof console != "undefined" && console.debug) console.debug(x);
}
function getUserEmail(){
	var lID=fs_getCookie('lID');
	if(lID){
	    var ref=fs_getCookie('ref');
	    var adRef=fs_getCookie('adRef');
	    if(!ref)ref="none";
	    if(!adRef)adRef="none";
		$.post('/site/siteajax.php',
			{f:'clID', clID:lID, ref:ref, adRef:adRef}, 
			gotResponseLID);
	}else{
	    updateView();
	}
}
function gotResponseLID(res)
{
    if(res){
		//res=eval(res);
        results=res.split(',');
        userEmail=results[0];
        mid=results[1];
        sid=results[2];
		//userEmail=res.em; mid=res.mid; sid=res.sid;
        var cookieexp=(getCookie('keepLoggedIn')=="1")?'never':'session'
        if(!cookieexp)cookieexp="session"
        //fs_setCookie('sid',sid, cookieexp);
        setCookie('em',userEmail, cookieexp);
		updateView(userEmail);
	}
}

function doLogout(){
    clearCookie('em'); clearCookie('lID'); clearCookie('sid'); clearCookie("keepLoggedIn");
    clearCookie('ud');
    clearCookie('exp_uniqueid'); clearCookie('exp_userhash');
    if(updateView) updateView();
    $("#user-login").append("<span id='tempnotice'><br/><b>You have been logged out.</b></span>");
    setTimeout("$('#tempnotice').hide('slow');",3000)
    if( typeof(onLogout)=="function" ){
        onLogout();
    }
}

if(typeof tr=="undefined")
{
	tr=function(x)
	{
		if(console!="undefined"){
			if(console.debug!="undefined"){
				console.debug(x);
			}
		}
	}
}



/**
 * Given a url: http://vyew.com/content/product/uses?a=1&b=2#hello
 * Examples:
 * 		urlSegment(0) = "content"
 *   	urlSegment(1) = "product"
 *   	urlSegment("#") = "hello"
 * 	 	urlSegment("?") = "a=1&b=2#hello"
 *   	urlSegment("?a") = "1"
 *   	urlSegment("?b") = "2"
 *		urlSegment("domain") = "vyew.com"
 *		urlSegment("base") = "http://vyew.com/content/product/uses"
 *		urlSegment("baserel") = /content/product/uses"
 *
 * TO SET THE URL HASH:
 *		top.location.hash = "abc"
 */
function urlSegment(num, win)
{
	if(!win) win=this;
    var loc=win.location.href.match(/\/\/(.*)/)[1];
    if (!loc) return "";
    if(!isNaN(num)){
		segs=loc.split("/");
        return segs[num+1];
    }else if(num=="#"){
        pl=loc.indexOf("#");
        if(pl==-1)return "";
        else {
            loc=loc.substr(pl+1);
            pl=loc.indexOf("?");
            if(pl==-1) return loc;
            else return loc.substr(0,loc.indexOf("?"));
        }
    }else if(num=="?"){
        pl=loc.indexOf("?");
        if(pl==-1)return "";
        else return loc.substr(pl+1);
    //check if "?variable"
    }else if(num.substr(0,1)=="?"){
        var getv=num.substr(1);
        if(loc.indexOf(getv)==-1) return '';
        pl=loc.indexOf("?");
        if(pl==-1) return "";
        var q=''+loc.substr(pl+1);
        if(q.indexOf(getv)==-1 || typeof q != "string") return '';
        var regx=new RegExp(getv+"=([^&#]*)");
        var res=q.match(regx)[1];
        return res;
    }else if(num=="domain"){
        return loc.match(/(.*?)\//)[1]
    }else if(num=="base"){
        var loc=win.location.href;
        pl=loc.indexOf("?");
        if(pl>=0) loc=loc.substr(0,pl);
        pl=loc.indexOf("#");
        if(pl>=0) loc=loc.substr(0,pl);
        return loc;
    }else if(num=="baserel"){
        var loc=win.location.href.match(/^.*?\/\/.*?(\/.*)/)[1];
        pl=loc.indexOf("?");
        if(pl>=0) loc=loc.substr(0,pl);
        pl=loc.indexOf("#");
        if(pl>=0) loc=loc.substr(0,pl);
        return loc;
    }

    return "";
}


/**
 * Returns specified segment after the hash (#) in url
 * A segment is text between "/" slashes
 * Examples with url : vyew.com/room#/abc/def/ghi
 *      hashUrlSegment(0) : "abc"  (leading slashes are removed)
 *      hashUrlSegment(1) : "def"
 *      hashUrlSegment() :
 * @param segNum - segment number
 * @return mixed - the segment specified or false
 */
function hashUrlSegment(segNum,win)
{
    if(!segNum) segNum=0;
    if(!win)win=this;
    var loc=this.location.href;
    pl=loc.indexOf("#");
    if(pl==-1) return false;
    if(!pl) return false;
    loc=loc.substr(pl+1)
    segs=loc.split("/");
    if(!segs[0]) segs.shift()
    return segs[segNum];
}




/*
//For Search Higllighting
var shOptions={exact:"partial", style_name_suffix:false};
$(function(){
	$(document).SearchHighlight(shOptions);
});
*/
