var n=0; 
var zoom = 1.1;
var move = -1;
var stop = "false";
var tip_car_num = 0;  

//******************************************************************************
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) {
  //
  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,"","");
  //
}

// 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;
}

//******************************************************************************
function tip_car() {
   //
   $('.item').eq(tip_car_num).animate({
    opacity: 0,
    top: '-300px',
   }, 0, function() {
     // Animation complete.
   });
   $('.item').eq(tip_car_num-1).hide();
   $('.item').eq(tip_car_num).show();
   $('.item').eq(tip_car_num).animate({
    opacity: 1,
    top: '0px',
   }, 200, function() {
     // Animation complete.
   });
   
   // 
   var timer = setTimeout(function(){
    tip_car_num++;
    if ( tip_car_num >= $('.item').length) tip_car_num = 0; 
    tip_car(); 
   }, 10000);
   //
}

//
// fade delay
//******************************************************************************
jQuery.fn.fadeDelay = function() {
 delay = 0;
 return this.each(function() {
  $(this).delay(delay).fadeIn(500);
  delay += 50;
 });
};

//
// 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_get_gal(blog_typ, page, search, uvod){
 //
 //alert(uvod);
 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+'&uvod='+uvod;
 //
 var all_count = 1;
 //
 $.getJSON(url, function(data) {
  $.each(data, function (index, item) {
    if (item.par_type == "item") { 
     if (uvod == "") {
      $('#blog_seznam_div').append("<div class='gal_img' align='center'><a href='"+item.href+"' class='gal_img_odkaz'><img src='/"+item.img+"' "+item.sizeIMGwidth + " " + item.sizeIMGheight+" id='"+item.imgid+"' alt='foto' /><span>"+utf8_decode(item.zkratka)+"</span></a></div>");
     } else {
      $("#blog_seznam_div").append("<div class='uvod_clanek'><a href='"+item.href+"' class='gal_img_odkaz'><span><div class='blog_nadpis'>"+utf8_decode(item.zkratka)+"</div><br>"+utf8_decode(item.html)+"</span><img src='/"+item.img+"' "+item.sizeIMGwidth + " " + item.sizeIMGheight+" id='"+item.imgid+"' alt='foto' /></a></div>");
      $("#blog_seznam_div").append("<br>");
     }
    }
    if (item.par_type == "system") {
     all_count = item.all_count;
     sel = item.select;
    }
  });
  //alert(sel);
  $(".gal_img").fadeDelay();
  
  if (uvod == "") {
   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() {
	 
	// 
  //setTimeout(function(){ nahledy(); }, 1000);
  setTimeout(function(){ tip_car(); }, 0);
  //
  $('img,span,a,.tip,input,select').hoverbox();
  //
  $(obj).hover(function() {
    //	 
    width = $(obj).width() * zoom;
    height = $(obj).height() * zoom;
    $(this).find('span').stop().fadeTo(100,0.8);
  }, function() {
      $(this).find('span').stop().fadeTo(100,0);
  });

	//
  

	 
});

