document.write('<div id="sizer" style="position:absolute;right:0px; bottom:0px"></div>');

function main()  {

	this.loaded = '';

	this.init = function(){
		return; // Deprecated
		try {
			var $last_visited_item = new RegExp("last_visited_item=([0-9]+)", "gi").exec(document.cookie)[1];
			var $item = document.getElementById("item-"+$last_visited_item);
			if ($item){
				$item.className = $item.className+" last-visited";
			}

		} catch($error){}
	}

	//--------------------------------------------------------------------------------------

	this.enableWindow = function(){
		var $win = window.document.getElementById("GTK_BG")
		if ($win){
			$win.parentNode.removeChild($win);
		}
	}

	this.disableWindow = function(){
		var $newDiv = document.createElement("DIV");
		$newDiv.innerHTML = '<div id="GTK_BG" style="background:#000000; position:absolute; left:0px; top:0px; z-index:100; opacity:0.50; filter:alpha(opacity=50)"></div><div style="position:absolute;" id="GTK_BG_VALUE"></div>';
		document.body.appendChild($newDiv);

		var $Layer 			= window.document.getElementById("GTK_BG");
		$Layer.style.width  	= "100%";
		$Layer.style.height  	= Math.max(document.body.scrollHeight, document.getElementById("sizer").offsetTop)+"px";
	}

	//--------------------------------------------------------------------------------------

	this.$blinking = new Array();
	this.blink = function($item, $color1, $color2){
		if (this.$blinking.length == 0){
			window.setInterval("$main.blink()", 500);
		}
		if ($item){
			this.$blinking.push($item);
			$item.setAttribute("color1", $color1 ? $color1 : "red");
			$item.setAttribute("color2", $color2 ? $color2 : "#85a710");
		}
		var $temp = this.$blinking.slice();
		while($item = $temp.pop()){
			if ($item.getAttribute("blink") == 1){
				$item.style.color = $item.getAttribute("color2");
				$item.setAttribute("blink", 2)
			} else {
				$item.style.color = $item.getAttribute("color1");
				$item.setAttribute("blink", 1)
			}
		}
	}

	//--------------------------------------------------------------------------------------

	this.get_position = function(obj){
		var $size = new Array(0,0);
		while (obj.offsetParent) {
			$size[1] += obj.offsetTop
			$size[0] += obj.offsetLeft
			obj = obj.offsetParent;
		}
		return $size;
	}

	//--------------------------------------------------------------------------------------

	this.add_to_basket = function($product_id, $quantity, $variation){
		this.disableWindow();

		var $left 	= document.getElementById("sizer").offsetLeft/2 - 150;
		var $height = document.getElementById("sizer").offsetTop;

		$top = $height/2 - 100 + document.body.scrollTop;
		var $div = document.createElement("DIV");
		$div.id = "basket-loader-container"

		$div.innerHTML = '<div id="basket-loader" style="position:absolute; top:'+$top+'px; left:'+$left+'px; z-index:1000; border:2px solid #85817c; padding:20px; background:#ffffff; width:300px; height:100px; text-align:center; color:#85817c; font-weight:bold"><img src="images/loading.gif" border="0" style="margin-top:20px"/><br>Updating basket ... </div>';
		document.body.appendChild($div)
		rpc_get_file('/wbg/modules/output/basket/rpc.basket.php?product_id='+$product_id+"&quantity="+($quantity?$quantity:1)+"&variation="+$variation, "basket-container", 0);
		
		if ($quantity == 0){
			document.getElementById("item-"+$product_id).className = "";
		} else {
			document.getElementById("item-"+$product_id).className = "inbasket";
		}
		
	}

	this.add_to_wishlist = function($product_id){
		this.disableWindow();

		var $left 	= document.getElementById("sizer").offsetLeft/2 - 150;
		var $height = document.getElementById("sizer").offsetTop;

		$top = $height/2 - 100 + document.body.scrollTop;
		var $div = document.createElement("DIV");
		$div.id = "basket-loader-container"

		$div.innerHTML = '<div id="basket-loader" style="position:absolute; top:'+$top+'px; left:'+$left+'px; z-index:1000; border:2px solid #85817c; padding:20px; background:#ffffff; width:300px; height:100px; text-align:center; color:#85817c; font-weight:bold"><img src="images/loading.gif" border="0" style="margin-top:20px"/><br>Updating basket ... </div>';
		document.body.appendChild($div)
		rpc_get_file('/wbg/modules/output/database_out/rpc.wishlist.php?product_id='+$product_id, "basket-container", 0);
	}


	this.close_basset = function(){
		this.enableWindow();
		var $c = document.getElementById("basket-loader-container")
		$c.parentNode.removeChild($c);
	}

	//--------------------------------------------------------------------------------------

	this.mpselect_on = function($element){
		
		var $position 	= this.get_position($element);
		var $width 		= $element.clientWidth;
		var $div 		= $element.nextSibling;
		
		$div.style.display 	= ($div.style.display == "none" || !$div.style.display) ? 'block' : 'none';
		$div.style.width 	= ($width+1)+"px";
		$div.style.top 		= ($position[1]+18)+"px";
		$div.style.left 	= $position[0]+"px";
	}


	this.submit_search = function(){
		var $link = document.getElementById("search-form").action;
		var $file = document.getElementById("search").value.replace(new RegExp("/", "gi"), " ");
		$file = $file.replace(new RegExp("(%)|(#)|(')|(\")", "gi"), "");
		document.location.href = $link+""+$file+".html?0.0";
		return false;
	}
	this.submit_megasearch = function(){
		var $link = document.location.href;
		var $str = document.getElementById("search").value.replace(new RegExp("%", "gi"), "");
		document.location.href = $link.replace(new RegExp("&search[^&]*", "gi"), "") + "&search="+$str;
		return false;
	}

	this.img_preveiw_close = function(){
		this.enableWindow();
		var $i = document.getElementById("basket-loader-container");
		$i.parentNode.removeChild($i);
		return false;
	}
	
	this.rpc_info = function($link){
		var $i = document.createElement("div");
		$i.id = "rpc-prod-info";
		$i.style.position = "aboslute";
		$i.style.zIndex = 2000;
		$i.style.marginTop = "-50px";
		$i.style.marginLeft = "50px";
		$i.style.width = "300px";
		$i.style.height = "auto";
		$i.style.background = "#ffffff";
		$i.style.border = "1px solid red";
		$link.parentNode.appendChild($i);
		wbg_rpc_get_file('/wbg/modules/output/database_out/rpc.product_info.php?product_id='+$link.id, "rpc-prod-info", 0);
		$link.onmouseout = $main.rpc_info_hide;
	}
	
	this.rpc_info_hide = function(){
		$v = document.getElementById("rpc-prod-info");
		$v.parentNode.removeChild($v);
	}

	this.img_preview = function($link, $imgid, $asimg, $new_mode){
		this.$last_opened_Img = $link;
		this.disableWindow();

		var $screenX = document.getElementById("sizer").offsetLeft;
		var $screenY = document.getElementById("sizer").offsetTop;

		//------------------------------------------------------------------------
		// <<< Loading Image

		var $top  = $screenY/2 - 100 + document.body.scrollTop;
		var $left = $screenX/2 - 150;

		var $div = document.createElement("DIV");

		$div.id = "basket-loader-container"
		$div.innerHTML = '<div id="basket-loader" style="position:absolute; top:'+$top+'px; left:'+$left+'px; z-index:1000; border:2px solid #eeeeee; background:#ffffff; width:300px; height:140px; text-align:center; color:#85817c; font-weight:bold"><div style="padding:20px"><img src="images/loading.gif" border="0" style="margin-top:10px"/><br>Loading Image ... </div></div>';

		document.body.appendChild($div)
		var $img = new Image();
		$img.onload = $main.img_preveiw_show
		if ($new_mode){
			var $popupsrc = document.getElementById('img'+$new_mode).src.replace(new RegExp("(.s2.)|(.s1.)", "gi"),".s0.");
		} else if ($asimg){
			var $popupsrc = $link.src.replace(new RegExp("(.s2.)|(.s1.)", "gi"),".s0.");
		} else {
			if ($link.tagName == "IMG"){
				var $popupsrc = document.getElementById($link.id.replace("i", "img")).src.replace(new RegExp("(.s2.)|(.s1.)", "gi"),".s0.");
			} else {
				var $popupsrc = $link.href;
			}
		}
		

		if ($imgid){
			$img.src = $popupsrc.replace(new RegExp("[.][0-9][.]", "gi"), "."+$imgid+".");
		} else {
			$img.src = $popupsrc;
		}
		
		// >>> Loading Image
		//------------------------------------------------------------------------
		// <<< Loading SubImage

			//$img.src = $link.nextSibling.firstChild.src.replace(".0.",".1.");
		
		// >>> Loading SubImage
		//------------------------------------------------------------------------
		return false;

	}

	this.img_preveiw_show = function(){
		var $imgHeight 	= this.height*1;
		var $imgWidth 	= this.width*1;
		this.style.margin = "0px";

		var $screenX = document.getElementById("sizer").offsetLeft*1;
		var $screenY = document.getElementById("sizer").offsetTop*1;

		var $container = document.getElementById("basket-loader");

		var $height = $imgHeight > $screenY - 40 ? $screenY - 30 : $imgHeight + 10;

		
		$container.style.top = (document.body.scrollTop + ($screenY/2-$height/2))+"px";
		$container.style.left = ($screenX/2-($imgWidth+150)/2)+"px";
		$container.style.width = ($imgWidth+160)+"px";
		$container.style.height = ($height+3)+"px";
		$container.style.overflow = "auto";
		$container.style.overflowX = "hidden";
		
		var $small = this.src.replace(".s0.", ".s1.");
		var $small2 = $small.replace(new RegExp("[.][0-9][.]", "gi"), '.0.');
		var $small3 = $small.replace(new RegExp("[.][0-9][.]", "gi"), '.1.');
		var $small4 = $small.replace(new RegExp("[.][0-9][.]", "gi"), '.2.');
		var $small5 = $small.replace(new RegExp("[.][0-9][.]", "gi"), '.3.');
		
		var $string = "<div style='position:absolute; width:50px; height:30px; margin:4px background:#ffffff; right:0px;cursor:pointer;' onclick='$main.img_preveiw_close();return false'><img src='i/close.png' border='0' style='margin-top:5px; margin-left:5px'/></div>";
		$string += '<table cellspacing=0 cellpadding=0>';
		$string += '<tr><td style="background:#eeeeee; text-align:center; border:1px solid #ffffff" width="135" valign=top><div style="padding:5px">';
		$string += '<img imgid=0 src="'+$small2+'" border="0" vspace="3" style="border:1px solid #cccccc;" onmouseover="$main.act_image(this)" onload="$main.checkimg(this)"/><br>';
		$string += '<img imgid=1 src="'+$small3+'" border="0" vspace="3" style="border:1px solid #cccccc" onmouseover="$main.act_image(this)" onload="$main.checkimg(this)"/><br>';
		$string += '<img imgid=2 src="'+$small4+'" border="0" vspace="3" style="border:1px solid #cccccc" onmouseover="$main.act_image(this)" onload="$main.checkimg(this)"/>';
		$string += '<img imgid=3 src="'+$small5+'" border="0" vspace="3" style="border:1px solid #cccccc" onmouseover="$main.act_image(this)" onload="$main.checkimg(this)"/>';
		$string += '</div></td><td id="mainimgcontainer" onclick="$main.img_preveiw_close();return false" style="cursor:pointer; border:1px solid #ffffff;" title="Close image"></td></tr>';
		$string += '</table>';
		
		this.alt = "Close Image";
		this.title = "Close Image";
		
		$container.innerHTML = $string;
		
		//$container.innerHTML = "<div onclick='$main.img_preveiw_close();return false' style='cursor:pointer;' title='Close image'><div style='position:absolute; width:50px; height:30px; background:#ffffff; right:0px;'><img src='images/close.gif' border='0' style='margin-top:10px'/></div></div>";
		//$container.firstChild.appendChild(this);
		
		document.getElementById("mainimgcontainer").appendChild(this);
	}

	this.act_image = function($image){
		$image.style.border = "1px solid green";
		$image.style.cursor = "pointer"
		$image.onmouseout = $main.deact_image;
		$image.onclick = $main.change_image;
	}
	
	this.change_image = function(){
		$main.img_preveiw_close();
		$main.img_preview($main.$last_opened_Img, this.getAttribute("imgid"));
	}
	
	this.deact_image = function(){
		this.style.border = "1px solid #cccccc"
	}
	
	this.checkimg = function($image){
		if ($image.width == 88 && $image.height == 89){ // Eto nofoto image
			if ($image.parentNode){
				$image.parentNode.removeChild($image);
			}
		}
	}

	//--------------------------------------------------------------------------------------

	this.$scroll_offset 		= 0;
	this.$scroll_offset_now 	= 0;
	this.$direction 			= 0;

	this.prod_scroll = function($direction){
		var $t = $all_items.slice();
		if ($direction == 1){ // Esli naverh zakonchilisj produkti
			$next_index = $top_index+1+(this.$scroll_offset_now.toFixed(0)*1);
			if (!$t[$next_index]){
				return;
			}
			$top_item = $t[$next_index];

		}
		if ($direction == -1){
			$next_index = $top_index-4+(this.$scroll_offset_now.toFixed(0)*1);
			if (!$t[$next_index]){
				return;
			}
			$top_item = $t[$next_index+3];
		}


		if (!document.getElementById("smi["+$t[$next_index]+"]")){
			this.show_loading(document.getElementById("b-"+($direction<0?'bottom':'top')));
			wbg_rpc_get_file('/wbg/modules/output/database_out/rpc.add_to_smallprods.php?index='+$next_index+"&direction="+$direction, "small_prod_rpc", 0);

		} else {

			if (Math.abs(this.$scroll_offset_now.toFixed(2)) == Math.abs(this.$scroll_offset.toFixed(2))){ // Esli scroll ne rabotajet
				this.$scroll_Interval = window.setTimeout("$main.prod_make_scroll()", 1);
			}
			this.$direction	= $direction;
			this.$scroll_offset += ($direction*1);
		}
	}

	this.prod_make_scroll = function(){
		var $c = document.getElementById("small-prods-container").firstChild;
		$c.style.marginTop = ($c.style.marginTop.replace("px", "")*1) + (11*this.$direction) + "px";

		this.$scroll_offset_now += (0.1*this.$direction);
		if (Math.abs(this.$scroll_offset_now.toFixed(2)) != Math.abs(this.$scroll_offset.toFixed(2))){
			window.setTimeout("$main.prod_make_scroll()", 10);
		}
	}

	this.show_loading = function($object){
		var $div = document.createElement("DIV");
		$div.id = "scroll_loader";
		$div.style.position = "absolute";
		$div.style.background = "#ffffff"
		$div.style.textAlign	= 'left';
		$div.style.marginLeft = '0px'
		$div.style.marginTop = '0px'
		$div.style.opacity 		= '1px'
		$div.style.border		= '1px solid #8cb011';
		$div.style.zIndex 	= 100;
		$div.style.width 	= 100+"px";
		$div.style.height 	= 20+"px";
		$div.style.lineHeight 	= 20+"px";
		$div.style.background = "#a7ce1f"
		$div.style.color  = '#ffffff'
		$div.innerHTML = '&nbsp;Loading <span id="load-dots" style="width:100px">....<span>';
		$object.parentNode.insertBefore($div, $object);
		this.change_dots();
	}

	this.change_dots = function(){
		var $e = document.getElementById("load-dots");
		if (!$e) {return;}
		var $c = $e.innerHTML.length;
		if ($c >= 8) {$e.innerHTML = "";}
		$e.innerHTML = $e.innerHTML + ".";
		window.setTimeout("$main.change_dots()", 200);
	}


	this.open_product = function($id){
		if (document.getElementById("open-prod-container")){
			wbg_rpc_get_file('/wbg/modules/output/database_out/rpc.add_to_smallprods.php?index='+$next_index+"&direction="+$direction, "small_prod_rpc", 0);
		}
	}
}

