function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var pornpups =
{
	/** uaktywnia wszystkie linki w podanym elemencie. z pewnoscia chcesz to wywolac (patrz koniec tego pliku) */
	init: function(element)
	{
		/** wymagany jest element i obsluga DOM */
		if (!element || !element.getElementsByTagName) {return false;}

		var as = element.getElementsByTagName('a');
		for(var i=0;i<as.length;i++)
		{
			/* dzieki ponizszej linijce upopupiane sa tylko linki do obrazkow. zakomentuj ja i bedzie dzialalo na wszystkie. mozesz tez zmienic warunek zeby dzialalo np. tylko na linki z okreslona klasa (tip: .className) */
			if ((as[i].href+'').match(/\.(jpe?g|png|gif)/i))
				as[i].onclick = this.click;
		}

		return true;
	},

	/** tworzenie dokumentu, ktory jest w nowootwartym oknie. zmien HTML wg gustu */
	writedoc: function(win,href,title,alt,h,img_x,img_y)
	{


		var doc = win.document;
		doc.open('text/html;charset=utf-8');
		doc.write(
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
		'<html id="popup">' +
		'<head><title>' + alt + '</title>' +
		'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">' +
		'<meta http-equiv="imagetoolbar" content="false">' +
		'<base href="http://efox.pl/" />'+
		'<link rel="stylesheet" href="public/styles/pornpups.css">' +
		(h?'<style type="text/css">.obrazek {min-height: ' + h + 'px} #iesux#popup .obrazek {height: ' + h + 'px;}</style>':'') +
		/* w FF1.5 klik nie zawsze zamykał przy body.onmouseup */
		'</head><body onclick="window.close();">' +
		(title?'<p style="background-color:#ffc32f;" class="tytul">'+title+'</p>':'') +
		'<p style="background-color:#FFF;padding-top:5px;" class="obrazek"><img src="'+href+'" alt="'+alt+'"'+'width="'+img_x+'"'+'height="'+img_y+'"/></p>' +
		'<p style="background-color:#FFF;" class="klik">Kliknięcie zamyka okno</p>' +
		'</body></html>'
		);
		doc.close();
	},

	get_size:function(href,wym_x,wym_y)
	{
		var im=new Image();
		im.src = href;
		var img_x = im.width;
		var img_y = im.height;

		if (img_x == 0 ) img_x = wym_x;
		if (img_y == 0 ) img_y = wym_y;


		if((img_x>wym_x) || (img_y>wym_y))
		{
			if(img_x>=img_y)
			{
				var asp = (wym_x-10)/img_x;
				img_x=wym_x;
				img_y=Math.round(asp*img_y);


			}
			else
			{
				var asp = wym_y/img_y;
				img_y=wym_y;
				img_x=Math.round(asp*img_x);

			}


		}
		return new Array(img_x,img_y);
	},

	/** funkcja ma za zadanie wydobyc wymiary z podanego ciagu (tytulu linku)
		zwraca tablice zawierajaca ladnie sformatowany tytul oraz
		wymiary pobrane z podanego ciagu w formacie dla window.open

		zmien ta funkcje, jesli chcesz zapisywac wymiary w inny sposob
	*/
	title2size: function(str,href)
	{
		if (str)
		{
			/* wyrazenie regularne szuka czegos na wzor "(111x222)" */
			var out = str.match(/\(([0-9]+)x([0-9]+)\)/);

			/* po czym usuwa znaleziony fragment */
			if (out) return new Array(str.replace(/\(([0-9]+)x([0-9]+)\)/g,''),parseInt(out[1]),parseInt(out[2]));
		}
		/* w przypadku problemow - podaje wartosci domyslne (zmien na takie, jakie ci pasuja) */
		var sx = screen.width/5;
		var sy = screen.height/5;

		var axa=pornpups.get_size(href,sx,sy);


		return new Array(str,axa[0],axa[1]);
	},

	/** obsluga klikniecia */
	click: function()
	{
		/* jesli okno tej miniatury jest otwarte - zamknij (będzie otworzone jeszcze raz, jakby użytkownik zgubił poprzednie...) */
		try {
			if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {this.pp_win.close(); this.pp_win=false;}
		}
		/* explorer ma z tym dziwne problemy, ktore na szczescie mozna olac */
		catch(e){}

		try {
			/* znajdz obrazek, jego alt i title */
			var imgs = this.getElementsByTagName('img');
			var title = imgs[0].getAttribute('title')?imgs[0].getAttribute('title'):this.getAttribute('title');
			var alt = imgs[0].getAttribute('alt');

			/* parametry okna, z wymiarami (tu wymiary sa powiekszane, aby bylo troche miejsca wokol obrazka) */
			var titleandsize = pornpups.title2size(title,this.href);
			var winopts = "left=200 , top=100 ,dependent=yes, toolbar=no,resizable=yes,width=" + (titleandsize[1]+30) + ',height=' + (titleandsize[2]+60);

			/* do the boogie! */
			var win = window.open(this.href,'_blank',winopts);
			if (win && win.opener)
			{
				this.pp_win = win;
				pornpups.writedoc(win,this.href,titleandsize[0],alt,titleandsize[2],titleandsize[1],titleandsize[2]);
				return false;
			}
		}
		/* siatka bezpieczenstwa, jakby ktoras funkcja nawalila */
		catch(e){}

		/* jesli nie udalo sie otworzyc okna - zwraca true, co otwiera obrazek w tym samym oknie */
		return true;
	},

	initNow: function()
	{
		this.init(document.body);
	},

	initLoad: function()
	{
		var oldOnload = window.onload;
		var that = this;
		window.onload = function()
		{
			if (oldOnload) try{oldOnload();}catch(e){/*explorer dziwne rzeczy plecie*/}
			that.initNow();
		}
	}
};




