$(document).ready(function() {

    if($('#clock').length){
	   //ajaxTime.php is called every second to get time from server
	   var refreshId = setInterval(function(){
            $.ajax({
              url: '/include/ajaxTime.php',
              cache: false,
              success: function(html){
                $('#clock').html(html);
              }
            });
	   }, 1000);
    }

    if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {
        var d = new Date();
        var hour = d.getHours();
        if(!(hour > 8 && hour < 18)){
            $('body').removeClass().addClass('night').fadeIn('slow');
            $('#layout').removeClass().addClass('night').fadeIn('slow');
            $('#celestial').removeClass().addClass('night');
            $('#footerTop').removeClass().addClass('night');
        }else{
            $('body').removeClass().addClass('day');
            $('#layout').removeClass().addClass('day');
            $('#celestial').removeClass().addClass('day');
            $('#footerTop').removeClass().addClass('day');        
        }
    }
    
    if($('ul.search').length)
        $("ul.search.tabs").idTabs();

    if($('ul.news').length)
        $("ul.news.tabs").idTabs();
        
    if($('ul.detail').length)    
        $("ul.detail.tabs").idTabs();
        
    $("#area-tab-trigger, #station-tab-trigger").click(function(){
      if(!$('#more-trigger').hasClass("on")){
        $('#more-trigger').toggleClass("on");
        $(".tab-container").slideDown();
        $('#more-trigger').html('<img src="/img/arrow_up.png" /> Hide search options');
      }
    });
    
    $("#more-trigger").click(function () {
      $(this).toggleClass("on");
      $(".tab-container").slideToggle();
            
      if($(this).hasClass("on")){
        $(this).html('<img src="/img/arrow_up.png" /> Hide search options');
      }else{
        $(this).html('<img src="/img/arrow_down.png" /> Show search options');
      }
    });
    
    $('#map-trigger').click(function() {
        setupMap();
    });
    
    $("#toggle-news").click(function () {
        
          if($(this).html() == 'show more'){
             $(this).html('hide this');
          }else{
             $(this).html('show more');
          }
            
          $("#more-news").slideToggle("fast");
    });     

    if($('.tip').length)
        $(".tip").tipTip({defaultPosition:'right'});
    
    $("a.fancybox:has(img)").fancybox({'overlayOpacity':0.8, 'overlayColor':'#000'});
        
    if($('#img-rotator').length){
        initRotator();
    }
   
});

    function popitup(action,popWidth,popHeight)
    {
        var LeftPosition = (screen.width) ? (screen.width-popWidth)/2 : 0;
        var TopPosition = (screen.height) ? (screen.height-popHeight)/2 : 0;
        newWindow=window.open(action,"isizejjmap","width="+popWidth+",height="+popHeight
                                +",toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,top=" + TopPosition + ",left=" + LeftPosition);
        newWindow.focus();
    }
    
    function initRotator(){
      var list = $("#img-rotator > a");
      var current_index = 0;

      // setup a function to show the current one and increase the index
      var doNext = function() {  
        // increment the index - if it is beyond the number of li's - reset it to 0
        var current_index = $('#img-rotator > a').index($('.show'));
        if (current_index+1 >= list.length){
            next_index = 0;
        }else{
            next_index = current_index + 1;
        }
        
        animateAd(list, current_index, next_index);
      };

      // set it up to be called every 5000 ms
      var t = setInterval(doNext, 7000);
      
      $('ul.topImgNumber li').click(function(){
         var next_index = $('ul.topImgNumber li').index(this);
         var current_index = $('#img-rotator > a').index($('.show'));
         animateAd(list, current_index, next_index);
         clearTimeout(t);
      });
    }
    
    function animateAd(list, current_index, next_index){
	   
	   var current = list.eq(current_index);		 
	   var next = list.eq(next_index);
	   
	   if(!next.hasClass("show")){
		   //Set the fade in effect for the next image, show class has higher z-index 
		   next.fadeIn(500).addClass('show');
           
		   //Hide the current image  
		   current.fadeOut(500).removeClass('show');
           
           $('ul.topImgNumber li').eq(current_index).removeClass('on');
           $('ul.topImgNumber li').eq(next_index).addClass('on');
	   }
    }
    
    function addFavourite(apt_id){
      if(apt_id){
        
        $.post("/favourite/add/", { id: apt_id },
        function(data){
            if(data.result == 1){
                alert('Added to favourites list');
            }else{
                alert(data.errormsg);
            }
        }, "json"); 
      }
      return false;
    }
    
    function deleteFavourite(apt_id){
      if(apt_id){
        $.post("/favourite/delete/", { id: apt_id },
        function(data){
            if(data.result == 1){
                //alert('Removed from favourite');
                $('.bukken#'+apt_id).fadeOut('normal', function(){
                    $(this).remove();
                    
                    if($("tr.bukken").length < 1){
                        $('#favourite-exist').slideUp('normal', function(){
                            $('#favourite-none').slideDown();
                        });
                    }
                });
            }else{
                alert(data.errormsg);
            }            
        }, "json"); 
      }
      
      return false;
    }
    