$main = new main();

//======================================================================================


function filter(){
	this.$last_opened = 0;

	this.show_filter_vendors = function($field, $id, $category, $file){
		if (!$file){
			$file = '/wbg/modules/output/navigation-middle/rpc.vendors.php?cat='+$category+"&id="+$id;
		}

		if (document.getElementById("vndf"+$id)){
			if (document.getElementById("vndf"+$id).style.display == "none"){
				document.getElementById("vndf"+$id).style.display = "";
				if ($id != this.$last_opened){
					document.getElementById("vndf"+this.$last_opened).style.display = "none";
				}
				this.$last_opened = $id;
			} else {
				document.getElementById("vndf"+$id).style.display = "none";
			}
			return;
		}


		if (this.$last_opened){
			document.getElementById("vndf"+this.$last_opened).style.display = "none";
		}
		this.$last_opened = $id;

		var $position = $main.get_position($field);

		var $div 	= document.createElement("DIV");
		var $width 	= $field.clientWidth;

		$div.id 				= "vndf"+$id;
		$div.className 			= "mpselect";
		$div.style.background  	= '#ffffff'
		$div.style.displey  	= ''
		$div.style.border  		= '1px solid #f52c73'
		$div.style.position 	= 'absolute';
		$div.style.left 		= ($position[0]+3)+ "px";
		$div.style.top 			= ($position[1])+"px";
		$div.style.marginLeft 	= ($width)+"px";
		$div.style.width 		= "200px";
		$div.style.textAlign 	= "center";
		$div.style.padding 		= "0px 0px";
		$div.innerHTML = '<img src="images/loading.gif" border="0" vspace="10"/>';

		document.body.appendChild($div);
		wbg_rpc_get_file($file, $div.id, 0);
	}

	this.show_filter_variations = function($field, $id, $category){
		this.show_filter_vendors($field, $id, $category, '/wbg/modules/output/navigation-middle/rpc.variations.php?cat='+$category+"&id="+$id);
	}

	this.show_filter_attributes = function($field, $id, $category, $fieldname){
		this.show_filter_vendors($field, $id, $category, '/wbg/modules/output/navigation-middle/rpc.attributes.php?cat='+$category+"&id="+$id+"&field="+$fieldname);
	}


}
$filter = new filter();

