$(document).ready(function(){
   
    if( typeof window.console == 'undefined'){
        window.console = {
            log:function(){}
        };
    }
   
    (function(){
        
        n = { 
            elm:{
                liNav : $('#liNav'),
                adNav : $('#adNav'),
                adNavC : $('#adNavC'),
                adBtNav : $('#theNav')
            }
        }
        
        //showNav();
        
        n.elm.liNav.hover(function(){
            if(isVisible()) return;
            n.elm.adNav.slideDown('slow',switchBt); },function(){});
        
        n.elm.adNav.hover(function(){},function(){
                if(!isVisible()) return;
                n.elm.adNav.slideUp('slow',function(){switchBt(true);});        
        });
    
        function isVisible(){
            if(n.elm.adNav.is(':visible'))
                return true;

            return false;
        }
        
        function showNav(){
            setTimeout(function(){
                n.elm.adNav.slideDown('slow');    
            },400);
        }
        
        function switchBt(y){
            if(y) n.elm.adBtNav.removeClass('navDown');
            else n.elm.adBtNav.addClass('navDown');
        }
        
    })();
    
    
    
    (function(){
        var nLink = $('.nLink'),
            c = $('#adListDate');
        nLink.each(function(index,elm){
            var self = $(this),
                date = self.attr('id');
            self.bind('click',function(e){
                e.preventDefault();
                getNewsByDate(date,function(){
                    getSepcificNews();
                    addClassNoBg();
                });
            });    
            
        });
        
        
        function display(html,callBack){
            c.fadeOut('slow',function(){
                c.html(html).queue(function(){
                    addClassNoBg();
                    $(this).dequeue();
                }).fadeIn('slow',function(){if(callBack) callBack(); });    
            });   
        }
        
        function getNewsByDate(date,callBack){
             $.ajax({
                url:'/newsPage/getNewsByMonth.php?date='+date,
                success:function(html){
                    display(html,callBack);
                }
             });
        }
        
        function addClassNoBg(){
            var list = $('#adListDate ul li:last-child');
            list.addClass('nobg');
        }
        
        function getSepcificNews(){
            var l = $('.pointerLink');
            l.bind('click',function(e){
                e.preventDefault();
                var id = $(this).attr('id');
                id = id.substring(5);
                 $.ajax({
                    url:'/newsPage/getSpecificNews.php?id='+id,
                    success:function(html){
                        display(html);
                    }
                 });
            });    
        }
        
        getSepcificNews();
        addClassNoBg();
        
    })();
    
    
    (function(){
        a = {
            elm : {
                chBg : $('.chBg')
            }    
        }
        
        a.elm.chBg.each(function(index,elm){
            var self = $(this);
            self.hover(function(){
                animateBg(self,true);
                chCss(self,true)
            },function(){animateBg(self,false);chCss(self)});
        });
        
        
        function chCss(elm,c){
            if(c) elm.css('color','white');
            else elm.css('color','');
        }
        
        function animateBg(elm,c,callBack){
            if(c) elm.stop(true,true).animate({backgroundPosition:'0 0'},'slow',function(){if(callBack) callBack();});
            else elm.stop(true,true).animate({backgroundPosition:'-340px 0'},150,function(){if(callBack) callBack();});
        }
        
    })();
    
    init();
    
});

function init(){
    evenOddBg();
    equalHeights();
    removeStyleSpan();
    displayWebSite(); //to activate
    backToTop();
}


function arrRandom(){
    var arr = new Array(),count,self = $('.shuffle .achievement');
    self.hide().each(function(index,elm){
        arr.push(index);
        arr = $.shuffle(arr);
    });
    count = arr.length;
    for(i = 0; i <= count; i++){
        $('.shuffle .achievement:eq('+arr[i]+')').delay(200*i).fadeIn('slow');
    }
    
    console.log(arr);
}

function backToTop(){
    $('.top').bind('click',function(e){
        e.preventDefault();
        $(window).scrollTo(0,1200); 
    });   
}

function displayWebSite(){
    return;
    var self = $('#access'),
        bt = self.find('#accOk'),
        c = $('#fakeContainer');
        c.hide();
        bt.bind('click',access);
        
    function access(){
        c.show();
        self.fadeOut('slow');
    }
}

function removeStyleSpan(){
    $('span').removeAttr('style');
}

function evenOddBg(){
    
    $('.adWrap:even').addClass('adEven');
    $('.adWrap:odd').addClass('adOdd');
    
    $('.left dl:odd, .adOdd .adBox:even, .imagePopOut:odd').addClass('bgBlue');
    $('.left dl:even, .adEven .adBox:even, .imagePopOut:even').addClass('bgWhite');
    $('.right dl:even, .adEven .adBox:odd').addClass('bgBlack');
    $('.right dl:odd, .adOdd .adBox:odd').addClass('bgGrey');
    
}

function equalHeights(){
    $('.twoColumn').sameHeights();
    $('.sameHeight').sameHeights();
    $('.equalHeight').sameHeights(true,140);
}

$.fn.sameHeights = function(h,n){
    $(this).each(function(){
        var tallest = 0;
        $(this).children().each(function(i,elm){
            if (tallest < $(this).height()) tallest =  h ? $(this).height() -n : $(this).height();
        });
    $(this).children().css({'min-height': tallest});
    });
    return this;
};

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);
