/*requires pozitron.js*/
function Product(rootnode, description, id, options)
{
	this.init(rootnode, description, id, options);
};

Product.prototype = {
	
	description: "",
	product_id: -1,
	options: null,
	
	init: function(parent, description, id, options)
	{
		this.parent = parent;
		this.description = description;
		this.id = id;
		this.options = options;
		if (this.options==null) this.options = new Object();
		
		if ( !this.options.urlprefix ) {
			this.options.urlprefix = "";
		}
		
		if ( !this.options.labelcolor ) {
			this.options.labelcolor = "#0000c0";
		}
		
		if ( !this.options.amount_type ) {
			this.options.amount_type = "NORMAL";
		}
		
		if ( this.options.given_label ) {
			this.label = this.options.given_label;
		} else {
			this.label = document.createElement("span");		
			this.label.innerHTML = description;			
		}
		
		if ( !this.options.no_rating ) {
			this.options.no_rating = "false";
		}
		
		this.label.style.cssText = "text-decoration: underline; cursor: pointer; color: "+this.options.labelcolor+";";
		this.label.className='clickableprint';
				
		//this.label.onmouseover = this.initcreation.bind(this);
		if ( ( this.id && this.id.product_id != -1 ) || !this.id.product_id ) {		
			this.label.onclick = this.initcreation.bind(this);
			this.label.onmouseout = this.cancelcreation.bind(this);
		}
		
		if ( !this.options.given_label ) {
			this.parent.appendChild(this.label);
		}
	},
	
	initcreation: function()
	{
		if(this.options && this.options.showhint_enabled && this.options.showhint_enabled.value == false)
		{return;}
	
		//this.timer = setTimeout(this.creatediv.bind(this),500);
		this.creatediv();
	},
	
	cancelcreation: function()
	{
		window.clearTimeout(this.timer);
		this.initdestroy();
	},

	creatediv: function()
	{
			this.outerdiv = document.createElement("div");	
			this.outerdiv.style.fontSize = "12px";
			this.outerdiv.style.position = "absolute";
			this.outerdiv.style.zIndex = "100000";
			this.outerdiv.style.width="310px";

			if(this.options.width){ this.outerdiv.style.width = this.options.width+"px";}
			if(this.options.height){ this.outerdiv.style.height = this.options.height+"px";}			
		
			
			this.labeldiv = document.createElement("div");
			this.labeldiv.style.width = this.label.offsetWidth;
			this.labeldiv.style.height = this.label.offsetHeight;
			this.labeldiv.innerHTML = "&nbsp;";
			this.labeldiv.onmouseout = this.initdestroy.bind(this);
			this.labeldiv.onmouseover = this.canceldestroy.bind(this);
			
			if ( this.options.no_arrow ) {
				this.labeldiv.style.display = "none";
			}
			
			//contentdiv: ebben van: felso lekerekites, tartalom, also lekerekites
			this.contentdiv =  document.createElement("div");
			this.contentdiv.style.width = "312px";
			this.contentdiv.style.height = (this.options.height ? this.options.height+"px":null);//Number(this.label.offsetHeight.toString().split("px",1)[0]);
			this.contentdiv.style.marginLeft = Math.max(5, Math.min(20, this.label.offsetWidth - 15)) + "px";
			this.contentdiv.onmouseout = this.initdestroy.bind(this);
			this.contentdiv.onmouseover = this.canceldestroy.bind(this);
			
			this.closeDiv = document.createElement("div");
			//this.closeDiv.style.backgroundColor = "#ff0000";
			this.closeDiv.style.height = "16px";
			this.closeDiv.style.width = "16px";			
			this.closeDiv.style.cssFloat = "right";
			this.closeDiv.style.styleFloat = "right";			
			this.closeDiv.style.cursor = "pointer";
			this.closeDiv.innerHTML = "&nbsp;";
			this.closeDiv.style.textAlign = "right";
			this.closeDiv.onclick = this.initdestroy.bind( this );
			this.closeDiv.style.backgroundImage = "url("+this.options.urlprefix+"img/close.png)"
			this.closeDiv.style.backgroundRepeat = "no-repeat"; 
			//this.closeDiv.style.backgroundPosition = "10px " + Number(-leftdivbgposy + 5) + "px";
			
			this.contentdiv.appendChild( this.closeDiv );
			
			
			//contentdivinner: ebben van: minden ertelmes tartalom
			this.contentdivinner = document.createElement("div");
			this.contentdivinner.style.borderRight = "1px solid #808080";
			this.contentdivinner.style.backgroundColor = (this.options.backgroundColor == null? "#ffffff" : this.options.backgroundColor);
			
			if ( this.options.no_arrow ) {
				this.contentdivinner.style.borderLeft = "1px solid #808080";
			}
			
			this.contentdivinner.style.backgroundColor = "#FEFCF6";
			this.contentdiv.appendChild(this.contentdivinner);
			
			//a felul levo div, amiben kozepen felkoveren benne van a termek neve
			var tempdiv = document.createElement("div");
			tempdiv.style.height = "20px";			
			tempdiv.style.textAlign = "center";
			tempdiv.style.fontSize = "14px";
			tempdiv.innerHTML = this.description;
			tempdiv.style.fontWeight = "bold";
			this.contentdivinner.appendChild(tempdiv);
			
			//torzs dolgai, ez pageloaderrel lesz betoltve kesobb
			this.chartdiv = document.createElement("div");
			this.chartdiv.id = "productchartdiv";
			this.chartdiv.innerHTML = " ";
			this.chartdiv.style.padding = "5px 5px 5px 5px";
			this.chartdiv.style.height = "380px";
			
			this.chartdiv.onmousemove = this.canceldestroy.bind(this);
			
			this.contentdivinner.appendChild(this.chartdiv);
			
			//alul levo, aktualis termekkel kapcsolatos parancsokat tartalmazo div
			this.commanddiv = document.createElement("div");
			this.commanddiv.style.height = "30px";
			this.commanddiv.style.padding = "0px 5px";
			if ( !this.options.nonewtransaction ) {
				var tempspan = document.createElement("span");
				tempspan.className="commandLinkMedium_basic";
				tempspan.style.color = "#0000BF";
				tempspan.innerHTML = Str.products.newTrans;
				tempspan.onclick = this.createNewTransaction.bind( this );
				this.commanddiv.appendChild(tempspan);
				
				if ( !this.options.noaddtowatchlist ) {
					this.commanddiv.appendChild( document.createElement( "br" ) );
				}				
			}
			

			if ( !this.options.noaddtowatchlist ) {			
				tempspan = document.createElement("span");
				tempspan.className="commandLinkMedium_basic";
				tempspan.style.color = "#0000BF";
				tempspan.innerHTML = Str.products.addPriceW;
				tempspan.onclick = this.addToWatchList.bind( this );
				this.addToWatchListLink = tempspan;
				this.commanddiv.appendChild(tempspan);						
			}				
			
			this.contentdivinner.appendChild(this.commanddiv);

			//akkor most eldol hova keruljon a div
			var offsets = Position.cumulativeOffset(this.label);
			//han nem ismerj?k a stilusban megadott outerdiv-magassagot, feltetelezzunk 100at..
			//outerdiv_bottom = Number(offsets[1]) + Number( this.outerdiv.style.height.split("px",1)[0] || 430  );
			outerdiv_bottom = Number(offsets[1]) + Number( this.options.height || 430  );
			//kb hol lehet az oldal alja
			masterdiv_bottom = $("master").offsetTop + $("master").offsetHeight;
			masterdiv_bottom = Math.max(masterdiv_bottom, document.body.clientHeight);

			//----- pozicio kitalalasa -----
			var frameheight = document.body.clientHeight;
			var labelfromwintop = (Position.cumulativeOffset(this.label)[1] - document.body.scrollTop);
			var labelfromwinbottom = document.body.clientHeight - labelfromwintop;
			//var contentheight = Number( this.outerdiv.style.height.split("px",1)[0] || 430  );
			var contentheight = Number( this.options.height || 430  );
			
			var leftdivbgposy = 0;
			
			
			if( labelfromwintop > 15 + contentheight/2 )
			{
				//kozepso sav
				if( labelfromwinbottom > 15 + contentheight/2 )
				{
					this.outerdiv.style.top = labelfromwintop - contentheight/2  + document.body.scrollTop + "px";
					this.labeldiv.style.top = contentheight/2 + "px";
					leftdivbgposy =  Number( contentheight/2 );
				}
				//also resz
				else
				{
					if( labelfromwinbottom > 35 + 10 )
					{
						this.outerdiv.style.top = frameheight - contentheight - 15 + document.body.scrollTop + "px";
						this.labeldiv.style.top = contentheight - labelfromwinbottom + 15  + "px";
						leftdivbgposy =  Number( labelfromwinbottom - 15 );
					}
					else
					{
						this.outerdiv.style.top = frameheight - contentheight + (60 - labelfromwinbottom) - 15 + document.body.scrollTop  + "px";
						this.labeldiv.style.top = contentheight - (60 - labelfromwinbottom) - labelfromwinbottom + 15 + "px";
						leftdivbgposy =  Number( labelfromwinbottom - 15 + (60 - labelfromwinbottom) );
					}
				}
			}
			//felso resz
			else 
			{
				if(labelfromwintop > 15 + 10 ){
					this.outerdiv.style.top = 15 + document.body.scrollTop + "px";
					this.labeldiv.style.top = labelfromwintop - 15 + "px";
					leftdivbgposy =  Number( contentheight - labelfromwintop + 15 );
				}else{
					this.outerdiv.style.top = 15 - (25 - labelfromwintop)  + document.body.scrollTop  + "px";
					this.labeldiv.style.top = labelfromwintop - 15 + (25 - labelfromwintop) + "px";
					leftdivbgposy =  Number( contentheight - labelfromwintop - (25 - labelfromwintop) + 15 );
				}
			}
			
			//----- EOF pozicio kitalalasa -----
			{
				var scrollX = 0;
				if ( this.options.scrollableParent != null ) {
					scrollX = this.options.scrollableParent.scrollLeft;
					var parentleft = Position.cumulativeOffset(this.options.scrollableParent)[0]; 
					this.outerdiv.style.left = (offsets[0] - scrollX >= parentleft ? offsets[0] - scrollX : parentleft) + "px";
				} else {
					//this.outerdiv.style.left = this.labeldiv.offsetLeft + ( this.options.forcedOffsetLeft ? this.options.forcedOffsetLeft : 0 )  + "px";
					this.outerdiv.style.left = getOffsetLeftByElement ( this.parent ) + 'px';
				}
				this.labeldiv.style.border = "1px solid silver";
				this.labeldiv.style.position = "absolute";
				//this.labeldiv.style.borderBottom = "none";
				this.outerdiv.appendChild(this.labeldiv);
				
				
				RoundTopBordered(this.contentdiv, "#FEFCF6", "", "#808080");
				RoundBottomBordered(this.contentdiv, "#FEFCF6", "", "#808080");
				
				this.contentdiv.style.position = "absolute";
				this.contentdiv.style.top = "0px";
				this.contentdiv.style.left = "0px";
				
					//az a div amiben a baloldali "nyil" es a bal keret van:
				this.leftdiv = document.createElement("div");
				if ( !this.options.no_arrow ) {
					this.leftdiv.style.backgroundImage = "url("+this.options.urlprefix+"img/product_left2.gif)"
					this.leftdiv.style.backgroundRepeat = "no-repeat"; 
					this.leftdiv.style.backgroundPosition = "10px " + Number(-leftdivbgposy + 5) + "px";
				}
				this.leftdiv.style.position = "absolute";
				this.leftdiv.style.top = "5px";
				this.leftdiv.style.left = (Math.max(5, Math.min(20, this.label.offsetWidth - 15))-20) + "px";
				this.leftdiv.style.width = "21px";
				//this.leftdiv.style.border = "1px dashed magenta";
				this.leftdiv.style.height = "442px"; //this.contentdivinner.offsetHeight;
				this.leftdiv.onmouseout = this.initdestroy.bind(this);
				this.leftdiv.onmouseover = this.canceldestroy.bind(this);
				this.leftdiv.onmousemove = this.onMouseMove.bindAsEventListener(this);

				this.outerdiv.appendChild(this.contentdiv);

				this.outerdiv.appendChild(this.leftdiv);
				
				//this.outerdiv.style.border = "1px dashed red";
				
				//this.contentdiv.style.border = "1px dashed green";
				//this.contentdivinner.style.border = "1px dashed blue";
			}
			$("master").appendChild(this.outerdiv);
			
			this.loadInfo();
	},
	
	onMouseMove: function(e) {
		var x = Event.pointerX(e);
		var y = Event.pointerY(e);
		if( this.outerdiv && ( (y > Position.cumulativeOffset(this.label)[1] && y < Position.cumulativeOffset(this.label)[1] + this.label.offsetHeight)
			 || x > Number(this.leftdiv.style.left.split("px",1)[0]) + Number(this.leftdiv.style.width.split("px",1)[0]) + Number(this.outerdiv.style.left.split("px",1)[0]) - 3 ) ) {
			this.canceldestroy();
		} else {
			this.initdestroy();					
		}
	},
	
	createNewTransaction: function() {
		this.destroydiv();
		this.mdialog = new ModalDialog("transactioneditor.jsp", "transaction_id=-1&init_product_id="+this.id.product_id+"&init_amount="+(this.id.darab!=null ? this.id.darab : "")+"&init_product_name="+this.id.product_name+"&init_product_type="+this.id.product_type_id+"&init_tr_type=VETEL_ELADAS&portfolio_id="+portfolioselector.getselectedid(), {dialogbodystyle:{ backgroundColor:"#ffffff",overflow:"visible"}});	
	},
	
	addToWatchList: function() {
		var conn = new Connector( ( this.options.urlprefix ? this.options.urlprefix : "" ) + "DispatcherServlet","req=updatewatchlist&action=additem&product_id=" + this.id.product_id + "&urlprefix="+this.options.urlprefix, this.addedToWatchList.bind( this ) );
	},
	
	addedToWatchList: function() {		
		this.commanddiv.removeChild( this.addToWatchListLink );
	},
	
	initdestroy: function()
	{
		if(this.outerdiv) {
			this.timer = setTimeout(this.destroydiv.bind(this),300);
		}
	},
	
	canceldestroy: function()
	{
		if( this.timer ) {
			window.clearTimeout( this.timer );
		}
	},
	
	destroydiv: function()
	{
		//log("destroydiv");
		window.clearTimeout(this.timer);
		if(this.outerdiv && this.outerdiv.parentNode )
		{
			this.outerdiv.innerHTML = "";
			//this.parent.removeChild(this.outerdiv);
			
			$("master").removeChild(this.outerdiv);
			this.outerdiv = null;
			
		}

	},
	
	loadInfo: function()
	{
		cleanUpAll( $( 'productchartdiv' ) );
		recRemoveAllChildren( $( 'productchartdiv' ) );
		//var chartconn = new Connector("DispatcherServlet","req=getproductinfo&product_id=" + this.product_id + "&action=getChart&lastxmonth="+lastxmonth,this.onchartreceived.bind(this) );				
		if ( this.id.product_id ) {
 			new PageLoader(( this.options.urlprefix ? this.options.urlprefix : "" ) + 'productsheet.jsp','product_id='+this.id.product_id+"&includeprice=true&no_rating="+this.options.no_rating+"&fromproductsheet=true&urlprefix="+this.options.urlprefix+"&amount_type="+this.options.amount_type, 'productchartdiv',globalthis, false);
 		} else {
 			new PageLoader(( this.options.urlprefix ? this.options.urlprefix : "" ) + 'productsheet.jsp','product_id='+this.id+"&includeprice=true&no_rating="+this.options.no_rating+"&fromproductsheet=true&urlprefix="+this.options.urlprefix+"&amount_type="+this.options.amount_type, 'productchartdiv',globalthis, false);
 		}
	}	
	
};
