var _POPUP_FEATURES = 'scrollbars=1, location=0, statusbar=0, menubar=0, width=500, height=500';

var lustro = 0;

var srcs = $("img").map(function(){
		return $(this).attr("src");
	}).get();

var hiddable_visible = 0;

var intervalID = new Array();
var intervalID2 = new Array();


$(document).ready(function()
{
	//zeby zdjecie sie zamykalo po kliknieciu gdizekolwiek:
	if ( document.getElementById("ZoomImage") ){
		var whole = document.getElementById("whole");
		whole.onclick = function (event) { zoomOut(document.getElementById("ZoomImage"), event); return true; };
	}



	$('#tech_container').hide();
	$('#pow_container').hide();
	$('#wliczony_container').hide();

//	SetVisible();
	SearchGaraz();
	SearchPowUzytStart();
	SearchGarazWlicz();
	policzProjekty();

	$('#search_0_clean').click(function()
	{
		$('input:checked').attr("checked","");
		$('input:text').val('');
		$("li:has(input)").each(function()
		{
			$(this).css("color","white");
		});
	});

	$('#szukaj1_select_pow').change(function()
	{
		SearchPowUzyt();
	});

	$('#garaz_select').change(function()
	{
		SearchGaraz();
	});

	$('.sel_gar').click(function(){
		$('#garaz_select').val(1);
	});


	$('input.sel_gar').click(function()
	{
		SearchGarazWlicz();

	});

	$('input.garaz').click(function()
	{
		SearchGaraz();
	});

	$('#menu_left').click(function(){
		d = NEWchangeVisible(1, 'change');
		$.ajax({
			url: "/index.php?module=ajax&action=setVisible",
			data: "search=1&visible="+d,
			type: "post"
		})
	});
	$('#search_0_szukaj2').click(function(){
		d = NEWchangeVisible(2, 'change');
		$.ajax({
			url: "/index.php?module=ajax&action=setVisible",
			data: "search=2&visible="+d,
			type: "post"
		})
	} );

	$('#szukaj_nazwa').change(function()
	{
		$(this).val($(this).val().replace("&", "").replace(/\"/g, ""));
	});

//	alert($("input:checked").length);

	$("li:has(input:checked)").each(function()
	{
		$(this).css("color","#FF6600");
	});


	$('#search_1_form input').change(function(){
		policzProjekty();
	});
	$('#search_1_form select').change(function(){
		policzProjekty();
	});


	$('#search_pow_uzyt_1').change(function(){
		SearchPowUzytStart();
	})
	$('#search_pow_uzyt_2').change(function(){
		SearchPowUzytStart();
	})



	$('.projekt_box img').mouseenter(function(){
		clearInterval(intervalID);
		//clearInterval(intervalID2);
		elem_to_blur = $(this);

		prj_opacity[$(this).parent().attr("id")] = 1.0;
		$(this).css("opacity", "1");

		intervalID = window.setInterval(rozjasnij, 15);
	})
	$('.projekt_box img').mouseout(function(){
		//clearInterval(intervalID2);
		clearInterval(intervalID);
		$(this).css("opacity",1);
	})

	$('#box_popularne img').mouseenter(function(){
		clearInterval(intervalID);
		//clearInterval(intervalID2);
		elem_to_blur = $(this);

		prj_opacity[$(this).parent().attr("id")] = 1.0;
		$(this).css("opacity", "1");

		intervalID = window.setInterval(rozjasnij, 15);
	})
	$('#box_popularne img').mouseout(function(){
		//clearInterval(intervalID2);
		clearInterval(intervalID);
		$(this).css("opacity",1);
	})

	$('#box_promocje img').mouseenter(function(){
		clearInterval(intervalID);
		//clearInterval(intervalID2);
		elem_to_blur = $(this);

		prj_opacity[$(this).parent().attr("id")] = 1.0;
		$(this).css("opacity", "1");

		intervalID = window.setInterval(rozjasnij, 15);
	})

	$('#box_promocje img').mouseout(function(){
		//clearInterval(intervalID2);
		clearInterval(intervalID);
		$(this).css("opacity",1);
	})

	$('.button_schowek_add').click( function(){
		var id = $(this).attr('id').replace("button_schowek_add","");
		schowek_add(id);
		$(this).hide();
		$('#button_schowek_del'+id).show();
	})

	$('.button_schowek_del').click( function(){
		var id = $(this).attr('id').replace("button_schowek_del","");
		schowek_del(id, "szukaj");
		$(this).hide();
		$('#button_schowek_add'+id).show();
	})

	$('.button_schowek_del2').click( function(){
		var id = $(this).attr('id').replace("button_schowek_del","");
		schowek_del(id, "schowek");
	})





	$('a.popup').each(function(){
		$(this).attr("title", $(this).attr("title")+" (Otwiera się w nowym oknie)");
	})


	$('a.popup').click( function(){
		var url = $(this).attr("href");
		var target = $(this).attr("target") || "_blank";
		//alert(url+" "+target);
		var theWindow = kreo_popup(url, target);
		return false;
	});



});

var prj_opacity = new Array();

function kreo_popup(url, target, features) {
  if (isUndefined(features)) {
    features = _POPUP_FEATURES;
  }
  if (isUndefined(target)) {
    target = '_blank';
  }

  return window.open(url, target, features);
}


function isUndefined(v) {
    var undef;
    return v===undef;
}

function rozjasnij(){
	//console.debug("rozjasniam: prj_opacity["+elem_to_blur.parent().attr("id")+"] = "+prj_opacity[elem_to_blur.parent().attr("id")]);
	prj_opacity[elem_to_blur.parent().attr("id")]-=0.06;
	//console.debug(prj_opacity);
	var o = prj_opacity[elem_to_blur.parent().attr("id")];
	var o_str = o.toString();
	o_str.substring(0,4);
	//console.debug("rgba(255,255,255,"+o_str+")");
	elem_to_blur.css("opacity", o_str);
	if ( prj_opacity[elem_to_blur.parent().attr("id")] < 0.7 )
		clearInterval(intervalID);
}

function przyciemnij(){
	prj_opacity[elem_to_blur.parent().attr("id")]+=0.06;
	//	console.debug("prj_opacity["+elem_to_blur.parent().attr("id")+"] = "+prj_opacity[elem_to_blur.parent().attr("id")]);
if ( prj_opacity[elem_to_blur.parent().attr("id")] >= 1.0 ){
		clearInterval(intervalID2);
	}
	//console.debug(prj_opacity);
	var o = prj_opacity[elem_to_blur.parent().attr("id")];
	var o_str = o.toString();
	o_str.substring(0,4);
	if ( !o )
		elem_to_blur.css("opacity", "1");
	else
		elem_to_blur.css("opacity", o_str);
}


/**
 * funkcja czyszczaca pola wyszukiwarki
 * potrzebna bo samo reset czyści tylko pola
 * zaznaczone przez usera przed wyslaniem zapytania
 * a jak przyjdzie zapamiętany np. checked przy checkboksie
 * to zwykły reset go nie odznaczy
 * no i nie zresetuje licznika
 **/
function szukajReset(){
	$('#search_1_form input:text').val("");
	$('#search_1_form select').val(0);
	$('#search_1_form input:checkbox').attr("checked", false);
	$('#search_1_form li').css("color", "");
	policzProjekty();
}


function NEWchangeVisible(search, action)
{

	elem = (search == '1') ? '#search_A' : '.hiddable';

	if ( action == 'change' )
		display = ( $(elem).css("display") == 'block' ) ? 1 : 0;
	else if ( action == 1 )
		display = 0;
	else if ( action == 0 ){
		display = 1;
		hiddable_visible = 1;
	}

	if (elem=="#search_A"){
		var e = 1;
		if(display){
			$('#plus').css("background-image","url(/images/big+.gif)");
			$(elem).slideUp();
			var d = 0;
		} else {
			$(elem).slideDown(function(){
				adjustContent()
			});
			$('#plus').css("background-image","url(/images/big-.gif)");
			var d = 1;
		}

	} else {
		var e = 2;
		if (hiddable_visible){
			$(elem).slideUp(function(){$(this).css("display","none")});
			$('#search_0_szukaj2').html('wyszukiwanie zaawansowane');
			hiddable_visible = 0;
			var d = 0;
		} else {
			$('#plus').css("background-image","url(/images/big-.gif)");
			if ( $("#search_A").css("display") == 'none' ){
				$("#search_A").slideDown(function(){
					$(elem).slideDown(function(){adjustContent()});});
			} else
				$(elem).slideDown(function(){adjustContent()});
			$('#search_0_szukaj2').html('wyszukiwanie podstawowe');
			hiddable_visible = 1;
			var d = 1;
		}
	}
	return d;


}


function adjustContent(){
	var content_h = $('#content').height();
	var search_h = $('#search_A').height()+$('#search_0_szukaj2').height()+30;

	if ( content_h < search_h ){
		$('#content').css("height", search_h+"px");
	}
}


function SetVisible()
{

	$.post("/index.php?module=ajax&action=getVisible",{},function(data)
	{
		NEWchangeVisible(1,data.podstawowa);
		NEWchangeVisible(2,data.dodatkowa);
	},"json");

}

function changeVisible(search,mod,test)
{

	elem = (search == '1') ? '#search_A' : '.hiddable';

		//alert(mod);
		display = !mod;
		if(test)
		display = ( $(elem).css("display") == 'block' ) ? 1 : 0;

		//alert( mod+' '+display);

		if(mod)
		{
			if(!display)
			{
				if (elem=="#search_A"){
					$(elem).slideUp();
					$('#plus').css("background-image","url(/images/big+.gif)");
				}
				else {
					$(elem).slideUp(function(){$(this).css("display","none")});
					$('#search_0_szukaj2').html('wyszukiwanie zaawansowane');
				}
				//alert(elem+' zwin (1)');
			}
			else
			{
				if (elem=="#search_A"){
					$(elem).slideDown(function(){
						var content_h = $('#content').height();
						var search_h = $('#search_A').height()+$('#search_0_szukaj2').height()+30;

						if ( content_h < search_h ){
							$('#content').css("height", search_h+"px");
						}
					});
					$('#plus').css("background-image","url(/images/big-.gif)");
				} else {
					$(elem).slideDown(function(){
						var content_h = $('#content').height();
						var search_h = $('#search_A').height()+$('#search_0_szukaj2').height()+30;

						if ( content_h < search_h ){
							$('#content').css("height", search_h+"px");
						}});
					$('#search_0_szukaj2').html('wyszukiwanie podstawowe');
				}
				//alert(elem+' rozwin (2)');
			}


		}
		else
		{
			if(display)
			{
				if (elem=="#search_A"){
					$(elem).slideDown(function(){
						var content_h = $('#content').height();
						var search_h = $('#search_A').height()+$('#search_0_szukaj2').height()+30;

						if ( content_h < search_h ){
							$('#content').css("height", search_h+"px");
						}


					});
					$('#plus').css("background-image","url(/images/big-.gif)");
				} else {
					$(elem).slideDown(function(){
						var content_h = $('#content').height();
						var search_h = $('#search_A').height()+$('#search_0_szukaj2').height()+30;

						if ( content_h < search_h ){
							$('#content').css("height", search_h+"px");
						}});
					$('#search_0_szukaj2').html('wyszukiwanie podstawowe');
				}
				//$('.hiddable').slideDown();
				//alert(elem+' rozwin (3)');
			}
			else
			{
				if (elem=="#search_A"){
					$(elem).slideUp();
					$('#plus').css("background-image","url(/images/big+.gif)");
				}
				else {
					$(elem).slideUp();
					$('#search_0_szukaj2').html('wyszukiwanie zaawansowane');
				}
				//alert(elem+' zwin (4)');
			}
		}

		/*
		var content_h = $('#content').height();
						var search_h = $('#search_A').height()+$('#search_bottom').height();

						if ( content_h < search_h ){
							$('#content').css("height", search_h+"px");
						}*/

}

function policzProjekty(){
	var queryString = $('#search_1_form').formSerialize();
	//var queryString = 'bzdura';

	$.ajax({
		url: "/index.php?module=ajax&action=ile",
		data: queryString,
		type: "post",
		success: function(resp){
			$('#liczba_projektow').html(resp);
		}
	})

}

function liczMTM(){
    $.ajax({
        url: "/index.php?module=ajax&action=liczMTM"
    });
}


function SearchGarazWlicz()
{
	gar = $('input.sel_gar:checked').val();
	if( gar == 1 || gar == 2 )
	$('#wliczony_container').show();
	else if( gar == 0 )
	$('#wliczony_container').hide();
}



function SearchGaraz()
{
	if( $('#garaz_select').val() != 1 ){
		$('.sel_gar').removeAttr("checked");
		//$('.sel_gar').removeAttr("value");
	}else
		$('#garaz_container').hide();


}


function SearchPowUzyt()
{
	pow_uzyt = $('#szukaj1_select_pow').val();

	if(pow_uzyt == 2 )
	{
		$('#search_pow_uzyt_1').val(0);
		$('#search_pow_uzyt_2').val(100);
	}
	else if(pow_uzyt == 4 )
	{
		$('#search_pow_uzyt_1').val(100);
		$('#search_pow_uzyt_2').val(120);
	}
	else if(pow_uzyt == 8 )
	{
		$('#search_pow_uzyt_1').val(120);
		$('#search_pow_uzyt_2').val(160);
	}
	else if(pow_uzyt == 16 )
	{
		$('#search_pow_uzyt_1').val(161);
		$('#search_pow_uzyt_2').val(200);
	}
	else if(pow_uzyt == 32 )
	{
		$('#search_pow_uzyt_1').val(200);
		$('#search_pow_uzyt_2').val('');
	}
	else if(pow_uzyt == 64 )
	{
		$('#pow_container').show();
	} else {
		$('#search_pow_uzyt_1').val('');
		$('#search_pow_uzyt_2').val('');
	}
}



function SearchPowUzytStart(){
	if ( $('#search_pow_uzyt_1').val() || $('#search_pow_uzyt_2').val())
		$('#szukaj1_select_pow').val(64);
	else
		$('#szukaj1_select_pow').val(0);
}


function schowek_add(id)
{
	$.post("/index.php?module=ajax&action=schowek_add",{'id':id},function()
	{
        $('#schowek_num').html( parseInt( $('#schowek_num').html() ) + 1 );
		//alert('Dodano projekt do schowka');
		$('#schowek_button_'+id).attr("onclick", "");

	});

	return false;
}

function schowek_del(id, page)
{
	$.post("/index.php?module=ajax&action=schowek_del",{'id':id},function()
	{
        $('#schowek_num').html( parseInt( $('#schowek_num').html() ) - 1 );
		//alert('Usunięto projekt ze schowka');
		if (page=="schowek")
			location.reload(true);
	});
	return false;
}


function toggle_field(id){
	$('#'+id).toggle("slow");
	$('#'+id+'_img').attr("src", $('#'+id+'_img').attr("src").replace("+", "__plus__").replace("-", "__minus__").replace("__plus__", "-").replace("__minus__", "+"));
}


function pokaz_lustro(id){
	if (lustro == 1){
		$('.wersja_podst').show();
		$('.wersja_lust').hide();

		/*$('img').each( function(){
			//zmien srce
			var new_src = $(this).attr("src").replace("_lustro", "");
			$(this).attr("src", new_src);

			//zmien hrefy
		});*/

		lustro = 0 ;
		$('#lustro').attr("src", "/images/wersja_lustrzana.jpg")
	} else {
		$('.wersja_podst').hide();
		$('.wersja_lust').show();
		/*
		$('img').each( function(){
			//zmien srce

			var nazwa = $(this).attr("src").split(".");
			var new_src = nazwa[0] + "_lustro." + nazwa[1];
			$(this).attr("src", new_src);

			//zmien hrefy

		});*/

		lustro = 1 ;
		$('#lustro').attr("src", "/images/wersja_podstawowa.jpg")
	}
}


function GoTo(adres)
{
	location.href=adres;
}


function new_window(url,w,h)
{
	if(!w)
		var w = 990;
	if(!h)
		var h = 800;

	window.open(url, '', "toolbar=no, location=no, scrollbars=yes, resizable=yes, width="+w+", height="+h);
}


function wopen(url)
{
	window.open(url, '','');
}

