$(document).ready(function() {
	var lightboxArr = new Array() 
	$('a[rel^=lightbox]').each(function(){

		if(jQuery.inArray($(this).attr('rel'),lightboxArr)<0) {
			lightboxArr.push($(this).attr('rel')); 
		}
	});
	
	jQuery.each(lightboxArr,function(){
		$('a[rel^='+this+']').lightBox();
	});


	if(jQuery('.show_product_options').length>0){

		change_prices();

		jQuery('.show_product_options').change(function(){

			change_prices();

		});
	}

	//ajaxowe divy
	try {
		jQuery('.popup').nyroModal();
	} catch(err)  {
	}
	
	$("#search_categories_select").change(function(){
		$("#search_attributes_div").html('<div style="width:100%;overflow:hidden;"><img src="themes/ajax-loader.gif" style="display:block;margin:30px auto 30px auto;" /></div>');
		$.ajax({
			url: "index.php?do=stale_cechy_ajax&cat="+$(this).val(),
			success: function(data){
				$("#search_attributes_div").html(data);
			}
		});
	});

	//ajax dla odzyskiwania hasla na 3 kroku kosza
	$("form[name='odzysk']").submit(function(event) {
		event.preventDefault();
		var $form = $( this ),
		term = $form.find( 'input[name="form_email"]' ).val(),
		url = $form.attr( 'action' );
 
		var test = {
			email:	function (input) {
				return ( (typeof input == 'string' ? input : input.value).length < 6 || !/^.+@.+\.[a-z0-9]{2,6}$/i.test( (typeof input == 'string' ? input : input.value) ) ? false : true);
			}
		};
		if (test.email(term))
		{
			$( "p.passwordForgot" ).html(  'Trwa sprawdzanie adresu email... <img src="themes/ajax-loader.gif"> ' );
			$.post( url, {
				form_email: term,
				odz_hasla: 'true'
			} ,
			function( data ) {
				var content = $( data ).find( '.pole_message' ).contents();
				if (content.length == 0) var content = $( data ).find( '.pole_message_err' ).contents();
				$('p.passwordForgot').html(content);
				alert ( content.text());
			}
			);
		}
		else alert('Niepoprawny adres email!');
	});
          
	//ajax do dodawania/usuwania do przechowalni  z list produktow
	$("span.storage_list_add").click(function(event) {
		event.preventDefault();
		var $a = $( this ),
		url = $a.attr( 'href' ),
		id  = $a.attr('id');
		$( "span#"+id ).html(  'Zapisuję... <img src="themes/ajax-loader.gif"> ' );

		$.get( url, 
			function( data ) {
				var content = $( data ).find( '.pole_message' ).contents();
				if (content.length == 0) var content = $( data ).find( '.pole_message_err' ).contents();
				$("span#"+id ).html(content.text());
			}
			);

	});
	$("span.storage_list_del").click(function(event) {
		event.preventDefault();
		var $a = $( this ),
		url = $a.attr( 'href' ),
		id  = $a.attr('id');
		$( "span#"+id ).html(  'Usuwam z listy... <img src="themes/ajax-loader.gif"> ' );

		$.get( url,
			function( data ) {
				var content = $( data ).find( '.pole_message' ).contents();
				if (content.length == 0) var content = $( data ).find( '.pole_message_err' ).contents();
				$("span#"+id ).html(content.text());
			}
			);

	});
	
	
	

	$('.compare_add').click(function(event){
		event.preventDefault();
		var button = $(this);

		id = button.attr('data-id');
		if(id>0)
		{
			cookie = $.cookie('compare');

			if(cookie != null)
			{
				ids_ = cookie.split(',')
			} else {
				ids_ = [];
			}

			id_num = Number(id);
			if($.inArray(id, ids_)<0 && !isNaN(id_num) && id_num>0)
			{
				ids_.push(id);
				ids = ids_.join(',');
				$.cookie('compare',ids);
				box_replace('compare');
			}

		
		}
	})



	$('.compare_remove_all').live('click', function(){
		$.cookie('compare',null);
		box_replace('compare');
	});

	$('.compare_remove').live('click',function(event){
		cookie = $.cookie('compare');

		var button = $(this);

		id = button.attr('data-id');
		if(cookie != null)
		{
			ids_ = cookie.split(',')
		} else {
			ids_ = [];
		}				alert('fsd');
		index = $.inArray(id,ids_ );
		if(index>=0)
		{
			ids_.splice(index,1);
			ids = ids_.join(',');
			$.cookie('compare',ids);
			box_replace('compare');
		}
	});


	
});


	function box_replace(name)
	{
		$.ajax({
			url: 'menubox/'+name,
			beforeSend: function(){
				$("body").css("cursor", "progress");
			},
			success: function(content){
				$("body").css("cursor", "default");
				$('.'+name+'-box').replaceWith(content);
			}
		});
	}


