/**
 * Copyright (c) 2008 Logisoft Szymon Sawicki
 *
 * All rights reserved
 *
 */
var Logishop = 
{
	indicatorId : 'indicator',
	
	/*********************
	* POKAZ INDICATOR *
	*********************/
	showIndicator: function(msg, overlay)
	{
		var indicator = $('#'+Logishop.indicatorId);
    
        var indicator_msg = $('#indicator_msg');
		
        indicator_msg.html('Proszę czekać - trwa '+msg);

		win_center = Logishop.winSize();
		pg_offset = Logishop.scrollSize();
		//alert(pg_offset[0]+' '+pg_offset[1]);
		indicator.css('left', (win_center[0]/2-195+pg_offset[0])+'px');
		indicator.css('top', (win_center[1]/2-150+pg_offset[1])+'px');
        if(indicator.css('visibility') == 'hidden')
          indicator.css('visibility', 'visible');
		
		$('#indicator_spinner').show();
        $('#indicator_cart_link').hide();
        $('#indicator_close_link').show();
        indicator.show();
        
        if(overlay)
        	Logishop.showOverlay();	
		
	}, // showIndicator()
	
	
		/*********************
		 * SCHOWAJ INDICATOR *
		 *********************/
	hideIndicator : function ()
	{
		if($('#'+Logishop.indicatorId))
		{
			$('#'+Logishop.indicatorId).hide();
			Logishop.hideOverlay();
		}
		return false;
	}, // hideIndicator()
	
	

	/*********************
	* POKAZ PODSUMOWANIE W INDICATOR (dodawanie do koszyka) *
	*********************/
	showCompleteIndicator: function(msg, type)
	{
		var indicator = $("#"+Logishop.indicatorId);
		indicator.stop();
    
        var indicator_msg = $('#indicator_msg');
        
        if(!msg)
        	 msg = 'Wybrany produkt został dodany do koszyka.<div style="height:10px;"></div>';

		
        indicator_msg.html('<br/>'+msg);

		win_center = Logishop.winSize();
		pg_offset = Logishop.scrollSize();
		//alert(pg_offset[0]+' '+pg_offset[1]);
		indicator.css('left', (win_center[0]/2-195+pg_offset[0])+'px');
		indicator.css('top', (win_center[1]/2-150+pg_offset[1])+'px');
        if(indicator.css('visibility') == 'hidden')
          indicator.css('visibility', 'visible');

        $('#indicator_cart_link').show();
		$('#indicator_spinner').hide();
		$('#indicator_close_link').hide();
		indicator.show();
		
		if(type=='l')
		{
			$('#indicator_later_button').show();
			$('#indicator_cart_button').hide();
		}
		else
		{
			$('#indicator_later_button').hide();
			$('#indicator_cart_button').show();
		}
		
		Logishop.showOverlay();
	}, // showCompleteIndicator()
	

	/**
	 * pokaż overlay
	 */
	showOverlay : function()
	{
		$('embed, object, select').css({ 'visibility' : 'hidden' });
		var objOverlay = document.getElementById('overlay');
		var pageSize = Logishop.pageSize();
		
		if(!objOverlay)
		{
			var objBody = document.getElementsByTagName("body").item(0);
			var objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','overlay');
			objOverlay.onclick = function () {Logishop.hideIndicator(); return false;}
			objOverlay.style.display = 'none';
			objOverlay.style.position = 'absolute';
			objOverlay.style.top = '0';
			objOverlay.style.left = '0';
			objOverlay.style.zIndex = '90';
		 	objOverlay.style.width = '100%';
		 	objOverlay.style.backgroundColor = '#000';
		 	objOverlay.style.opacity = '0.8';
			objBody.insertBefore(objOverlay, objBody.firstChild);
		}
		
		objOverlay.style.height = (pageSize[1] + 'px');
		$(objOverlay).css({
			backgroundColor:	'#000',
			opacity:			'0.8'
		}).show();

	}, // showOverlay()
	
	/**
	 * ukryj overlay
	 */
	hideOverlay : function()
	{
		var objOverlay = document.getElementById('overlay');
		if(objOverlay)
			objOverlay.style.display = 'none';
	}, // hideOverlay()
	
	
	//------------------------------------

	winSize : function () {
		var x,y;
		// wszystko poza IE
		if (window.innerHeight) {
			x = window.innerWidth;
			y = window.innerHeight;
		}
		// IE 6 Strict Mode
		else if (document.documentElement && document.documentElement.clientHeight) {
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		// pozostale IE
		else if (document.body) {
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return new Array (x, y);
	},

	scrollSize : function  () {
		var x,y;
		// wszystko poza IE
		if (self.pageYOffset || self.pageXOffset) {
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		// IE 6 Strict
		else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		// pozostale IE
		else if (document.body) {
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		return new Array (x, y);
	},	
		
	viewSize : function  ()
	{
		var x,y;
		// wszystko poza IE
		if (self.innerHeight) {
			x = self.innerWidth;
			y = self.innerHeight;
		}
		// IE 6 Strict Mode
		else if (document.documentElement && document.documentElement.clientHeight) {
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		// pozostale IE
		else if (document.body) {
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return new Array (x, y);
	},
	

	pageSize : function() 
	{
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}


		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
		
	}, // pageSize()

	popUp : function(url, name, left, top, width, height, scrollbars) 
	{
	    window.open(url, name, "left="+left+",top="+top+",width="+width+",height="+height+",scrollbars="+scrollbars);
	    return false;		
	},
	
	updateAssortmentData : function (url, editor_id, target_area, type)
	{
		new Ajax.Request(
			url, 
			{
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(request, json)
				{
					try
					{
						tinyMCE.execInstanceCommand(editor_id, 'mceSetContent', false, request.responseText);
						Logishop.hideIndicator();
						new Effect.Highlight(target_area, {});
					}
					catch(e)
					{
						alert(e);
					}
				}, 
				onLoading:function(request, json)
				{
					Logishop.showIndicator('Pobieranie opisu z asortymentu ...');
				}, 
				parameters:'assortment_id=' + $('#product_assortment_id').value + '&type='+type
			}
		);
	},

	printContent : function(url, _w, _h)
	{
		var h=700;
		var w=800;
		if(_h)
		{
			h = _h;
		}
		if(_w)
		{
			w = _w;
		}
		window.open(url,  "printable_window", "scrollbars=1, left=100, top=100, width="+w+", height="+h);
	},
	
	toggleElement: function(element_id, duration)
	{
    	if(!duration)
    		duration = 500;
  		$('#'+element_id).slideToggle(duration);
	  	return false;
	},
	
	fancySubmit: function(select)
	{
		value = select.options[select.selectedIndex].value;
		form = select.form;
		
		//form.action += value;
		this.showIndicator('przekierowanie do producenta');
		window.location=form.action + value;
		//form.submit();
	},
	
	// wyczyść firmę z formularza
	clearFirm: function()
	{
		firm_check = $('#profile_register_firm');
		if(firm_check && !firm_check.checked)
		{
			$('#profile_firm').val(''); 
			$('#profile_nip').val(''); 
		} 
	},
	
	// wyczyść dane dostawy z formularza
	clearAddressDelivery: function()
	{
		address_delivery_check = $('#profile_register_address_delivery');
		if(address_delivery_check && !address_delivery_check.checked)
		{
			$('#profile_street_name_delivery').val('');
			$('#profile_street_no_delivery').val(''); 
			if($('#profile_home_no_delivery'))
				$('#profile_home_no_delivery').val(''); 
			$('#profile_postal_code_delivery').val(''); 
			$('#profile_city_delivery').val(''); 
		} 
	},
	
	calculatePrice: function(ob, netto_field, brutto_field, tax_rate)
	{
		var price_netto = $('#'+netto_field).val(); 
		var price_brutto = $('#'+brutto_field).val(); 

		if(ob.id==netto_field)
	    {
		    if(isNaN(price_netto.replace(",", ".")))
		    {
		    	$('#'+brutto_field).val(0);
		    }
		    else
		    {
				$('#'+brutto_field).val(Math.round(price_netto.replace(",", ".") * (100+parseInt(tax_rate))) / 100);
		    } 
	    }
	    else
	    {
		    if(isNaN(price_brutto.replace(",", ".")))
		    {
		    	$('#'+brutto_field).val(0);
		    }
		    else
		    {
				$('#'+netto_field).val( Math.round(price_brutto.replace(",", ".")*100000 / (100+parseInt(tax_rate)))/1000);
		    } 
		}
	}
	
} // end Logishop


/**
 * 
 * klasa do zdarzeń związanych z asortymentem
 * 
 */
var LgsAssortment = 
{
	imageFader: null,	

		/******************************************
		 * zamiana aktywnego obrazka (micro->midi)
		 ******************************************/
		 
	switchImage: function (no)
	{
		var i=0;

		while(image_div = document.getElementById('assortment_image_'+i))
		{
			image_div.style.display = 'none';
			i++;
		}
		document.getElementById('assortment_image_'+no).style.display = 'block';

	},
	
		/************************************
		 * przelaczanie zakladek w produkcie
		 ************************************/
		 
	switchTab : function (container_id, tab, object)
	{
		tabs = $('assortment_tabs').getElementsByTagName('a');
			
		for(i=0; tabs[i]; i++)
			tabs[i].className = '';
		object.className = 'on';
		object.blur();
		
		divs = document.getElementsByClassName("assortment_tab_content");
		for(i=0; divs[i]; i++)
			Element.hide(divs[i]);
		Element.show($('tab_'+tab));
	},
	
			/***************************************
			 * czyszczenie filtru wyszukiwania
			 ***************************************/
	
	clearFilter: function()
	{
		$('filter_price_from').value = '';
		$('filter_price_to').value = '';
		$('filter_name').value = '';
		params = $('filter_content').getElementsByTagName('select');
		for(i=0; params[i]; i++)
		{
			options = params[i].getElementsByTagName('option');
			for(j=0; options[j]; j++)
				options[j].selected = '';
			options[0].selected = 'selected';
		}
	} // end clearFilter()
	
} // LgsAssortment


/**
 * AssortmentScore
 * Copyright (c) 2008 Logisoft Szymon Sawicki
 *
 * All rights reserved
 *
 */

  imgExt = '.png';
  var STARS_RATING_IMG = '/images/stars/star_full'+imgExt;
  var STARS_RATING_IMG_HALF = '/images/stars/star_half_19x19'+imgExt;
  var STARS_RATING_IMG_BG = '/images/stars/star_empty'+imgExt;


var AssortmentScore = function(maxStars, objectName, ratingMessageId)
{
    this.maxStars = maxStars;
    this.objectName = objectName;
    this.ratingMessageId = ratingMessageId
    this.messages = new Array("Oceń produkt!", "Beznadziejny", "Słaby", "Przyzwoity", "Warto kupić", "Bardzo dobry", "Fantastyczny");
  
    this.starTimer = null;
    this.starCount = 0;
  
    // pre-fetch image
    (new Image()).src = STARS_RATING_IMG;
    (new Image()).src = STARS_RATING_IMG_HALF;
  
    function showStars(starNum, skipMessageUpdate)
    {
      this.clearStarTimer();
      this.greyStars();
      this.colorStars(starNum);
      if(!skipMessageUpdate)
        this.setMessage(starNum, this.messages);
    }
  
    function setMessage(starNum)
    {
      document.getElementById(this.ratingMessageId).innerHTML = this.messages[starNum];
    }
  
    function colorStars(starNum)
    {
      for (var i=0; i < starNum; i++) {
        document.getElementById('star__' + (i+1)).src = STARS_RATING_IMG;
      }
    }
  
    function greyStars()
    {
      for (var i=0; i < this.maxStars; i++)
        if (i <= this.starCount) {
          document.getElementById('star__'  + (i+1)).src = STARS_RATING_IMG_BG;
        }
        else
        {
          document.getElementById('star__'  + (i+1)).src = STARS_RATING_IMG_BG;
        }
    }
  
    function setStars(starNum)
    {
      this.starCount = starNum;
      this.drawStars(starNum);
    }
  
  
    function drawStars(starNum, skipMessageUpdate)
    {
      this.starCount=starNum;
      this.showStars(starNum, skipMessageUpdate);
    }
  
    function clearStars()
    {
      this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
    }
  
    function resetStars()
    {
      this.clearStarTimer();
      if (this.starCount)
        this.drawStars(this.starCount);
      else
        this.greyStars();
      this.setMessage(0);
    }
  
    function clearStarTimer()
    {
      if (this.starTimer) {
        clearTimeout(this.starTimer);
        this.starTimer = null;
      }
    }
  
    this.clearStars = clearStars;
    this.clearStarTimer = clearStarTimer;
    this.greyStars = greyStars;
    this.colorStars = colorStars;
    this.resetStars = resetStars;
    this.setStars = setStars;
    this.drawStars = drawStars;
    this.showStars = showStars;
    this.setMessage = setMessage;
} // end AssortmentScore


PromoCounter = function init(dateId, endMsg) 
{
	var interval, h, m, s;
	this.endMsg = endMsg;
	this.dateEnd = new Date();
	this.dateElement = document.getElementById(dateId);
	if ((interval = $(this.dateElement).html())) {
		interval = interval.split(':');
		if (interval.length >= 3) {
			this.dateEnd.setSeconds(this.dateEnd.getSeconds() + parseInt(interval[2], 10));
			this.dateEnd.setMinutes(this.dateEnd.getMinutes() + parseInt(interval[1], 10));
			this.dateEnd.setHours(this.dateEnd.getHours() + parseInt(interval[0], 10));
		}
		this.update();
		this.intervalId = window.setInterval(dateId+'.update()', 1000);
	}
};

PromoCounter.prototype.update = function () 
{
	var delta, n, days, interval = [0, 0, 0], dateNow = new Date();
	delta = this.dateEnd.getTime() - dateNow.getTime(); //ilość milisekund
	if (delta > 0) {
		delta /= 1000;
		n = Math.floor(delta) % 60;
		interval[2] = (n > 9 ? String(n) : '0' + n); //ilość sekund
		delta /= 60;
		n = Math.floor(delta) % 60;
		interval[1] = (n > 9 ? String(n) : '0' + n); //ilość minut
		delta /= 60;
		interval[0] = Math.floor(delta); //ilość godzin
		days = Math.floor(interval[0] / 24); //ilość dni
		if (days > 1) {
			this.dateElement.firstChild.nodeValue = days + ' dni';
		} else if (days > 0) {
			this.dateElement.firstChild.nodeValue = '1 dzień';
		} else {
			this.dateElement.firstChild.nodeValue = interval.join(':');
		}
	} else {
		this.dateElement.firstChild.nodeValue = this.endMsg;
		clearInterval(this.intervalId);
	}
	this.dateElement.style.display = 'inline-block';
}

$(document).ready(function(){
	$(".jscl_polecamy").click(function() {
		$("#jsid_polecamy").css('display','block');
		$("#jsid_promocje").css('display','none');
		$("#jsid_nowosci").css('display','none');
	});
	$(".jscl_polecamy").filter(".pass").hover(function(){
		$(".jscl_polecamy").css('cursor','pointer');
		$(".jscl_polecamy").css('text-decoration','underline');
	},function(){
		$(".jscl_polecamy").css('text-decoration','none');
		$(".jscl_polecamy").css('cursor','auto');
	});
	$(".jscl_promocje").click(function() {
		$("#jsid_polecamy").css('display','none');
		$("#jsid_promocje").css('display','block');
		$("#jsid_nowosci").css('display','none');
	});
	$(".jscl_promocje").filter(".pass").hover(function(){
		$(".jscl_promocje").css('text-decoration','underline');
		$(".jscl_promocje").css('cursor','pointer');
	},function(){
		$(".jscl_promocje").css('text-decoration','none');
		$(".jscl_promocje").css('cursor','auto');
	});
	$(".jscl_nowosci").click(function() {
		$("#jsid_polecamy").css('display','none');
		$("#jsid_promocje").css('display','none');
		$("#jsid_nowosci").css('display','block');
	});
	$(".jscl_nowosci").filter(".pass").hover(function(){
		$(".jscl_nowosci").css('text-decoration','underline');
		$(".jscl_nowosci").css('cursor','pointer');
	},function(){
		$(".jscl_nowosci").css('text-decoration','none');
		$(".jscl_nowosci").css('cursor','auto');
	});

});

$(document).ready(function(){
	
	/* tab description */
	if($(".jscl_description"))
	{
		$(".jscl_description").click(function() {
			$("#jsid_description").css('display','block');
			$("#jsid_opinion").css('display','none');
			if($("#jsid_parameters"))
				$("#jsid_parameters").css('display','none');
		});
		$(".jscl_description").filter(".pass").hover(function(){
			$(".jscl_description").css('cursor','pointer');
			$(".jscl_description").css('text-decoration','underline');
		},function(){
			$(".jscl_description").css('text-decoration','none');
			$(".jscl_description").css('cursor','auto');
		});
	}
	
	/* tab opinion */
	if($(".jscl_opinion"))
	{
		$(".jscl_opinion").click(function() {
			$("#jsid_description").css('display','none');
			$("#jsid_opinion").css('display','block');
			if($("#jsid_parameters"))
				$("#jsid_parameters").css('display','none');
		});
		$(".jscl_opinion").filter(".pass").hover(function(){
			$(".jscl_opinion").css('text-decoration','underline');
			$(".jscl_opinion").css('cursor','pointer');
		},function(){
			$(".jscl_opinion").css('text-decoration','none');
			$(".jscl_opinion").css('cursor','auto');
		});
	}
	
	/* tab parameters */
	if($(".jscl_parameters"))
	{
		$(".jscl_parameters").click(function() {
			$("#jsid_description").css('display','none');
			$("#jsid_opinion").css('display','none');
			$("#jsid_parameters").css('display','block');
		});
		$(".jscl_parameters").filter(".pass").hover(function(){
			$(".jscl_parameters").css('text-decoration','underline');
			$(".jscl_parameters").css('cursor','pointer');
		},function(){
			$(".jscl_parameters").css('text-decoration','none');
			$(".jscl_parameters").css('cursor','auto');
		});
	}

});

