﻿//
$(document).ready(function () {

    //NEWS & EVENTS Webpart Functions

    //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;
    var textWidth = $(".window2").width();
    var textSum = $(".text_item div").size();
    var textWidth = textWidth * textSum;
    var triggerID = 0;
    var previousTriggerID = 0;

    //Adjust the image reel to its new size
    $(".image_reel").css({ 'width': imageReelWidth });
    $(".text_item").css({ 'width': textWidth });

    //Paging + Slider Function
    rotate = function () {

        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
        var text_itemPosition = triggerID * 170; //was textWidth, now matches window2a

        $(".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
        }, 200);
        $(".text_item").animate({
            left: -text_itemPosition
        }, 200);

    };

    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every 3 seconds
            $active = $('.paging a.active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            fadeOutText();
            rotate(); //Trigger the paging and slider function
            fadeInText(triggerID);
        }, 7000); //Timer speed in milliseconds (3 seconds)
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $(".image_reel 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
        fadeOutText();
        rotate(); //Trigger rotation immediately
        fadeInText(triggerID);
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });

    // Fade in Text
    function fadeInText(triggerID) {
        $("#news_feed p").fadeIn("slow", function () {
            $("#news_feed p").text(getNewsByTriggerId(triggerID));
        });

        previousTriggerID = triggerID;
    }

    // Fade out Text
    function fadeOutText() {
        $("#news_feed p").fadeOut("slow", function () {
            $("#news_feed p").text(getNewsByTriggerId(previousTriggerID));
        });
    }

    function getNewsByTriggerId(triggerID) {
        news = "";
        switch (triggerID) {
            case 0:
                news = "This is 1. Trigger id =" + triggerID + "wish you a safe and fun Halloween! Here are some of the many awesome.";
                break;
            case 1:
                news = "This is 2. Trigger id =" + triggerID + "Look at it this way; even if Clarke scored a few runs today";
                break;
            case 2:
                news = "This is 3. Trigger id =" + triggerID + "Experiment # 2: if Australia is going to challenge in T20 cricket";
                break;
            case 3:
                news = "This is 4. Trigger id =" + triggerID + "And so it proved....his inability to hit the faster bowlers over.";
                break;
        }

        return news;
    }



    //HOME PAGE ACCORDION Functions
    $maxAWidth = 618; //618
    $minAWidth = 29; //29

    //Panel and Count defaults
    $LastAnchor = $("#a1");
    $CurrentPanel = $($LastAnchor).parent();

    $count = 1;
    $total = 0;

    //shrinkall function
    shrinkAllAccordians = function () {
        $('#homepageAccordion ul li a').each(function () {
            $('#homepageAccordion ul li a').animate({ width: $minAWidth + "px" }, { queue: false, duration: 400 })
        });
    }

    //function to count and number archors in the accordian
    countAccordians = function () {
        $('#homepageAccordion ul li').each(function () {
            $total += 1;
            $classtoAdd = "li" + $total;
            $(this).addClass($classtoAdd);
        });
    }

    //Rotate the Accordian
    rotateAccordian = function () {
        //Shrink the current panels child anchor element				  
        shrinkAllAccordians();
        //perform actions depending on count
        if ($count == $total) { //If the 4th panel
            $($LastAnchor).animate({ width: $maxAWidth + "px" }, { queue: false, duration: 400 }); 					//Expand the First Panels anchor element
            $CurrentPanel = $($LastAnchor).parent(); 																//Reset the parent
            $count = 1; 																							//Reset count
        } else {	//else
            $CurrentPanel.next().children("a").animate({ width: $maxAWidth + "px" }, { queue: false, duration: 400 }); //Expand the next panels anchor element			
            $CurrentPanel = $CurrentPanel.next(); 																	//Set the parent to the next panel
            $count += 1; 																							//Iterate the count				
        }
    }

    //Auto Rotate the Accordian
    autoRotateAccordian = function () {
        playAccordian = setInterval(function () { //Timer interval function

            rotateAccordian();

        }, 4000); //Timer speed
    }

    //Manual Rotate function
    $("#homepageAccordion ul li a").click(function () {
        currentAnchor = $(this);
        shrinkAllAccordians();
        $(currentAnchor).animate({ width: $maxAWidth + "px" }, { queue: false, duration: 400 });
    });

    //On Hover HOME PAGE ACCORDION
    $("#homepageAccordion ul li").hover(function () { clearInterval(playAccordian) }, function () { autoRotateAccordian() });

    //Call the count accordian function on load
    countAccordians();
    //Call the Rotate the accordian function on load
    autoRotateAccordian();

});

function ShowDialog(url) {
    var options = SP.UI.$create_DialogOptions();
    options.url = url;
    SP.UI.ModalDialog.showModalDialog(options);
}



