jQuery().ready(function(){

    var ticker = $("#twitter_list");
    
    function animator(currentItem) {
            var distance = currentItem.width(),
            duration = (distance - Math.abs(parseInt(currentItem.css("marginLeft")))) / 0.035;
            currentItem.animate({ marginLeft: -distance }, duration, "linear", function() {
                    currentItem.appendTo(currentItem.parent()).css("marginLeft", 0);
                    animator(currentItem.parent().children(":first"));
            });
    };

    $(document).ready(function(){

		var widescreen = false;
		var size = window.getSize();

        // switch styles wide/classic
        $("a.switch").toggle(function(){
            $(this).addClass("swap");
            $(".classiconly").fadeOut("fast", function() {
                $("body").addClass("wide");
                $(".wideonly").fadeIn("slow");
                animator(ticker.children(":first"));
            });
        }, function () {
            $(this).removeClass("swap");
            ticker.children().stop();
            $(".wideonly").fadeOut("fast", function() {
                $("body").removeClass("wide");
                $(".classiconly").fadeIn("slow");
            });
        });

        // viewport resize
        window.onresize = function(){
            dSize();
	        console.log(widescreen);
        };

		function dSize(){

            if (size.x > 1500) {

            	//if(widescreen == false){
		            $("a.switch").addClass("swap");
		            $(".classiconly").fadeOut("fast", function() {
		                $("body").addClass("wide");
		                $(".wideonly").fadeIn("slow");
		                animator(ticker.children(":first"));
		            });
		            
                    widescreen = true;
            	//}

            } else if (size.x < 1501) {
            	
            	//if(widescreen == true){
		            $("a.switch").removeClass("swap");
		            ticker.children().stop();
		            $(".wideonly").fadeOut("fast", function() {
		                $("body").removeClass("wide");
		                $(".classiconly").fadeIn("slow");
		            });

	                widescreen = false;
            	//}
            	
                
            }
	}
        
    });
    
    //animator(ticker.children(":first"));
    
    ticker.mouseenter(function() {
            ticker.children().stop();
    });    
    ticker.mouseleave(function() {
            animator(ticker.children(":first"));
    });

    // switch play/pause ticker
    $("a.play").toggle(function(){
        $(this).addClass("pause");
        ticker.children().stop();
        ticker.mouseleave(function() { });
    }, function () {
        $(this).removeClass("pause");
        animator(ticker.children(":first"));
    });

    // open/close article
    $("article#front .more").click(function(){
        $(this).fadeOut("slow");
        $("#colactus").addClass("col1ab");
        $("#colonne2").addClass("col2ab");
        $("article#front").animate({"width": "+=600px", "height": "+=0"}, "slow", function(){
            $("#followmore").fadeIn("5");
        });
    });

    $("article#front .less").click(function(){
        $("#followmore").hide("0");
        $("article#front").animate({"width": "-=600px", "height": "-=0"}, "slow", function(){
            $("article#front .more").fadeIn("fast");
            $("#colactus").removeClass("col1ab");
            $("#colonne2").removeClass("col2ab");
        });
    });

   // open/close actutexte dans la colactus
    $("#colactus .actutexte").css("display","none");

    $("#colactus .actutitre").click(function(){
        $("#colactus .actutitre").removeClass("swap");
        $(".open").slideUp(500, function(){
            $(this).css("display","none");
            $(this).removeClass("open");
        });
    });


    $("#colactus .actutitre").toggle(function(){
        $(this).addClass("swap");
        //$(".open").slideUp(500, function(){
        //    $(this).css("display","none");
        //    $(this).removeClass("open");
        //});
        $("#colactus .swap + .actutexte").slideDown(500, function(){
            $(this).css("display","block");
            $(this).addClass("open");
        });
    }, function () {
        $(this).removeClass("swap");
        $(".open").slideUp(500, function(){
            $(this).css("display","none");
            $(this).removeClass("open");
        });
    });
      
});
