var Rater = function(d, bf, pb) {
    d=$(d);
    var that = this;
    var over = function(evt) {
        d.addClassName("selecting");
    }
    var out = function(evt) {
        d.removeClassName("selecting");
    }
    var click = function(i) {
        $(bf).value = i;
        eval(pb);
    }
    this.setRating = function(i) {
        span.style.width = (40*i)+"px";
    }
    d.down(".one").observe('click', function(event) { click(1); return false; } );
    d.down(".two").observe('click', function(event) { click(2); return false; } );
    d.down(".three").observe('click', function(event) { click(3); return false; } );
    d.down(".four").observe('click', function(event) { click(4); return false; } );
    d.down(".five").observe('click', function(event) { click(5); return false; } );
    d.observe('mouseover', over);
    d.observe('mouseout', out);
    
    return this;
}
Popup = new function() {
  this.open = function(options) {
    this.options = {
      url: '#',
      width: 800,
      height: 600,
      name:"_blank",
      location:"no",
      menubar:"yes",
      toolbar:"no",
      status:"no",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});
    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }
    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name, openoptions);
    return false;
  }
}
URLUtil = new function() {
  var baseAppVirtualPath;
  var ensureInited = function() {
    if (baseAppVirtualPath==null) alert("URLUtil wasn't initalized");
  }
  this.init = function(baseUrl) {
    baseAppVirtualPath = baseUrl;    
  }
  this.resolveUrl = function(url) {
      ensureInited();
      if (url && url.indexOf("~/") >= 0)
        return url.replace("~/", baseAppVirtualPath || "/");
      return url;
  }
}
PromptUser = new function() {
    var that = this;
    var isInited = false;
    var shim = document.createElement('div');
    var update = function(id, params) {
        var my_params = Object.extend({'id':id},params);
        var handlerUrl = URLUtil.resolveUrl('~/handlers/promptuser.aspx');
        new Ajax.Request(handlerUrl, {'method':'get', 'parameters':my_params, onSuccess:success});        
    }
    var success = function(res) {
        shim.style.visibility = 'hidden';
        shim.update(res.responseText);
        fixPngs();
        resize();
        shim.style.visibility = 'visible';
        $(window).scrollTo($(shim), {offset:-50});
    }
    var resizeIFrame = function() {
        var pop = $('pop');
        if (pop) {
            //pop.height = pop.contentWindow.document.body.scrollHeight;
			pop.height = 500;
            resize(true);
        }
        shim.style.visibility = 'visible';
    }
    var resize = function() {
        shim.style.left = Math.floor((maxSize() - shim.clientWidth) / 2) + "px";
    }
    var maxSize = function() {
        var pageSize = getPageSize();
        return parent == window ? 975 : pageSize[0];
    }
    this.init = function(id) {
        if (isInited) return;
        shim.id = id;
        document.body.appendChild(shim);
        shim = $(shim);
        isInited = true;
    }    
    this.show = function(id) {
        update(id);
        if (id != 'rotm_legal')
            grayOut(true);        
    }
    this.showLogin = function(id, url) 
    {
        update(id,{url:url});
        grayOut(true);
    }
    this.showPopup = function(id, url) {
       // shim.style.visibility = 'hidden';
        //shim.update('<div id="popup" class="'+ id +'"><iframe scrolling="no" id="pop" name="pop" frameborder="0" src="'+url+'" /></div>');
		var winfront=window.open(url,"popup","resizable=1,width=650,height=400");
		winfront.focus();
        var pop = $('pop');
        if (pop) {
            Event.observe(pop,'load',resizeIFrame);
            Event.observe(pop, 'load', function() { $(window).scrollTo(0,0) });
        }
       // if (!(id == 'edit_avatar') )
       //     grayOut(true);
    }
    this.showConfirm = function(id, title, msg, reload) {
        update(id,{msg:msg, title:title, reload:reload});
        grayOut(true, 1);
    }
    this.showEmailConfirm = function(id,to,link,from) {
        update(id,{to:to, link:link, from:from});
        grayOut(true, 1);
    }
    this.close = function() {
        shim.update();
        grayOut(false);
    }
}

