var isSlideshowRunning = false; // Je spustená slideshow referencii? (true aj ked prebieha slideshow galerie v overlay) (true/false)
var active = null;
var startSlideShow = 0;
var clicked = false;
var in_overlay = 0; // zobrazit overlay pri slideshow referencii? (1/0)
var in_gallery = 0; // spustit slideshow galerie pri otvoreni overlay? (1/0)
var playing=false;
var overlayClosed=true;
var line_height = 16;
var scroller = null;
var timeOut = null; // premenna do ktorej sa nastavuje setTimeOut

$(document).ready(function(){//view_fields()
 // $(".item").addClass("sel");
  scroller = $('.scrollable').jScrollPane({showArrows:true , scrollbarWidth:23, scrollbarMargin:1,animateTo:true, animateInterval:50, animateStep:5 });
});

//spracuje get a vytiahne z neho pozadovanu premennu
function getGetVar(searchStr)
{//view_fields()
   start = location.search.indexOf(searchStr)+searchStr.length+1;
   end = (location.search.indexOf('&', start) == -1)? location.search.length : location.search.indexOf('&', start);
   return unescape(location.search.substring(start, end));
}

//pre referencie, nastavuje filter
function set_filter(param,callObj) {//view_fields()
 var listContainer = $(".referencies_list");
 var refparam;
 if(param == '0'){
 	// maju sa vypisat vsetky referencie
 	refparam = "";
 } else {
 	// maju sa vypisat urcite
 	refparam = "ref=" + param + "&";
 }
 
  $.ajax({
    type:"GET",
    url:"set_filter.php",
    data: refparam+"lg="+getGetVar("lg"),
    beforeSend: function(msg) {
      if(active != null) {
        //alert('kuk');
      } else {
        //alert("ajaxovy request na setnutie filtra, uz to nehul\n\n"+param);
      }
    },
    success: function(param){//view_fields()
      var slsw_autoplay = param.charAt(0);
      param = param.substring(1);
      listContainer.html(param);

      //reset scrollbars
      $('.scrollable').jScrollPane({showArrows:true , scrollbarWidth:23, scrollbarMargin:1,animateTo:true, animateInterval:50, animateStep:5 }).parent().css('float','left');
      //reset slideshow
      jQuery().slideshow('stop');
      clicked=true;
      playing=false;
		  isSlideshowRunning = false;
      var href_sel;
      var id_reference;
      //zobrazenie noveho oznaceneho
      href_sel = $('.scrollable .referencies_list .sel').attr('href');
      if(href_sel != undefined){
	      id_reference = href_sel.substring(href_sel.search("&r=")+3);
	      change_content(id_reference);
      }
      
      //if(sel_all==1)
      //{
       $(".item").removeClass("sel");
      //}
      //else $(callObj).toggleClass("sel");
      $(callObj).toggleClass("sel");
      slideshowOverlayClosed = true;
      if(slsw_autoplay == '1'){
        updateSlideshow();
      	jQuery().slideshow('play');
      	$(".play a").css("background-position","center -23px");
      	$(".player_header .mytext").css("color","#F67812");
      }
    }
  });
}

function updateSlideshow(){//view_fields()
	startSlideShow = 1;
	i = 1;
}

function change_content_byClick(param, callObj)
{//view_fields()
  isSlideshowRunning = false;
  clicked=true;
  playing=false;
  slideshowRunned=false;
  
  //Prehodi index "i" na index kliknuteho odkazu
  var elements=$("#reference_list a");
  var current=callObj;
  var index=elements.index(current);
  i=index;
  
  change_content(param, callObj);
}

