//slider
var timerlen = 5;
var slideAniLen = 250;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();

function slidedown(objname)
	{
    if(moving[objname]) return;
            
	// cannot slide down something that is already visible
    if(document.getElementById(objname).style.display != "none") return;
             
    moving[objname] = true;
    dir[objname] = "down";
    startslide(objname);
	}

function slideup(objname)
	{
    if(moving[objname]) return;
            
	// cannot slide up something that is already hidden
    if(document.getElementById(objname).style.display == "none") return;

    moving[objname] = true;
    dir[objname] = "up";
    startslide(objname);
	}

function startslide(objname)
	{
    obj[objname] = document.getElementById(objname);

    endHeight[objname] = parseInt(obj[objname].style.height);
    startTime[objname] = (new Date()).getTime();

    if(dir[objname] == "down") obj[objname].style.height = "1px";

    obj[objname].style.display = "block";

    timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
	}

function slidetick(objname)
	{
    var elapsed = (new Date()).getTime() - startTime[objname];
    if (elapsed > slideAniLen)
    	{
    	endSlide(objname);
    	} 
    else{
        var d =Math.round(elapsed / slideAniLen * endHeight[objname]);
        if(dir[objname] == "up") d = endHeight[objname] - d;
        obj[objname].style.height = d + "px";
    	}
    return;
	}

function endSlide(objname)
	{
    clearInterval(timerID[objname]);

    if(dir[objname] == "up") obj[objname].style.display = "none";

    obj[objname].style.height = endHeight[objname] + "px";
    
    delete(moving[objname]);
    delete(timerID[objname]);
    delete(startTime[objname]);
    delete(endHeight[objname]);
    delete(obj[objname]);
    delete(dir[objname]);

    return;
	}


function toggleSlide(objname)
	{
	if(document.getElementById(objname).style.display == "none")
		{//show div
		slidedown(objname);
		Cookie.set('cBar','true');
		document.getElementById("toggleOption").src="/images/cbar/hide.gif";
		document.getElementById("community_bar_footer").style.display="block";
		document.getElementById("community_bar_out").style.height="42px";
		}
	else{//hide div
		slideup(objname);
		Cookie.set('cBar','false');
		document.getElementById("toggleOption").src="/images/cbar/show.gif";
		document.getElementById("community_bar_footer").style.display="none";
		document.getElementById("community_bar_out").style.height="10px";
		}
	}
	
//multiple onload events
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') window.onload = func;
  else{
      window.onload = function()
      	{
      	if (oldonload) oldonload();
	    func();
    	}
  	  }
}

function initCbar()
{
updateCBarStatus();
updateCurrentPage();
document.getElementById("community_bar_out").style.width=document.getElementById("community_wrapper").clientWidth;
}


