var Cookies = Class.create({
    initialize: function(path, domain) {
        this.path = path || '/';
        this.domain = domain || null;
    },
    // Sets a cookie
    set: function(key, value, days) {
        if (typeof key != 'string') {
            throw "Invalid key";
        }
        if (typeof value != 'string' && typeof value != 'number') {
            throw "Invalid value";
        }
        if (days && typeof days != 'number') {
            throw "Invalid expiration time";
        }
        var setValue = key+'='+escape(new String(value));
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var setExpiration = "; expires="+date.toGMTString();
        } else var setExpiration = "";
        var setPath = '; path='+escape(this.path);
        var setDomain = (this.domain) ? '; domain='+escape(this.domain) : '';
        var cookieString = setValue+setExpiration+setPath+setDomain;
        document.cookie = cookieString;
    },
    // Returns a cookie value or false
    get: function(key) {
        var keyEquals = key+"=";
        var value = false;
        document.cookie.split(';').invoke('strip').each(function(s){
            if (s.startsWith(keyEquals)) {
                value = unescape(s.substring(keyEquals.length, s.length));
                throw $break;
            }
        });
        return value;
    },
    // Clears a cookie
    clear: function(key) {
        this.set(key,'',-1);
    },
    // Clears all cookies
    clearAll: function() {
        document.cookie.split(';').collect(function(s){
            return s.split('=').first().strip();
        }).each(function(key){
            this.clear(key);
        }.bind(this));
    }
});

var JsonCookies = Class.create(Cookies, {});
JsonCookies.addMethods({
    // Overridden set method to JSON-encode value
    set: function($super, key, value, days) {
        switch (typeof value) {
            case 'undefined':
            case 'function':
            case 'unknown':
                throw "Invalid value type";
                break;
            case 'boolean':
            case 'string':
            case 'number':
                value = String(value.toString());
            break;
        }
        $super(key, Object.toJSON(value), days);
    },
    // Overriden get method to JSON-decode the value
    get: function($super, key) {
        var value = $super(key);
        return (value) ? value.evalJSON() : false;
    }
});

var GeekWindow = Class.create({
   initialize: function (name)
   {
        this.name=name;
        this.url=location.href;
        this.options=$A();
        
        this.optionsKeys=['location' ,'toolbar','menubar','status','resizable','scrollbars','height','width' , 'fullscreen', 'top' , 'left'];
        
        this.options['location']='no';
        this.options['fullscreen']='no';
        
        this.options['toolbar']='no';
        this.options['menubar']='no';
        this.options['status']='no';
        this.options['resizable']='yes';
        this.options['scrollbars']='yes';
        this.options['height']='480';
        this.options['width']='700';
        this.options['top']='0';
        this.options['left']='0';
        this.windowObj=false;
   },
   setUrl: function (url)
   {
        this.url=url;
   }, 
   getUrl: function ()
   {
        return this.url;
   },
   setOptions: function (options)
   {
      this.options=options;
   },
   setOption: function (key , value)
   {
        if(this.optionsKeys.indexOf(key)!=-1)
            this.options[key]=value;
   },
   getOption: function (key)
   {
        if(this.optionsKeys.indexOf(key)!=-1)
          return  this.options[key];
        return null;
   },
   getWindowObj: function ()
   {
     return this.windowObj;
   },
   open: function ()
   {
       var os="";
       for(var i=0; i<this.optionsKeys.length; i++)
       {
            var k=this.optionsKeys[i];
            var v=this.options[k];
            if(i!=0)
                os+=",";
            os+=k+"="+v;
       }
       this.windowObj=window.open(this.url , this.name, os , false);
   }
   
   
});

var GeekForm = Class.create({

initialize : function(form_id)
{
    this.form=$(form_id);
    this.formElements=this.form.getElements();
    this.attachEvents();
},
attachEvents : function()
{
   var eids=new Array(); 
   for(var i=0; i<this.formElements.length; i++)
   {
        var e=this.formElements[i];
        var eid=e.readAttribute('id');
        
        eids[eids.length]=eid;
        Event.observe(e , 'focus' , function (ev)
        {
              var element=ev.element();
              var elementId=element.readAttribute('id');
              var desc_id=elementId +"_desc";
              var desc=$(desc_id);
              if(desc)
                desc.style.visibility="";
        });
        Event.observe(e , 'blur' , function (ev)
        {
              var element=ev.element();
              var elementId=element.readAttribute('id');
              var desc_id=elementId +"_desc";
              var desc=$(desc_id);
              if(desc)
                desc.style.visibility="hidden";
        });
   } 
   
}
});

Event.observe(window , 'load' , function()
{
   var geekForms=$$('form.geek-form');
   for(var i=0; i<geekForms.length; i++)
   {
    var gf=new GeekForm(geekForms[i]);
   }
});

function geekPageRefresh()
{
    // Hiding old messages when ajax request is made
    var sm=$('sessionMessages');
    if(sm)
        sm.hide();
}
function elementHasPoint(e , x , y , thl , tht , thr, thb)
{
	e=$(e);
	var t , l , w , h;
	var tl=Element.cumulativeOffset(e);
	t=tl[1];
	l=tl[0];
	w=Element.getWidth(e);
	h=Element.getHeight(e);
	var r=l+w;
	var b=t+h;
	t+=tht;
	b-=thb;
	l+=thl;
	r-=thr;
	if(x>=l && x<=r && y>=t && y<=b)
		return true;
	return false;
}
function popWindowLink(a , name)
{
   var w=new GeekWindow(name);
   w.setUrl(a.getAttribute('href'));
   w.setOption('resizable' , 'yes');
   w.setOption('scrollbars' , 'yes');
   w.open();
}