function change_content(param,callObj) {//view_fields()
if(typeof(callObj)=="undefined")
	callObj = null;

 var contentContainer = $(".referencies_content .content");
 //oznacime vsetky anchor deti v div.referencies_list ul li
 var listContainerChilds = $(".referencies_list ul li > a");
 //var headerContainer = $(".referencies_header .content");
 var headerContainer = $(".referencies_header .content");
 var footerContainer = $(".referencies .moreinfo"); 
 var reply = null;

 
 headerContainer.fadeOut('fast'); 
 contentContainer.fadeOut('fast',function(){

    var launchAjax = $.ajaxSync({
      type:"GET",
      url:"change_content.php",
      data: "r="+param+"&lg="+getGetVar("lg"),
      beforeSend: function(msg) {
        //ak sme zmackli item ked sa animovalo/prebiehal interval ajax ci daco, zrusime...
        if(callObj != null) {
          clearInterval(active); active=null; exec=null;
        }
      },
      success: function(reply) {//view_fields()
        //alert(reply)
      
        string2html = reply;
        in_overlay = string2html.charAt(0);
        in_gallery = string2html.charAt(1);
        string2html = string2html.substring(2);
        if(playing==true && in_overlay == 1) 
        {
          show_reference(param);
          clearInterval(active); active=null; exec=null;
          clicked=true;
          timeOut = setTimeout(function(){
            if(overlayClosed==false && playing==true) closeOverlay(1, this, true);
            // tu by som pridal pokracovanie v prehravani
            if(overlayClosed==true && playing==true && active==null && exec==null) jQuery().slideshow('play');
            
            /*  //Zrejme toto bola iba zaplata, ktora po oprave posuvala 'i' zbytocne
            if(slideshowOverlayClosed==false){
            	i = i - 2;
            	slideshowOverlayClosed = true;
            } else {
            	i = i - 1;
            }
            */
           
          }, 3000);
        }
        
      },
      complete: function() {//view_fields()
        
        //vyberieme si obsah v rozlisovcacich tagoch
        more = string2html.substring(string2html.search("<div class='moreinfo'>"),string2html.search("<!--moreinfo--></div>"));
  	    string2html = string2html.replace(more,'');
  	    more = jQuery.trim(more);
  	    string2html = string2html.replace("<div class='moreinfo'>",'');
        string2html = string2html.replace("<!--moreinfo--></div>",'');
  	    
        
        //vyberieme si obsah v rozlisovcacich tagoch
        sub = string2html.substring(string2html.search("<span class='hiddenHeader'>"),string2html.search("</span>"));
  	    string2html = string2html.replace(sub,'');

  	    //precistime od rozlisovacich tagov...
        sub = sub.replace("<span class='hiddenHeader'>",'');
        sub = sub.replace("</span>",'');
        sub = jQuery.trim(sub);
        //-------
        
        //ak sme zmackli item ked sa animovalo, reset play-buttonu
        if(callObj != null) {
          $(".play a").css("background-position","center 0px");	// comment added on 7.8.2008 by Harry
          $(".player_header .mytext").css("color","#BFBFBF");
        }

        //naplnime
        headerContainer.html(sub);
        footerContainer.html(more);
        //spustime color prechod na tlacitko
        $(".moreinfo a").colorBlend([{
          fromColor:"white",
          toColor:"#F67812",
          param:"background-color",
          fps:"60",
          cycles:2
        }]).colorBlend([{
          fromColor:"#898989",
          toColor:"white",
          param:"color",
          fps:"60",
          cycles:2
        }]);
    	        
        
        contentContainer.html(string2html).each(function(){
          //zobrazime
          footerContainer.fadeIn('fast');
          
          headerContainer.fadeIn('fast');
    	    contentContainer.fadeIn('fast', function(){
    	        
    	      	//oznacime aktivnu
    		      if(callObj!==null) {
    		      	listContainerChilds.removeClass("sel");
    		      	$(callObj).addClass("sel");
    		      }
    		      
    		  //tb_init('a.thickbox, area.thickbox, input.thickbox');
    	     });

         });
         
         
       }
  });
  

 });
 
}

function closeOverlay_byClick(param, callObj, slide) {
  closeOverlay(param, callObj, slide);
  jQuery().slideshow('stop');
  
  //prepocitanie indexu koli tomu ze v slideshow je posunuty o jedno dopredu
  var elements=$("#reference_list a");
  var current=$("#reference_list a.sel").get(0);
  i=elements.index(current);
  
  if (timeOut!=null) clearTimeout(timeOut);
  //view_fields()
}

var slideshowOverlayClosed = false;

function closeOverlay(param, callObj, slide) {
  if ($.browser.msie && (parseInt($.browser.version, 10) == 7 || parseInt($.browser.version, 10) == 6)) {
    $('#ajax_main_wrapper').attr('class', '');
    $('#ajax_main_wrapper').attr('class','bg1');
    //$('#ajax_overlay').fadeOut('slow', function(){
    $('#ajax_overlay').fadeOut('10000','easeOutQuad' , function(){ 
    $('#ajax_main_wrapper').attr('class', '');
    $('#ajax_main_wrapper').attr('class','bg2');
     if(slide==true || playing==true)
     {
       // jQuery().slideshow('play');
      }
    });
  }
  else
  {
    $('#ajax_overlay').fadeOut('1000','easeOutQuad' , function(){ 
      if(slide==true || playing==true) 
      {
       // jQuery().slideshow('play');
      }
    });
  }
  overlayClosed=true;
  //jQuery().slideshow('stop');
  jQuery().overlay_slideshow('stop');
  //view_fields()
}

function show_reference_byClick(param, callObj)
{//view_fields()
	isSlideshowRunning = false;
  playing=false;
  show_reference(param,callObj);
}

