$.ajaxSetup ({
		cache: false
	});
var ajax_load = "<center><img src='../img/load.gif' alt='loading...' /><br /><p>nahrávám data...</p></center>";
var scriptUrl = "admin_scripts.php";
var contentUrl = "/source/content.php";
var contentHeight;

$(document).ready(function(){
 /* $("#menu > span > a").click(function(e){
    e.preventDefault();
    var id = $(this).attr("id");
    $("#content").html("<center><img src='/img/load.gif' alt='loading...' /><br /><p>nahrávám data...</p></center>").load(contentUrl,{page: id});  
    });      */
  $("#admin_menu > a").click(function(e){
    var action = this.name;
    var loadPrices = "no";
    e.preventDefault();
    if(action == "add" || action == "edit") {
      loadPrices = "yes";
      };
    $("#resources").html(ajax_load).load(scriptUrl,{action: action, load_prices: loadPrices},function(){
      $(this).hide().fadeIn();
      });
    }); 
  $("#resources").html(ajax_load).load(scriptUrl,{load_prices: "yes"}); 
  $("#resources_edit").html("");
  $("#content").hide().animate({
    opacity: 'show',      
    },300);
  contentHeight = $("#content").height();
  if($("#content").height() < 600) $("#content").height(600);

  /* + Label align */
  var max = 0;
  $("label").each(function(){
    if ($(this).width() > max)
      max = $(this).width();   
    });
  $("label").width(max);
  /* - Label align*/
  
});


/* + AJAX refresh scripts */
$(document).ajaxComplete(function() {
  var timeReload = Math.round(new Date().getTime() / 1000);
  $("#EditSubmit").click(function(e){
    var desc = $("select[name=description]").val();
    if($("select[name=r]").attr("value") == "remove") {     
      var confirmDeletion = confirm("Opravdu odstranit "+desc+"?");
      if(confirmDeletion == false) e.preventDefault(); 
      }
    else {
      e.preventDefault();
      $("#resources_edit").html(ajax_load).load(scriptUrl,{description: desc});
      }    
    });
  $("#content").animate({
    opacity: 'show',      
    },300);
  $("#content").height(contentHeight+$("#resources").height());
  if($("#content").height() < 600) $("#content").height(600);
  
  /* + Modify page menu */
  $("#modify_menu > a").click(function(e){
    e.preventDefault();
    $("#resources_edit").html(ajax_load).load(scriptUrl,{place: this.name});
    });
  /* - Modify page menu */
  
  /* + Modify page elements */
  $("span[name=edit_page_element] > a").click(function(e) {
    var created = $(this).parent("span").attr("id");
    e.preventDefault();
    if(this.name == "edit"){
      $(this).parent("span").next().next("div").html(ajax_load).load(scriptUrl,{created: created});
      }
    else if(this.name == "delete"){
      var confDel = confirm("Opravdu smazat položku?");
      if(confDel == true) $(this).parent("span").next().next("div").html(ajax_load).load(scriptUrl,{remove: created},function(){
        $("#"+created).next("br").remove();
        $("#"+created).next("div").remove();
        $("#"+created).next("br").remove();
        $("#"+created).remove();
        });  
      }
    if(this.name == "add"){
      var place = $("input[name=place]").val();
      var time = Math.round(new Date().getTime() / 1000);
      var div = $(this).parent("span").next().next();
      $.post(scriptUrl,{add: place, created: time},function(){
        div.after($('<br /><span id="'+time+'" name="edit_page_element" style="float:right;"><a href="" name="edit">Upravit</a> | <a href=\"\" name=\"delete\">Smazat</a></span><br /><div>Nový záznam</div>'));      
        });
      }   
    });
    
  $("input[name=modify_element]").click(function(e){
    e.preventDefault();
    var textarea = $(this).prev("textarea");
    $(this).parent().html(ajax_load).load(scriptUrl,{created: textarea.attr("name"),content: textarea.val()});    
    });
  /* - Modify page elements */
  
  /* + Label align */
  var max = 0;
  $("label").each(function(){
    if ($(this).width() > max)
      max = $(this).width();   
    });
  $("label").width(max);
  /* - Label align */
  
  /* + Group edit rules */
  $("input[name=group_edit]").focus(function(){
    $("#edit_info").slideDown();
    });
  $("input[name=group_edit]").blur(function(){
    $("#edit_info").slideUp();
    });
  /* - Group edit rules */
  
  });
/* - AJAX refresh scripts */     

function ChangeTab(page) {
  document.change_tab.r.value = page;
  document.change_tab.submit();
  }

function LoadFunction(place) {
    var opt = document.getElementsByTagName('option');
    for(var i in opt){
      if(opt[i].value == place) opt[i].selected = true;
      }
  }
  
function nahledShow() {
  Encoder.EncodeType = "entity";
  var html = "";
  document.getElementById("preview").innerHTML = Encoder.htmlDecode(document.getElementsByTagName('textarea')[0].value);
  }  
  
$(document).ready(function() {
  $("a[href*=/anywayautoskola-praha/am]").click(function(e) {
    e.preventDefault();
    $(this).parent().next().load('/inc/motorcycles_am.php',function() {
      $(this).slideToggle('slow');
      });
    });
  });  
