/**
 * Copyright (c) 2008 Logisoft Szymon Sawicki
 *
 * All rights reserved
 *
 */

var Logishop = 
{

	indicatorId : 'indicator',
	
	/*********************
	* POKAZ INDICATOR *
	*********************/
	showIndicator: function(msg)
	{
		// overlay
    	Logishop.showOverlay();
		var indicator = jQuery('#'+Logishop.indicatorId);
		var offset_top = -200;
		var offset_left = -160;
    
        var indicator_msg = jQuery('#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+offset_left+pg_offset[0])+'px');
		indicator.css('top', (win_center[1]/2+offset_top+pg_offset[1])+'px');
        if(indicator.css('visibility') == 'hidden')
          indicator.css('visibility', 'visible');
        
        jQuery('#indicator_spinner').show();
        jQuery('#indicator_cart_link').hide();
        indicator.show();		
        
	}, // showIndicator()
	
	
		/*********************
		 * SCHOWAJ INDICATOR *
		 *********************/
	hideIndicator : function ()
	{
		var indicator = jQuery('#'+Logishop.indicatorId);
		if(indicator)
		{
			indicator.hide();
	        Logishop.hideOverlay();
		}
	}, // hideIndicator()
	
	
	/**
	 * pokaż overlay
	 */
	showOverlay : function()
	{
		var objOverlay = document.getElementById('indicator_overlay');
		var pageSize = Logishop.pageSize();
		
		if(!objOverlay)
		{
			var objBody = document.getElementsByTagName("body").item(0);
			var objOverlay = document.createElement("div");
			objOverlay.setAttribute('id','indicator_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%';
			objBody.insertBefore(objOverlay, objBody.firstChild);
		}
		
		objOverlay.style.height = (pageSize[1] + 'px');
		objOverlay.style.display = 'block';

	}, // showOverlay()
	
	/**
	 * ukryj overlay
	 */
	hideOverlay : function()
	{
		var objOverlay = document.getElementById('indicator_overlay');
		if(objOverlay)
			objOverlay.style.display = 'none';
	}, // hideOverlay()
	
	/*********************
	* POKAZ PODSUMOWANIE W INDICATOR (dodawanie do koszyka) *
	*********************/
	showCompleteIndicator: function()
	{
		Logishop.showOverlay();
		var indicator = jQuery('#'+Logishop.indicatorId);
		var offset_top = -200;
		var offset_left = -160;

        var indicator_msg = jQuery('#indicator_msg');

        var msg = 'Wybrany <strong>produkt został dodany do koszyka</strong>. '
    		+'Możesz teraz zakończyć zakupy i <a href="/koszyk">przejść do koszyka</a> lub <a href="#" onclick="Logishop.hideIndicator();return false;">kontynuować zamawianie</a>. '
    		+'W każdej chwili możesz sprawdzić zawartość koszyka i dokonać zapłaty za zakupy klikając w ikonę "do kasy" na górze strony.';

        indicator_msg.html(msg);

        win_center = Logishop.winSize();
		pg_offset = Logishop.scrollSize();
		//alert(pg_offset[0]+' '+pg_offset[1]);
		indicator.css('left', (win_center[0]/2+offset_left+pg_offset[0])+'px');
		indicator.css('top', (win_center[1]/2+offset_top+pg_offset[1])+'px');
        if(indicator.css('visibility') == 'hidden')
          indicator.css('visibility', 'visible');
		jQuery('#indicator_cart_link').show();
		jQuery('#indicator_spinner').css('visibility', 'hidden');
		indicator.show();
	}, // showCompleteIndicator()
	
	
	//------------------------------------

	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;
  		jQuery('#'+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 = jQuery('#profile_register_firm');
		if(firm_check && !firm_check.attr('checked'))
		{
			jQuery('#profile_firm').val(''); 
			jQuery('#profile_nip').val(''); 
		} 
	},
	
	// wyczyść dane dostawy z formularza
	clearAddressDelivery: function()
	{
		address_delivery_check = jQuery('#profile_register_address_delivery');
		if(address_delivery_check && !address_delivery_check.attr('checked'))
		{
			jQuery('#profile_street_name_delivery').val('');
			//$('profile_street_no_delivery').value = ''; 
			//$('profile_home_no_delivery').value = ''; 
			jQuery('#profile_postal_code_delivery').val(''); 
			jQuery('#profile_city_delivery').val(''); 
		} 
	},
	
	// pokaż/ukryj kalkulator
	toggleCalculator: function(id)
	{
		//link = $('calculator_link_'+id);
		ob = document.getElementById('product_quantity_calculator_'+id);
		if(ob.style.display=='block')
			ob.style.display = 'none';
		else
		{
			ob.style.display = 'block';
			ob.style.position = 'absolute';
			ob.style.marginLeft = '25px';
			ob.style.marginTop = '-140px';
		}

		return false;
	}
	
} // end Logishop