function show_reference(param,callObj) {//view_fields()

 var contentContainer = $(".references_content");

         $.ajaxSync({
          type:"GET",
          url:"getOverlay.php",
          data: "r="+param+"&lg="+getGetVar("lg"),
          queue: "autocomplete",
          cancelExisting: true,
          beforeSend: function(msg) {
          //ak sme zmackli item ked sa animovalo/prebiehal interval ajax ci daco, zrusime...
          if(callObj != null) {
            clearInterval(active); active=null; exec=null;
          }
        },
          success: function(param){//view_fields()
          
            $('#ajax_main').html(param);
            
            if ($.browser.msie && (parseInt($.browser.version, 10) == 7 || parseInt($.browser.version, 10) == 6)) 
            {//kvoli bugu s priesvitnym png zmeni pozadie na sede s opacitou, zanimuje a nasledne zmeni opat na povodne pozadie
              $('#ajax_main_wrapper').attr('class', '');
              $('#ajax_main_wrapper').attr('class','bg1');
              $("#ajax_reference_content").css({"filter":"alpha(opacity=100)","-moz-opacity":"1","opacity":"1"});
              
              $('#ajax_overlay').fadeIn('1200', function(){
                $('#ajax_main_wrapper').attr('class', '');
                $('#ajax_main_wrapper').attr('class','bg2');
                
              });
              
            }
            else
            {
              $('#ajax_overlay').fadeIn({duration:1000,queue:"global"});          
            }
            overlayClosed=false;
            jQuery().overlay_slideshow();
            overlay_exec = null;
		        clearTimeout(overlay_active);
		        overlay_active = null;
		        overlay_first=false;
		        overlay_i = 1;
		        //resetneme PLAY button
		        //$(".play a").css("background-position","center 0px");	// comment aaded on 7.8.2008 by Harry
		        overlay_clicked = false;
		        overlay_playing=false;
            tb_init('a.thickbox, area.thickbox, input.thickbox');
            if(callObj != null) {
              $(".play a").css("background-position","center 0px"); // comment added on 7.8.2008 by Harry
              $(".player_header .mytext").css("color","#BFBFBF");
            }
            
					  // Automaticke prehravanie galerie
            if (in_gallery==1) {
              if (timeOut!=null) clearTimeout(timeOut); 
              stopPlaying(); 
              jQuery().overlay_slideshow('play',$("#ajax_main #player .play a").get(0));
            };
            
				    
					
          }
        });
        

  contentContainer.html();
}


// Spusta alebo zastavuje slideshow s tym ze zobrazi overlay prveho (ak má)
// Doplnil Ondrej Jaura 25.6.2010
function start_slideshow(e) {
	isSlideshowRunning = true;
	
	// prirobene koli autoplay (pri autoplay niesu nastavene in_overlay a in_gallery)
	var $elem=$("#reference_list a.sel");
	if ($elem.hasClass("s11")) {in_overlay=1;in_gallery=1;}
	if ($elem.hasClass("s10")) {in_overlay=1;in_gallery=0;}
	
  if (in_overlay==1 && !playing) {
  	vyraz = "r=(\\d+)";
  	reg = new RegExp(vyraz,"g");
  	var regResult = reg.exec($elem.attr("href"));
    show_reference(regResult[1],e);
    timeOut = setTimeout(function(){
      closeOverlay(1, e, true);
      jQuery().slideshow('play',e);
    }, 3000);
  } else {
    jQuery().slideshow('play',e);
  }
  
}
function get_current_index() {
  var elements=$("#reference_list a");
  var current=$("#reference_list a.sel").get(0);
  return elements.index(current);
}

//rotacnik
var rotater = null;
//pocitadlo
var i=startSlideShow;
//ajax
var ajaxRequest=null;
//spustac
var exec = null;
//checker ci je animacia spustena
var active = null;
//urcuje
var overallCycleNum = 0;
//first time?
var first = true;
// has been running
var slideshowRunned = false;

var contentContainer = $(".referencies_content .content");
//oznacime vsetky anchor deti v div.referencies_list ul li
var listContainerChilds = $(".referencies_list ul li > a");
var headerContainer = $(".referencies_header .content");

