//Efetky
$(document).ready(function() {
	InitTopBigMenu();
	InitMenuTabele();
	InitModNewsScroller();
	InitBxFotoScroller();
	InitOther();	
	$('#add_to_favs').attr('rel','sidebar');
});

function pokazNaj(poka,ukryj1,ukryj2)	{
	document.getElementById(ukryj1).style.display = 'none';
	document.getElementById(ukryj2).style.display = 'none';
	document.getElementById(poka).style.display = '';
	
}

function InitTopBigMenu() {

	// 1 - pomiary szerokosci elementow glownych menu i dostosowanie do szerokosci kontenera menu 
	MenuContainerWidth = $(".TopBigMenuContainer").width();

	var AllOdstepWidth = 0;	
	$(".TopBigMenuContainer .MenuOdstep").each(function(i) {
		OdstepWidth = $(this).width();
		AllOdstepWidth += OdstepWidth;
	});

	var AllItem = $(".MenuItem").length;
	var AllItemWidth = 0;
	$(".MenuItem").each(function(j) {
		ItemWidth = $(this).width();
		AllItemWidth += ItemWidth;
	});	
	
	ItemPadding = Math.round((MenuContainerWidth - AllItemWidth - AllOdstepWidth) / (AllItem * 2));
	NewAllWidth = AllItemWidth + AllOdstepWidth + ((ItemPadding*2)*AllItem);

	$(".MenuItem").css({paddingLeft: ItemPadding, paddingRight: ItemPadding});
	
	if ( NewAllWidth > MenuContainerWidth ) {
		Correction = NewAllWidth - MenuContainerWidth;
		a = Math.round(Correction/2);
		b = Correction - a;		
		$(".MenuItem").last().css({paddingLeft: (ItemPadding-b), paddingRight: (ItemPadding-a)});
	}
		else if ( NewAllWidth < MenuContainerWidth ) {
			Correction = MenuContainerWidth - NewAllWidth;
			a = Math.round(Correction/2);
			b = Correction - a;		
			$(".MenuItem").last().css({paddingLeft: (ItemPadding+b), paddingRight: (ItemPadding+a)});
		}
		
	$(".SubMenuContent").css({paddingLeft: ItemPadding, paddingRight: ItemPadding});

	// 2 - zdarzenia mouse over 
	$(".MenuItem").hover(function() {
		$(this).parent().find("#ActiveItem .SubMenuItem").hide();
		$(this).find(".SubMenuItem").show();
		$(this).css({borderTop: "2px solid #98ca00", marginTop: "-2px"});
		$(this).find(".StrzalkaTopMenu").attr("src", "images/strzalka_top_big_menu_a.gif");
	}, function() {		
		if ( $(this).attr("id") != "ActiveItem" ) {
			$(this).parent().find("#ActiveItem .SubMenuItem").show();
			$(this).find(".SubMenuItem").hide();
			$(this).css({borderTop: "0px solid #98ca00", marginTop: "0px"});
			$(this).find(".StrzalkaTopMenu").attr("src", "images/strzalka_top_big_menu.gif");
  	}
	});	
	
	// 3 - wlaczenie aktywnych elementow menu
	if ( $("#ActiveItem").length != 0 ) {
		$(".TopBigMenuContainer").find("#ActiveItem .SubMenuItem").show();
		$(".TopBigMenuContainer").find("#ActiveItem a").first().addClass("ColorItemA");
		$(".TopBigMenuContainer").find("#ActiveItem .SubMenuItem #ActiveSub").addClass("ColorSubA");
		$(".TopBigMenuContainer").find("#ActiveItem").css({borderTop: "2px solid #98ca00", marginTop: "-2px"});
		$(".TopBigMenuContainer").find("#ActiveItem .StrzalkaTopMenu").attr("src", "images/strzalka_top_big_menu_a.gif");
	}	

}


