
var ScoreBoxPrefix = "ScoreBox_";

function CScoreBoxFactory(){
	//methods
	this.show = function(oOwner,id,languageCode,offsetX,offsetY){
		oBox = this.getBox(id);
		if(!oBox) oBox = this.createBox(oOwner,id,languageCode,offsetX,offsetY);
		oBox.show();
	}
	this.hide = function(id){
		oBox = this.getBox(id);
		if(oBox) oBox.hide();
	}
	this.getBox = function(id){
		div = document.getElementById(ScoreBoxPrefix+id);
		return (div)?div.object:null;
	}
	this.createBox = function(oOwner, id, languageCode,offsetX,offsetY){
		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;
			}
		}
		x += offsetX;
		y += offsetY;
		return new CScoreBox(x,y,id,languageCode);
	};
}

function CScoreBox(x,y,id,languageCode){
	//properies
	this.boxWidth = 460;
	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.languageCode = languageCode;

	this.timerID = null; //for hide ScoreBox when mouse out
	this.interval = 150000;
	this.toHide = false;
	
	//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;
		this.div.style.left = (x-this.boxWidth/2)+'px';
		this.div.style.top = (y-this.boxHeight)+'px';
//		this.div.style.top = y+'px';
		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.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.init = function(){
		var oThis = this;
		this.div = document.createElement("DIV");
		this.div.id=ScoreBoxPrefix+this.id;
		this.div.object = this;
		this.div.className='scorebox';
		style = this.div.style;
			style.display = 'none';
			style.position = 'absolute';
			style.width= this.boxWidth+'px';
			style.height= this.boxHeight+'px';

		this.div.innerHTML = 
			'<div style="border: 1px solid #000000; background: #F7F2EF;">' + 
			'<form name="cart" action="/de/lease/cart/add/' + this.id + '.html" method="post">' +
			'<table border="0" cellpadding="5">' +
			'<tr><td colspan="4" align="right"><a href="javascript: sbf.hide(' + this.id + ')"><img src="/images/close.gif" width="10" height="10" style="1px solid #000000;" alt="Schlie&szlig;en"></a></td></tr>' +
			'<tr>' +
				'<td width="50"><b>Menge:</b></td>' +
				'<td width="180"><b>Abholdatum:</b></td>' +
				'<td width="180"><b>R&uuml;ckgabedatum:</b></td>' +				
				'<td width="50" rowspan="2" valign="bottom"><a href="javascript: addToCart(document.cart);">Mieten ></a></td>' +
			'</tr><tr>' +
				'<td><input type="text" name="quantity" size="3" value="1"></td>' +
				'<td><span id="beginLabel"></span><input type="text" id="begin" name="begin" value="dd-mm-yyyy" style="width: 80px;"> <a href="javascript: createCalendar(\'begin\');" id="beginButton"><img src="/images/img.gif"></a></td>' +
				'<td><span id="endLabel"></span><input type="text" id="end"  name="end" value="dd-mm-yyyy" style="width: 80px;"> <a href="javascript: createCalendar(\'end\');" id="endButton"><img src="/images/img.gif"></a></td>' +
			'</tr>' +
			'</table>' +
			'</form>' +
			'</div>';
		this.div.onmouseover = function(){oThis.over(this);}
		this.div.onmouseout = function(){oThis.out(this);}

		var anchor = document.getElementById('anchor');
		if(!anchor) anchor = document.body;
		anchor.appendChild(this.div);
		if(this.blending) changeOpac(0,this.div.id);
	}
	
	//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 addToCart(form) {
	if (form.quantity.value == '')
		alert('Pflichtfeld Menge bitte ausfüllen.');
	else if (!isDate(form.begin.value))
		alert('Pflichtfeld Abholdatum bitte ausfüllen.');
	else if (!isDate(form.end.value))
		alert('Pflichtfeld Rückgabedatum bitte ausfüllen.');
	else form.submit();
}

function createCalendar(name) {
	Calendar.setup({
		inputField : name,
		ifFormat : "%d-%m-%Y",
		displayArea : name + "Label",
		daFormat : "",
		button : name + "Button",
		firstDay : 1,
		weekNumbers : false,
		cache : false,
    	singleClick : true});
}

var sbf = new CScoreBoxFactory();
