var refreshTime=25;
var descrMovement;
var prod1Change;
var prod2Change;
var mainRotateTime=4000;

function setHeight(){
var hajt1=document.body.clientHeight;
var width1=document.body.clientWidth;
var hajt2=hajt1-93-19;
var hajt=hajt1-19;
if (hajt2!=null) $("#content").css({height:hajt2});
if (hajt!=null) $("#bottomMenu").css({position:'absolute',width:width1,top:hajt+'px',left:0});

 //test=$("a.recImg/img").css('height');

}
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();
	 setHeight();
    /**
    * wyłączamy autocomplete zeby nie kolidowac z hintami ajaxowymi
    * _i_ zachowac zgodnosc z xhtml
    */
  //  var s = document.getElementById('search_query_words');
//    s.setAttribute("autocomplete", "off");
}

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');
    }
}


/* przesuwanie opisu produktu */
function scrollStop(){
	clearInterval(descrMovement);	

}

function scrollUp(){
var descr1=$("#infoDescrScroll");

	top1=parseInt(descr1.css("top"));
	//height1=parseInt(descr1.css("height"));
	height1=descr1.height();
	if (top1>154-height1){
		top1--;
		descr1.css("top",top1);	
	}
}
function scrollDown(){
	var descr1=$("#infoDescrScroll");
 	top1=parseInt(descr1.css("top"));
  //	height1=parseInt(descr1.css("height"));
	if (top1<0){
		top1++;
		descr1.css("top",top1);	

	}
}
function scrollDescrUp(){
		descrMovement = window.setInterval("scrollUp()", refreshTime);
}
function scrollDescrDown(){
  	descrMovement = window.setInterval("scrollDown()",refreshTime);
}



/* zmiana zdjecia w opisie produktu */
function changePhoto(){
	var infoImgs=$(".infoImages");
	var infoCount=infoImgs.length;
	var shown=$("#infoImage > span").index( $(".infoImages:visible")[0] );

  	if (shown==-1){
		shown=0;
		infoImgs.eq(shown).css({visibility:"visible"});
	}
	else{
		infoImgs.eq(shown).css({visibility:"hidden"});
		shown++;
		if (shown>=infoCount)
			shown=0;
	 	infoImgs.eq(shown).css({visibility:"visible"});
	}

 //	alert(shown.index+" "+infoCount);
}

/* zmiana zdjecia na stronie glownej */
function stopProd1(){
	clearInterval(prod1Change);	
}
function startProd1(){
		prod1Change = window.setInterval("rotateProd1()", mainRotateTime);
}
function rotateProd1(){
	var infoImgs1=$("#prod1 .rotateProd");
	var infoCount=infoImgs1.length;
	var shown=$("#prod1 > div").index( $(".rotateProd:visible")[0] );

  	if (shown==-1){
		shown=0;
		infoImgs1.eq(shown).css({visibility:"visible"});
	}
	else{
		infoImgs1.eq(shown).css({visibility:"hidden"});
		shown++;
		if (shown>=infoCount)
			shown=0;
	 	infoImgs1.eq(shown).css({visibility:"visible"});
	}

 //	alert(shown.index+" "+infoCount);
}
function stopProd2(){
	clearInterval(prod2Change);	
}
function startProd2(){
		prod2Change = window.setInterval("rotateProd2()", mainRotateTime);
}
function rotateProd2(){
	var infoImgs2=$("#prod2 .rotateProd2");
	var infoCount=infoImgs2.length;

	var shown=$("#prod2 > div").index( $(".rotateProd2:visible")[0] );
	
  	if (shown==-1){
		shown=0;
		infoImgs2.eq(shown).css({visibility:"visible"});
	}
	else{
		infoImgs2.eq(shown).css({visibility:"hidden"});
		shown++;
		if (shown>=infoCount)
			shown=0;
	 	infoImgs2.eq(shown).css({visibility:"visible"});
	}

 //	alert(shown.index+" "+infoCount);
}

/* funkcja poczatkowa */
$().ready(function()
	{
	
	var winParams='width=400,height=432';
	var winName='photo';

		$("body").resize(setHeight());
		var smallPhoto=$("#productList > a");
		smallPhoto.hover(function(e) 
			{ 
				idLarge=$(this).attr("id").replace("Small","Large");
			 //	alert("#"+idLarge);
				$("#"+idLarge).css("left","-240px");//left="-240px";//({left:"-240px"});
			},function(e) 
			{ 
				idLarge=$(this).attr("id").replace("Small","Large");
				$("#"+idLarge).css("left","-1240px");//$("#"+idLarge).attr({left:"-1240px"});

			});
		var heig=$("#infoDescrScroll").height();
		
		if (heig>154){
	 //	alert(heig);
			$("#infoDescrScroller").css({display:"block"});
			$("#descrScrollUp").hover(function(e) 
			{ scrollDescrDown()},function(e) 
			{ scrollStop()});
			$("#descrScrollDown").hover(function(e) 
			{ scrollDescrUp()},function(e) 
			{ scrollStop()});
			
		}
		
		var infoImgs=$(".infoImages");
		if (infoImgs.length>1){
			
			window.setInterval("changePhoto()", 2000);
		}
		
		var rotateProd1=$("#prod1 .rotateProd");
			if (rotateProd1.length>1){
				rotateProd1.eq(0).css({visibility:"visible"});
				startProd1();
				$("#prod1").hover(function(e) {stopProd1()},function(e) {startProd1()} );
			
			}
		var rotateProd2=$("#prod2 .rotateProd2");
			if (rotateProd2.length>1){
			//	rotateProd2[0].css({visibility:"visible"});
				rotateProd2.eq(0).css({visibility:"visible"});
				startProd2();
				$("#prod2").hover(function(e) {stopProd2()},function(e) {startProd2()} );
			
			}
		var baskOptions=$("#attrib .selectR");
		if (baskOptions.length>0) {
			
			$("#infoPrice").css({top:((baskOptions.height()+1)*baskOptions.length+6)+"px"})
		//	$("#infoAvail").css({top:((baskOptions.height()+1)*baskOptions.length+20)+"px"})
			}
		for (n=0;n<baskOptions.length;n++){
/*			if (n==0) 
				t="";
			else
				t=n;*/
  			p=document.getElementById('prodChange'+(n==0?"":n));
			if (p) p.remove(0);
		}

		
		var infoZoom=$("a#zoomImg");	
	  	if (jQuery.browser.msie){
			infoZoom.attr("target","photo");
			infoImgs.each(function(e) 	{
				$(this).click(function(e) 	{
//				alert($("#zoomImg").href+" "+winName+" "+winParams )
					window.open(infoZoom.attr("href"),'photo',winParams);
					return false;
					});
			  	});	
		 }
	 //	 else{
			infoZoom.click(function(e) 	{
					window.open(infoZoom.attr("href"),'photo',winParams);
					return false;
					});	
			  //	}
	});