//slideshow ^^ puki puk puk
jQuery.fn.slideshow = function(action,callObj,config) {//view_fields()
  
  $("#player .play a").addClass("mon");
  if(clicked == true) {
  
    //zistime id aktualneho 
    
    var elements=$("#reference_list a");
    var current=$("#reference_list a.sel").get(0);
    var index=elements.index(current)+1;
    //if (index<1) index=1;
  	startSlideShow=index;
    
    /* //Toto je stara verzia toho co je hore, ktora brala viac <a> tagov nez bolo treba (bralo vsetky z tagu s id="content" a pri overlay pribudli dalsie 2 odkazy)
    var testClass = new RegExp("(^|\\s)" + 'sel' + "(\\s|$)");
  	var tag = 'a';
  	var elm = document.getElementById('content');
  	var elements = elm.getElementsByTagName(tag);
  	var current;
  	var x = 0;
  	startSlideShow=0;
    do
    {
      if(elements.length == x) break;
      current = elements[x];
      vyraz = "r=\\d+";
  	  reg = new RegExp(vyraz,"g");
  	  var regResult = reg.exec($(current).attr("href"));
      if(regResult!=null)
      {
        startSlideShow++;
        //alert(startSlideShow);
  		}
  		x++;
  	} while(testClass.test(current.className)==false);
  	*/
  	
    if(exec != true || action != 'play')
    {
      exec = null;
  	  clearInterval(active);
  	  active = null;
  	  first=false;
  	  i = startSlideShow;
  	  
    }
  }
	if(action == 'play') {
		if(exec == true && action == 'play') {
		  isSlideshowRunning = false;
			clearInterval(active);
			active = null;
			exec = null;
			first = false;
			//zobrazime PLAY button
			$(callObj).css("background-position","center 0px"); // comment aaded on 7.8.2008 by Harry
			$(".player_header .mytext").css("color","#BFBFBF");
			playing = false;
		} else {
		  isSlideshowRunning = true;
		  if (first) $("#player .play a").removeClass("first");
			exec = true;
			active = true;
			//zobrazime PAUSE button
			$(callObj).css("background-position","center -23px");
			$(".player_header .mytext").css("color","#F67812");
			// pridane 7.8.2008 by Harry
			playing = true;
		}
		clicked = false;
	}
	if(action == 'stop') {
		isSlideshowRunning = false;
		exec = null;
		clearInterval(active);
		active = null;
		first=false;
		//i = 0;
		//resetneme PLAY button
		$(".play a").css("background-position","center 0px");
		$(".player_header .mytext").css("color","#BFBFBF");
		clicked = false;
		playing=false;
	}
	
	if(action==null) return false;

	var setting = jQuery.extend({
		animationSpeed : 'normal', //slow, normal, fast OR exact value in miliseconds
		changeSpeed : 3000, //speed in miliseconds for changing content minimum suggested 2000
		animatedObjectsList : $(".referencies_list a"), // name of link, that have links for ajax requests to get proper datas
		cycleItems : 2
	}, setting);
	
	return this.each(
		function() {//view_fields()
			var objList = setting.animatedObjectsList;
			
			if(typeof(objList)!='object'){
				alert("ERROR: Error accessing objects for animation!!!\n\n"+objList);
			}
			rotater = function(id) {
			//alert(i);
				//ak tocime po prvy krat, ideme na nasledujuci..
				if(first===true && i==0){id++;i++;first=false;}

				slideshowRunned = true;

				if(objList.length == 0 || objList.length == 1) return false;
					
				//zistime id aktualneho
				vyraz = "r=\\d+";
				reg = new RegExp(vyraz,"g");
				var regResult = reg.exec(objList.eq(id).attr("href"));
				if(regResult == null){
					//alert(objList.eq(id).attr("href"));
					regResult = reg.exec(objList.eq(1).attr("href"));
					regResult = String(regResult).substr(2);
					i = 1;
				} else {
					regResult = String(regResult).substr(2);
				}
				
				//odstranime oznacene
				objList.removeClass("sel");
				//oznacime aktivnu polozku v liste
				objList.eq(id).addClass("sel");
				//naplnime obsah
				
				
				//playing = true;	// commented on 7.8.2008 by Harry
				change_content(regResult);
        
        //posuvanie scrollera, aby selected item bol v strede
        if((i%objList.length)>4 && (i%objList.length)<objList.length-4)
        {
            scroller[0].scrollTo((((i%objList.length)-4)*line_height)-1);
        }
        else if((i%objList.length)<=6) scroller[0].scrollTo(0);
        else if((i%objList.length)>=objList.length-5) scroller[0].scrollTo((objList.length-11)*line_height+16);
        
				//inkrementujeme pocitadlo, ak sa da, inak znulujeme
				//pocita zaroven kolkokrat to cele zbehlo, ak je nastavene v overallCycleNum ciselna hodnota
				if(setting.cycleItems === false) {
					if(i == objList.length-1) {exec=null;clearInterval(active);first=true; active=null; i=0;$(".play a").css("background-position","center 0px");$(".player_header .mytext").css("color","#BFBFBF");}
					else {i++;}
				} else if(setting.cycleItems === true) {
					if(i == objList.length-1) {i=0;}
					else{i++;}
				} else if( typeof(setting.cycleItems) == "number" ) {
					if(setting.cycleItems == overallCycleNum) {exec=null;clearInterval(active);first=true;active=null;i=0;overallCycleNum=0;$(".play a").css("background-position","center 0px");$(".player_header .mytext").css("color","#BFBFBF");}
					else{
						if(i == objList.length-1) {i=0; overallCycleNum++;}
						else{i++;}
					}
				} else return false;
				
				
			}//ROTATER END

			if(exec == true) {
				if(setting.cycleItems===false) active = setInterval("this.rotater(i);",setting.changeSpeed);
				else if(setting.cycleItems!=overallCycleNum) active = setInterval("this.rotater(i);",setting.changeSpeed);
				else if(setting.cycleItems==overallCycleNum) {clearInterval(active); active=null}
				else {exec=null;clearInterval(active);active=0;i=0;objList.removeClass("sel");};
			} else {
				if(active != null) {
					clearInterval(active);
					active = null;
				}
			}

		}
	);  //this.each END
	
}; //fn.slideshow END

