﻿
/* Corner */
$(document).ready(function () {
    $('#footer').corner('6px');
    $('#footer .menu').corner('20px');
    $('.campaign .content').corner('bottom 15px');
    $('.campaign h2').corner('top 15px');
    $('.feed h2').corner('15px');
    $('.xform input[type="submit"]').corner('6px');
    $('.attachmentsContainer').corner('15px');
    $('.productListItemContainer').corner('15px');
    $('.productListItemContainer .productImageContainer').corner('8px');
    
});

/* Menu */
$(document).ready(function () {
    var submenu = $("ul#menu li.selected .submenu");
    if (submenu.length) {
        submenu.css("left", "-" + submenu.parent().position().left + "px");
        submenu.show();
    }
    $("ul#menu > li").hover(function () {
        $("ul#menu li .submenu").hide();
        var submenu = $(this).children("div:first");
        if (submenu.length) {
            submenu.css("left", "-" + submenu.parent().position().left + "px");
            submenu.show();
        }
    },
    function () {
        $("ul#menu li .submenu").delay(5000).hide();
        $("ul#menu li.selected .submenu").show();
    });
});

/* LanguageSelector */
$(document).ready(function () {
    $("#languageSelector li").first().addClass("firstitem");
});


/* Custom scrollbars */
$(document).ready(function () {
    $('#coursesContainer').jScrollPane(
                                {
                                    verticalGutter: 30,
                                    verticalDragMinHeight: 16,
                                    verticalDragMaxHeight: 16
                                });

    $('#tweetsContainer').jScrollPane(
                                { 
                                    verticalGutter: 30,
                                    verticalDragMinHeight: 16,
			                        verticalDragMaxHeight: 16
                                });
});


/* Slideshow */
/* Slideshow expects all images to be in the same size */
$(document).ready(function () {
    //set the right size of the slideshow
    var img = $("#slideshowImages img:first");
    $("#folioBlock").css("width", img.width());
    $("#slideshowWindow").css(
        {
            "width": img.width(),
            "height": img.height()
        });

    $("#slideshowPager").css("top", "");

    //Set Default State of each portfolio piece
    $("#slideshowPager").show();
    $("#slideshowPager .page:first").addClass("active");
    $("#slideshowImages img:first").addClass("current");

    //Get size of images, how many there are, then determin the size of the image reel.
    var imageWidth = $("#slideshowWindow").width();
    var imageSum = $("#slideshowImages img").size();
    var imageReelWidth = imageWidth * imageSum;

    //Adjust the image reel to its new size
    $("#slideshowImages").css({ 'width': imageReelWidth });

    //Paging + Slider Function
    rotate = function () {
        var triggerID = $active.find("a").attr("rel") - 1; //Get number of times to slide
        var imagecarouselPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $("#slideshowImages img").removeClass("current");
        $("#slideshowImages img:nth-child(" + $active.find("a").attr("rel") + ")").addClass("current");

        $("#slideshowPager .page").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $("#slideshowImages").animate({
            left: -imagecarouselPosition
        }, 500);
    };


    //Rotation + Timing Event
    rotateSwitch = function () {
        play = setInterval(function () { //Set timer - this will repeat itself every N seconds
            $active = $('#slideshowPager .active').next();
            if ($active.length === 0) { //If paging reaches the end...
                $active = $('#slideshowPager .page:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 10000); //Timer speed in milliseconds
    };

    rotateSwitch(); //Run function on launch

    //On Hover of the image
    $("#slideshowImages img").hover(function () {
        clearInterval(play); //Stop the rotation
    }, function () {
        rotateSwitch(); //Resume rotation
    });


    //On Hover of one of the pagerbuttons
    $("#slideshowPager .page").hover(
        function () {
            //clearInterval(play); //Stop the rotation
            $(this).addClass("hover");
        },
        function () {
            //rotateSwitch(); //Resume rotation
            $(this).removeClass("hover");
        }
        );

    //On Click on the number in the pagerbutton
    $("#slideshowPager .page a").click(function () {
        $active = $(this).closest(".page"); //Activate the clicked page
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });

    //On Click on the the pagerbutton
    $("#slideshowPager .page").click(function () {
        $active = $(this); //Activate the clicked page
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation
        return false; //Prevent browser jump to link anchor
    });   
});

$(document).ready(function () {
    /* Frontpage carousel*/
    $("#frontpageSlideshow .slide:first").addClass("active");
    setInterval("slideSwitch()", 10000);

    $("#frontpageSlideshowPager a").attr("src", "#");

    /* Add click handler to pager */
    $("#frontpageSlideshowPager a").click(function () {
        var index = $(this).attr("rel");
        $("#frontpageSlideshow .slide").removeClass("active last-active");
        $("#frontpageSlideshow .slide:nth-child(" + index + ")").addClass("active");
        slideSwitch();
        return false;
    });

});

/* Frontpage Carousel rotate images */
function slideSwitch() {
    var $active = $('#frontpageSlideshow .slide.active');
    if ($active.length == 0) $active = $('#frontpageSlideshow .slide:first');
    var $next = $active.next().length > 0 ? $active.next() : $('#frontpageSlideshow .slide:first');

    // uncomment the 3 lines below to pull the images in random order
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');
    
    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 2000, function () {
            $active.removeClass('active last-active');
        });
}

(function ($) {
    $("#campaigns > .campaign").each(function () {
        var campaign = $(this),
            link = campaign.find(".header h2 a");
        if (!link.length) return;
        var href = link.attr("href");
        if (!href) return;
        campaign.css("cursor", "pointer");
        campaign.click(onClick);

        function onClick(e) {
            document.location.href = href;
        }
    });
})(jQuery);
    
