$(document).ready(function () {
	var homerWidths = [];
	var currentProduct = 0;
	var numberOfProducts = 0;
	$('.homer').each(function(){
		var homer = $(this);
		numberOfProducts = homer.find('> div > div').length;
	
	    homer.wrap('<div class="homer-container"/>');
	    homer.width(homer.width() * 2).height(homer.height());
	
	    homer.find('> div  > div').each(function (i, div) {
	        var width = $(this).width();
	        homerWidths.push(width);
	        
	        $(this).width(width).removeClass('left center right');
	        $(this).click(function(){
	        	if ($('a:first', this).size()){
	        		var href = $('a:first', this).attr('href');
	        		if (href!='') {
	        			document.location.href=href;
	        		}
	        	}
	        	return false;
	        }).css({cursor: 'pointer'});
	    });
	    homerWidths[(homerWidths.length + 1)] = homerWidths[homerWidths.length];
	    homer.find('> div > div:first').addClass('imgactive active');
	    $('.fast-products ul > li:first').addClass('current');

	});
	
	var timer, homeDone;

    playHome = function () {
        clearInterval(timer);

        var first = $('.homer > div > div:first');
        var width = first.outerWidth();
        var clone = first.clone();
        clone.removeClass('imgactive active').appendTo('.homer > div');
        $('ul', clone).hide();
        $('.homer > div > div').each(function (i, div) {

            if (i == 1) {
                $(this).add('h2', this).animate({ borderBottomColor: '#8FAE02', borderTopColor: '#8FAE02', color: '#8FAE02' }, 200);
            }

            if (i > 0) $(this).css({ width: homerWidths[(i - 1)] });
        });
        $('.homer p').each(function () {
            $(this).width($(this).width());
        });
        $('.homer > div > div:eq(1)').addClass('imgactive')
        $('.homer').animate({ marginLeft: -width }, 400, 'linear', function () {
            $('.homer p').width('auto');
            $('.active').removeClass('active');
            first.remove();
            $('> div > div, h2', this).css({ borderBottomColor: '', borderTopColor: '', color: '' });
            $('.homer > div > div:first').addClass('active');
            $('.homer').css({ marginLeft: 0 });
            
            currentProduct = (currentProduct < numberOfProducts-1) ? currentProduct+1 : 0;
            $('.fast-products ul > li.current').removeClass('current');
            $('.fast-products ul > li:eq('+currentProduct+')').addClass('current');
            

            playUSPs();

        });

    }
    playUSPs = function () {
        var ul = $('.homer > div > div:first ul');
        $('li', ul).fadeTo(1, 0).hide();
        ul.show();
        timer = setInterval(function () {
            if ($('li:hidden', ul).size()) $('li:hidden:first', ul).show().fadeTo(200, 1);
            else if (!homeHover) {
                ul.fadeTo(100, 0, function () {
                    $(this).hide();
                });
                clearInterval(timer);
                playHome();


            }
        }, 800);
    }
    var homeHover = false;
    if ($('.homer').size()) {
        $('.homer').hover(function () {
            homeHover = true;
        }, function () {
            homeHover = false;
        });
        playUSPs();
    }

    $('.center ul li').each(function () {
        $(this).addClass('li_' + Math.ceil(Math.random() * 4));
    });

    /* spinning news
    * roulate every li in every unordered list inside a '.spinning-news' div
    */
    var newsHover = false;
    $('ul.spinning-news').each(function () {
        $(this).hover(function () {
            newsHover = true;
        }, function () {
            newsHover = false;
        })
        $('li', this).hide();
        $('li:first', this).addClass('shown').show();
    });
    var newstimer = setInterval(function () {
        if (newsHover) return;
        $('.spinning-news li').hide();
        $('ul.spinning-news').each(function () {
            var next = $('li:not(.shown):first', this);
            if (!next.size()) {
                $('li.shown', this).removeClass('shown');
                next = $('li:not(.shown):first', this);
            }
            next.addClass('shown').show();
        });

    }, 10000);


    $('.container span').filter(function (index) {

        if ($(this).attr("style") === null || $(this).attr("style") === undefined) return false;
        return $(this).attr("style").toLowerCase().match("text-decoration: underline") != null;


    }).removeAttr("style").addClass('underline');

    if ($('.bignews-item').size()) {
	    var pager = $('<ul class="pager"/>');
	    $('.bignews-item').each(function(i){
	    	var title = $('h2:first', this).text();
	    	pager.append('<li class="'+((i==0)?'on':'')+'"><a title="'+title+'" href="#">'+title+'</a></li>');
	    });
	    
	    $('.bignews-item:first').show().parent().prepend(pager);
	    $('.pager a').click(function(){
	    	var i = $(this).parent().index();
	    	$('.bignews-item').hide();
	    	$('.pager .on').removeClass('on');
	    	$(this).parent().addClass('on');
	    	$('.bignews-item:eq('+i+')').show();
	    	return false;
	    });
    }
});
