var timeee,timeee2;

function Buttons(){
    inputs = document.getElementsByTagName('input');
    for(i = 0; i < inputs.length; i++){
        if(inputs[i].type != 'image')
        inputs[i].className+=' input';
        if(inputs[i].type == 'submit')
        inputs[i].className+=' button';
    }
}
function Init(){
    Buttons();
    /**
    * wyłączamy autocomplete zeby nie kolidowac z hintami ajaxowymi
    * _i_ zachowac zgodnosc z xhtml
    */
    var s = document.getElementById('search_query_words');
  if (s)  s.setAttribute("autocomplete", "off");
	
  
}


function priceupdate(select){

	var _priceOld = $('#first_price').val();
	var selects = $('.prodChangePrice');
	_priceOld = _priceOld.replace('.','');


	var foo = [];
	var currentPrice = 0;
	$('.prodChangePrice :selected').each(function(i, selected){
		var _text = $(selected).text();
		var _title = $(selected).attr("title");
		
		if (_title.substr(0,1) == '+'){
			if (_title.split('.').length > 1){
				__title = _title.replace('.','');
			}
			else {		
				_title = parseInt(_title)+'00';	
			}
			
		currentPrice = parseFloat(currentPrice)+parseFloat(_title);
		}
		else {
			if (_title.split('.').length > 1){
				_title = _title.replace('.','');
			}
			else {
				_title = parseInt(_title)+'00';		
			}
		}
	});

			_newPrice = parseFloat((parseFloat(_priceOld)+currentPrice)/100).toFixed(2);

			$("#current_price").val(_newPrice);
			_newPrice = _newPrice+" PLN";

	$(".font_cena_karta").html(_newPrice);


}
function countupdate(select){
	var $select = $(select);
	var index= select.selectedIndex;
	var _title = parseFloat($('option:eq('+index+')', $select).val());

	var $price= $('.font_cena_karta');
	var _current_price = parseFloat($("#current_price").val());

	var _newPrice = _current_price*_title;

	$(".font_cena_karta").html(_newPrice+" PLN");
	$("#count_prod").val(_title);
	
	$("#id_count_").remove();
	$("#attrib").append('<input class=" input" id ="id_count_" type="hidden" value="'+_title+'" name="num">');



}


function imageScale(sender, max_size) {
    p=0;
    if (sender.width > max_size) {
        p = (max_size / sender.width);
    }
    else {
        if (sender.height > max_size) {
            p = (max_size / sender.height);
        }
    }
    if(p > 0) {
        sender.width = p * sender.width;
    }
}


function searchOver (what) {
    document.getElementById(what).className = 'HintHighlight';
    showHints();
}
function searchOut (what) {
    document.getElementById(what).className = 'HintUnHighlight';
}
function searchDown (str) {
    document.getElementById('search_query_words').value = str;
}
var timeout=1000;
var oldQuery='';
function hintMe() {
    query=document.getElementById('search_query_words').value;
    /*hint.className='searchDivHidden';*/
    document.getElementById('searchHint').style.visibility='visible';
    if (query != oldQuery) {
        oldQuery=query;
        if (query && query.length>3) {
            window.setTimeout('xajax_searchHints(query);',timeout);
        } else {
            hideHints();
        }
    }
}
function showHints() {
    query=document.getElementById('search_query_words').value;
    if (query && query.length>3) {
        hint=document.getElementById('searchHint');
        if (hint.style.visibility!='visible') hint.style.visibility='visible';
    }
}
function hideHints() {
    hint=document.getElementById('searchHint');
    hint.style.visibility='hidden';
}
function auto_off() {
if (s)
    alert(s.toString);
}

function hide_list(list)
{
    list.style.display='none';
}

function show_list(list)
{
    list.style.display='block';
}

function visible(list)
{
    if ((list.style.display=='block') || list.style.display=='')
    return true;

    return false;
}

function _$(id)
{
    return document.getElementById(id);
}

function ShowBox(id, ms, timer) {
    var timer=(timer==null) ? 0 : timer;
    var objref=document.getElementById(id);

    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    //  wysrodkowanie w pionie
    var topBoxOffset=scrOfY+(screen.availHeight/3)-(objref.offsetHeight/2);
    //	wysrodkowanie w poziomie
    var leftBoxOffset=scrOfX+(screen.availWidth/2)-(objref.offsetWidth/2);

    objref.style.top=topBoxOffset+"px";
    objref.style.left=leftBoxOffset+"px";
    if (timer < ms) {
        objref.style.display="block";
        timer+=10;
        setTimeout("ShowBox('"+id+"',"+ms+","+timer+")",1);
    } else {

        objref.style.display="none";

    }
}

function zagielCheckOrderValue(value, message) {
    if (value < 100) {
        window.alert(message);
        return false;
    } else {
        return true;
    }
}

