/// <reference path="./jQuery/jquery-1.4.1-vsdoc.js" />

function goNews() {

    //Set Default State of each portfolio piece
    $(".paging").show();
    $(".paging a:first").addClass("active");

    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = $(".window").width();
    var imageSum = $(".image_reel img").size();
    var imageReelWidth = imageWidth * imageSum;

    //Adjust the image reel to its new size
    $(".image_reel").css({ 'width': imageReelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $(".paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $(".image_reel").animate({ 
            left: -image_reelPosition
        }, 10);

        // show div
        $(".newstext .story").removeClass('active');
        $(".newstext .s" + triggerID).addClass('active');

    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every 7 seconds
            $active = $('.paging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 7000); //Timer speed in milliseconds (7 seconds)
    };

    ToggleDirections = function (on) {
        if (on) {
            $("#news_sx").css("display", "block");
            $("#news_sx").css("visibility", "visible");
            $("#news_dx").css("display", "block");
            $("#news_dx").css("visibility", "visible");
            Debug.log("FRECCE ON");
        }
        else {
            $("#news_sx").css("display", "none");
            $("#news_sx").css("visibility", "hidden");
            $("#news_dx").css("display", "none");
            $("#news_dx").css("visibility", "hidden");
            Debug.log("FRECCE OFF");
        }
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $(".main_view a").hover(function () {
        clearInterval(play); //Stop the rotation

    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    $(".paging a").click(function () {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        //clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        //rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });

    $("#topNewsWin").mouseover(function () {
        ToggleDirections(true);
    });

    $("#topNewsWin").mouseout(function () {
        ToggleDirections(false);
    });

    $("#news_dx").click(function () {
        $active = $('.paging .active').next();
        if ($active.length === 0) { //If paging reaches the end...
            $active = $('.paging a:first'); //go back to first
        }
        rotate();
    });

    $("#news_sx").click(function () {
        $active = $('.paging .active').prev();
        if ($active.length === 0) { //If paging reaches the start...
            $active = $('.paging a:last'); //go back to last
        }
        rotate();
    });
}

var HorizSliding = false;

function goHorizSlide() {

    //    var divList = $(".h_image_reel div.news");
    //    var divWidth = $(".h_image_reel div.news").width();

    shift = function (dir) {

        HorizSliding = true;
        setTimeout("HorizSliding = false;", 350);

        var newsCount = $(".h_image_reel div.news").size();
        var mult;

        if (dir == "sx") {
            if ($($(".h_image_reel div.news")[newsCount-1]).position().left <= 600)
                return;
            mult = -1;
        }
        else {
            if ( $($(".h_image_reel div.news")[0]).position().left >= 0)
                return;
            mult = +1;
        }

        for (var i = 0; i < newsCount; i++) {
            var divToMove = $($(".h_image_reel div.news")[i]);

            var moveTo = divToMove.position().left + (200 * mult);
            divToMove.animate({ left: moveTo }, 250, "linear");
        }

    };


    $("#freccia_sx").click(function () {
        if (!HorizSliding)
            shift("dx"); //Trigger rotation immediately
        return false; //Prevent browser jump to link anchor
    });

    $("#freccia_dx").click(function () {
        if (!HorizSliding)
            shift("sx"); //Trigger rotation immediately
        return false; //Prevent browser jump to link anchor
    });
    
    $("#news_orizontal_preview").find(".news").mouseenter(function(){
		$(this).find(".titolo").addClass("blue");
    });
    $("#news_orizontal_preview").find(".news").mouseleave(function(){
		$(this).find(".titolo").removeClass("blue");
	});

}

function goActiveVerticalNews() {
	$("#news_vertical_preview").find(".news").each(function(){
		$(this).mouseenter(function() {
			$(this).find(".block").animate({top:"-=95px"}, 200);
		}).mouseleave(function() {
			$(this).find(".block").animate({top:"+=95px"}, 200);
		});
	});
}

//
// READY:
$(document).ready(function () {
    goNews();
    goHorizSlide(); 
    goActiveVerticalNews();
});