function grayOut(vis, op) {
    this.resize = function() { 
        var ps = getPageSize();
        this.dark.style.width = ps[0] + 'px';
        this.dark.style.height = ps[1] + 'px';
        alert('hi');
    }
    var zindex = 50;
    var opacity = op ? op : 60;
    var opaque = (opacity / 100);
    var bgcolor = '#000000';
    var selects = document.getElementsByTagName("select"); 
    this.dark = $('darkenScreenObject');
    for (i = 0; i != selects.length; i++) 
    { 
      selects[i].style.visibility = "hidden"; 
    }    
    if (!this.dark) {
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');           
        tnode.id='darkenScreenObject';                       
        this.dark=tbody.appendChild(tnode);                  
    }
    if (vis) {
        //set the shader to cover the entire page and make it visible.
        this.dark.style.opacity=opaque;                      
        this.dark.style.MozOpacity=opaque;                   
        this.dark.style.filter='alpha(opacity='+opacity+')'; 
        this.dark.style.zIndex=zindex;        
        this.dark.style.backgroundColor=bgcolor;  
        var pageSize = getPageSize();
        this.dark.style.width = pageSize[0] + "px";
        this.dark.style.height = pageSize[1] + "px";
        this.dark.style.display = "block";
        
    } else {
         this.dark.style.display='none';
         var selects = document.getElementsByTagName("select"); 
         for (i = 0; i != selects.length; i++) 
             selects[i].style.visibility = "visible"; 
         
    }
}


LengthEnforcer = function(ele, cb){
    var setLength = function(){
        var rel = that.ele.getAttribute('rel');
        return rel ? parseInt(rel) : -1;
    }
    var enforce_length = function(){
        if (that.length >= 0 && that.ele.value.length > that.length) {
            that.ele.value = that.ele.value.substr(0,that.length);
            if (typeof cb == "function") cb();
        }
    }
    var that = this;
    this.ele = $(ele);
    this.length = setLength();
    Event.observe(ele, 'keydown', enforce_length, false);
    Event.observe(ele, 'keyup', enforce_length, false);
};
/*
    getPageSize was lifted in it's entirety from lightbox.  
    check it out here: http://www.huddletogether.com/projects/lightbox2/
*/
getPageSize = function() {
    var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	return [pageWidth,pageHeight];
}

ImageCheckBox = new function() {
    var that = this;
    this.toggle = function(i,arr) {
        var img = $(i);
        var div = img.up("div");
        var chk = div.down("input");
        chk.checked = !chk.checked;
        if (arr) $A(arr).each(function(j) { if ((j = $(j)) && j != div) { j.down("input").checked=false; j.removeClassName("selected") } });
        if (chk.checked) div.addClassName("selected");
        else div.removeClassName("selected");
    }    
}

/// Necessary for the moment, do not remove unless you reimplement
if (typeof reload=="undefined") {
    var reload = function() { (document.forms['aspnetForm'] || document.aspnetForm).submit(); }
}

fixPngs = function()
{
    $j('img[@src$=.png], input[@src$=.png], .png').ifixpng();
};

Resizer = function(fe, doc) {
    var that = this;
    this.resize = function() {
        if (fe.height == doc.body.scrollHeight) return;
        //fe.height = doc.body.scrollHeight;
		fe.height = 500;
        window.setTimeout(that.resize, 1000);
    }
}
checkFlashVersion = function(min){
    var ver = GetSwfVer();
    ver = ver.replace('WIN', '');
    ver = ver.split('.');
    if (typeof(ver) == 'string')
        ver = ver.split(',');
    for (var i = 0; i < ver.length; i++){
        if (ver[i] > min[i])
            return true;
        if (ver[i] < min[i])
            return false;
    }    
    return true;
}
hasMinReqFlash = function(){
    return checkFlashVersion([9,0,124]);
}