var Screenshot = Class.create();
Screenshot.prototype =
{
	initialize: function(path_http)
	{
		this.path_http = path_http;
		this.max_tries = 5;
		this.tries = 0;
	    this.big_loader = '<img src="' + path_http + 'img/loading.gif" border="0" alt="" />';
	},

	getScreenshot: function ( key , id, tool )
	{
		var obj_temp = this;
		obj_temp.key = key;
		obj_temp.id = id;
		obj_temp.tool = tool || '';
		obj_temp.lv = false;
		var ajax_path = 'get.screenshot.ajax/';
		var screen_img_css = 'screenShotLoaderBox';
		if (tool == 'link_value')
		{
			obj_temp.lv = true;
			ajax_path = 'get.lv_screenshot.ajax/';
			obj_temp.screen_img_css = 'lvScreenShotLoaderBox';

		}
		var img_id = 'screenShot_'+ key;
		var uri = encodeURI( this.path_http + ajax_path + key );
		var loader =
			'<div class="screenShotLoaderBox" id="screenBox_' + key + '"><div class="spacer50"></div>' +
				this.big_loader +
			'</div>';
		new Ajax.Request(
			uri ,
			{
				onSuccess: function(t)
		      	{
		      		obj_temp.tries++;
		      		var obj_res = eval(t.getResponseHeader('X-JSON'));
		      		var src = '';
		      		var alt = '';
		      		var css_class = '';
		      		var width = 150;
		      		var height = 100;
		      		if (obj_res['retry'] && (obj_temp.tries < obj_temp.max_tries))
		      		{
						obj_temp.getScreenshot(obj_temp.key, obj_temp.id, obj_temp.tool);
		      		}
		      		else if (obj_temp.tries >= obj_temp.max_tries)
		      		{
		      			css_class = 'screenShotImg';
						var img = '<img src="' + obj_res['src'] + '" width="' + width + '" height="' + height + '" alt="' + alt + '" class="' + css_class + '" />';
						$('screenBox_'+key).innerHTML=img;
		      		}
		      		else
		      		{
		      			src = obj_res['src'];
		      			alt = obj_res['url'];
		      			width = obj_res['width'];
		      			height = obj_res['height'];

		      			obj_temp.imgHeight = height;
		      			obj_temp.imgWidth = width;
	      				if (obj_temp.tool == 'link_value')
	      				{
							var wrapper_h = parseInt(height) + 20;
							$('linkvalue-wrapper').setStyle({'height':wrapper_h+'px'});
						}
		      			obj_temp.src = src;
		      			css_class = 'screenShotImg';
						if (obj_temp.screen_img_css == 'lvScreenShotLoaderBox')
						{
			      			css_class = obj_temp.screen_img_css;
						}
						var img = '<img id="'+ img_id +'" src="' + src + '" width="' + width + '" height="' + height + '" alt="' + alt + '" class="' + css_class + '" />';
						$('screenBox_'+key).innerHTML=img;
						if(obj_temp.lv == true)
						{
			    			obj_temp.showLinkValue(obj_temp.key);
				    		obj_temp.showLinkValueLayers();
			    		}
		      		}
				},
		      	onFailure: function(t){},
		      	onComplete: function(t){}
			}
		);
		$(id).update(loader);
	},

	showLinkValue: function(key)
	{
	    var obj_temp = this;
		obj_temp.lv = '';
		new Ajax.Request(
	    	encodeURI(this.path_http + 'get_lv.ajax/' + key),
			{
        		onSuccess: function(t)
        		{
	       			var obj_lv = eval(t.getResponseHeader('X-JSON'));
	       			$('lv-max').innerHTML = obj_lv['linkvalue_formatted'];
	       			$('lv-max-calc').innerHTML = obj_lv['linkvalue'];
	       			$('linkvalue-legend').setStyle({'display':'block'});
        		},
        		onFailure: function(t) {},
        		onComplete: function(t) {}
			}
	   	);
	    return obj_temp.lv;
	},

	showLinkValueLayers: function()
	{
		var obj_temp = this;
		// add elements link value layers to the DOM tree
		// set height and top distance for the screenshot layers
		var img_h = this.imgHeight;
		var img_w = this.imgWidth;

		// the following values will be dynamically set via php
		// currently screens are at least 375 px high
		var std_img_h = 375;
		var std_img_w = 500;
		var img_h_diff = img_h - std_img_h;

		// default layer heights
		var ll1_h = std_img_h;
		var ll2_h = 0;
		var ll3_h = 0;

		// layer 1
		var ll1 = document.createElement('div');
		$('screenshot-scaled').appendChild(ll1);
		ll1.id ='linkvalue-layer-1';

		if (this.isIE()) {
			ll1.className = 'linkvalue-layer';
			ll1.style.height = ll1_h+'px';
			ll1.attachEvent('onmouseover', this.showPriceLayer);
			ll1.attachEvent('onmouseout', this.removePrice);
		} else {
			ll1.addClassName('linkvalue-layer');
			ll1.setStyle({'height':ll1_h+'px'});
			ll1.addEventListener('mouseover', this.showPriceLayer, false);
			ll1.addEventListener('mouseout', this.removePrice, false);
		}

		if (img_h_diff <= 0)
		{
			ll1_h = img_h;
		}
		// layer2 = 2/3 and layer3 = 1/3 of the remaining height
		else
		{
			ll2_h = (img_h_diff/3)*2;
			var ll2 = document.createElement('div');
			$('screenshot-scaled').appendChild(ll2);
			ll2.id ='linkvalue-layer-2';

			if (this.isIE()) {
				ll2.className = 'linkvalue-layer';
				ll2.style.height = ll2_h+'px';
	    		ll2.style.top = ll1_h+'px';
				ll2.attachEvent('onmouseover', this.showPriceLayer);
				ll2.attachEvent('onmouseout', this.removePrice);
			} else {
				ll2.addClassName('linkvalue-layer');
				ll2.setStyle({'height':ll2_h+'px','top':ll1_h+'px'});
				ll2.addEventListener('mouseover', this.showPriceLayer, false);
				ll2.addEventListener('mouseout', this.removePrice, false);
			}

			ll3_h = (img_h_diff/3);
			var ll3 = document.createElement('div');
			$('screenshot-scaled').appendChild(ll3);
			ll3.id ='linkvalue-layer-3';

			if (this.isIE()) {
				ll3.className = 'linkvalue-layer';
				ll3.style.height = ll3_h+'px';
	    		ll3.style.top = (ll1_h+ll2_h)+'px';
				ll3.attachEvent('onmouseover', this.showPriceLayer);
				ll3.attachEvent('onmouseout', this.removePrice);
			} else {
				ll3.addClassName('linkvalue-layer');
				ll3.setStyle({'height':ll3_h+'px','top':(ll1_h+ll2_h)+'px'});
				ll3.addEventListener('mouseover', this.showPriceLayer, false);
				ll3.addEventListener('mouseout', this.removePrice, false);
			}
		}
	},
	showPriceLayer: function(event)
	{
		var e = Event.element(event);
		if($('current-price'))
		{
			$('current-price').remove();
		}
		var factor = 1;
		var lid = '';
		if (!(lid = e.id)) {lid = e.readAttribute('id');}

		lid = lid.charAt(lid.length-1);

		var lv_max = $('lv-max').innerHTML;
		var currency = lv_max.charAt(lv_max.length-1);
		if (lid == 2)
		{
			factor = lid / 2.40;
		}
		else if  (lid == 3)
		{
			factor = lid / 4.8;
		}
		var curr_price = ($('lv-max-calc').innerHTML * factor).toFixed(2);
		var price = document.createElement('strong');
		price.setAttribute('id','current-price');
		price.innerHTML = curr_price  + ' ' + currency;
		$('wrapper').appendChild(price);
		Event.observe(document, 'mousemove', function(event)
		{
			if ($('current-price'))
			{
				var posy = Event.pointerY(event) - 30;
				var posx = Event.pointerX(event) + 30;
				$('current-price').setStyle({ 'top': posy + 'px', 'left': posx + 'px' });
			}
		});
	},
	isIE: function()
	{
		return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	},
	removePrice: function()
	{
		if($('current-price'))
		{
			$('current-price').remove();
		}
	}
}