var n=0; 
var zoom = 1.1;
var move = -1;
var stop = "false";
var tip_car_num = 0;  

function writeConsole(content) {
 top.consoleRef=window.open('','myconsole',
  'width=350,height=250'
   +',menubar=0'
   +',toolbar=1'
   +',status=0'
   +',scrollbars=1'
   +',resizable=1')
 top.consoleRef.document.writeln(
  '<html><head><title>Console</title></head>'
   +'<body bgcolor=white onLoad="self.focus()">'
   +content
   +'</body></html>'
 )
 top.consoleRef.document.close()
}

//******************************************************************************
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); 
            else parts[key] = array2json(value);
        } else {
            var str = "";
            str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}    

// provede filtr podle parametru
//******************************************************************************
function change_par(blog_typ, this_par_id, this_html_id, to_change_par_id, to_change_html_id, type) {
  //
  if (to_change_par_id > 0) {
   $("#"+to_change_html_id).children().remove();
   $(new Option("", "")).appendTo('#'+to_change_html_id);
   //
   sel_val_id = $("#"+this_html_id).val();
   //
   url = '/modules/blog/blog_f_pars_json.php?this_par_id='+this_par_id+'&sel_val_id='+sel_val_id+'&to_change_par_id='+to_change_par_id;
   //
   $.getJSON(url, function(data) {
    $.each(data, function (index, item) {
      $(new Option(item.val, item.val)).appendTo('#'+to_change_html_id);
    });
   });
   //
  }
  //
  ajax_get_gal(blog_typ,1,"",type);
  //
}

// otevre okno pro vyber adresare
//******************************************************************************
function fnc_view_dir( name, element ) {
 retObj = name;
 el = element;
 window.open('incl/adm_view_dir.php', 'view_dir','width=800,height=450');
}

// vrati se zpet
//******************************************************************************
function zpet(){ 
 document.frm_zpet.submit();
} 

// prime submitnuti formu
//******************************************************************************
function submitForm( formName ) {
 document[formName].submit();
}

// zmena stranek +- jedna
//******************************************************************************
function fnc_page( pageNumber ) {
 document.frm_submit.page.value = pageNumber;
 document.frm_submit.submit();
}

//
//******************************************************************************
function fnc_editdel( id, action ) {
 document.frm_editdel.id.value = id;
 document.frm_editdel.action.value = action;
 document.frm_editdel.submit();
}

//
//******************************************************************************
function DoNav(theUrl){
  document.location.href = theUrl;
}

//
// fade delay
//******************************************************************************
jQuery.fn.fadeDelay = function() {
 delay = 0;
 return this.each(function() {
  $(this).delay(delay).fadeIn(1500);
  delay += 500;
 });
};

//
// ajax error
//******************************************************************************
//$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
//      alert(textStatus);
//      alert(errorThrown);
//      alert(XMLHttpRequest.responseText);
//}});

//******************************************************************************
var utf8_decode = function(utftext){ 
 var string = ''; 
 var i = 0; 
 var c = c1 = c2 = 0; 

 while( i < utftext.length ){ 
  c = utftext.charCodeAt(i); 

  if( c < 128 ){ 
   string += String.fromCharCode(c); 
   i++; 
  } else if( (c > 191) && (c < 224) ) { 
   c2 = utftext.charCodeAt( i+1 ); 
   string += String.fromCharCode( ((c & 31) << 6) | (c2 & 63) ); 
   i += 2; 
  } else { 
   c2 = utftext.charCodeAt( i+1 ); 
   c3 = utftext.charCodeAt( i+2 ); 
   string += String.fromCharCode( ((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63) ); 
   i += 3; 
  } 
 } 
 return string; 
};

function ajax_menu (name, id, blog_typ, type){
 //
 document.all[name].value = id;
 ajax_get_gal(blog_typ, 1, "", type); 
 //
}
         

//******************************************************************************
function ajax_get_gal(blog_typ, page, search, type){
 //
 var spec_params = new Array();
 // 
 $('#spec_params > input, select').each(function() { 
   check = false;
   //
   name = $(this).attr('name');
   checked = $(this).attr('checked');
   value = $(this).val();
   //
   if (name.substring(0,5) === "filtr") {
    if (value != "" && value != "on" ) { check = true; val = value; }
    if (checked == true) { check = true; val = checked; }
   }
   
   if (check) { spec_params[name] = val; }
   // 
 });
 var spec_json = array2json(spec_params);
 //
 //alert(spec_json);
 $('#blog_seznam_div').empty();
 $('#blog_paging').empty();
 $('#blog_paging_down').empty();
 
 //
 url = '/modules/blog/blog_f_seznam_json.php?blog_typ='+blog_typ+'&filtr='+spec_json+'&page='+page+'&search='+search+'&type='+type;
 //
 var all_count = 1;
 //
 $.getJSON(url, function(data) {
  $.each(data, function (index, item) {   
    if (item.par_type == "item") {
      //
      $.ajax({
        url: "/modules/blog/blog_tmpl_"+type.toLowerCase()+".html",
        cache: false,
        async:false,
        success: function(html){
          //
          html = html.replace("#HREF#", item.href);
          html = html.replace("#NADPIS#", utf8_decode(item.zkratka));
          html = html.replace("#HTML#", utf8_decode(item.html));
          html = html.replace("#IMG#", item.img);
          html = html.replace("#SIZES#", item.sizeIMGwidth + " " + item.sizeIMGheight);
          html = html.replace("#IMG_ID#", item.imgid);
          //
          $("#blog_seznam_div").append(html);
          //
        }
      });
    }
    if (item.par_type == "system") {   
     all_count = item.all_count;
     sel = item.select;
    }
  });
  //
  $(".gal_img").fadeDelay();
  if (type == "table") {
   var stranek = Math.ceil(all_count / 50);
   for (i=1;i<=stranek;i++){
    if (i == page) {
     $('#blog_paging').append("<span class='gal_page_curr'>"+i+"<span>");
     $('#blog_paging_down').append("<span class='gal_page_curr'>"+i+"<span>");
    } else {
     $('#blog_paging').append("<a href='javascript:ajax_get_gal("+blog_typ+","+i+",\"\",\"\")' class='gal_page'>"+i+"</a>");
     $('#blog_paging_down').append("<a href='javascript:ajax_get_gal("+blog_typ+","+i+",\"\",\"\")' class='gal_page'>"+i+"</a>");
    }
   }
   $('#blog_paging_down').append("&nbsp;<span>nalezeno: "+all_count+"</span>");
   $('#blog_paging_down').show();
  }
  //
 });
 //
}

//******************************************************************************
$(document).ready(function() {
	 
 //
 $('img,span,a,.tip,input,select').hoverbox(); 
 //
     
	 
});