var __fbl = {

detectData : function()
	{
	src = 'http://adultfans.com/fb/ct.php#' + encodeURIComponent(document.location.href);
	var obj=document.getElementById('fb-login-box');
	if(obj) obj.src = src;
	this.XD.receiveMessage(function(message){if (message.data.indexOf('&rdata=true')!=-1) __fbl.jumpRegister(message.data);},'http://adultfans.com');
	},
	
jumpRegister : function(tmpHash)
	{
	tmpObj = this.parseString(tmpHash);
	registerPath = "/bpage.php?proc=register";
	window.location=('http://'+window.location.host+registerPath+'&'+this.buildQuery(tmpObj));
	},

parseString : function(query_string)
	{
	QObj=new Object();
	var vars = query_string.split('&');
	for (var i=0;i<vars.length;i++)
		{
		var pair = vars[i].split('=');
		if(pair[0]!='') QObj[pair[0]]=pair[1];
		}
	return QObj; 
	},

buildQuery : function(newData)
	{
	var query = window.location.search.substring(1);
	
	QObj={};		
	var vars = query.split('&');
	for (var i=0;i<vars.length;i++)
		{
		var pair = vars[i].split('=');
		if(pair[0]!='') QObj[pair[0]]=pair[1];
		}
	for(vars in newData)QObj[vars]=newData[vars];
		
	var tmpStr='';var flag=false;
	for(vars in QObj) {if(flag)tmpStr+='&';tmpStr+=vars+'='+QObj[vars];flag=true;}
	return tmpStr;
	},

//Post message object	
XD : function(){
  
    var interval_id,
    last_hash,
    cache_bust = 1,
    attached_callback,
    window = this;
    
    return {
        postMessage : function(message, target_url, target)
        	{
            if (!target_url) return; 
            target = target || parent;  // default to parent
    
            if (window['postMessage']){target['postMessage'](message,target_url.replace( /([^:]+:\/\/[^\/]+).*/, '$1'));}
            else if (target_url) {target.location = target_url.replace(/#.*$/, '') + '#' + (+new Date) + (cache_bust++) + '&' + message;}
        	},
  
        receiveMessage : function(callback, source_origin)
        	{
            if (window['postMessage']) {
                // bind the callback to the actual event associated with window.postMessage
                if (callback) {
                    attached_callback = function(e) {
                        if ((typeof source_origin === 'string' && e.origin !== source_origin)
                        || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                            return !1;
                        }
                        callback(e);
                    };
                }
                if (window['addEventListener']) {
                    window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                } else {
                    window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                }
            } else {
                interval_id && clearInterval(interval_id);
                interval_id = null;

                if (callback) {
                    interval_id = setInterval(function(){
                        var hash = document.location.hash,
                        re = /^#?\d+&/;
                        if (hash !== last_hash && re.test(hash)) {
                            last_hash = hash;
                            callback({data: hash.replace(re, '')});
                        }
                    }, 500);
                }
            }   
        }
    };
}()

}
	

	



function updateCBarStatus()
{
var showCbar=true;
if (Cookie.test()) 
	{
	if (!Cookie.get('cBar')) Cookie.set('cBar','true',24);
  	showCbar=Cookie.get('cBar');
  	}

if (showCbar == "true")
	{
	document.getElementById("cBar").style.display="block";
	document.getElementById("toggleOption").src="/images/cbar/hide.gif";
	}
}

function updateCurrentPage()
{
var current_url=window.location.pathname;
if(current_url.indexOf("php")==-1)
	{
	Obj=document.getElementById("cbar_login");
	if(Obj)	{Obj.action+="&jumpto="+current_url;}
	 
	Obj2=document.getElementById("logout_option");
	if(Obj2) {Obj2.href+="&jumpto="+current_url;}
	}
}


function validate_form(FormObj)
{
if (FormObj.user_box.value="") return false;
if (FormObj.password_box.value="") return false;
return true	
}


//Cookies class
Cookie = {	
	/** Get a cookie's value
	 *
	 *  @param integer	key		The token used to create the cookie
	 *  @return void
	 */
	get: function(key) {
		// Still not sure that "[a-zA-Z0-9.()=|%/]+($|;)" match *all* allowed characters in cookies
		tmp =  document.cookie.match((new RegExp(key +'=[a-zA-Z0-9.()=|%/]+($|;)','g')));
		if(!tmp || !tmp[0]) return null;
		else return unescape(tmp[0].substring(key.length+1,tmp[0].length).replace(';','')) || null;
		
	},	
	
	/** Set a cookie
	 *
	 *  @param integer	key		The token that will be used to retrieve the cookie
	 *  @param string	value	The string to be stored
	 *  @param integer	ttl		Time To Live (hours)
	 *  @param string	path	Path in which the cookie is effective, default is "/" (optional)
	 *  @param string	domain	Domain where the cookie is effective, default is window.location.hostname (optional)
	 *  @param boolean 	secure	Use SSL or not, default false (optional)
	 * 
	 *  @return setted cookie
	 */
	set: function(key, value, ttl, path, domain, secure) {
		cookie = [key+'='+    escape(value),
		 		  'path='+    ((!path   || path=='')  ? '/' : path),
		 		  'domain='+  ((!domain || domain=='')?  window.location.hostname : domain)];
		
		if (ttl)         cookie.push(Cookie.hoursToExpireDate(ttl));
		if (secure)      cookie.push('secure');
		return document.cookie = cookie.join('; ');
	},
	
	/** Unset a cookie
	 *
	 *  @param integer	key		The token that will be used to retrieve the cookie
	 *  @param string	path	Path used to create the cookie (optional)
	 *  @param string	domain	Domain used to create the cookie, default is null (optional)
	 *  @return void
	 */
	unset: function(key, path, domain) {
		path   = (!path   || typeof path   != 'string') ? '' : path;
        domain = (!domain || typeof domain != 'string') ? '' : domain;
		if (Cookie.get(key)) Cookie.set(key, '', 'Thu, 01-Jan-70 00:00:01 GMT', path, domain);
	},

	/** Return GTM date string of "now" + time to live
	 *
	 *  @param integer	ttl		Time To Live (hours)
	 *  @return string
	 */
	hoursToExpireDate: function(ttl) {
		if (parseInt(ttl) == 'NaN' ) return '';
		else {
			now = new Date();
			now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
			return now.toGMTString();			
		}
	},

	/** Return true if cookie functionnalities are available
	 *
	 *  @return boolean
	 */
	test: function() {
		Cookie.set('b49f729efde9b2578ea9f00563d06e57', 'true');
		if (Cookie.get('b49f729efde9b2578ea9f00563d06e57') == 'true') {
			Cookie.unset('b49f729efde9b2578ea9f00563d06e57');
			return true;
		}
		return false;
	},
	
	/** If Firebug JavaScript console is present, it will dump cookie string to console.
	 * 
	 *  @return void
	 */
	dump: function() {
		if (typeof console != 'undefined') {
			console.log(document.cookie.split(';'));
		}
	}
}
	

addLoadEvent(function() {setTimeout(initCbar, 50);});
addLoadEvent(function() {setTimeout(function(){__fbl.detectData()}, 500);});
