var MagnatSchowek = 
{
	Refresh : function()
	{
		$('#schowek').showAjaxIndicator();
		
		$.post(ajaxLinks.MagnatSchowekRouter,
				  {},
					  function(data){

					  $('#schowek').html(data);
					  $('#schowek').hideAjaxIndicator();
                   
                    $.fn.jScrollPane.defaults.showArrows = true;
                    $('.scroll-pane').jScrollPane();
                    // check for what is/isn't already checked and match it on the fake ones
                    $("input:checkbox").each( function() {
                        (this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
                    });
                    // function to 'check' the fake ones and their matching checkboxes
                    $(".fakecheck").click(function(){
                        ($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
                        $(this.hash).trigger("click");
                        return false;
                    }); 
				  }
			);
	},

	Toggle : function()
	{
	    if($('#schowekZawartosc').hasClass('ukryj-schowek'))
		{
		    	$('#schowekZawartosc').removeClass('ukryj-schowek');
				$('#schowekZawartosc').addClass('pokaz-schowek');
		}
	    else
		{
		    	$('#schowekZawartosc').addClass('ukryj-schowek');
				$('#schowekZawartosc').removeClass('pokaz-schowek');
		}
		
		return false;
	},
	
	SelectAll : function()
	{
		$('#schowekZawartosc input[type=checkbox]').attr('checked', true);
		$('#schowekZawartosc a[id^=fakeschowekItem]').addClass('fakechecked');
		return false;
	},
	
	RemoveSelected : function()
	{
		$('#schowek').showAjaxIndicator();
		
		var elementy = new Array();
		$('#schowekZawartosc input[type=checkbox]:checked').each(
			function()
			{
				elementy[elementy.length] = $(this).val();
			}
		);
		
		$.post(ajaxLinks.MagnatSchowekRouter+"&akcja=usun",
				  {'usun_elementy[]' : elementy},
					  function(data){
					  $('#schowek').html(data);
					  $('#schowek').hideAjaxIndicator();

                    $.fn.jScrollPane.defaults.showArrows = true;
                    $('.scroll-pane').jScrollPane();
                    // check for what is/isn't already checked and match it on the fake ones
                    $("input:checkbox").each( function() {
                        (this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
                    });
                    // function to 'check' the fake ones and their matching checkboxes
                    $(".fakecheck").click(function(){
                        ($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
                        $(this.hash).trigger("click");
                        return false;
                    });
				  }
			);
			
		return false;
	}
	
	
}


var MagnatKreator =
{
	Wyslij : function(id_wersji_pokoju)
	{
		$('#WyslijFormularz').dialog({ modal: true,  buttons: { "ANULUJ" : function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val("");}, "Ok": function() { $(this).dialog("close"); MagnatKreator.WyslijPotwierdz(); } } });
		$('#id_wersji_pokoju').val(id_wersji_pokoju);
	},
	
	WyslijPotwierdz : function()
	{
		var id_wersji_pokoju = $('#id_wersji_pokoju').val();
		var id_wersji_schowka = $('#id_wersji_schowka').val();
		var email = $('#sendto_mail').val();
		
		$.post(ajaxLinks.MagnatWyslij+"&akcja=wyslij",
			  {id_wersji_pokoju : id_wersji_pokoju, id_wersji_schowka : id_wersji_schowka, email: email},
				  function(data){
                  
                    var dat = data.split('#');
                  
                      if(dat[0] == 'error')
                      {
                            $('#wyslijKomunikat').html(dat[1]);
                            $('#WyslijFormularz').dialog({ modal: true,  buttons: { "ANULUJ" : function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val("");}, "Ok": function() { $(this).dialog("close"); MagnatKreator.WyslijPotwierdz(); } } });
    
                      }
                      else
                      {
                        $('#wyslijKomunikatOk').html(dat[1]);
                        $('#WyslijFormularzOk').dialog({ modal: true,  buttons: { "Ok": function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val(""); } } });
                      }
			  }
		);
	},
	
    WyslijSchowek : function()
	{
		$('#WyslijFormularz').dialog({ modal: true,  buttons: { "ANULUJ" : function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val("");}, "Ok": function() { $(this).dialog("close"); MagnatKreator.WyslijSchowekPotwierdz(); } } });

	},
	
    WyslijSchowekPotwierdz : function()
	{
		var id_wersji_schowka = $('#id_wersji_schowka').val();
		var email = $('#sendto_mail').val();
		
		$.post(ajaxLinks.MagnatWyslij+"&akcja=wyslijschowek",
			  {id_wersji_schowka : id_wersji_schowka, email: email},
				  function(data){
                    
                    var dat = data.split('#');
                    
                  if(dat[0] == 'error')
                  {
                        $('#wyslijKomunikat').html(dat[1]);
                        $('#WyslijFormularz').dialog({ modal: true,  buttons: { "ANULUJ" : function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val("");}, "Ok": function() { $(this).dialog("close"); MagnatKreator.WyslijSchowekPotwierdz(); } } });

                  }
                  else
                  {
                    $('#wyslijKomunikatOk').html(dat[1]);
                    $('#WyslijFormularzOk').dialog({ modal: true,  buttons: { "Ok": function() { $(this).dialog("close"); $(".wyslijKomunikat").html(""); $("#sendto_mail").val(""); } } });

                  }
			  }
		);
	},
	
    Drukuj : function()
	{
		$.post(ajaxLinks.MagnatDrukuj,
				  {},
					  function(data){
                        alert(data);				  }
			);
	}
}