function InitMenuTabele() {

	if ( $(".TablicaInfContainer .TablicaInfMenu").length != 0 || $(".TablicaCzytContainer .TablicaCzytMenu").length != 0 ) {	
		// dodaniestrzalem do menu
		$(".TablicaInfMenu, .TablicaCzytMenu").css({cursor: "pointer"})
		$(".TablicaInfMenu").find(".MenuTabItem").append('<img src="images/strzalka_submenu.gif" class="StrzalkaTabItem" />');
		$(".TablicaCzytMenu").find(".MenuTabItem").append('<img src="images/strzalka_submenu.gif" class="StrzalkaTabItem" />');

		// dodanie atrybutow id, title oraz stworzenie ciasteczek to obslugi tabow, wlaczenie aktywnych tabow po odswiezeniu strony
		$("div[rel=TablicaCount]").each(function(t) {
			ThisTab = "Tab_" + (t+1);
			$(this).attr("id", ThisTab);
			
			$(this).find(".MenuTabItem").each(function(m) {
				$(this).attr("rel", (ThisTab + "_Sub_" + (m+1)));
			});

			$(this).find(".Tabs").each(function(s) {
				$(this).attr("rel", (ThisTab + "_Sub_" + (s+1)));
				
				// nowe dw
				SprCzyLista = $(this).is("ul");
				SprTytul = $(this).find(".TablicaPodTytul").length;
				SprTresc = $.trim( $(this).html() ).length;
				if ( SprTresc != 0 && SprTytul == 0 && SprCzyLista == false ) {
					$(this).prepend('<div class="Odstep15px"></div>');
				}
			});

			if ( $.cookie(ThisTab) == null ) {
				FirstTab = $(this).find(".MenuTabItem").first().attr("rel");
				$.cookie(ThisTab, FirstTab);
			}

			$(".MenuTabItem[rel="+$.cookie(ThisTab)+"] a").addClass("ColorItemTab");
			$(".MenuTabItem[rel="+$.cookie(ThisTab)+"]").find(".StrzalkaTabItem").show();
			$(".MenuTabItem[rel="+$.cookie(ThisTab)+"]").css({borderTop: "2px solid #98ca00", marginTop: "-2px"});
			$(".Tabs[rel="+$.cookie(ThisTab)+"]").show();
		});
		
		// zdarzenia hover dla menu
		$(".MenuTabItem").hover(function() {
			$(this).find(".StrzalkaTabItem").show();
			$(this).css({borderTop: "2px solid #98ca00", marginTop: "-2px"});
		}, function() {
			if ( $(this).find("a").attr("class") != "ColorItemTab" ) {
				$(this).find(".StrzalkaTabItem").hide();
				$(this).css({borderTop: "0px solid #98ca00", marginTop: "0px"});
	  	}
		});
		
	  // zdarzenia clickdla menu
	  $(".MenuTabItem").click(function() {
			ThisTabId = $(this).parents("div[rel=TablicaCount]").attr("id");
			ThisItemTitle = $(this).attr("rel");
			$.cookie(ThisTabId, ThisItemTitle);
			
			// gaszenie wszystkiego - tak szybciej, bez zadnych warunkow i petli
			$(this).parents("div[rel=TablicaCount]").find(".Tabs").hide();
			$(this).parents("div[rel=TablicaCount]").find(".MenuTabItem a").removeAttr("class");
			$(this).parents("div[rel=TablicaCount]").find(".StrzalkaTabItem").hide();
			$(this).parents("div[rel=TablicaCount]").find(".MenuTabItem").css({borderTop: "0px solid #98ca00", marginTop: "0px"});
			// wlaczenie, aktywawanie kliknietego taba 
			$(this).parents("div[rel=TablicaCount]").find(".Tabs[rel="+$.cookie(ThisTabId)+"]").show();
			$(this).find("a").addClass("ColorItemTab");
			$(this).find(".StrzalkaTabItem").show();
			$(this).css({borderTop: "2px solid #98ca00", marginTop: "-2px"});			
	  });		
	}

}


function InitModNewsScroller() {

	if ( $(".BxSliderNews .BxSliderItem").length > 1 ) {
		$(".BxSliderNews").bxSlider({
			alignment: 'vertical',
			controls: false,
			speed: 900,
			pager: false,
			margin: 0,
			auto: true,
			pause: 4500,
			wrapper_class: 'BxSliderNewsWrap'
		});
	}

}