//======================================================================================

function scroll_to(){

	this.scroll = function($obj_name){
		if (!$obj_name){
			return;
		}
		var $item = document.getElementById("item-"+$obj_name);
		if (!$item) {return;}
		$pos = this.findPos($item);
		$item.style.backgroundColor = "#eeeeee";
        window.scrollTo(0,$pos - 200);
	}
    this.findPos = function($obj) {
        var $curtop = 0;
        while ($obj.offsetParent) {
                $curtop += $obj.offsetTop
                $obj = $obj.offsetParent;
        }
        return $curtop;
    }
}
$scroll_to = new scroll_to();

//======================================================================================
// <<< Eto navigacija bazi dannih, vnutrennih razdelov

function gto(){
	
	var $chk = document.getElementById("left-select").getElementsByTagName("input");
	var $l = $chk.length;
	var $checked = new Array();
	for ($x = 0; $x < $l; $x++){
		if ($chk[$x].checked){
			$checked.push($chk[$x].value);
		}
	}
	var $link = $gto_link;
	
	if (!$checked.length){$checked.push($cat2_title);}
	$link = $link.replace("{LINK}", $checked.join(","));
	document.location.href = $link;
}

function cookie() {
	
	this.create = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	this.read = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	this.erase = function(name) {
		createCookie(name,"",-1);
	}
}

$cookie = new cookie();
