  $(function() {
  	
  	
  	  $('.producer img, #producer img').error(function() {
	  	$(this).parent().html($(this).attr('alt'));
	  });
	  
	/*$('#products input[type=image]').click(function() {
		if ($('input[name=limit_'+$(this).val()+']').val() == '0')
		{
			alert("Produkt dostępny na zamówienie!");
			return false;
		}
	});*/
  	
  	/** INDEX */
	$('#promo ul').each(function() {
		$(this).find('a').click(function() {
			$(this).closest('ul').parent().find('.promo-main').attr('src', ($(this).attr('href')));  
			return false;	
		});
  	});
  	
  	/** LABEL - VALUE */
	$('input.cap-text').focus(function(){
		if (this.value == this.getAttribute('value')) 
			$(this).val('').removeClass('inactive');
	}).blur(function(){
		if (this.value == '') 
			$(this).val(this.getAttribute('value')).addClass('inactive');
	}).addClass('inactive');
	
	$('form:has(input.cap-text)').submit(function() {
		$(this).find('input.cap-text').each(function(){
			if (this.value == this.getAttribute('value')) 
				$(this).val('').css('text-align', 'left');
		});
		return true;
	});
	
	
	/** TOP CATEGORIES */
	function try_hide(ul) {
		if (!ul.data('ul') && !ul.data('li'))
		{
			ul.stop().fadeOut('fast');
		}
	}
	
	$('#top-categories > li').each(function() { $(this).hover(function() {
		ul = $(this).find('ul');
		if (ul.is(':hidden'))
		{
			ul.css({display: 'block', opacity: '0'});
		}
		ul.data('li', true).stop().animate({opacity: '1'});
	}, function() {
		try_hide($(this).find('ul').data('li', false));
	}); });
	
	$('#top-categories ul').each(function() { $(this).hover(function() {
		$(this).data('ul', true).fadeIn();
	}, function() {
		try_hide($(this).data('ul', false));
	}).hide().css('left', 0); });
	
	
	/** CATEGORIES */
	
	var cats = $('#categories > ul');
	
	if (cats.length>0) {
		var lia = cats.find('li.active');
		if (lia.length>0)
		{
			if (lia.find('li').length>0) {
				// Główna kategoria
				lia.prependTo(cats);
			} else {
				lia.parents('li').prependTo(cats);
			}
		}
	}
	
	
	/** PRODUCT - TABS */
	
	$('#product-nav li').each(function() { $(this).width($(this).width()+"px"); });
	
	$('#product-tabs > div').hide();
	
	to_show = (window.location.hash != '' && $('#product-tabs > div'+window.location.hash).length != 0)?window.location.hash:'#description';
	$(to_show).show();
	$('a[href="'+to_show+'"]').addClass('active');
	
	$('#product-nav a').click(function() {
		$('#product-nav a').removeClass('active');
		$(this).addClass('active');
		$('#product-tabs > div').hide();
		var hr = $(this).attr('href');
		var ts = $(hr);
		ts.show();//.removeAttr('id');
		//window.location.hash = hr;
		//hr = hr.replace( /^#/, '');
		//ts.attr('id', hr); 
		return false;
	});
	
	/** PRODUCT */
	$('#product-main a[href=#similar]').click(function() { $('#product-nav a[href=#similar]').click(); return false; });
	
	$('#stars-container label').hide();
	$('[name=ocena]').rating();
	$('[name=ocena2]').rating();
	
	var mimg = $('#product-img');
	var pgfx = $('#product-gfx ul a');
	
	var mimgs = $('#product-gfx a');
	
	/*$('#product-gfx ul a').click(function() {
		$('#product-img').attr('src', $(this).attr('href'));
		return false;
	});*/
	
	if (mimg.length>0)
	{
		//(pgfx.length>0?pgfx:mimg.parent()).fancybox({
		mimgs.fancybox({
			'zoomSpeedIn': 600,
			'zoomSpeedOut': 500,
			'easingIn': 'easeOutBack',
			'easingOut': 'easeInBack' 
		});
		
		
	

		mimg.load(function() {
			if (mimg.length>0) {
				if (mimg[0].width>190)
				{
					mimg.css({height: 'auto', width: 185});
				}
			}
		});
/*		mimg.parent().click(function() {
			return false;
		});
		
		if (pgfx.length>0)
			mimg.parent().click(function() {
				$('#product-gfx li:last-child a').click();
				return false;
			});*/
	}
	
	$('.fav').show().click(function() {
		var url = $(this).attr('href') == '#' ? location.href : $(this).attr('href');
		var title = $(this).attr('title') == '' ? $('title').text() : $(this).attr('title');
		
		if (window.sidebar && window.sidebar.addPanel)
    	{
			window.sidebar.addPanel(title,url,null); return false
		}
        
		if (window.external && window.external.addFavorite)
        {
			window.external.addFavorite(url,title); return false
		}
	});
	
	
	/** CATALOG */
	var cimgs = $('td.td-img img');
	if (cimgs.length>0) {
		cimgs.each(function() {
			$(this).load(function() {
				if (this.width>135)
				{
					$(this).removeAttr('height').attr('width', 130);
				}
			});
		});
	}
	
	/** OPINIONS */
	/*if ($('#opinions li').length>0) {
		$('#comment-form').hide();
		$('#cfs').click(function() {
			$(this).closest('div').hide();
			$('#comment-form').show('slow');
			return false;	
		});
	} else {
		$('#cfs').closest('div').hide();
	}*/
	
	$('#cproducer').change(function() {
		if ($(this).val() != -1)
			window.location = $(this).val();
	});
	
	$('#searchbar').click(function() {
		if (window.external.AddSearchProvider != undefined)
		{
			addp('http://www.mediaking.pl/op_mediaking.xml');
		} else {
			alert("Twoja przeglądarka nie obsługuje wyszukiwarek");
		}
		return false;
	});
	
	var max = parseInt($("#ifilter-max").val());
	var min = parseInt($("#ifilter-min").val());
	$('#filter-range').slider({
		range: true,
		min: min,
		max: max,
		step: 50,
		values: [min, max],
		slide: function(event, ui) {
			if (ui.values[0] == ui.values[1])
				return false;
			$("#ifilter-min").val(ui.values[0]);
			$("#filter-min").text(ui.values[0]);//.css('left', $($('.ui-slider-handle')[0]).css('left'));
			$("#ifilter-max").val(ui.values[1])
			$("#filter-max").text(ui.values[1]);//.css('left', (parseInt($($('.ui-slider-handle')[1]).css('left'))>93?'93%':$($('.ui-slider-handle')[1]).css('left')));
		}
	});
	$("#filter-min").text(min);
	$("#filter-max").text(max);

  });
  	
	function addp(csurl) {
		try {
			window.external.AddSearchProvider(csurl);
		} catch (e) {
			alert("You need to use Internet Explorer (7.0 or later) or Firefox (2.0 or later) to install the OpenSearch plug-in.");
		}
	}
	
$(window).load(function() {
	$('.producer img, #producer img').error(function() {
	  	$(this).parent().html($(this).attr('alt'));
	});
	var cimgs = $('td.td-img img');
	if (cimgs.length>0) {
		cimgs.each(function() {
			if (this.width>135)
			{
				$(this).removeAttr('height').attr('width', 130);
			}
		});
	}
	
	var mimg = $('#product-img');
	if (mimg.length>0) {
		if (mimg[0].width>190)
		{
			mimg.css({height: 'auto', width: 185});
		}
	}
});