/* Author: Dan */

/**
 * Et modul, der sørger for, at starter en billed- fader/cycler.
 * funktionaliteten er simpel og kører automatisk uden knapper
 *  
 * @module fader
 * @namespace CMS
 * @param   {Object}              $   jQuery object
 * @return  {Object}              {}  returns the public functions
 */
CMS.fader = (function($){
    var params, 
        init = function(inparams) {
            params = inparams;
            
            // check if cycle plugin is loaded
            if(typeof $.fn.cycle !== 'function') {
                $.getScript('templates/top-side/media/js/mylibs/jquery.cycle.lite.min.js', applyCycle);
            } else {
                applyCycle()
            }
        },

        applyCycle = function() {
            // loop all subjects in faderItems
            // and apply cycle plugin
            $.each(
                params, 
                function(index,value){
                    $(value.id).cycle(value.style);
                }
            );
        };

    // hook up on init event
    $.subscribe("/cms/fader/init", function(params) {
        init(params);
    });
})(window.jQuery);


/**
 * Et modul, der sørger for, at starter en billed- fader/cycler.
 * funktionaliteten har knapper, så det er muligt at bladre mellem
 * billederne
 *  
 * @module slider
 * @namespace CMS
 * @param   {Object}              $   jQuery object
 * @return  {Object}              {}  returns the public functions
 */
CMS.slider = (function($){
    var play,
        imageWidth,
        $active,
        $paging = $(".paging"),
        $pagingFirst = $paging.find('a:first'),
        $image_reel = $(".image_reel"),

        init = function(parms){
            // Set Default State of each portfolio piece
            $paging.show();
            $pagingFirst.addClass("active");
                
            // Get size of images, how many there are, then determin the size of the image reel.
            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});
    
            // bind events
            bindEvents();

            // Start the rotation
            rotateSwitch(); 
        },

        // 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.find('a').removeClass('active'); // Remove all active class
            $active.addClass('active'); // Add active class (the $active is declared in the rotateSwitch function)

            $image_reel.stop(true,true);
            
            //Slider Animation
            $image_reel.animate({ 
                left: -image_reelPosition
            }, 300 );
            
        }, 
        
        //Rotation + Timing Event
        rotateSwitch = function() {		
            play = setInterval(
                function() { //Set timer - this will repeat itself every 3 seconds
                    $active = $paging.find('a.active').next();
                    if ( $active.length === 0) { //If paging reaches the end...
                        $active = $pagingFirst; //go back to first
                    }
                    rotate(); //Trigger the paging and slider function
                }, 5000); //Timer speed in milliseconds (3 seconds)
        },
        

        bindEvents = function() {
            // On Click
            $paging.delegate('a','click', handleClick);
            
            // On Hover
            $image_reel.find('a').hover(
                function() {
                    clearInterval(play); //Stop the rotation
                    $image_reel.stop(true,true);
                }, 
                function() {
                    rotateSwitch(); //Resume rotation
                }
            );	
        },
        
        //On Click
        handleClick = 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
        };	

    // hook up on init event
    $.subscribe("/cms/slider/init", function(params) {
        init(params);
    });
})(window.jQuery);

/**
 * Et modul, der ansvarlig for, at starte andre moduler op, så
 * funktionalitet kommer ind på siden
 *  
 * @module init
 * @namespace CMS
 * @param   {Object}              $   jQuery object
 * @return  {Object}              {}  returns the public functions
 */

CMS = CMS || {};
CMS.init = (function($){
    var init = function() {
            // check if any modules are needed
            if(!CMS.modules) return;

            gfx();

            // loop all modules and start them
            $.each(
                CMS.modules,
                function(key,value) {
                    if(typeof value === 'function') { 
                        value(); 
                    } else {
                        $.publish("/cms/"+key+"/init", [value]);
                    }
                }
            );
        },

        gfx = function() {
            $(function(){
                $("#sidebar_right_mid.subpage").css({"height":$("#container_top.subpage").outerHeight() + 10 + "px"});
            });
        };

    ///////////////////////////////////////////////////////////
    // run the modules
    init();
})(window.jQuery);



