/*
 * cStore
 */

window.alert = function(text) {
	$("body").append("<div class=\"pole_message_info\">"+text+"</div>");
	$("#blackout").css("display","block");
	wysoko=$("body").css("height");
	if(wysoko=="auto") {
		wysoko=$(document).height();
	}
	$("#blackout").css("height",wysoko);
	$('html, body').animate({scrollTop:0}, 'fast');
	$(".pole_message_info").click(function(){
		$(".pole_message_info").fadeTo('fast','0',function(){
			$(".pole_message_info").css("display","none");
			if($(".pole_message_info").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){

			}else{
				$("#blackout").css("display","none");
			}
			$(".pole_message_info").remove();
		});
	});
}



$(document).ready(function () {


    if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
		$("#blackout").css("display","block");
		wysoko=$("body").css("height");
		if(wysoko=="auto") {
			wysoko=$(document).height();
		}
		$("#blackout").css("height",wysoko);
		$('html, body').animate({scrollTop:0}, 'fast');

	}
	$(".pole_message_err").click(function(){
		$(".pole_message_err").fadeTo('fast','0',function(){
			$(".pole_message_err").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){

			}else{
				$("#blackout").css("display","none");
			}
		});
	});
	$(".pole_message").click(function(){
		$(".pole_message").fadeTo('fast','0',function(){
			$(".pole_message").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){

			}else{
				$("#blackout").css("display","none");
			}
		});
	});
	$(".pole_message_info").click(function(){
		$(".pole_message_info").fadeTo('fast','0',function(){
			$(".pole_message_info").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){

			}else{
				$("#blackout").css("display","none");
			}
		});
	});











/*
 * listy typy
 * żeby ikon było wiele
 */
    var i = 0;
    $(".productList_Type").each(function(){
        $(this).html($(this).html().replace(/p_list/gi,'p_list'+i));
        i++;
    })

	/**
	 * IEFix < 8
	 *
	 * dodaje clear div'a(<div class="clearFix"> </div>) po elemencie z clasa "clear"
	 */
	if( $.browser.msie == true && parseInt(jQuery.browser.version) < 8 )
	{

		try {
			$('.clear').each( function () {

				try {	$(this).append('<div class="clearFix"> </div>');	}
				catch(err)	{ }
			});
		}
		catch(err)	{ }
	}

	/**
	 * Automatycznie czyszczenie inputa z zapamientanym stanem podstawowym
	 */
	$('input.autoClearInput').each( function (k,e){
		$(e).attr('data-valuedefault', $(e).attr('value') );
		$(e).blur(function () {
			if( $(this).val() == undefined || $(this).val() == '' ) $(this).val( $(this).attr('data-valuedefault') );
		});

		$(e).focus( function () {
			if( $(this).val() == $(this).attr('data-valuedefault') ) $(this).val( '' );
		});
	});

	/**
	 * LightBox/PopUp bind
	 */

	if( typeof gallery_type != 'undefined' && gallery_type == 'lighbox' )
	{
		var lightboxArr = new Array()
		$('a[rel^=gallery]').each(function()
		{
			if( jQuery.inArray($(this).attr('rel'),lightboxArr) < 0 )
			{
				lightboxArr.push($(this).attr('rel'));
			}
		});

		$('a[rel^=gallery]').click(function () {return false;});

		$.each(lightboxArr,function(){
			$('a[rel^='+this+']').lightBox();
		});
	}
	else
	{
	    $('a[rel^=gallery]').click(function() {
			var link_zdj=$(this).attr('href');
			var i=0;
			$('a[rel^=gallery]').each(function()
			{
				if($(this).attr('href')==link_zdj) {
					return false;
				}else{
					i++;
				}
			});
			zdj = window.open('pokaz_zdjecie.php?id_prod='+product_id.toString()+'&img='+ i.toString() , 'Zdjęcie', 'screenY=0,screenX=0,left=0,top=0,width=200,height=200,status=no,scrollbars=yes,resizable=yes,menubar=no');
			zdj.focus();
			return false;
	    });

	}

	/**
	 * Cech wpywajace na modyfikacje ceny
	 */
	if( $('.show_product_options').length > 0 ) {
		change_prices();
		$('.show_product_options').change(function(){
			change_prices();
		});
	}

	/**
	 * Inicjalizacja modulu filtrow
	 */
	filters_init();

	/**
	 * Inicjalizacja "podpowiadacza" do szukaj
	 */
    $("input.autocomplete").each( function () {

		$(this).autocomplete('smartsearch.php', {
			minChars: 2,
			max: 50,
			width: $(this).width(),
			selectFirst: false
		} );
	});
	$(".stars").find('img').hover(function(){
		$("#make_mark").show();
	},function(){});
	$("#make_mark").hover(function(){
		
	},function(){
		$(this).hide();
	});
	var c_href=$("#make_mark").parent().attr("href");
	
	$(".star2").each(function(){
		$(this).hover(function(){
			var to=$(this).attr("id").substr(1,1);
			var to2=$(this).attr("title");
			for(i=1;i<to;i++){
				$("#s"+i).css("background","url(/themes/red/img/gw_pe.gif)");
			}
			if(to2%2==0){
				var gw_img="gw_pe.gif";
			}else{
				var gw_img="gw_po.gif";
			}
			$("#s"+to).css("background","url(/themes/red/img/"+gw_img+")");
			$("#make_mark").parent().attr("href",c_href+"&ocena="+$(this).attr("title"));
		},function(){
			$(".star1").css("background","url(/themes/red/img/gw_pu.gif)");
		});
	});
	/**
	 * Inicjalizacja ToolTip'a
	 */
	ToolTip.init();

        th_var=0;