/*
  *
  */

/*  przykładowe dane w szablonie
             *  <span id="price" >[%price%]</span>
                <span id="price_net" >[%price_net%]</span>
                <span id="price_formated">[%price_formated%]</span>
                <span id="price_formated_net">[%price_formated_net%]</span>
                <span id="prom_price">[%prom_price%]</span>
                <span id="prom_price_net">[%prom_price_net%]</span>
                <span id="prom_price_formated">[%prom_price_formated%]</span>
                <span id="prom_price_formated_net">[%prom_price_formated_net%]</span>
             */
function change_prices() {

	var tmp_price_add=0;
	var tmp_price_set=0;

	jQuery('select.show_product_options option:selected').each(function(){

		str=jQuery(this).html().replace(/\s|&nbsp;/g,'').match(/\(.*\)/g)+'';

		if(str=='null') return null;

		is_add=str.search(/\+/);
		is_rem=str.search(/\-/);

		str=str.match(/\d+\.?\d*/g)+'';
		number=parseFloat(str.replace(/,/g,'.'));
	 
		if(number!=0){
			if(is_add>0)
				tmp_price_add+=number;
			else if(is_rem>0)
				tmp_price_add-=number;
			else
				tmp_price_set=number;

		} else return null;

	})



	//jeżeli ustawiono cene w cechach powiązanych
	if(tmp_price_set>0){

		//jezeli produkt ma cenę promocyjną
		if(prom_price>0) {
			vat_value=((show_product_tax*(tmp_price_set+tmp_price_add))/(show_product_tax+100)).toFixed(2);
			netto=(tmp_price_set+tmp_price_add-vat_value);
			netto=parseFloat(netto.toFixed(2));

			jQuery('#prom_price').html((tmp_price_set+tmp_price_add).toString().replace(/\./g,','));
			jQuery('#prom_price_net').html(netto.toString().replace(/\./g,','));
			jQuery('#prom_price_formated').html((tmp_price_set+tmp_price_add).number_format());
			jQuery('#prom_price_formated_net').html(netto.number_format());

		} else {

			vat_value=((show_product_tax*(tmp_price_set+tmp_price_add))/(show_product_tax+100)).toFixed(2);
			netto=(tmp_price_set+tmp_price_add-vat_value);
			netto=parseFloat(netto.toFixed(2));

			jQuery('#price').html((tmp_price_set+tmp_price_add).toString().replace(/\./g,','));
			jQuery('#price_net').html(netto.toString().replace(/\./g,','));
			jQuery('#price_formated').html((tmp_price_set+tmp_price_add).number_format());
			jQuery('#price_formated_net').html(netto.number_format());
		}

	} else if(tmp_price_set==0) {

		//jezeli produkt ma cenę promocyjną
		if(prom_price>0){

			vat_value=((show_product_tax*(prom_price+tmp_price_add))/(show_product_tax+100)).toFixed(2);
			netto=(prom_price+tmp_price_add-vat_value);
			netto=parseFloat(netto.toFixed(2));
			jQuery('#prom_price').html((prom_price+tmp_price_add).toString().replace(/\./g,','));
			jQuery('#prom_price_net').html(netto.toString().replace(/\./g,','));
			jQuery('#prom_price_formated').html((prom_price+tmp_price_add).number_format());
			jQuery('#prom_price_formated_net').html(netto.number_format());

		} else {

			vat_value=((show_product_tax*(price+tmp_price_add))/(show_product_tax+100)).toFixed(2);
			netto=(price+tmp_price_add-vat_value);
			netto=parseFloat(netto.toFixed(2));

			jQuery('#price').html((price+tmp_price_add).toString().replace(/\./g,','));
			jQuery('#price_net').html(netto.toString().replace(/\./g,','));
			jQuery('#price_formated').html((price+tmp_price_add).number_format());
			jQuery('#price_formated_net').html(netto.number_format());

		}
	}
}