function zagielProductCalcLink(shopNo, value, message) {

    if (zagielCheckOrderValue(value, message)) {
        window.open('https://www.zagiel.com.pl/kalkulator/index_smart.php?action=getklientdet_si_rata&shopNo='+shopNo+'&goodsValue='+value,'Policz_rate','width=630,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
    }
}
function pokazBaner(nr){
	$(".mainNew1Cont:visible").fadeOut("slow");
	$(".mainNew1Cont:eq("+nr+")").fadeIn("slow");
}
function przesunLewo(){
	tw=$("#baner-lista span.podswietl").attr("rel");
	cnt=$("#baner-lista span").length;
	if (cnt>0){
		$("#baner-lista span.podswietl").removeClass("podswietl");
		tw--;
		if (tw<0)
			tw=cnt-1;
		$("#baner-lista span:eq("+tw+")").addClass("podswietl");
		pokazBaner(tw);
	}
}
function przesunPrawo(){
	tw=$("#baner-lista span.podswietl").attr("rel");
	cnt=$("#baner-lista span").length;
	if (cnt>0){
		$("#baner-lista span.podswietl").removeClass("podswietl");
		tw++;
		if (tw>cnt-1)
			tw=0;
		$("#baner-lista span:eq("+tw+")").addClass("podswietl");
		pokazBaner(tw);
	}
}
function pokazGlowne(nr){
	$(".glowna-lista-tr:visible").fadeOut("slow");
	$(".glowna-lista-tr:eq("+nr+")").fadeIn("slow");
}
function przesunDol(){
	tw=$("#glowna-lista table tr.wyswietl").attr("rel");
	cnt=$("#glowna-lista table tr").length;
	if (cnt>0){
		$("#glowna-lista table tr.wyswietl").removeClass("wyswietl");
		tw++;
		if (tw>cnt-1)
			tw=0;
		$("#glowna-lista table tr:eq("+tw+")").addClass("wyswietl");
		pokazGlowne(tw);
	}
}

$().ready(function(){



	$("#next_stemp_order").click(function(){
		if($("#spr_rules:checked").length == 0){
			alert("Proszę znaznaczyć opcję: Zaświadczam, że zapoznałem się z regulaminem sklepu...");
			return false;
		}
		if($("#spr_zgoda:checked").length == 0){
			alert("Proszę znaznaczyć opcję: Wyrażam zgodę na przetwarzanie i przetrzymywanie moich danych...");
			return false;
		}

	});


if ($('.show_podcat').length > 0)
$('.box_tytul_karty:eq(1)').hide();

if ($('.forg').length > 0)
$('.box_tytul_karty:eq(1)').hide();

$('.kategoria_cechy').hide();

$('.button_wyszukiwarka').bind('click',function(){
	$('.szukaj').submit();	
});

$('#add_email').bind('click',function(){
	$('.dodaj').submit();		
});


$('#xml-do-koszyka').bind('click',function(){
	$('#attrib').submit();	
	$('form[action="/go/_basket/index.php"]').submit();
});
	
$('.karta_srodek div').addClass('maine-tab');
$('.karta_srodek .maine-tab .maine-tab').removeClass('maine-tab');

var $tabs= $('.karta_srodek .maine-tab');

var tabs = {
	init: function(){
		tabs.hideAll();
		$('.maine-tab:first', $tabs.parent()).show();
	},
	hideAll: function(){		
		$tabs.hide();
	},
	show: function(_class){
		$('#'+_class).show();
		$('div:hidden',$('#'+_class)).show();
	}
}
tabs.init();
$('.karta_opis, .karta_dane, .karta_komentarze').bind('click',function(){
	var _current = $(this).attr('class');	

	tabs.hideAll();
	tabs.show(_current);
	
});


var $container= $('#mainNew1');

	if ($container.length >0){
		$container.height($('img:first',$container).height());
	}
		

$('.input_wyszukiwarka, .input_newsletter').each(function() {
            var default_value = this.value;
            $(this).focus(function() {
                this.value = (this.value == default_value)? ''  : this.value;
            });
            $(this).blur(function() {
                this.value= (this.value== '') ? default_value   : this.value;
            });
        });



$("a[rel*=gb_imageset]").lightBox(); 
	/* drzewko kategorii */
	$("#kat-none p.toggler").click(function(){
		var divw=$(this).attr("rel");

		$("#kat-none #"+divw).toggle();
	});
	if ($.browser.msie && $.browser.version.indexOf("6.0")!=-1){
	//	alert("ie");
	  	$("#submit_proceed").click(function(){
	  //		$("form[name=basketForm]").append("<input type='hidden' name='submit_proceed' value='dalej'/>");
/*			var ee=$(this).attr('value');
			var en=$(this).attr('name');
		 //	alert(ee+" "+en+" ");
		   $("#submit_proceed2").val(ee).attr("name",en);*/
	
		});/*
		$("#submit_update").click(function(){

		   $("#submit_proceed2").remove();
			$("#submit_proceed").attr("name","stary_submit").remove();
			alert($("#submit_proceed").attr("name")+" "+$("#submit_proceed").val());
		});*/
	}
	/* banner na stronie glownej */
	t='<span id="baner-wybierajka"><span id="baner-lewo"></span><span id="baner-lista">';

	for (i=0;i<$(".mainNew1Cont").length;i++)	{
		t+='<span id="baner-guzik-'+i+'" rel="'+i+'">'+(i+1)+'</span>';	
	}
	t+='</span><span id="baner-prawo"></span></span>'

	$("#mainNew1").append(t);	
	$("#baner-guzik-0").addClass("podswietl");

	$("#mainNew1").hover(function(){

		window.clearInterval(timeee);
	},function(){

		timeee = window.setInterval('przesunPrawo()', 5000);
	});
	
	
	$("#info-top-tabelka form").submit(function(){
		
		var vl=$("#product-count").val();
		$(this).append("<input type='hidden' name='product-num' value='"+vl+"'/>");
		
	});
	
	$(".tabelka-element .border0").hide();


	$("#baner-lista span").click(function(){

		tw=$(this).attr("rel");
		$("#baner-lista span.podswietl").removeClass("podswietl");
		$(this).addClass("podswietl");
		pokazBaner(tw);		
	//window.clearInterval(timeee);
	 //	timeee = window.setInterval('przesunPrawo()', 5000);
	});
	
	$("#baner-lewo").click(przesunLewo);
	$("#baner-prawo").click(przesunPrawo);	

	/* automatyczne przesuwanie banneru co okreslony czas */
	timeee = window.setInterval('przesunPrawo()', 5000);
	$("#attrib select").each(function(){
		$(this).children("option:eq(0)").remove();
	  
	});
	
   /* zakladki od recenzji */
	$("#lista-menu strong").click(function(){
		var ee=$(this).attr("rel");
		$("#zakladki-tresc > div[rel!="+ee+"]").slideUp("fast",function(){
			$("#zakladki-tresc > div[rel="+ee+"]").slideDown("slow");		
		});
	});
	
	/* przycisk recenzji */
	$("#no-review-yet a").attr({"href":"#info-zakladki","onclick":""}).click(function(){
		$("#tab2").click();
	});
	
	/* formularz nowej recenzji - wyslanie */
	$("#reviewProductForm").submit(function(){
		er=$("#review-description").val();
		$("#sbl1").val(er);
		er=$("#review-author").val();
		$("#sbl2").val(er);
		
		var $ff=$("#reviewProductForm :input");
		var addr=$(this).attr("action");
		var tab= $ff.serialize();
		
		$("#wynik").slideUp("fast");
	 	$("#loader").slideDown("fast",
			function(){		
				$.post(addr,tab
					,function(content){						
						$("#loader").slideUp("fast");
						$("#wynik").html(content).slideDown("slow");
					}
				,"html");
	
				
			});
		
		return false;
		});
	$("#info-ocena img").hover(function(){
		$(this).prevAll().andSelf().each(function(){
			src1=$(this).attr("src");
			$(this).attr("rel",src1);
			src2=src1.replace("-pusta","");
			$(this).attr("src",src2);
		});
		$(this).nextAll().each(function(){
			src1=$(this).attr("src");
			$(this).attr("rel",src1);
			src2=src1.replace("ka.","ka-pusta.");
			$(this).attr("src",src2);
		});
	},function(){
		$(this).siblings().andSelf().each(function(){
			src1=$(this).attr("rel");
			$(this).attr("src",src1);
			$(this).attr("rel","");
		});
	}).click(function(){
		var ocena=$(this).index()+1;
		$("#score").val(ocena);
	});
	
	$(".glowna-lista-tr:eq(0)").addClass("wyswietl");
	timeee2 = window.setInterval('przesunDol()', 5000);
	
	$("#glowna-lista").hover(function(){
		window.clearInterval(timeee2);
	},function(){
		timeee2 = window.setInterval('przesunDol()', 5000);
	});
});

function setLinks(nr,limit){
	nr=parseInt(nr);
	limit=parseInt(limit);
	for (i=1;i<=limit;i++){
		var bf=_$("before"+i);
		if (bf) {
			if (i<nr)
				bf.rel="lytebox[g]";
			else
				bf.rel="";
		}
		
		var af=_$("after"+i);
		if (af) {
			if (i>nr)
				af.rel="lytebox[g]";
			else
				af.rel="";
		}
		
		
	}
	
		
}

