$(document).ready( function() {

	$("input[type=text], input[type=password], textarea").focus( function() {
		if ( $(this).val() == $(this).attr("title") ) {
			$(this).val("")
		}
	}).blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( $(this).attr("title") );
		}
	});	
	
	$("#addToCart").click( function() {
		var prod_id = $("#prod_id").val();
		addToCart( $("#prod_id").val(), $("#model_id").val(), $("#rozmiar_id").val(), $("#ilosc").val() );
	});
	
	$("a.thickbox").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	$("#ilosc").keyup(function(e){
		checkAmount();
	});
	
	
	$("#search_adv_start").click( function() {
		$.ajax({
			type: "GET",
			url: "/_ajax/filter.php",
			data: { "state":'show'},
			success: function(data){
				laduj_filtry('1');
			}
		});
		return false;
	});
	
	$("#search_adv_stop").click( function() {
		$(".search_adv_toggle").toggle();
		$("#search_adv").hide();
		$.ajax({
			type: "GET",
			url: "/_ajax/filter.php",
			data: { "state":'hide'}
		});
		return false;
	});
	
	
	$('#search_adv .tabs a').click( function() {
		var rel = $(this).attr('rel');
		if ( $(this).hasClass('current') == false ) {
			var rel_current = $('#search_adv .tabs a.current').attr('rel');
			$('#search_tab_'+rel_current+' input').attr('checked', false);
			$('#search_tab_'+rel_current+' div.current').removeClass('current');
			$('#search_adv .tabs a.current').removeClass('current');
			$('#search_tab_'+rel_current).hide();
			
			$(this).addClass('current');
			$('#search_tab_'+rel).show();
		}
		return false;
	});
	
	$('#search_adv .tab_content div').click( function() {
		if ( $(this).hasClass('current') ) {
			$(this).removeClass('current');
			$(this).find('input').attr('checked',false);
		} else {
			$(this).addClass('current');
			$(this).find('input').attr('checked',true);
		}
	});
	
	$('#search_adv .search_color div').click( function() {
		if ( $(this).hasClass('checked') ) {
			$(this).removeClass('checked');
			$(this).find('input').attr('checked',false);
			$(this).find('img.checked').hide();
		} else {
			$(this).addClass('checked');
			$(this).find('input').attr('checked',true);
			$(this).find('img.checked').show();
		}
		return false;
	});
	
	$(".tab").click( function() {
		var tabid = $(this).attr("rel");
		var tabdi = $("#"+tabid).css("display");
		if ( tabdi == "block" ) {
			$(this).removeClass("open").addClass("close");
			$("#"+tabid).hide('fast');
			var state = "hide";
		} else {
			$(this).removeClass("close").addClass("open");
			$("#"+tabid).show('fast');
			var state = "show";
		}
		$.ajax({
			type: "GET",
			url: "/_ajax/tabs.php",
			data: { "tab":tabid, "state":state }
		});
		
		return false;
	});
	
	showCart();
});


$.fn.equalHeights = function(px) {
	var currentTallest = 0;
	$(this).each(function() {
		var currentHeight = $(this).height();
		if ( currentHeight > currentTallest) {
			currentTallest = currentHeight;
		}
	});
	currentTallest += 'px';
	// for ie6, set height since min-height isn't supported
	if ($.browser.msie && $.browser.version == 6.0) {
		$(this).css({'height': currentTallest});
	}
	$(this).css({'min-height': currentTallest}); 
	
	return this;
};

function laduj_filtry(czy_tak)
{
	if ( czy_tak == '1' )
	{
		$.ajax({
			url: "/_ajax/getFilters.php",
			type: "GET",
			cache: false,
			beforeSend: function() {
				$('#doladuj_filtry').html( '<img src="/gfx/filter-loader.gif" alt="" class="filtr_loader" />' );
			},
			success: function(data) { 
				$(".search_adv_toggle").toggle();
				$("#search_adv").show();
				$("#doladuj_filtry").html( data );
				zakladki_rozmiarowe();
				zaznaczanie_rozmiarow();
				wybor_koloru();
				grupy_rozmiarowe();
			}
		});	
	}
}

