var n=0; 
var zoom = 1.1;
var move = -1;
var obj = '.nahledy_odkaz';
var stop = "false";  

//******************************************************************************
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
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + 
    '}';//Return associative JSON
}


// 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 nahledy() {
  var npoc = 1;
   $('SPAN').each(function(index) {
    if ( $(this).parent().parent().parent().get(0).id === "nahledy" ){
     var act = $(this).css('opacity');
     if (n==npoc) {
      if (act<0.8) $(this).stop().fadeTo(2000,0.7); 
     }
     if (npoc<n && npoc>0) {
      if (act<0.8) $(this).stop().fadeTo(1000,0); 
     }
     npoc++;
    }
   });
   if (n>npoc) n=0;
   var timer = setTimeout(function(){ n++; nahledy(); }, 2000);
}

//
// fade delay
//******************************************************************************
jQuery.fn.fadeDelay = function() {
 delay = 0;
 return this.each(function() {
  $(this).delay(delay).fadeIn(350);
  delay += 50;
 });
};

//
// ajax error
//******************************************************************************
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
      alert(textStatus);
      alert(errorThrown);
      alert(XMLHttpRequest.responseText);
}});


//******************************************************************************
function ajax_get_gal(blog_typ){
 //
 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();
 //
 url = '/modules/blog/blog_f_seznam_json.php?blog_typ='+blog_typ+'&filtr='+spec_json;
 //
 $.getJSON(url, function(data) {
  $.each(data, function (index, item) {
    $('#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>"+item.zkratka+"</span></a></div>");
  });
  $(".gal_img").fadeDelay();
 });
}

  $(window).resize(function(){
    browserwidth = $(window).width();
    browserheight = $(window).height();
    $(".slideshow > img").each(function(){ $(this).css("width", browserwidth); });
  });

//******************************************************************************
$(document).ready(function() {
	 
	// 
  setTimeout(function(){ nahledy(); }, 500);
  //
  $('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);
  });
    
	//    
  $('.slideshow').cycle({
    fx:      'fade',
    width: '100%',
    height: 'auto',
    timeout:   20000,
    random: 1
	});
	
	//
	$('#filtr_vyhledat').click(function() {
   //
   ajax_get_gal($(this).attr('name'));
   //
  });

	 
});
