//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;
}

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);});