var $j = jQuery.noConflict();

function toggleDisplay()
{
	var headerEl = $j(this).parent();
    var el = $j('.modul-box-content', headerEl.parent()); // find();
    var alpha = el.css('opacity');
    var animateSetting = {height: 'toggle'};
	animateSetting.opacity = (el.css('display') == 'none') ? 1 : 0;
	headerEl.toggleClass('active');
	el.animate(animateSetting, 250, "easeOutQuad", updateCookie);
}

var news_timeout = null;
var current_item = 1;
var blnPlay = true;
var blnFirstPlay = true;
var blnJump = false;
var nextID = 0;
var blnTween = false;

function news_box(){
    $j('#boxcontent div').css({
        opacity : 0
    });
    
    $j('#photos .item').siblings(':first-child').css({left : '0px'});
    $j('#boxcontent div').siblings(':first-child').css({opacity : 1, top : 0});
    $j('#interface span').click(doJump);
    $j('#interface #play').click(togglePlay);
    $j('#interface #next').click(doNext);
    $j('#interface #previous').click(doPrev);
    
    $j('#photos .item h2').each(function (){$j(this).transBGdraw({'z-index' : 1});});
    
    $j('#photos .item div').each(function (){
        if ($j(this).html() != "") {
            $j(this).css({opacity : 0});
        }
    });
    doFadeTitle()
}

function doJump(){
    nextID = parseInt($j(this).attr('id').replace('boxitem', ''));
    if (current_item != nextID && !blnTween){
        if (news_timeout) {
            clearTimeout(news_timeout);
        }
        if (current_item + 1 == nextID || (nextID == 1 && current_item == 5)) {
            doNext();
            return
        }
        var posToActivate = (nextID - current_item) + 1;
        if (posToActivate < 1) {
            posToActivate += 5;
        }
        if (!blnFirstPlay){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':first-child'));
            $j('#boxcontent div').siblings(':last-child').css({opacity : 0, top : '192px'});

            $j('#photos').append($j('#photos .item').siblings(':first-child'));
            $j('#photos .item').siblings(':last-child').css({left : '286px', position : 'absolute'});
            $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});
        }
        $j('#boxcontent').append($j('#boxcontent div').siblings(':nth-child(1)').clone());
        $j('#boxcontent div').siblings(':last-child').css({opacity : 0, top : '192px'});
        
        $j('#photos').append($j('#photos .item').siblings(':first-child').clone());
        $j('#photos .item').siblings(':last-child').css({left : '286px', position : 'absolute'});
        $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});

        var i = 2;
        while (i < posToActivate){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':nth-child(2)'));
            $j('#boxcontent div').siblings(':last-child').css({top : '192px'});
            $j('#photos').append($j('#photos .item').siblings(':nth-child(2)'));
            $j('#photos .item').siblings(':last-child').children('div').css({opacity : 0});
            i++
        }
        blnJump = true;
        
        $j('#boxitem' + current_item).removeClass('active');
        current_item = nextID;
        
        $j('#boxitem' + current_item).addClass('active');
        
        $j('#boxcontent div').siblings(':first-child').animate({opacity : 0},250, "easeOutQuad");
        $j('#boxcontent div').siblings(':nth-child(2)').css({top : 0});
        $j('#boxcontent div').siblings(':nth-child(2)').animate({opacity : 1}, 500, "easeOutQuad");
        
        $j('#photos .item').siblings(':first-child').children('div').animate({ opacity : 0},250, "easeOutQuad");
        $j('#photos .item').siblings(':first-child').next().animate({left : 0},500, "easeOutQuad", doResetJump);
        blnTween = true;
    }
}

$j(document).ready(function(){
	$j('#boxoverlay').hover(
		function(){
			togglePlay();
		},
		function(){
			togglePlay();
		}
	);
})

