if (!Partner_ProductDetailData){
	var Partner_ProductDetailData = new Object();
}

var detailHandler = {
	left:		0,
	top:		0,
	mx:			0,
	my:			0,
	pid:		null,
	locked:		false,
	pdTPL:		null,
	inited:		false,
	parent:		null,

	lock: function(){
		if(detailHandler.inited){
			detailHandler.locked = true;
		}
	},
	unlock: function(){
		detailHandler.locked = false;
	},
	init: function(){
		// TEMPLATE PARSING
		var tplBoundaryRegex = /(^|.|\r|\n)((?:%=|#\{)\s*(\w+)\s*(?:%|\}))/;
		if ( $('ajax_templates') != null && $('ajax_templates').select('div.productdetail').length>0 ) {
			var pdHTML = $('ajax_templates').select('div.productdetail')[0];
			pdHTML.remove();
			detailHandler.pdTPL = new Template( pdHTML.innerHTML, tplBoundaryRegex);
		}
		detailHandler.inited = true;
	},
	checkMouse: function(evt,pid,parent){
		try{
		if(!evt){evt=window.event;}
		var loading = "<img src='/images/loading.gif' width='32' height='32'/>";
		ddrivetip(loading);
		this.pid = pid;
		setTimeout('detailHandler.ajaxRequest()',1);
		this.left = evt.clientX;
		this.top  = evt.clientY;
		}
		catch(e){
			//alert(e);
		}
	},
	ajaxRequest: function(){
		// CHECK IF PRODUCT_DETAILDATA OBJECT EXIST OR NOT
		if(Partner_ProductDetailData[detailHandler.pid]){
			return detailHandler.process();
		}

		if (!detailHandler.locked){
			detailHandler.locked = true;
			var scriptObj = document.createElement( 'script' );
			var rnd = Math.random()*9999999;
			scriptObj.setAttribute( 'type', 'text/javascript' );
			scriptObj.setAttribute( 'charset', 'utf-8' );
			scriptObj.setAttribute( 'id', 'detailHandler_dataset' );
			scriptObj.setAttribute( 'onload', 'detailHandler.process();' );
			// IE uglyhack.
			scriptObj.onreadystatechange = function() {
				if ( this.readyState == "complete" || this.readyState == "loaded" ) {
					eval( 'detailHandler.process()' );
				}
			}
			scriptObj.setAttribute( 'src', '/index.php?page=Partner_SearchPage&event=ProductDetail&id='+detailHandler.pid+'&r='+rnd);
			if ( $('detailHandler_dataset') != null ) {
				$('detailHandler_dataset').replace( scriptObj );
			} else {
				document.body.appendChild( scriptObj );
			}
		}

	},
	ajaxRequestOffer: function(){
		// CHECK IF PRODUCT_DETAILDATA OBJECT EXIST OR NOT
		if(Partner_ProductDetailData[detailHandler.pid]){
			return detailHandler.processOffer();
		}

		if (!detailHandler.locked){
			detailHandler.locked = true;
			var scriptObj = document.createElement( 'script' );
			var rnd = Math.random()*9999999;
			scriptObj.setAttribute( 'type', 'text/javascript' );
			scriptObj.setAttribute( 'charset', 'utf-8' );
			scriptObj.setAttribute( 'id', 'detailHandler_dataset' );
			scriptObj.setAttribute( 'onload', 'detailHandler.processOffer();' );
			// IE uglyhack.
			scriptObj.onreadystatechange = function() {
				if ( this.readyState == "complete" || this.readyState == "loaded" ) {
					eval( 'detailHandler.processOffer()' );
				}
			}
			scriptObj.setAttribute( 'src', '/index.php?page=Partner_SearchPage&event=ProductDetail&id='+detailHandler.pid+'&r='+rnd);
			if ( $('detailHandler_dataset') != null ) {
				$('detailHandler_dataset').replace( scriptObj );
			} else {
				document.body.appendChild( scriptObj );
			}
		}

	},
	process: function(){
		// INITIALIZE TEMPLATE
		if(!detailHandler.inited){
			detailHandler.init();
		}
		var out = detailHandler.pdTPL.evaluate({

			'product_id': 				''+Partner_ProductDetailData[detailHandler.pid].id,
			'product_name': 			''+Partner_ProductDetailData[detailHandler.pid].name,
			'product_isOffer': 			''+Partner_ProductDetailData[detailHandler.pid].isOffer,
			'product_prolongationDays':	''+Partner_ProductDetailData[detailHandler.pid].prolongationDays,
			'product_minimumDays':		''+Partner_ProductDetailData[detailHandler.pid].minimumDays,
			'product_arrivalDays':		''+Partner_ProductDetailData[detailHandler.pid].arrivalDays,
			'product_text1':			''+Partner_ProductDetailData[detailHandler.pid].text1,
			'product_picture1':			''+Partner_ProductDetailData[detailHandler.pid].picture1,
			'product_text2':			''+Partner_ProductDetailData[detailHandler.pid].text2,
			'product_picture2':			''+Partner_ProductDetailData[detailHandler.pid].picture2
		}
		)
		$('dhtmltooltip').innerHTML = out;
		detailHandler.locked = false;
	},
	closePage: function() {
		$('dhtmltooltip').style.display = 'none';
		detailHandler.locked = false;
	},
	prevPage: function() {
		$('partner_pdetails_pic').style.display = 'block';
		$('partner_pdetails_pic2').style.display = 'none';
		$('partner_pdetails_text').style.display = 'block';
		$('partner_pdetails_text2').style.display = 'none';
	},
	nextPage: function() {
		$('partner_pdetails_pic').style.display = 'none';
		$('partner_pdetails_pic2').style.display = 'block';
		$('partner_pdetails_text').style.display = 'none';
		$('partner_pdetails_text2').style.display = 'block';
	},

	showPrice: function(productId,priceType) {
		this.switchPrice(productId,'OS',priceType);
		this.switchPrice(productId,'UF',priceType);
		this.switchPrice(productId,'HP',priceType);
		this.switchPrice(productId,'VP',priceType);
		this.switchPrice(productId,'AI',priceType);
		$('price_head_'+productId+'_OS').className = priceType=='OS' ? 'tin_act' : 'tin';
		$('price_head_'+productId+'_UF').className = priceType=='UF' ? 'tin_act' : 'tin';
		$('price_head_'+productId+'_HP').className = priceType=='HP' ? 'tin_act' : 'tin';
		$('price_head_'+productId+'_VP').className = priceType=='VP' ? 'tin_act' : 'tin';
		$('price_head_'+productId+'_AI').className = priceType=='AI' ? 'tin_act' : 'tin';
	},
	switchPrice: function(pid,pt,apt) {
		var pcs = $('prices_'+pid).select('tr.price_'+pid+'_'+pt);
		for(var i=0;i<pcs.length;i++) {
			pcs[i].style.display = pt==apt ? '' : 'none';
		}
	},
	switchOfferDetail: function(pid) {
		var on = $('offer_detail_button_'+pid).innerHTML=='+';

		$('offer_detail_button_'+pid).innerHTML = on ? '-' : '+';
		var pcs = $('products_'+pid).select('tr.offer_detail_'+pid);
		for(var i=0;i<pcs.length;i++) {
			pcs[i].style.display = on ? '' : 'none';
		}
		if(on) {
			this.pid = pid;
			var loading = "<table class='product_info' width='100%' height='50'><tr><td align='center'><img src='/images/loading.gif' width='32' height='32'/></td></tr></table>";
			$('product_details_'+pid).innerHTML = loading;
			setTimeout('detailHandler.ajaxRequestOffer()',1);
		}
	},
	processOffer: function(){
		// INITIALIZE TEMPLATE
		if(!detailHandler.inited){
			detailHandler.init();
		}
		var picture = Partner_ProductDetailData[detailHandler.pid].picture1 && Partner_ProductDetailData[detailHandler.pid].picture1.length>0 ? Partner_ProductDetailData[detailHandler.pid].picture1 : Partner_ProductDetailData[detailHandler.pid].picture2;
		var out = detailHandler.pdTPL.evaluate({
			'product_id': 				''+Partner_ProductDetailData[detailHandler.pid].id,
			'product_name': 			''+Partner_ProductDetailData[detailHandler.pid].name,
			'product_isOffer': 			''+Partner_ProductDetailData[detailHandler.pid].isOffer,
			'product_prolongationDays':	''+Partner_ProductDetailData[detailHandler.pid].prolongationDays,
			'product_minimumDays':		''+Partner_ProductDetailData[detailHandler.pid].minimumDays,
			'product_arrivalDays':		''+Partner_ProductDetailData[detailHandler.pid].arrivalDays,
			'product_text1':			''+Partner_ProductDetailData[detailHandler.pid].text1,
			'product_picture':			''+picture,
			'product_text2':			''+Partner_ProductDetailData[detailHandler.pid].text2
		}
		)
		$('product_details_'+detailHandler.pid).innerHTML = out;
		detailHandler.locked = false;
	}
};
