String.prototype.rgb2hex = function(asArray){

	if(!this.match(/^rgb/i)){return false;}

	var re = new RegExp('rgb\\((\\d{1,}),(\\d{1,}),(\\d{1,})\\)', "ig");
	var colors = re.exec(this.replace(new RegExp("\\s", "g"), ""));
	//var colors = re.exec(String.prototype.stripWhitespace.call(this));
	var r= parseInt(colors[1], 10).toString(16);
	var g= parseInt(colors[2], 10).toString(16);
	var b= parseInt(colors[3], 10).toString(16);
	
	r = (r.length<2) ? r+r : r;
	g = (g.length<2) ? g+g : g;
	b = (b.length<2) ? b+b : b;
	
	if(asArray){
		return [r,g,b];
	} else {
		return '#'+r+''+g+''+b;
	}
};

function hideFavInfo()
{
  jQuery("#favoriteAdd").fadeOut();
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( jQuery(window).height() - this.height() ) / 2+jQuery(window).scrollTop() + "px");
    this.css("left", ( jQuery(window).width() - this.width() ) / 2+jQuery(window).scrollLeft() + "px");
    return this;
}


jQuery(document).ready( function()
{

        if ( jQuery("#promo").length == 1 )
        {
          jQuery(".product").click( function()
          {
            var mi = jQuery("#promo").attr("rel");
            
            if ( jQuery(".product:checked").length > 0 )
            {
              jQuery("#vouchers_NOPROMO").show();
              jQuery("#order_delivery").attr("length", "7");
            }
            else
            {
              jQuery("#vouchers_NOPROMO").hide();
              jQuery("#order_delivery").attr("length", "0");
            }
            
            if ( jQuery(".product:checked").length == mi )
            {
                jQuery(".product").not(":checked").each( function() {
                  jQuery(this).attr("disabled", "disabled");
                });
            } else {
              jQuery(".product:disabled").removeAttr("disabled");
            }
            
            var test = new order;
          });
        }

        jQuery("#showMoreL").click( function()
        {
          jQuery(this).parents("tr:first").hide();
          
          jQuery(".showMore").css("display", "table-row");
        });

          jQuery('#goTop').click( function()
          {
            jQuery.scrollTo( 0 );
          });
  			    
         jQuery('.btn-usun').click( function()
         {
            jQuery.post("./ajax/scripts.php", { "data" : jQuery(this).attr('rel'), "hash" : jQuery(this).attr('rev') }, function(str)
            {
              if ( str == 'already' )
              {
                jQuery("#favoriteAdd").html("Tej pozycji nie ma na liście Twoich ulubionych.").center();
                jQuery("#favoriteAdd").fadeIn(); 
                setTimeout("hideFavInfo()", 1500);
              }
              else
              {
                jQuery("#favoriteAdd").html("Usunięto z ulubionych.").center();
                jQuery("#favoriteAdd").fadeIn(); 
                top.location.href = top.location.href;
              }
            });
         });

  			    
         jQuery('.btn-favorite').click( function()
         {
            jQuery.post("./ajax/scripts.php", { "data" : jQuery(this).attr('rel'), "hash" : jQuery(this).attr('rev') }, function(str)
            {
              if ( str == 'already' )
              {
                jQuery("#favoriteAdd").html("Już dodałeś(aś) tę pozycję do ulubionych.").center();              
                jQuery("#favoriteAdd").fadeIn(); 
                setTimeout("hideFavInfo()", 1500);
              }
              else
              {
                jQuery("#favoriteAdd").html("Dodano do ulubionych!").center();              
                jQuery("#favoriteAdd").fadeIn(); 
                setTimeout("hideFavInfo()", 1500);
              }
            });
         });
         
         
        jQuery("#price").change( function()
        {        
        
          range = jQuery(this).val();
          
          if ( range.indexOf("-") !== -1 )
          {
            ranges = range.split("-");
            jQuery("#price-1").val( ranges[0] );
            jQuery("#price-2").val( ranges[1] );
          }
          
        });         

        jQuery("#keyword").parents("form:first").submit( function()
        {
        
          if ( jQuery("#keyword").val().length > 0 )
            jQuery("input[name=name]").val( jQuery("#keyword").val() );
        
        });
         
        if ( jQuery('input[name=js]').length > 0 )
          jQuery('input[name=js]').val('1');
          
          
   
   jQuery("[rel^=lytebox]").lightBox();   
          
         
});