//---------------------------------------------funkcie pre overlay galeriu----------------------------------

var overlay_active = null;
var overlay_startSlideShow = 1;
var overlay_clicked = false;
var overlay_playing=false;
var scroller = null;
/*$(document).ready(function(){
  scroller = $('.scrollable').jScrollPane({showArrows:true , scrollbarWidth:23, scrollbarMargin:1 });
});
*/
function overlay_change_content_byClick(param, callObj, pager)
{
  isSlideshowRunning = false;
  overlay_clicked=true;
  overlay_playing=false;
  clearTimeout(overlay_active); 
  overlay_active=null; 
  overlay_exec=null;
  clearInterval(active); active=null; exec=null;
  clicked=true;
  playing=false;
  $("#ajax_main #player .play a").css("background-position","center 0px");// comment added on 7.8.2008 by Harry
  overlay_change_content(param, callObj, pager);
  //view_fields()
}

function overlay_change_content(param,callObj, pager) {//view_fields()
 var galery = $("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery");
 var previous_sel = $("#ajax_overlay #ajax_main_wrapper #ajax_main #player .sel");
 if(typeof(callObj)=="undefined")
	callObj = null;
  var reply = null;

 //$("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery > .image_holder").fadeOut('slow', function(){

 galery.animate({'opacity':0}, 'fast',function(){

    var launchAjax = $.ajaxSync({
      type:"GET",
      url:"get_galery.php",
      data: "r="+param+"&pg_page="+pager+"&lg="+getGetVar("lg"),
      beforeSend: function(msg) {
       /* //ak sme zmackli item ked sa animovalo/prebiehal interval ajax ci daco, zrusime...
        if(callObj != null) {
          clearInterval(overlay_active); overlay_active=null; overlay_exec=null;
        }*/
      },
      success: function(reply) {//view_fields()
        string2html = reply;
        all_reply = string2html;        
      },
      complete: function(){//view_fields()
        string2html = string2html.substring(string2html.search("<div class=\"ajax_reference_gallery\">"),string2html.search("<div id=\"player\">"));
  	    string2html = string2html.replace("<div class=\"ajax_reference_gallery\">",'');
        pos = string2html.lastIndexOf("</div>");
        string2html=string2html.substring(0, pos);
        galery.html(string2html);
        //alert(string2html); 

        left = all_reply.substring(all_reply.search("<div class=\"left\">"),all_reply.search("<ul class=\"pager\">"));
  	    left = left.replace("<div class=\"left\">",'');
        pos = left.lastIndexOf("</div>");
        left=left.substring(0, pos);
        $("#ajax_overlay #ajax_main_wrapper #ajax_main #player .left").html(left);
        //alert(left);
        
        pager = all_reply.substring(all_reply.search("<ul class=\"pager\">"),all_reply.search("<div class=\"right\">"));
  	    pager = pager.replace("<ul class=\"pager\">",'');
        pager = pager.replace("</ul>",'');
        $("#ajax_overlay #ajax_main_wrapper #ajax_main #player .pager").html(pager);
        //alert(pager);
        
        right = all_reply.substring(all_reply.search("<div class=\"right\">"));
  	    right = right.replace("<div class=\"right\">",'');
        right = right.replace("</div>",'');
        right = right.replace("</div>",'');
        //right.lastIndexOf("</div>");
        //right=right.substring(0, pos);
        $("#ajax_overlay #ajax_main_wrapper #ajax_main #player .right").html(right);
        //alert(right);                
     
        //ak sme zmackli item ked sa animovalo, reset play-buttonu
        if(overlay_clicked == true) {
          //$(".play a").css("background-position","center 0px");// comment added on 7.8.2008 by Harry
        }
        //oprava bugu s poziciovanim v ie6
        if ($.browser.msie && parseInt($.browser.version, 10) == 6) 
        {
          $("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery > .image_holder:nth-child(2)").addClass("ie6fix");
          $("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery > .image_holder:only-child").addClass("ie6fix");
        }
        //naplnime
        //$("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery > .image_holder").fadeIn('slow', function(){
        galery.animate({'opacity':100}, 'fast', function(){
          //$("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery > .image_holder > a").addClass("thickbox");
          if(callObj!==null) {
    		  	//previous_sel.removeClass("sel");
    		   	//$(callObj).addClass("sel");
    		  }
         });
        tb_init('a.thickbox, area.thickbox, input.thickbox');	
        //alert(galery.html());		  
       
        
    				// Zisti ci je dany obrazok flash a ak ano, tak nastavy v nastaveniach dobu trvania, inak da default
    				// Tato funkcia v podstate patri priamo do overlay_slideshow no koli roznemu casu flashov musi byt tu
    				if (overlay_playing) {
      				var duration=$("#ajax_main .flash_holder param[name='duration']").attr("value");
      				duration=parseInt(duration)*1000;
      			  if (duration>0) dur=duration;
      			  else dur=3000;
      			  overlay_active = setTimeout("this.overlay_rotater(i);",dur);
            }
            //Koniec
       }
  });
  

 });
 
}