function wybor_koloru()
{
	// wybor koloru
	$("#advSearchForm .search_color div").click( function() {
	
		if ( $(this).find("input").attr("checked") ) {
			$(this).find("input").attr("checked", false);
			$(this).removeClass("checked");
			$(this).find('img.checked').hide();
		} else {
			$(this).addClass("checked");
			$(this).find("input").attr("checked", true);
			$(this).find('img.checked').show();
		}
	});		
}

function grupy_rozmiarowe()
{
	$("#search_adv_size").change(function(){
		var grupa = $(this).val();
		pobierzRozmiaryGrupy( grupa );
	});	
}


function checkAmount()
{
	var ile_chce = parseInt($("#ilosc").val(),10);
	var ile_jest = parseInt($("#max_amount").val(),10);
	if ( ile_chce > ile_jest )
	{
		$("#ilosc").val( ile_jest );	
	}
}

function addAmount()
{
	var ile_chce = parseInt($("#ilosc").val(),10);
	var ile_jest = parseInt($("#max_amount").val(),10);
	if ( ile_chce >= ile_jest )
	{
		//$("#ilosc").val( ile_jest );	
	}
	else
	{
		$("#ilosc").val( ile_chce+1 );
	}
}

function removeAmount()
{
	var ile_chce = parseInt($("#ilosc").val(),10);
	if ( ile_chce <= 1 )
	{
		//$("#ilosc").val( 1 );	
	}
	else
	{
		$("#ilosc").val( ile_chce-1 );
	}
}

function addAmountProd(klucz)
{
	alert($("#ilosc_"+klucz).val());
	var ile_chce = parseInt($("#ilosc_"+klucz).val(),10);
	var ile_jest = parseInt($("#max_amount_"+klucz).val(),10);
	if ( ile_chce >= ile_jest )
	{
		$("#ilosc_"+klucz).val( ile_jest );	
	}
	else
	{
		$("#ilosc_"+klucz).val( ile_chce+1 );
	}
}

function removeAmountProd()
{
	var ile_chce = parseInt($("#ilosc").val(),10);
	if ( ile_chce <= 1 )
	{
		$("#ilosc").val( 1 );	
	}
	else
	{
		$("#ilosc").val( ile_chce-1 );
	}
}

function switchOfferMenu( oid ) {
	$("#om"+oid).toggle();
	return false;
}

function zakladki_rozmiarowe()
{
	$("#advSearchForm #dostepne_rozmiary .tabs li a").click( function() {
		$("#advSearchForm #dostepne_rozmiary .tabs li a").removeClass("current");
		$(this).addClass("current");
		var rel = $(this).attr( "rel" );
		$("#advSearchForm .tab_content").hide();
		$("#advSearchForm #filtr_tab_"+rel).show();
		return false;
	});	
}

function zaznaczanie_rozmiarow()
{
	$("#advSearchForm #dostepne_rozmiary .tab_content div[class!=disabled]").click( function() {
		var state = $(this).find("input").attr("checked");
		if ( $(this).find("input").attr("checked") ) {
			$(this).find("input").attr("checked", false);
			$(this).removeClass("selected");
		} else {
			$(this).addClass("selected");
			$(this).find("input").attr("checked", true);
		}
	});
}	

// dodawanie do koszyka
function addToCart( pid, mid, rid, ilosc ) 
{
	var ilosc = parseInt(ilosc);
	var lang = $("#jakiLang").val();
	if ( lang == '' )
	{
		lang = 'pl';	
	}
	var status_dostepnosci = $("#status_dostepnosci").val();
	var ikona_dostepnosci = $("#sizeIco").attr( "class" );

	$.ajax({
		url: "/_ajax/cart.php",
		type: "POST",
		data: { 'action':'update', pid:pid, mid:mid, rid:rid, ilosc:ilosc, status_dostepnosci:status_dostepnosci, ikona_dostepnosci:ikona_dostepnosci },
		cache: false,
		success: function() { 
			//recountCart(lang);
			$("#goToCart").show();
			$("#ilosc").val( "1" )
			showCart(); 
			getSizeInfo( rid );
		}
	});
	
}

