// Javascript home

$(document).ready(function(){
    
    setInterval(swapFeature, 3000);
    $("#featureList a").css({backgroundPosition: "-400px 0"});
    //swapFeature();
    
    var imgActive = document.createElement('img');
    imgActive.setAttribute('id','featureImgActive');
    imgActive.setAttribute('src','');
    var imgInactive = document.createElement('img');
    imgInactive.setAttribute('id','featureImgInactive');
    imgInactive.setAttribute('src','');
    
    
    var bannerActive = document.createElement('img');
    bannerActive.setAttribute('id','featureBannerActive');
    bannerActive.setAttribute('src','');
    
    var bannerInactive = document.createElement('img');
    bannerInactive.setAttribute('id','featureBannerInactive');
    bannerInactive.setAttribute('src','');
    
    $("#featureList").append(imgInactive);
    $("#featureList").append(imgActive);
    $("#featureBanner").append(bannerInactive);
    $("#featureBanner").append(bannerActive);
    
    $("#featureImgInactive").hide();
    $("#featureBannerInactive").hide();
    $("#featureList a").click(function()
    {                 
       if($(this).attr('class') == 'active') return true;
       setActive($(this));
       disable = true;
       setTimeout(function(){ disable= false},1000);
       return false;
       
    });
    $("#featureList a").last().addClass('active');
    swapFeature();
    
    var headlinesImageActive = document.createElement('img');
    headlinesImageActive.setAttribute('id','headlinesImageActive');
    $("#headlinesImage").before(headlinesImageActive);
    $("#headlinesImage").hide();
    startSlideShow();
    
    $("#LatestMatchVersus .imgBox").each(function(){
        var image = $(this).find("img").first();
        $(this).css('backgroundImage','url(\''+image.attr('src')+'\')');
        image.attr('src','/images/clans/overlay.png');
        image.fadeTo(100,0.9,pulseIn);
    });
    $("#LastMatchDetailsOverlay").fadeTo(100,0.9,pulseIn);
});


function pulseIn(){
    $(this).fadeTo(2000,1,pulseOut);
}
function pulseOut(){
    $(this).fadeTo(2000,0.75,pulseIn);
}
function startSlideShow(){
    $.get('/ajax/home/news',function(responseXml,statusText, xmlhttprequest)
    {
        if(statusText == 'success')
        {
            var items = $(responseXml).find('news');
            var firstItem = items.first()
            showItem(firstItem,firstItem);
        }
    });
}

function showItem(item,firstItem){
    var title = item.attr('title');
    var id = item.attr('id');
    var banner = item.attr('banner');
    var type = item.attr('type');
    swapNewsSlide(banner,id,type,title);
    setTimeout(function(){ 
        var next = item.next();
        if(next.size() == 0){
            next = firstItem;
        }
        showItem(next,firstItem);
    }, 3000);
}

function swapNewsSlide(banner, id, type, title)
{
    var inactive = $("#headlinesImage");
    var active = $("#headlinesImageActive");
    inactive.attr('src','/images/news/thumbs/'+banner);
    $("#headlineLink").attr('href','/'+type+'/'+id);
    inactive.fadeIn(500,function(){	
        active.attr('src',inactive.attr('src'));   
        inactive.hide();
    });
}

var disable = false;
var inAnimation = false;
function setActive(nextLink)
{   
    if(!inAnimation)
    {
        var currentLink = $("#featureList a.active");
        if(currentLink.size() == 0)
        {   
            currentLink = $("#featureList a").last();
        } 
        if(currentLink.attr('id') != nextLink.attr('id'))
        {
            nextLink.addClass('active');
            currentLink.removeClass('active');  
            currentLink.animate({backgroundPosition: '-400px center'},{duration: 500});
        	
            nextLink.animate({backgroundPosition: '0px center'},{duration: 500});  
            inAnimation = $("#featureImgInactive").is(':hidden');
            var src = '/images/news/features/front/'+$(nextLink).attr('rel');
            $("#featureImgInactive").attr('src',src);	
            
            $("#featureImgInactive").fadeIn(500);
            var a = $("#featureImgActive").first();
            var b = $("#featureImgInactive").first();            
            a.attr('id', "featureImgInactive");
            b.attr('id', "featureImgActive");
            a.fadeOut(500);
            
            $("#featureBannerInactive").attr('src','/images/news/features/banners/'+$(nextLink).attr('rel'));	
            
            $("#featureBannerInactive").fadeIn(500);            
            a = $("#featureBannerActive").first();
            b = $("#featureBannerInactive").first();            
            a.attr('id', "featureBannerInactive");
            b.attr('id', "featureBannerActive");
            a.fadeOut(500);
            setTimeout(function(){ inAnimation = false}, 500);
        }
    }
}

function swapFeature()
{
    if(!disable)
    {
        var currentLink = $("#featureList a.active");
        if(currentLink.size() == 0)
        {   
            currentLink = $("#featureList a").last();
        }
        var currentListItem = currentLink.parent();
        //if(currentListItem.size() == 0) alert('misssing current listitem');
        
        var nextListItem = currentListItem.next();
        if(nextListItem.size() == 0){
            nextListItem = $("#featureList li").first();
        }
        
        var nextLink = nextListItem.children('a').first();
        //if(nextLink.size() == 0) alert('misssing next link');
        
        setActive(nextLink);   
    }
}