function start_overlay_slideshow_byClick(action,e) {
  if (overlay_exec == true && action == 'play') action = 'stop'; 
  stopPlaying();
  jQuery().overlay_slideshow(action,e);
  isSlideshowRunning = false;
  $("#ajax_main #player .play a").addClass("mon");
  //view_fields()
}

//rotacnik
var overlay_rotater = null;
//pocitadlo
var overlay_i=overlay_startSlideShow;
//ajax
var overlay_ajaxRequest=null;
//spustac
var overlay_exec = null;
//checker ci je animacia spustena
var overlay_active = null;
//urcuje
var overlay_overallCycleNum = 0;
//first time?
var overlay_first = true;

var overlay_contentContainer = $("#ajax_overlay #ajax_main_wrapper #ajax_main .ajax_reference_gallery");
//oznacime vsetky anchor deti v div.referencies_list ul li
var overlay_listContainerChilds = $("#ajax_overlay #ajax_main_wrapper #ajax_main #player .pager .page");

//////////////////////////////////
// Zmena setInterval na setTimeout koli casovemu trvaniu flashov. Ondrej (6.Jul.2010)
//////////////////////////////////
				
//slideshow ^^ puki puk puk
jQuery.fn.overlay_slideshow = function(action,callObj,config) {//view_fields()
  
  if(overlay_clicked == true)
  { 	  
    //zistime id aktualneho 
    vyraz = "pg_page=\\d+";
		reg = new RegExp(vyraz,"g");
		var regPager = reg.exec($("#ajax_overlay #ajax_main_wrapper #ajax_main #player ul li > a.sel").attr("href"));
		if(regPager == null)
			alert("ERROR: No match found!!!");
		else
			overlay_startSlideShow = String(regPager).substr(8);
		//	alert(overlay_startSlideShow);
    if(overlay_exec != true || action != 'play')
    {
      overlay_exec = null;
  	  clearTimeout(overlay_active);
  	  overlay_active = null;
  	  overlay_first=false;
  	  overlay_i = overlay_startSlideShow;
    }
  }
	if(action == 'play') {
		if(overlay_exec == true && action == 'play') {
			clearTimeout(overlay_active);
			overlay_active = null;
			overlay_exec = null;
			overlay_first = false;
			//zobrazime PLAY button
			$(callObj).css("background-position","center 0px");
			overlay_playing = false;
			alert("tu")
		} else {
			overlay_exec = true;
			overlay_active = true;
			overlay_overallCycleNum = 0;
			//zobrazime PAUSE button
			$(callObj).css("background-position","center -23px");
		}
		overlay_clicked = false;
	}
	if(action == 'stop') {
		overlay_exec = null;
		clearTimeout(overlay_active);
		overlay_active = null;
		overlay_first=false;
		//overlay_i = 0;
		//resetneme PLAY button
		$("#ajax_main #player .play a").css("background-position","center 0px");	// comment added on 7.8.2008 by Harry
		overlay_clicked = false;
		overlay_playing = false;
	}
	if(action==null) return false;
  
  // Pri spusteni muci ceknut ci prvy nieje flash a podla neho nastavit prvy timeOut
	var duration=$("#ajax_main .flash_holder param[name='duration']").attr("value");
	duration=parseInt(duration)*1000;
  if (duration>0) dur=duration;
  else dur=3000;
  
	var setting = jQuery.extend({
		animationSpeed : 'normal', //slow, normal, fast OR exact value in miliseconds
		changeSpeed : dur, //speed in miliseconds for changing content minimum suggested 2000
		animatedObjectsList : $("#ajax_overlay #ajax_main_wrapper #ajax_main #player ul li > a"), // name of link, that have links for ajax requests to get proper datas
		cycleItems : 2
	}, setting);
	return this.each(function() {
			objList = setting.animatedObjectsList;
			
			if(typeof(objList)!='object'){
				alert("ERROR: Error accessing objects for animation!!!\n\n"+objList);
			}
			overlay_rotater = function(id) {
				//ak tocime po prvy krat, ideme na nasledujuci..
				if(overlay_first===true && overlay_i==0){id++;overlay_i++;overlay_first=false;}
        if(overlay_i>objList.length-1) overlay_i=0;
				//zistime id aktualneho
				vyraz = "r=\\d+";
				reg = new RegExp(vyraz,"g");
				var regResult = reg.exec(objList.eq(overlay_i).attr("href"));
				if(regResult == null)
					alert("ERROR: No match found!!!");
				else
					regResult = String(regResult).substr(2);
			
				vyraz = "pg_page=\\d+";
				reg = new RegExp(vyraz,"g");
				var regPager = reg.exec(objList.eq(overlay_i).attr("href"));
				if(regPager == null)
					alert("ERROR: No match found!!!");
				else
					reg_pager = String(regPager).substr(8);
      
				//naplnime obsah
				overlay_playing = true;
				
				overlay_change_content(regResult, callObj, reg_pager);
				
				// !!!! Presunute do onComplete eventu v metode overlay_change_content() koli tomu, ze to bralo informacie pred tym nez sa nahodil novy slide !!!!
				// Zisti ci je dany obrazok flash a ak ano, tak nastavy v nastaveniach dobu trvania, inak da default
				/* 
				var duration=$("#ajax_main .flash_holder param[name='duration']").attr("value");
				duration=parseInt(duration)*1000;
			  if (duration>0) dur=duration;
			  else dur=3000;
			  overlay_active = setTimeout("this.overlay_rotater(i);",dur);
			  */
				
				
        //objList.removeClass("sel");
				//objList.eq(overlay_i).addClass("sel");
        
        // Po poslednom slide, ak je spustena prezentacia referencii, prejde k dalsej referencii
        if (overlay_overallCycleNum == 1 && isSlideshowRunning) {
          closeOverlay(1, this, true);
          jQuery().slideshow('play',$("#main .play a").get(0));
        }
        
        //alert(objList.length);
				//alert(setting.cycleItems);
        //inkrementujeme pocitadlo, ak sa da, inak znulujeme
				//pocita zaroven kolkokrat to cele zbehlo, ak je nastavene v overallCycleNum ciselna hodnota
				if(setting.cycleItems === false) {
					if(overlay_i == objList.length-1) {overlay_exec=null;clearTimeout(overlay_active);overlay_first=true; overlay_active=null; overlay_i=0;$(".play a").css("background-position","center 0px");$(".player_header .mytext").css("color","#BFBFBF");}
					else {overlay_i++;}
				} else if(setting.cycleItems === true) {
					if(overlay_i == objList.length-1) {overlay_i=0;}
					else{overlay_i++;}
				} else if( typeof(setting.cycleItems) == "number" ) {
					if(setting.cycleItems == overlay_overallCycleNum) {
            overlay_exec=null;clearTimeout(overlay_active);overlay_first=true;overlay_active=null;overlay_i=0;overlay_overallCycleNum=0;$(".play a").css("background-position","center 0px");$(".player_header .mytext").css("color","#BFBFBF");
          }
					else{
						if(overlay_i == objList.length-1) {overlay_i=0; overlay_overallCycleNum++;}
						else{overlay_i++;}
					}
				} else return false; 
      }//ROTATER END
      
      
			if(overlay_exec == true) {
			   
				if(setting.cycleItems===false) overlay_active = setTimeout("this.overlay_rotater(i);",setting.changeSpeed);
				else if(setting.cycleItems!=overallCycleNum) overlay_active = setTimeout("this.overlay_rotater(i);",setting.changeSpeed);
				else if(setting.cycleItems==overallCycleNum) {clearTimeout(overlay_active); overlay_active=null}
				else {overlay_exec=null;clearTimeout(overlay_active);overlay_active=0;overlay_i=0;objList.removeClass("sel");};
			} else {
				if(overlay_active != null) {
					clearTimeout(overlay_active);
					overlay_active = null;
				}
			}

		}
	);  //this.each END
}; //fn.slideshow END