function recountCart(lang) {
	$.get("/_ajax/cart.php", { action: "recount" },
		function(data){
			location.href='/order/'+lang+'/cart.html';	
	});
}
/*
function nowaIloscReload( rid, ilosc, eid )
{
	var ilosc = parseInt(ilosc);
	$.ajax({
		url: "/_ajax/cart.php?action=number&rid="+rid+"&ilosc="+ilosc,
		type: "GET",
		async: true,
		cache: false,
		success: function(data) { 
			showCart(); 
			if ( eid && data == "change" )
			{
				getSizeInfo( eid );
				
			}
		},
		complete: function(data) { 
			window.location.reload();
		}
	});	
}
*/

function nowaIloscReload( rid, akcja ) {
	var ilosc = parseInt( $('#ilosc_'+rid).val() );
	if ( akcja == 'plus' ) {
		var nowa_ilosc = ilosc + 1;
	} else if ( akcja == 'minus' ) {
		var nowa_ilosc = ilosc - 1;
	}
	$('#ilosc_'+rid).val( nowa_ilosc );
	$('#cartFormCart').submit();
}


function nowaIlosc( rid, ilosc, eid )
{
	var ilosc = parseInt(ilosc);
	$.ajax({
		url: "/_ajax/cart.php?action=number&rid="+rid+"&ilosc="+ilosc,
		type: "GET",
		cache: false,
		success: function(data) { 
			showCart(); 
			var size = $("#rozmiar_id").val();
			if ( size == eid && data == "change" )
			{
				getSizeInfo( eid );
			}
		}
	});	
}


// wyświetlanie koszyka
function showCart() {
	$.get("/_ajax/cart.php", { action: "show" },
		function(data){
			$("#box_basket").html(data);
	});
	$.get("/_ajax/cart.php", { action: "show2" },
		function(data){
			$("#cart_menu").html(data);
	});
}

function printWindow( url ) {
	okno = window.open(url,'Drukuj','toolbar=no,scrollbars=yes,status=no,top=50,left=50,width=685,height=600');
}

function resetujFiltry()
{
	$("#postAkcja").val("resetujFiltry");
	document.filtrForm.submit();
}

function getSizeInfo( rid ) {
	if ( rid != '' && rid > 0 )
	{
		$("#allInCart").hide();
		$.ajax({
			url: "/_ajax/dostepnosc.php",
			data: { 'rid': rid },
			dataType: "json",
			type: "POST",
			cache: false,
			beforeSend: function() {			
				$("#noSize").hide();
				$("#stan_ponizej").hide();
				$("#stan_powyzej").hide();
				$("#tymcza").show();
			},
			success: function( data ) {
				$("#max_amount").val( data.dostepnosc );
				$("#ilosc").val( "1" );
				if ( data.blokuj == "1" ) {
					$("#sizeInfo").text(data.info);
					$("#addToCart").hide();
				} else if ( data.dostepnosc == "0" ) {
					$("#addToCart").hide();
					$("#sizeInfo").text("");
					$("#sizeIco").attr( "class", "product_status hidden" );
					$("#tymcza").hide();
					$("#allInCart").show();
					$("#ilosc").val('0');
				} else {
					$("#sizeInfo").text(data.info);
					$("#sizeIco").attr( "class", "product_status "+data.ico );
					$("#status_dostepnosci").val(data.info);
					//


					//
					$("#addToCart").show();
					if ( data.dostepnosc > 3 )
					{
						$("#stan_powyzej").show();
						$("#tymcza").hide();
					}
					else
					{
						$("#stan_ponizej").show();
						$("#tymcza").hide();
					}
				}
			}
		});
	}
	else
	{
		$("#allInCart").hide();
		$("#sizeInfo").text("");
	}
}

