/* ------------------------------------------------- *
 * ToggleVal Plugin for jQuery
 * -------------------------------------------------- *
 * Author:   Aaron Kuzemchak
 * Date:     8/18/2007
 * ------------------------------------------------- */
jQuery.fn.toggleVal = function(focusClass){this.each(function(){$(this).focus(function(){if($(this).val() == this.defaultValue){$(this).val("");}if(focusClass){$(this).addClass(focusClass);}}).blur(function(){if($(this).val() == ""){$(this).val(this.defaultValue);}if(focusClass){$(this).removeClass(focusClass);}});});}

/* ------------------------------------------------- *
 * Global Functions
 * ------------------------------------------------- */
$(document).ready(function(){
// For inputs
	$("input.toggle").each(function(){$(this).toggleVal();});

// Promo news

    if($(".privileged").length > 0 ) {
	$(".index #right").prepend("<div class='promo_wrapper' />");
	var promoGroup = $(".index #right .promo").clone();
	$(".index #right .promo").remove();
	promoGroup.prependTo(".index #right .promo_wrapper");
	var wrapper = $(".index #right .promo_wrapper");
	var wrapperDiv = $(".index #right .promo_wrapper").find('.promo');
	wrapperDiv.each(function(){
		$(this).prependTo( $(this).parent() );
	});
	wrapperDiv.eq(0).addClass('active');
	wrapperDiv.not('.active').hide();
	wrapper.append("<ul id='pager' />");
	wrapper.cycle({speed:1000, pause:1, timeout:5000, pager:'#pager'});
	var pager = $(".index #right #pager");
	pager.find("a").wrap('<li />').removeClass();
	pager.find("li:first-child").addClass('activeSlide');
}
// Promo category
	$(".index #content .category").each(function(){
		if ( $(this).hasClass('promo') ){
			$(this).append("<span class='badge' />");
		}
	});

// For main accordion
	$("#content .link").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");}); // For IE6
	$(".category .folding").hide().parent().find('.link').attr('title','Rozwiń kategorię').append("<a href='#' class='on'>rozwiń</a>");
	$(".category h2").click(function() {
		$(this).next('div').slideToggle('fast',function(){
			if ($(this).is(':hidden')) {
				$(this).parent().find('.link').attr('title','Rozwiń kategorię').find("a").replaceWith("<a href='#' class='on'>rozwiń</a>");
			} else if ($(this).is(':visible')) {
				$(this).parent().find('.link').attr('title','Zwiń kategorię').find("a").replaceWith("<a href='#' class='off'>zwiń</a>");
			}
			return false;
		});

		$(".category h2").not(this).parent().find('.folding:visible').slideUp('fast',function(){
			if ($(this).is(':hidden')) {
				$(this).parent().find('.link').attr('title','Rozwiń kategorię').find("a").replaceWith("<a href='#' class='on'>rozwiń</a>");
			} else if ($(this).is(':visible')) {
				$(this).parent().find('.link').attr('title','Zwiń kategorię').find("a").replaceWith("<a href='#' class='off'>zwiń</a>");
			}
			return false;
		});
	});

// For sidebar accordion
	$("#right .side_nav li").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");}); // For IE6
	$("#right .folding").hide().parent().find('.link').attr('title','Rozwiń kategorię').append("<img src='/Public/images/frontend/down.gif' alt='' />");
	$("#right #active_ul .folding").show().parent().find('.link').attr('title','Zwiń kategorię').find("img").replaceWith("<img src='/Public/images/frontend/up.gif' alt='' />");
	$("#right .link").click(function() {
		$(this).parent().parent().find(".folding").slideToggle('fast',function(){
			if ($(this).is(':hidden')) {
				$(this).parent().removeClass('folded').find('.link').attr('title','Rozwiń kategorię').find("img").replaceWith("<img src='/Public/images/frontend/down.gif' alt='' />");
			} else if ($(this).is(':visible')) {
				$(this).parent().addClass('folded').find('.link').attr('title','Zwiń kategorię').find("img").replaceWith("<img src='/Public/images/frontend/up.gif' alt='' />");
			}
			return false;
		});

		$("#right .link").not(this).parent().parent().find('.folding:visible').slideUp('fast',function(){
			if ($(this).is(':hidden')) {
				$(this).parent().removeClass('folded').find('.link').attr('title','Rozwiń kategorię').find("img").replaceWith("<img src='/Public/images/frontend/down.gif' alt='' />");
			} else if ($(this).is(':visible')) {
				$(this).parent().addClass('folded').find('.link').attr('title','Zwiń kategorię').find("img").replaceWith("<img src='/Public/images/frontend/up.gif' alt='' />");
			}
			return false;
		});
	});
});

// Tabs
function reloadProductForTab(tabId){
	$("#product_reload").load("/default/producttab/show", {tabId: tabId});
	$("#tabs").find("li").attr("class", "");
	$("#tab_" + tabId).attr("class", "active");
}