function stopPlaying (param, callObj)
{
  overlay_playing=false;
  clearTimeout(overlay_active); 
  overlay_active=null; 
  overlay_exec=null;
  clearInterval(active); active=null; exec=null;
  clicked=true;
  playing=false;
  $(".play a").css("background-position","center 0px");
  $(".player_header .mytext").css("color","#BFBFBF");
  //view_fields()
}


function FlashVideo_Ready(id){//view_fields()

	

}


function FlashVideo_Stop(id, param){//view_fields()

	

}



function ContactFormMouseOver(){//view_fields()
	$("#content #contact .contact_sub_header input.btn").css("color","#f67812");
}

function ContactFormMouseOut(){//view_fields()
	$("#content #contact .contact_sub_header input.btn").css("color","#898989");
}

function ReferenciesMoveDown(){//view_fields()
  isSlideshowRunning = false;

	stopPlaying(null, null);
	
	if(slideshowRunned==true){
		i = i - 1;
		slideshowRunned = false;
	}

	var objList = $(".referencies_list a");


	if(objList.length == 0 || objList.length == 1)
	return false;
	
	i = i + 1;
	if(i==objList.length) i = 0;
	
	vyraz = "r=\\d+";
	reg = new RegExp(vyraz,"g");
	var regResult = reg.exec(objList.eq(i).attr("href"));
	regResult = String(regResult).substr(2);
	
	objList.removeClass("sel");
	objList.eq(i).addClass("sel");
	change_content(regResult);

	//posuvanie scrollera, aby selected item bol v strede
    if((i%objList.length)>4 && (i%objList.length)<objList.length-4)
    {
        scroller[0].scrollTo((((i%objList.length)-4)*line_height)-1);
    }
    else if((i%objList.length)<=6) scroller[0].scrollTo(0);
    else if((i%objList.length)>=objList.length-5) scroller[0].scrollTo((objList.length-11)*line_height+16);


}