function setAddButton(button) {
	var atrib_val = $("#selectAtribVal option:selected").length;
	if ( atrib_val > 0 ) {
		$("#"+button).show();
	} else {
		$("#"+button).hide();
	}
}

function setAddButtonMulti( rel ) {
	var atrib_val = $("#selectAtribVal_"+rel+" option:selected").length;
	if ( atrib_val > 0 ) {
		$("#addAttr_"+rel).show();
	} else {
		$("#addAttr_"+rel).hide();
	}
}



function deleteAtrybut( aid, pid ) {
	$.ajax({
		url: "/_ajax/atrybuty.php",
		data: { 'action': 'del', 'aid': aid, 'prod': pid },
		type: "POST",
		cache: false,
		success: function( data ) {
			if ( data == 'ok' ) {
				$(".patr_"+aid).fadeOut();
			}
		}
	});
}

function deleteAtrybutKolor( kid, aid, pid ) {
	$.ajax({
		url: "/_ajax/atrybuty.php",
		data: { 'action': 'del2', 'aid': aid, 'prod': pid, 'kid':kid },
		type: "POST",
		cache: false,
		success: function( data ) {
			if ( data == 'ok' ) {
				$("#atrybuty_"+kid+" .patr_"+aid).fadeOut();
			}
		}
	});
}


function pobierzRozmiaryGrupy( grupa )
{
	$.ajax({
		url: "/_ajax/rozmiary_grupy.php",
		data: { action: "show", grupa: grupa },
		dataType: "json",
		type: "POST",
		cache: false,
		beforeSend: function() {			
			$('#dostepne_rozmiary').html( '<p><img src="/gfx/params-loader.gif" alt="" /></p>' );
		},
		success: function( data ) {
			$("#dostepne_rozmiary").html( data.html );
			zakladki_rozmiarowe();
			zaznaczanie_rozmiarow(); 
		}
	});

}

function chooseTab( tab , classid )
{
	switch(tab)
	{
		case 'e1':
			$('#'+classid+'_e1').addClass("current");
			$('#'+classid+'_e2').removeClass("current");
			$('#'+classid+'_e3').removeClass("current");
			//
			$('#'+classid+'_e2_content').hide('fast');
			$('#'+classid+'_e3_content').hide('fast');
			$('#'+classid+'_e1_content').show('fast');
			break;
		case 'e2':
			$('#'+classid+'_e2').addClass("current");
			$('#'+classid+'_e1').removeClass("current");			
			$('#'+classid+'_e3').removeClass("current");			
			//
			$('#'+classid+'_e1_content').hide('fast');
			$('#'+classid+'_e3_content').hide('fast');
			$('#'+classid+'_e2_content').show('fast');
			break;
		case 'e3':
			$('#'+classid+'_e3').addClass("current");
			$('#'+classid+'_e1').removeClass("current");			
			$('#'+classid+'_e2').removeClass("current");			
			//
			$('#'+classid+'_e1_content').hide('fast');
			$('#'+classid+'_e2_content').hide('fast');
			$('#'+classid+'_e3_content').show('fast');
			break;
	}
}

function swapPhoto( src, file )
{
	document.getElementById('bigFoto').src = src+'b'+file;
	document.getElementById('zoomLink').href = src+file;
	document.getElementById('bigFotoA').href = src+file;	
}

/*function setBigPhoto( src , file )
{
	if ( $.browser.msie && $.browser.version < 7 )
	{
		setTimeout("swapPhoto('"+src+"','"+file+"')", 100);
	}
	else
	{
		$('#foto a img').attr( 'src' , src + 'b'+file );
		$('#foto a, #zoomLink').attr( 'href' , src+file );
	}
}*/
