$(document).ready(pageLoaderInit);

function pageLoaderInit(){
  $("a.ajaxbox").click(function(event){       
      // stop default behaviour
      event.preventDefault();
      
      // remove click border
      this.blur();

      var target = $(this); 
         
  		$.ajax({  
       type: "GET",  
       url: $(this).attr("href"),  
       data: "",  
       dataType: "html",    
       success: function(data){                
           $.fn.colorbox.close(); //close all colorbox                   
           
           var content = data;
           
            content = content.split('id="main"')[1];
            content = content.substring(content.indexOf('>')+1);
            var depth=1;
            var output='';
            while(depth>0){
              temp = content.split('</div>')[0];
              //count occurrences
              i=0;
              pos = temp.indexOf("<div");
              while(pos!=-1){
                i++;
                pos = temp.indexOf("<div",pos+1);
              }
              //end count
              depth=depth+i-1;
              output=output+content.split('</div>')[0]+'</div>';
              content = content.substring(content.indexOf('</div>')+6);
            }

            var vysledek = '<div class="cb" id="cugg">'+output;            

          $.fn.colorbox({open:true, html:vysledek});
          
          //if we use cufon or need other rebind
          $().bind('cbox_complete', function(){
              Cufon.replace('h1')('h2')('h3')('h4')('h5')('h6');
              
              //odkazy z boxu
              $('#cboxContent a[href^="http://"]')
                .attr({
                  target: "_blank", 
                  title: "Otevře mapu novém okně"
                });
          });
          
      },
            error: function (XMLHttpRequest, textStatus) {
             alert("error " + textStatus);
            }
      });
  });
};