function togglePlay(){
    if (blnPlay){
        $j('#interface #play').removeClass("pause");
        $j('#interface #play').addClass("play");
        if (news_timeout) {
            clearTimeout(news_timeout)
        }
    }else{
        $j('#interface #play').removeClass("play");
        $j('#interface #play').addClass("pause");
        doNext()
    }
    blnPlay = !blnPlay
}

function doFadeTitle(){
    if (!blnFirstPlay){
        $j('#boxcontent div').siblings(':first-child').css({opacity : 0, top : '192px'});
    }
    news_timeout = setTimeout(doSetNextTimeout, 500);
}

function doSetNextTimeout(){
    blnTween = false;
    if (blnFirstPlay){
        $j('#photos .item').siblings(':first-child').children('div').animate({opacity : 1},500, "easeOutQuad");
    }else{
        $j('#photos .item').siblings(':first-child').next().children('div').animate({opacity : 1},500, "easeOutQuad");
    }
    
    if (blnPlay) {
        news_timeout = setTimeout(doNext, 5000);
    }
}
function doResetJump(){
    $j('#boxcontent div').siblings(':first-child').remove();
    $j('#photos .item').siblings(':first-child').remove();
    blnFirstPlay = true;
    doFadeTitle();
}

function doNext(){
    if (!blnTween){
        if (news_timeout) {
            clearTimeout(news_timeout);
        }
        $j('#boxitem' + current_item).removeClass('active');

        current_item++;
        if (current_item == 6) {
            current_item = 1
        }

        $j('#boxitem' + current_item).addClass('active');
        if (!blnFirstPlay){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':first-child'));
            $j('#boxcontent div').siblings(':last-child').css({top : '192px'});
        }
        
        $j('#boxcontent div').siblings(':first-child').animate({opacity: 0},250, "easeOutQuad");
        $j('#boxcontent div').siblings(':nth-child(2)').css({top: 0});
        $j('#boxcontent div').siblings(':nth-child(2)').animate({opacity: 1},500, "easeOutQuad");
        if (!blnFirstPlay) {
            $j('#photos').append($j('#photos .item').siblings(':first-child'));
        }
        $j('#photos .item').siblings(':first-child').children('div').animate({opacity: 0}, 250, "easeOutQuad");
        $j('#photos .item').siblings(':last-child').css({left: '286px', position: 'absolute'});
        $j('#photos .item').siblings(':last-child').children('div').css({opacity: 0});
        $j('#photos .item').siblings(':first-child').next().animate({left: 0}, 500, "easeOutQuad", doFadeTitle);
        blnFirstPlay = false;
        blnTween = true;
    }
}

function doPrev(){
    if (!blnTween){
        if (news_timeout) {
            clearTimeout(news_timeout);
        }
        $j('#boxitem' + current_item).removeClass('active');

        current_item--;
        if (current_item == 1) {
            current_item = 6
        }

        $j('#boxitem' + current_item).addClass('active');
        if (!blnFirstPlay){
            $j('#boxcontent').append($j('#boxcontent div').siblings(':last-child'));
            $j('#boxcontent div').siblings(':first-child').css({top : '192px'});
        }
        
        $j('#boxcontent div').siblings(':last-child').animate({opacity: 0},250, "easeOutQuad");
        $j('#boxcontent div').siblings(':nth-child(2)').css({top: 0});
        $j('#boxcontent div').siblings(':nth-child(2)').animate({opacity: 1},500, "easeOutQuad");
        if (!blnFirstPlay) {
            $j('#photos').append($j('#photos .item').siblings(':last-child'));
        }
        $j('#photos .item').siblings(':last-child').children('div').animate({opacity: 0}, 250, "easeOutQuad");
        $j('#photos .item').siblings(':first-child').css({left: '286px', position: 'absolute'});
        $j('#photos .item').siblings(':first-child').children('div').css({opacity: 0});
        $j('#photos .item').siblings(':last-child').next().animate({left: 0}, 500, "easeOutQuad", doFadeTitle);
        blnFirstPlay = false;
        blnTween = true;
    }
}

$j(document).ready(launch);
function launch()
{
    news_box();
}