function InitBxFotoScroller() {

	if ( $(".BxFotoSlideContent .BxFotoSlideItem").length > 1 ) {
		$(".BxFotoSlideContent").bxSlider({
				alignment: 'horizontal',
				controls: true,
				speed: 900,
				pager: true,
				pager_short: false,
				pager_short_separator: ' / ',				
				margin: 0,
				next_text: 'next',
				next_image: 'images/foto_slider_next.jpg',
				prev_text: 'prev',
				prev_image: 'images/foto_slider_back.jpg',
				auto: true,
				pause: 16000,
				auto_hover: true,
				auto_controls: false,
				stop_text: 'stop',
				start_text: 'start',								
				wrapper_class: 'BxSliderFotoWrap'				
		});
		
		$(".BxFotoSlideInfo").css({ bottom: "20px" });
		$(".BxSliderFotoWrap .bx_pager").css({ opacity: 0.86 });
		
		if ( $.browser.msie && $.browser.version.substr(0,3) == "6.0" ) {
			$(".BxSliderFotoWrap .bx_pager").css({ backgroundColor: "#272829", opacity: 1 });
		}		
	}

}


function InitOther() {
	
	// Select - wybierz opcje
	WybierzOpcje = function() {
		
	}
  
	// Nowy wylad input file
	$("input[type=file]").filestyle ({
		image: "images/choose-file.gif",
		imageheight: 23,
		imagewidth: 75,
		width: 430
	});
	
	// formatowanie pol liczbowych 
	if ( $("form[name=ZamowMiesiecznik]").length != 0 ) {
		$("input[name=NIP]").mask("9999999999");
		$("input[name=KodPocztowy]").mask("99-999");
		$("input[name=TelefonStacjonarny]").mask("(99) 999-99-99");
		$("input[name=TelefonKomorkowy]").mask("(99) 999-999-999");

		$("input[name=NIP]").keypress(function() {
			$("input[name=FakturaVat]").removeAttr("disabled");
	  }).mouseout(function() {
	  	if ( isNaN($(this).val()) ) {
		  	$("input[name=FakturaVat]").removeAttr("checked");
				$("input[name=FakturaVat]").attr("disabled", true);
			}
	  });
  }
  
  // box spolecznosciowki
  if ( $(".SerwisySpolIcons a").length > 5 ) {
  	$(".SerwisySpolMore").css({ cursor: "pointer" });
	  $(".SerwisySpolMore").click(function() {
	  	if ( $(this).parents().find(".SerwisySpolContainer").css("overflow") == "hidden" ) {
	  		$(this).parents().find(".SerwisySpolContainer").css({ overflow: "visible", borderLeft: "0", paddingLeft: "1px" });
	  		$(this).parents().find(".SerwisySpolIcons").css({ backgroundColor: "#FFFFFF", border: "1px solid #c2c9cf", borderTop: "0" });
	  		$(this).parents().find(".SerwisySpolContainer").hover(function() {
		  		$(this).parents().find(".SerwisySpolContainer").css({ overflow: "hidden", borderLeft: "1px solid #c2c9cf", paddingLeft: "0px" });
		  		$(this).parents().find(".SerwisySpolIcons").css({ backgroundColor: "transparent", border: "0" });  			
				});
	  	} else {
	  		$(this).parents().find(".SerwisySpolContainer").css({ overflow: "hidden", borderLeft: "1px solid #c2c9cf", paddingLeft: "0px" });
	  		$(this).parents().find(".SerwisySpolIcons").css({ backgroundColor: "transparent", border: "0" });		
			}
	  });
  }

	// dodaj do ulubionych
	AddToFavorites = function(obj, url, title) {
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url, "");
			return false;
		}
		else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
			return false;
		}
		else if(window.opera && window.print) { // Opera Hotlist
			obj.setAttribute("href", url);
			obj.setAttribute("title", title);
			return true;
		}
		else {
		    alert('Przepraszamy, ale ta przegladarka nie jest obslugiwana.');
		}
	}

	// set font size - OLD
	setFontSize = function(size) {
		$(".ArtykulTresc").css("font-size", size);
	}
	
	// set font size - NEW
	$(".ZmienWielkosc a").click(function() {
		size = $(this).attr("rel");
	
		$(".ZmienWielkosc a").each(function(j) {
			$(this).find("span").removeAttr("id");
		});		
		
		$(this).find("span").attr("id", "ActiveSize");
		$(".ArtykulTresc").css("font-size", size);
	});
	
	
	// pokaz opinie
	pokazAddPinie = function() {
		document.getElementById('add_opinie').style.display = '';
		document.getElementById('OpinieTytul').style.display = 'none';
		document.getElementById('OpinieListaContainer').style.display = 'none';
	}
	
	// form submit
	form_submit = function(id) {
		document.getElementById('form_'+id).submit();
	}
	
}