th_left=$("#theme_color").css('left');
$("#theme_button").click(function(){
if(th_var==0){
$("#theme_button").css('background-image','url(/themes/red/img/th_arr2.jpg)');
$("#theme_color").animate({
left: '0'
}, 500,function(){
th_var=1;
});
}else{
$("#theme_button").css('background-image','url(/themes/red/img/th_arr1.jpg)');
$("#theme_color").animate({
left: ''+th_left+''
}, 500,function(){
th_var=0;
});
}
});



});

var ToolTip = {

	text: '',
	toolTipOver: false,

	hide: function (obj) {
		obj.attr('title', ToolTip.text);
		$('#tooltip').css('display', 'none');
	},

	show: function (obj, e) {
		$('#tooltip2').css('display', 'block');
		ToolTip.text = obj.attr('title');
		if( ToolTip.text.length == 0 ) return;
		obj.attr('title', '');
		$('#tooltip2content').html(ToolTip.text);
		$('#tooltip2').css('display', 'block');

		$('#tooltip2').css('left', e.pageX - $('#tooltip2').get(0).offsetWidth - 5 + 'px');
		$('#tooltip2').css('top', e.pageY - $('#tooltip2').get(0).offsetHeight - 5 + 'px');
	},

	init: function() {
		$('.tt_rollover').hover(function(e) {
			var handle = $(this);
			$(this).css('cursor', 'default');
			ToolTip.show(handle, e);
		}, function() {
			ToolTip.hide($(this));
		});
	}
};

/**
 * Modyfikuje ceny na podstawie wybranych cech
 */
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;

		is_add=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
				tmp_price_set=number;

		}
	});

	//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());

		}
	}
}

/**
 * Obsluga filtrow
 */
function filters_init()
{
	$('div.filters div.filter_option div.option p span[rel]').click(function () {

		if( $( 'input[name="'+$(this).attr('rel')+'"]' ).attr('checked') == undefined ||
			$( 'input[name="'+$(this).attr('rel')+'"]' ).attr('checked') == false )
			$( 'input[name="'+$(this).attr('rel')+'"]' ).attr('checked', 'checked');

		$(this).parents('form').submit();
	});

	$('.showall').click( function () {

		var height_old = $(this).parent().parent().css('height');
		$(this).parent().parent().css('height', 'auto');

		$(this).css('display', 'none');
		$('.all[rel="'+$(this).attr('rel')+'"]' ).css('display', 'block');

		var height_new = $(this).parent().parent().height();

		$(this).parent().parent().css('height', height_old);
		$(this).parent().parent().animate( {
			height: height_new
		}, 200, 'linear' );

	});

	$('div.filters div.filter_option > div.label').click( function () {

		if( $(this).parent().hasClass('active') == true )
		{
			$(this).parent().find('div.option').animate( {
				height: 0
			}, 200, 'linear', function(){
				$(this).parent().removeClass('active');
				$(this).css('display', 'none');
			});
		}
		else
		{
			var div_option = $(this).parent().find('div.option');
			div_option.css('height', 'auto');
			var height = div_option.height();

			div_option.css('display', 'block');
			div_option.css('height', 0);
			$(this).parent().addClass('active');

			$(this).parent().find('div.option').animate( {
				height: height
			}, 200, 'linear', function(){
				$(this).parent().addClass('active');
			});
		}
	});
}
