$(window).load(
    function()
    {
        rotator.init();
    }
);

rotator = {
    mouseOn:false,
    rotationTime: 7000,
    currentMain:0,
    listCount:3,
    timeout:null,
    mainTextPaneTimeout:null
};

rotator.init = function()
{    
    $("#news-rotator #news-list .news").each(function(d,i){ 
        $("#news-main").append($(this).clone());
        $(this).data("listPos", d); 
    });
    
    rotator.getDisplay(0);
    rotator.startInterval();
    
    $("#news-rotator").bind("mouseenter", rotator.mouseEnter);
    $("#news-rotator").bind("mouseleave", rotator.mouseLeave);
    
    $("#news-rotator #news-main .news-text").bind("mouseenter", rotator.textMouseEnter);
    $("#news-rotator #news-main .news-text").bind("mouseleave", rotator.textMouseLeave);
    
    $("#news-rotator #news-list .news").bind("mouseenter", rotator.newsMouseEnter);
    $("#news-rotator #news-list .news").bind("mouseleave", rotator.newsMouseLeave);
    $("#news-rotator #news-list .news").bind("click", rotator.newsClick);
};

rotator.getDisplay = function(n)
{
    n = Math.min(n, rotator.listCount-1);
    
    if(n!=rotator.currentDisplay)
    {
        var el = $("#news-main .news").eq(n);
        $("#news-main .news").not(el).css("z-index", 1).animate({opacity:0},{duration:1000,easing:'easeOutExpo'});
        $("#news-list .news").removeAttr("style").removeClass("selected").eq(n).addClass("selected");
        el.css("z-index", 10).animate({opacity:1},{duration:1000,easing:'easeOutExpo'});
        
        // Image movement
        var img = el.find("img.full");
        var move = img.attr("alt").split(" ");
        
        for(d in move)
        {
            switch(move[d])
            {
                case "top": move[d] = 0; break;
                case "bottom": move[d] = -(img.height()-$("#news-main").height()); break;
                case "left": move[d] = 0; break;
                case "right": move[d] = -(img.width()-$("#news-main").width()); break;
                case "center" : move[d] = ((d + 1) % 2 == 0) ? -(img.height()-$("#news-main").height()) / 2 :  -(img.width()-$("#news-main").width()) / 2; break;
                default : move[d] = -parseInt(move[d]);
            }
        }
        
        img.css({top:move[0], left:move[1]});
        img.stop().dequeue().animate({top:move[2], left:move[3]},{duration:rotator.rotationTime+1000, easing:'easeOutSine'})
        
        rotator.currentMain = n;
    }
};

rotator.getNextDisplay = function()
{
    if(!rotator.mouseOn)
    {
        var next = (rotator.currentMain + 1) % rotator.listCount;
        rotator.getDisplay(next); 
    }
};

rotator.startInterval = function()
{ 
    rotator.timeout = setInterval(rotator.getNextDisplay, rotator.rotationTime); 
};

rotator.clearInterval = function()
{ 
    clearInterval(rotator.timeout); 
};

rotator.mouseEnter = function()
{
    rotator.mouseOn = true;
};

rotator.mouseLeave = function()
{
    rotator.mouseOn = false;
};

rotator.textMouseEnter = function()
{
    $(this).animate({height:90},{duration:500, easing:'easeOutExpo'});
    $("#news-main .more").animate({opacity:0},{duration:200, easing:'easeOutExpo',complete:function(){ $(this).css("display", "none"); }});
};

rotator.textMouseLeave = function()
{
    $(this).animate({height:20},{duration:300, easing:'easeOutExpo'});
    $("#news-main .more").css("display", "block").animate({opacity:1},{duration:500, easing:'easeOutExpo'});
};

rotator.newsMouseEnter = function()
{
    $(this).stop().dequeue().animate({opacity:1},{duration:300});
};

rotator.newsMouseLeave = function()
{
    if(!$(this).is(".selected"))
       $(this).stop().dequeue().animate({opacity:0.8},{duration:150});
};

rotator.newsClick = function()
{
    rotator.getDisplay($(this).data("listPos"));
    rotator.clearInterval();
    rotator.startInterval();
};