function ReferenciesMoveUp(){//view_fields()
  isSlideshowRunning = false;
	stopPlaying(null, null);
	
	var objList = $(".referencies_list a");

	if(slideshowRunned==true){
		i = i - 1;
		slideshowRunned = false;
	}

	if(objList.length == 0 || objList.length == 1)
	return false;
		
	i = i - 1;
	if(i==-1) i = objList.length - 1;
	
	vyraz = "r=\\d+";
	reg = new RegExp(vyraz,"g");
	var regResult = reg.exec(objList.eq(i).attr("href"));
	regResult = String(regResult).substr(2);
	
	objList.removeClass("sel");
	objList.eq(i).addClass("sel");
	change_content(regResult);

	//posuvanie scrollera, aby selected item bol v strede
    if((i%objList.length)>4 && (i%objList.length)<objList.length-4)
    {
        scroller[0].scrollTo((((i%objList.length)-4)*line_height)-1);
    }
    else if((i%objList.length)<=6) scroller[0].scrollTo(0);
    else if((i%objList.length)>=objList.length-5) scroller[0].scrollTo((objList.length-11)*line_height+16);
	
	//i = i+1;
}

////////KONZOLKA S PREMENNYMI//////////////////
///////////////////////////////////////////////
/*
function view_fields() {
  var list="";
  list += "isSlideshowRunning: "+isSlideshowRunning+"<br />";
  list += "active: "+active+"<br />";
  list += "startSlideshow: "+startSlideShow+"<br />";
  list += "clicked: "+clicked+"<br />";
  list += "in_overlay: "+in_overlay+"<br />";
  list += "in_gallery: "+in_gallery+"<br />";
  list += "playing: "+playing+"<br />";
  list += "overlayClosed: "+overlayClosed+"<br />";
  list += "line_height: "+line_height+"<br />";
  list += "scroller: "+scroller+"<br />";
  list += "<br />";
  list += "i: "+i+"<br />";
  list += "ajaxRequest: "+ajaxRequest+"<br />";
  list += "exec: "+exec+"<br />";
  list += "active: "+active+"<br />";
  list += "overallCycleNum: "+overallCycleNum+"<br />";
  list += "first: "+first+"<br />";
  list += "slideshowRunned: "+slideshowRunned+"<br />";
  list += "<br />";
  list += "overlay_i: "+overlay_i+"<br />";
  list += "overlay_ajaxRequest: "+overlay_ajaxRequest+"<br />";
  list += "overlay_exec: "+overlay_exec+"<br />";
  list += "overlay_active: "+overlay_active+"<br />";
  list += "overlay_overallCycleNum: "+overlay_overallCycleNum+"<br />";
  list += "overlay_first: "+overlay_first+"<br />";
  list += "overlay_playing: "+overlay_playing+"<br />";
  
  $("#fields").html(list);
}
$(document).ready(function(){
  $(document.body).append("<div style='position: absolute;top: 10px;left:900px;width: 200px;height: 100px; color:white;'><input type='button' value='refresh' onclick='view_fields()' /><br ><br><div id='fields'></div></div>");
});
  */