$(document).ready(AddListen);

var GalleryArray;

function AddListen()
	{
	$('img[src$=.png]').ifixpng(); 
	var popups = $("img.popup_image");
	popups.css({cursor:"pointer"}).click(LoadImg);
	GalleryArray = new Array();
	popups.each( function(i)
		{
		im = new Image();
		im.src = getLargeImage(this.src);
		GalleryArray[i] = im;
		});
	PreloadImages();
	$("#fade").click(CloseEnlarged);
	$("li.pup-li a").click(function(e){e.preventDefault();});
	$("a.div_prev").click(PrevGallery);
	$("a.div_next").click(NextGallery);
	$("a.div_close").click(CloseEnlarged);
	
	$("#zabor-height").slider({
			orientation: "vertical",
			range: "min", 
			step: $("#zabor-step-y").html()*1,
			min: $("#zabor-minheight").html()*1,
			max: $("#zabor-maxheight").html()*1,
			value: $("#zabor-maxheight").html()/2,
			slide: function(event, ui) {
				$("#sam-zabor").css('height', $("#zabor-height a").css('bottom'));
				$("#size-ver").val(ui.value);
				CountZabor();
			},
			stop: function(event, ui) {
				$("#sam-zabor").css('height', $("#zabor-height a").css('bottom'));
			}
		});
	$("#size-ver").val($("#zabor-maxheight").html()/2);
	
	$("#zabor-width").slider({
			range: "min", 
			min: $("#zabor-minwidth").html()*1,
			max: $("#zabor-maxwidth").html()*1,
			step: $("#zabor-step-x").html()*1, 
			value: $("#zabor-maxwidth").html()/2,
			slide: function(event, ui) {
				$("#sam-zabor").css('width', $("#zabor-width a").css('left'));
				$("#size-hor").val(ui.value);
				CountZabor();
			},
			stop: function(event, ui) {
				$("#sam-zabor").css('width', $("#zabor-width a").css('left'));
			}
		});
	$("#size-hor").val($("#zabor-maxwidth").html()/2);
	CountZabor();
	
	$("#size-hor").keyup(function(){
		var val = parseFloat(this.value);
		val = Math.min(val, $("#zabor-maxwidth").html());
		if(val > 0)
			{
			this.value = val;
			$("#zabor-width").slider('option', 'value', val);
			$("#sam-zabor").css('width', $("#zabor-width a").css('left'));
			CountZabor();
			}
		});
	$("#size-ver").keyup(function(){
		var val = parseFloat(this.value);
		val = Math.min(val, $("#zabor-maxheight").html());
		if(val > 0)
			{
			this.value = val;
			$("#zabor-height").slider('option', 'value', val);
			$("#sam-zabor").css('height', $("#zabor-height a").css('bottom'));
			CountZabor();
			}
		});
	}
function CountZabor()
	{
	$("#zabor-sum").html(Math.round($("#size-ver").val()*Math.ceil($("#size-hor").val()/1.1)*1.1*$("#zabor-price").html()));
	}

function getElement(event)
	{
	if (event.srcElement)
		return event.srcElement;
	else if (event.target)
		return event.target;
	}
function PreloadImages()
	{
	var image = new Array(
		'controls.png',
		's00.png',
		's01.png',
		's10.png',
		's11.png',
		'st.png',
		'sb.png',
		'sr.png',
		'sl.png'
		);
	var oImage = new Array();
	for(var i in image)
		{
		oImage[i] = new Image();
		oImage[i].src = '/images/'+image[i];
		}
	}
function getLargeImage(oUrl)
	{
	var what=oUrl.split("/");
	var wh="";
	for(i=0;i<what.length-1;i++)
		wh+=what[i]+'/';
	wh+='__large/'+what[i];
	return wh;
	}
function LoadImg(event)
	{
	el = getElement(event);
	
	oUrl=el.src;
	wh = getLargeImage(oUrl);
		
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i].src == wh)
			{
			Img = GalleryArray[i];
			setTimeout('_fill()',200);
			return;
			}
		}
	}
function NextGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == GalleryArray.length - 1 )
				{
				i = -1;
				}
			Img = GalleryArray[i + 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function PrevGallery(event)
	{
	for(i = 0; i < GalleryArray.length; i++ )
		{
		if(GalleryArray[i] == Img)
			{
			if( i == 0 )
				{
				i = GalleryArray.length;
				}
			Img = GalleryArray[i - 1];
			setTimeout('_fill()',10);
			return;
			}
		}
	}
function isImageOk(img)
	{
	if(!img.complete)
		return false;
	if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        	return false;
	return true;
	}
function _fill()
	{
		if(!isImageOk(Img))
			{
			setTimeout('_fill()',10);
			return;
			}
		
		shadow = $("#div_shadow");
		container = $("#div_enlarged");
		ofade = $("#fade");
		
		container.empty().append(Img);
		var w = Img.naturalWidth || Img.width;
		var h = Img.naturalHeight || Img.height;
		
		shadow.css({margin:"-"+Math.round(h/2)+"px 0 0 -"+Math.round(w/2)+"px"});
		if(!jQuery.browser.msie)
			{
			shadow.fadeIn(300);
			ofade.fadeIn(300);
			}
		else
			{
			shadow.show();
			ofade.show();
			}
	}
function CloseEnlarged()
	{
	if(!jQuery.browser.msie)
		{
		$("#div_shadow").fadeOut(300);
		$("#fade").fadeOut(300);
		}
	else
		{
		shadow.hide();
		ofade.hide();
		}
	}
function run_movie(arg, w, h) 
	{ 
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+arg+'" ALIGN=""><PARAM NAME=movie VALUE="'+arg+'.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <EMBED src="'+arg+'.swf"  quality=high wmode=transparent  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+arg+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>'); 
	}