
var BoxPrefix = "Box_";

function CBoxFactory(){
	//methods
	this.show = function(oOwner,id){
		oBox = this.getBox(id);
		if(!oBox) oBox = this.createBox(oOwner,id);
		oBox.show();
	}
	this.hide = function(id){
		oBox = this.getBox(id);
		if(oBox) oBox.hide();
	}
	this.getBox = function(id){
		div = document.getElementById(BoxPrefix+id);
		return (div)?div.object:null;
	}
	this.createBox = function(oOwner, imageid){
		var x=0; var y=0;
		if(oOwner){
			x += oOwner.offsetWidth/2;
			y += oOwner.offsetHeight/2;
			while( oOwner.tagName.toUpperCase() != "BODY" )
			{
				x += oOwner.offsetLeft;
				y += oOwner.offsetTop; 
	            oOwner = oOwner.offsetParent;
			}
		}
		return new CBox(x,y,imageid);
	};
}

function CBox(x,y,id){
	//properies
	this.boxWidth = 130;
	this.boxHeight = 70;
	this.blending=true;
	this.blendtime = 100; //must be greate then 100
	this.blendingdirect=0;
	this.opacity=0;
	this.x = x;
	this.y = y;
	this.id = id;
	
	this.timerID = null; //for hide Box when mouse out
	this.interval = 1500;
	this.toHide = true;
	
	//methods
	this.hide = function() { 
		var thisObj = this;
		var id=this.timerID;
		this.timerID = null;
		if(null!=id)window.clearInterval(id);
		if(this.opacity > 0){
			this.blendingdirect = -1;
			ms = Math.round(this.blendtime / 100);
			for(var i=0; i<=100; i++)
				window.setTimeout( function(){thisObj.changeVisibility();},ms*i);
		}
	};
	this.show = function() {
		var thisObj = this;
		if(this.opacity < 100){
			this.blendingdirect = 1;
			ms = Math.round(this.blendtime / 100);
			for(var i=0; i<=100; i++)
				window.setTimeout( function(){thisObj.changeVisibility();},ms*i);
		}
		if(null==this.timerID) this.timerID = window.setInterval( function(){thisObj.checkVisibility();},this.interval);
	};
	
	this.changeVisibility = function(){
		if(this.blendingdirect==0)return;
		if(this.blendingdirect>0 && this.opacity==0){
			this.div.style.display = 'block';
			this.div.style.left = (this.x-this.div.offsetWidth/2)+'px';
			this.div.style.top = (this.y-this.div.offsetHeight/2)+'px';
		}
		this.opacity+=this.blendingdirect;
		if(this.blending) changeOpac(this.opacity,this.div.id);
		if(this.opacity==0){
			this.div.style.display = 'none';
		}
		if(this.opacity==0 || this.opacity==100){
			this.blendingdirect=0;
			if(null!=this.blendtimerID){
				var id = this.blendtimerID;
				this.blendtimerID = null;
				clearInterval(id);
			}
			if(this.opacity==0) this.div.removeNode(true);
		}
	}

	this.checkVisibility = function(){
		if(this.toHide){
			this.hide();
		}
	}
	this.out = function(){
		this.toHide = true;
	}
	
	this.over = function(){
		this.toHide = false;
		this.show();
	}
	this.click = function(){
		this.toHide = true;
		this.hide();
	}
	
	this.init = function(){
		var oThis = this;
		this.div = this.createDiv(this.id);
		this.div.object = this;
		style = this.div.style;
		style.display = 'none';
		style.position = 'absolute';
		this.div.onmouseover = function(){oThis.over(this);}
		this.div.onmouseout = function(){oThis.out(this);}
		this.div.onclick = function(){oThis.click(this);}
		if(this.blending) changeOpac(0,this.div.id);
	}
	
	this.createDiv = function(id){
		var div = document.createElement("DIV");
		div.id=BoxPrefix+id;
		div.className='box';
		div.style.padding="4px";
		txtBuff = '';
		var dat = new Date();
		var i = 12;
		var j = i-1;
		while(i--){
			txtJs = 'cal.setMonth(new Date('+dat.getFullYear()+','+dat.getMonth()+',1));'
			txtA = '<a href="javascript:'+txtJs+'" class="red">';
			txtBuff += '<tr><td onclick="'+txtJs+'" class="red" style="cursor:hand">'+txtA+months[dat.getMonth()]+'</a></td>'+
						   '<td onclick="'+txtJs+'" class="red"> | '+txtA+dat.getFullYear()+'</a></td></tr>';
			if(dat.getMonth()==0){
				dat.setYear(dat.getYear()-1);
				dat.setMonth(11);
			}
			else
				dat.setMonth(dat.getMonth()-1);
		}
		div.innerHTML = '<table cellpadding="0" cellcpacing="0" border="0">'+txtBuff+'</table>';
		var anchor = document.getElementById('anchor');
		if(!anchor) anchor = document.body;
		anchor.appendChild(div);
		return div;
	}
	
	//init code
	this.init();
	return this;
}

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100); 
	object.MozOpacity = (opacity / 100); 
	object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function setMonth(id,mark){
	document.forms.rateform.imageid.value = id;
	document.forms.rateform.mark.value = mark;
	document.forms.rateform.submit();
}

var sbf = new CBoxFactory();

