var bivvyPassword = ""

//LOCAL
//var bivvyScript = ""  // no slash

//REMOTE
var bivvyScript = "http://www.orhplc.com/bivvy/bivvy.cgi"  // no slash


function bivvyInit()
{    
    var str = location.search.substring(1, location.search.length);

    if(str.match("bivvy_password="))
    {
        bivvyPassword=str.replace("bivvy_password=","");
            var now=new Date();
        now.setTime(now.getTime()+365*24*60*60*1000);
        setCookie("bivvy_password",bivvyPassword,now);
        //location.href = location.href.replace(location.search,'')
    }
    else
    {
        bivvyPassword=getCookie("bivvy_password");
    }
   
    if(bivvyPassword && bivvyPassword!="")
    {      
        Event.observe(window, 'load', bivvyLogin)
        //addLoadEvent() // try to login....
    }
}

function bivvyLogin()
{

    url = bivvyScript + '/login?bivvy_password=' + (bivvyPassword ? bivvyPassword : '')
    myAjax = new Ajax.Request(url,{method:'get', evalScripts: true})

}

function getCookie(name)
{
	var cname=name+"=";
	dc=document.cookie;
	if (dc.length>0)
	{
		begin=dc.indexOf(cname);
		if (begin!=-1)
		{
			begin+=cname.length;
			end=dc.indexOf(";",begin);
			if (end==-1) end=dc.length;
			return unescape(dc.substring(begin,end));
		}
	}
	return null;
}

function setCookie(name,value,expires, path,domain,secure)
{
	document.cookie	= name + "=" + escape(value) +
	((expires == null) ? ""	: "; expires=" + expires.toGMTString())	+
	((path == null)	? "" : "; path=" + path) +
	((domain == null) ? "" : "; domain=" + domain) +
	((secure == null) ? "" : "; secure");
}

bivvyInit()
 

bivvyFailure = function(e)
{
    alert('failed')
}



bivvySetEditable = function()
{
     
    els = $$('.editable');
    
    for (var i = 0; i < els.length; i++) 
    {
       
        el = els[i]
        type = el.nodeName
        id = el.attributes['id'].value.replace('_editable','')
        rows = el.offsetHeight / 12
        if(rows < 10)
            rows = 10
        cols = el.offsetWidth / 12
        cols = cols * 2
        rows = rows
   
        file = location.pathname //.replace(bivvyRoot, '')  // relative to RootPath
        
        
       
        get_partial_url = bivvyScript + '/getpartial?elem=' + id + '&path=' + file 
        set_partial_url = bivvyScript + '/setpartial?elem=' + id + '&path=' + file 
        options = {     rows: rows, 
                        cols: cols, 
                        onFailure: bivvyFailure,
                        okText: "submit this",
                        hoverClassName: 'bivvy_hover', 
                        loadTextURL : uncache(get_partial_url), 
                        stripTags : false,
			            ajaxOptions : {method: 'get'}
		            }
          
        new Ajax.InPlaceEditor( el, uncache(set_partial_url), options)
    
    }
    

}

reload = function()
{
   if(location.search != '')
        location.href = location.href.replace(location.search,'')
   else
    location.reload(true)
}

bivvyLogout = function()
{
    var now=new Date();
    now.setTime(now.getTime()+365*24*60*60*1000);
    setCookie("bivvy_password","",now);
    myAjax = new Ajax.Request(bivvyScript + '/logout',{method:'get'})
}

bivvyBuild = function()
{
    myAjax = new Ajax.Request(bivvyScript + '/build',{method:'get'})
}

uncache = function(url)
{ 
return url;
  var d = new Date();
  var time = d.getTime();
  ch= url.match(/\?/) ? '&' : '?' 
  return url +ch+ 'time='+time;
}

bivvyInitAdmin = function()
{
 $('bivvy_admin').style.display='block'
 bivvySetEditable()
}