function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}


function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX +
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY +
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function mouseMove(ev) {
    ev = ev || window.event;
    var target   = ev.target || ev.srcElement;
    var mousePos = getPosition(ev);
    var pos = findPos(target);
    var x = mousePos.x - pos[0];

    //podmien klasy
    if(x>=0 && x<=16) target.className = "star1";
    else if(x>=17 && x<=32) target.className = "star2";
    else if(x>=33 && x<=48) target.className = "star3";
    else if(x>=49 && x<=64) target.className = "star4";
    else if(x>=66 && x<=80) target.className = "star5";
}

function stars(element,current_class) {
    element.onmousemove = mouseMove;
    element.onmouseout = function(){this.className = current_class;}
    element.onmousedown = clickStar;
}

function clickStar(ev) {
    ev = ev || window.event;
    var target   = ev.target || ev.srcElement;
    var mousePos = getPosition(ev);
    var pos = findPos(target);
    var x = mousePos.x - pos[0];

    var rate;

    //zbadaj ocene
    if(x>=0 && x<=16) rate=1;
    else if(x>=17 && x<=32) rate=2;
    else if(x>=33 && x<=48) rate=3;
    else if(x>=49 && x<=64) rate=4;
    else if(x>=66 && x<=80) rate=5;
    var cont = target.parentNode;
    var link_id = target.id;
    cont.innerHTML = "<img src=\""+global_path+"pub/images/ajax-loader.gif\" />";
    $.get( global_path+"ajax_product_rate.html?product_token="+link_id+"&rate="+rate, null, function(obj){
       //alert(obj);
        if(obj=="error") {
            cont.innerHTML = '<div class="homeRate">Wystąpił błąd, spróbuj ponownie później.</div>';
            return;
        }
    var parts = obj.split("-");
    var new_rate = parseFloat(parts[0]);
    var klasa = "star"+Math.round(new_rate);
    if (cont.id =='multi_rates')
        cont.innerHTML = '<span class="software_rate"><em>Dziękujemy za głos.</em></span> <div class="'+klasa+'" style="float:left;" title="'+new_rate+'"></div>';
    else
        cont.innerHTML = '<div style="float:left;position:relative;top:-4px;left:3px;" class="'+klasa+'" title="'+new_rate+'"></div><div style="float:left;">(głosów: '+parts[1]+')</div>';
        //cont.innerHTML = '<div class="homeRate">Thanks for your rate</div><div class="'+klasa+'" style="margin:0 auto;line-height:22px;"></div><div class="homeVote" style="margin:0 auto;line-height:22px;">('+parts[1]+' votes, average: '+new_rate+' out of 5)</div>';
        //cont.innerHTML = '<span class="video_rate"><em>Thanks for your rate.</em></span> <div class="'+klasa+'" style="float:left;" title="'+new_rate+'"></div>';
     })
}

function starsShop(element,current_class) {
    element.onmousemove = mouseMove;
    element.onmouseout = function(){this.className = current_class;}
    element.onmousedown = clickStarShop;
}

function clickStarShop(ev) {
    ev = ev || window.event;
    var target   = ev.target || ev.srcElement;
    var mousePos = getPosition(ev);
    var pos = findPos(target);
    var x = mousePos.x - pos[0];

    var rate;

    //zbadaj ocene
    if(x>=0 && x<=16) rate=1;
    else if(x>=17 && x<=32) rate=2;
    else if(x>=33 && x<=48) rate=3;
    else if(x>=49 && x<=64) rate=4;
    else if(x>=66 && x<=80) rate=5;
    var cont = target.parentNode;
    var link_id = target.id;
    cont.innerHTML = "<img src=\""+global_path+"pub/images/ajax-loader.gif\" />";
    $.get( global_path+"ajax_shop_rate.html?shop_code="+link_id+"&rate="+rate, null, function(obj){
        if(obj=="error") {
            cont.innerHTML = '<div class="homeRate">Wystąpił błąd, spróbuj ponownie później.</div>';
            return;
        }
    var parts = obj.split("-");
    var new_rate = parseFloat(parts[0]);
    var klasa = "star"+Math.round(new_rate);
    if (cont.id =='multi_rates')
        cont.innerHTML = '<span class="software_rate"><em>Dziękujemy za głos.</em></span> <div class="'+klasa+'" style="float:left;" title="'+new_rate+'"></div>';
    else
        cont.innerHTML = '<div style="float:left;position:relative;top:-4px;left:3px;" class="'+klasa+'" title="'+new_rate+'"></div><br style="clear:both;"/><div style="float:left;">(głosów: '+parts[1]+')</div>';
        //cont.innerHTML = '<div class="homeRate">Thanks for your rate</div><div class="'+klasa+'" style="margin:0 auto;line-height:22px;"></div><div class="homeVote" style="margin:0 auto;line-height:22px;">('+parts[1]+' votes, average: '+new_rate+' out of 5)</div>';
        //cont.innerHTML = '<span class="video_rate"><em>Thanks for your rate.</em></span> <div class="'+klasa+'" style="float:left;" title="'+new_rate+'"></div>';
     })
}

function toggle_description(state,offer_id)
{
    if (state == 1)
    {
        $("#desc_whole"+offer_id).show();
        $("#desc_short"+offer_id).hide();
    }
    else
    {
        $("#desc_whole"+offer_id).hide();
        $("#desc_short"+offer_id).show();
    }
}

function clear_textbox()
{
if (document.searchForm.q.value == "Co chcesz dziś kupić?")
document.searchForm.q.value = "";
} 