//jQuery.noConflict();
//$.noConflict();
var $jq1 = jQuery.noConflict();

$jq1(document).ready(function () {
    //Using jQuery to request XML through AJAX.
    $jq1.ajax({
        type: "GET",        
        url: "/rotator/xml/rotator.xml",
        dataType: "xml",
        success: parseXml,
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(xhr.status);
            //alert(thrownError);
        }
    });

    var count = 0;

    function parseXml(xml) {
        // add the image and link for the rotator + paging.
        $jq1(xml).find("rotator").each(function () {
            $jq1(xml).find("mktg").each(function () {
                $jq1(xml).find("image").each(function () {
                    $jq1(".image_reel").append("<a href=\"" + $jq1(this).find("link").text() + "\">" + "<img src=\"" + $jq1(this).find("src").text() + "\" alt>" + "</a>");
                    count = $jq1(".image_reel img").size();
                    $jq1(".paging").append("<a href=\"" + "#\"" + "rel=\"" + count + "\"" + "style=\"color:white; text-decoration: none;\"" + "class=\" \"" + ">" + count + "</a>");
                });
            });
        });

        afterLoad();
    }
});

function afterLoad() {
    //Set Default State of each portfolio piece
    $jq1(".paging").show();
    $jq1(".paging a:first").addClass("active");

    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = $jq1(".window").width();
    //var imageSum = $jq1(".image_reel img").size();
    var imageSum = $jq1(".image_reel img").size();    
    var imageReelWidth = imageWidth * imageSum;

    //Adjust the image reel to its new size
    //$jq1(".image_reel").css({ 'width': imageReelWidth });
    $jq1(".image_reel").css({ 'width': imageReelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $jq1active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $jq1(".paging a").removeClass('active'); //Remove all active class
        $jq1active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        //$jq1(".image_reel").animate({        
        $jq1(".image_reel").animate({
            left: -image_reelPosition
        }, 500);
    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds
            $jq1active = $jq1('.paging a.active').next();
            if ($jq1active.length === 0) { //If paging reaches the end...
                //$jq1(".image_reel").animate({ left: 0 }, 0); //Set left position to zero and loading to zero.
                $jq1(".image_reel").animate({ left: 0 }, 0); //Set left position to zero and loading to zero.
                $jq1active = $jq1('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 3000); //Timer speed in milliseconds (3 seconds)
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    //$jq1(".image_reel a").hover(function () {
    $jq1(".image_reel a").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });

    //On Click
    $jq1(".paging a").click(function () {
        $jq1active = $jq1(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
    });
}
