$(document).ready(function() {

	// Banner fix for IE7
	
	$(".header-links-wrapper").animate({'height' : '80px'}, 0);

	
	
	// Pictures paginator clear
	
	if($(".picture_links span a[disabled=disabled]").size()==2) {
		$(".picture_links span").hide();
	}
	
	// Menu arrow

	$(".headermenu").append('<div id="menu_arrow"></div>');
	$("#menu_arrow").css({
		'width' : $(".li_selected").width() + 'px',
		'left' : $(".li_selected").position().left + 'px'
	});
	$(".menu_bottom li").mouseenter(function() {
		$("#menu_arrow").stop().animate({
			'width' : $(this).children("a").width() + 'px',
			'left' : $(this).children("a").position().left + 'px'
		}, Math.abs($(this).children("a").position().left - $("#menu_arrow").position().left));
	}).mouseleave(function() {
		$("#menu_arrow").stop().animate({
			'width' : $(".li_selected").width() + 'px',
			'left' : $(".li_selected").position().left + 'px'
		}, Math.abs($(".li_selected").position().left - $("#menu_arrow").position().left));
	});
	
	// Searchbox
	
	if($(".searchbox").children(".searchboxtext").attr('value')!='') {
		$(".searchbox").children("label").fadeTo(0, 0).hide();
	}
	$(".searchbox").children("label").click(function() {
		$(this).stop().fadeTo(250, 0).delay(250).hide();
		$(".searchbox").children(".searchboxtext").focus();
	});
	$(".searchbox").children(".searchboxtext").focus(function() {
		$(".searchbox").children("label").stop().fadeTo(250, 0).delay(250).hide();
	}).blur(function() {
		if($(this).attr('value')=='') {
			$(".searchbox").children("label").stop().show().fadeTo(500, 1);
		}
	});
	
	// Adds
	
	$(".manufacturer-navigation .listbox span span").each(function() {
		$(this).find("img").removeAttr('style').each(function() {
			if($(this).height()>130) {
				$(this).css({'width': 'auto', 'height': '130px'});
			}
		});
		$(this).css('margin-top', ((130 - $(this).height()) / 2) + 'px');
		$(this).fadeOut(0);
	});
	var ban = 0;
	var bans = $(".manufacturer-navigation .listbox span span").size();
	$(".manufacturer-navigation .listbox span span:eq(0)").fadeIn(0);
	setInterval(function() {
		$(".manufacturer-navigation .listbox span span:eq(" + ban + ")").fadeOut(500);
		ban++;
		if(ban==bans) {
			ban = 0;
		}
		$(".manufacturer-navigation .listbox span span:eq(" + ban + ")").fadeIn(500);
	}, 5000);
	
	// Paginators
	
	$("#paginator a, .ProductDetailsPage .picture_links span a").each(function() {
		if($(this).attr('disabled')=='disabled') {
			$(this).hide();
		}
	});

	// Articles accordion
	
	$("#Products .ProductTitle").each(function() {
		$(this).parent().accordion({
			autoHeight: false,
			navigation: true
		});
	});
	var active_item = $('.ui-state-active');
	$("#Products .ProductTitle").click(function(e) {
		if(e.target.className==''&&(($(this).index()==active_item.index()))) {
			window.location.href = $(".ui-accordion-content-active .read_more a").attr('href');
		}
		active_item = $('.ui-state-active');
		$(this).blur();
	});
	
	// Categories width
	
	$(".category-navigation li").each(function() {
		$(this).css('width', ($(this).width() - parseInt($(this).css('margin-left').replace('px', ''))) + 'px');
	});
	
	// Categories icons
	
	$(".category-navigation .treeview").append('<div class="icons"></div>');
	$(".category-navigation li").each(function() {
		var url = $(this).children("a").attr('href').split('=');
		var cat = url[url.length - 1];
		$(".category-navigation .treeview .icons").append('<div id="icon_' + cat + '" class="icon"></div>');
		$("#icon_" + cat).css({
			'background-image' : $("#icon_" + cat).css('background-image').replace('cat', cat),
			'top' : $(this).position().top + 'px'
		});
	});	
	
	// Gradients
	
	$(".category-navigation .gradient_right").each(function() {
		$(this).gradient({
			from:      'fffcf6',
			to:        'dedede',
			direction: 'horizontal'
		});
	});
	$(".column_narrow .gradient_both").each(function() {
		$(this).gradient({
			from:      'fffcf6',
			to:        'dedede',
			direction: 'horizontal'
		});
	});
	$(".manufacturer-navigation .gradient_both").each(function() {
		$(this).gradient({
			from:      'fffcf6',
			to:        'dedede',
			direction: 'horizontal'
		});
	});
	
	// Categories arrow
	
	$(".category-navigation .treeview").append('<div id="categories_arrow"></div>');
	var subcategory = false;
	var default_top = 0;
	var default_cat = 0;
	if($(".category-navigation .li_selected").size()>0) {
		subcategory = true;
		default_top = $(".category-navigation .li_selected").position().top + 7;
		var url = $(".category-navigation .li_selected a").attr('href').split('=');
		default_cat = url[url.length - 1];
		$("#icon_" + default_cat).css('background-position', '30px 0');
	}
	$("#categories_arrow").css({
		'top' : default_top + 'px'
	});
	if(!subcategory) {
		$("#categories_arrow").hide();
	}
	$(".category-navigation li").mouseenter(function() {
		var url = $(this).children("a").attr('href').split('=');
		var cat = url[url.length - 1];
		$("#icon_" + cat).css('background-position', '30px 0');
		if(!subcategory) {
			$("#categories_arrow").show();
		}
		$("#categories_arrow").stop().animate({
			'top' : ($(this).position().top + 7) + 'px'
		}, 2 * Math.abs(($(this).position().top + 7) - $("#categories_arrow").position().top));
	}).mouseleave(function() {
		var url = $(this).children("a").attr('href').split('=');
		var cat = url[url.length - 1];
		if(cat!=default_cat) {
			$("#icon_" + cat).css('background-position', '0 0');
		}
		$("#categories_arrow").stop().animate({
			'top' : default_top + 'px'
		}, Math.abs(default_top - $("#categories_arrow").position().top));
		if(!subcategory) {
			$("#categories_arrow").hide();
		}
	});
	
	// Nowości & Kultura.pl boxes
	
	$(".column_narrow .hidden_items").hide();
	$(".column_narrow .hidden_items .ItemBox:odd").each(function() {
		$(this).parents(".column_narrow").find(".ItemBox:eq(0) .rotator").append('<div class="rotator_item">' + $(this).html() + '</div>');
	});
	$(".column_narrow .hidden_items .ItemBox:even").each(function() {
		$(this).parents(".column_narrow").find(".ItemBox:eq(1) .rotator").append('<div class="rotator_item">' + $(this).html() + '</div>');
	});
	$("#Nowosci .rotator").each(function() {
		var $this = $(this);
		var single_width = $this.children().width() + 2 * parseInt($this.children().css('padding-right').replace('px', ''));
		var size = $this.children().size();
		var width = size * single_width;
		$(this).css('width', width + 'px');
		var i = 0
		setInterval(function() {
			i++;
			single_width = $this.children().width() + 2 * parseInt($this.children().css('padding-right').replace('px', ''));
			if(i<size) {
				$this.animate({'left' : '-' + (i * single_width) + 'px'}, 500);
			} else {
				$this.animate({'left' : '0px'}, 1000);
				i = 0;
			}
		}, 20000);
	});
	setTimeout(function() {
		$("#Kultura .rotator").each(function() {
			var $this = $(this);
			var single_width = $this.children().width() + 2 * parseInt($this.children().css('padding-right').replace('px', ''));
			var size = $this.children().size();
			var width = size * single_width;
			$(this).css('width', width + 'px');
			var i = 0
			setInterval(function() {
				i++;
				single_width = $this.children().width() + 2 * parseInt($this.children().css('padding-right').replace('px', ''));
				if(i<size) {
					$this.animate({'left' : '-' + (i * single_width) + 'px'}, 500);
				} else {
					$this.animate({'left' : '0px'}, 1000);
					i = 0;
				}
			}, 20000);
		});
	}, 10000);
	
	// Footer menu
	
	$("#footer_menu").html($(".infoblock-navigation:first ul").html());
	var padding = (600 - $("#footer_menu").width() - 10) / 2;
	$("#footer_menu").css('padding-left', padding + 'px');
	
	// Fancybox
	
	$('a[rel=photos]').each(function() {
		$(this).fancybox({'titleShow' : false, 'padding' : 10, 'overlayColor' : '#000', 'overlayOpacity' : 0.3});
	});

});
