﻿/*






Created by PYRAMID COMMUNICATIONS AB - SWEDEN 






*/



/* ---------- Variables ---------- */
//Set 'flipButtons' to false if whole circle is visible
//or true if only the back half or circle is visible
var flipButtons = true;

//Set 'autoRotate' to true if the carousell should autorotate
var autoRotate = true;

//Set 'autoRotateCW' to true if the carousell should auto rotate clockwise or false if counter clockwise
var autoRotateCW = false;

//Set 'rotateTime' to the time in milliseconds between each autoRotate
var rotateTime = 5000;

//Set 'Middle' to the point where the Middle of the circle should be
var xMiddle = 480;

//Set 'xRadius' to the width of the circle
var xRadius = 450;

//Set 'Middle' to the point where the Middle of the circle should be
var yMiddle = 170;

//Set 'yRadius' to the height of the circle
var yRadius = 0;

//Set 'minWidth' to the minimum width of item (always have to be larger than 0)
var minWidth = 0;

//Set 'maxWidth' to the maximum width of item (always have to be larger than 0)
var maxWidth = 135;

//Set 'circelParts' to the number of parts to devide the whole circle in
var circleParts = 12;

//Set 'numItems' to the number of items to be shown
var numItems = 5;

//Set 'focus' to which of the items to be in focus
var focus = 3;

//Set 'startOffset' to what part in the circle the item in 'focus' should be
//The first part is counted from 0 degres in the circle
var startOffset = 9;

//Set 'maxWidthOffest' to what part in the circle the item with max width should be.
//Often this is the same as the item in focus
var startOffsetMaxWidth = 9;

//Set 'numSteps' to the number of steps each pic should take in a movement.
//The larger the number the smoother the move will be
var numSteps = 20;

//Set 'stepTime' to the time in milliseconds an item move from one circle part to the next
var stepTime = 500;

//Set 'stepTimeAuto' to the time in milliseconds an item move from one circle part to the next for auto rotation
var stepTimeAuto = 1000;

/* ------------------------------- */


/* -------- Do not alter! -------- */
var steppingCount = 0;
var steppingInterval;
var cycleInterval;
var isStepping = false;
var stepping = 1 / numSteps;
var rounding = 10;
var auto = true;
/* ------------------------------- */

var opacity = !(jQuery.browser.msie);
var ie6 = (jQuery.browser.msie && jQuery.browser.version == '6.0');
jQuery(document).ready(function () {

    jQuery('ul#RD_Rotator img').each(function (i) {
        var img = new Image();
        img.src = this.src;
        var width = img.width;
        var height = img.height;
        jQuery(this).attr('owidth', width);
        jQuery(this).attr('oheight', height);
        if (ie6) {
            jQuery(this).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + this.src + "'");
            this.src = "Images/Rotator/Clear.gif";
        }
    });



    Stop = function () {
        clearInterval(steppingInterval);
        steppingCount = 0;
        isStepping = false;
        LoadInFocus(jQuery('ul#RD_Rotator li:eq(' + (focus - 1) + ')').find('.RD_InFocus'));
    };

    Start = function (handle) {
        if (isStepping) return false;
        steppingCount = 0;
        isStepping = true;
        var time = auto ? stepTimeAuto : stepTime;
        steppingInterval = setInterval(handle, Math.floor(time / numSteps));
        return false;

    };
    CycleInterval = function () {
        if (autoRotate) {
            if (autoRotateCW)
                Start('RotateCW(1)');
            else
                Start('RotateCCW(1)');
        }
    }
    RotateCCW = function (steps) {

        if (steppingCount == 0) {
            jQuery('ul#RD_Rotator li.first').removeClass('first');
            jQuery('#RD_Info').hide();
        }

        steppingCount += stepping;

        if (steppingCount > 1)
            steppingCount = 1;

        var list = jQuery('ul#RD_Rotator li');

        for (var i = 0; i < list.size(); i++) {
            //Get the angle
            var angle = (startOffset + focus - 1 - i + steppingCount) * (Math.PI * 2 / circleParts);

            //Fade out old item
            if (i == 0)
                ObjectFadeOut(list[i], 1 - steppingCount);

            //Fade in new item
            if (i == numItems)
                ObjectFadeIn(list[i], steppingCount);

            //Move items
            if (i <= numItems)
                ObjectRender(list[i], angle, i);

        }

        if (steppingCount >= 1) {
            jQuery('ul#RD_Rotator li:first').removeAttr('style').appendTo(jQuery('ul#RD_Rotator'));
            Stop();
            steps--;
            if (steps > 0)
                Start('RotateCCW(' + steps + ')');
            else {
                jQuery('#RD_Info').show();
                jQuery(list[focus]).addClass('first');
            }

        }

    };

    RotateCW = function (steps) {

        if (steppingCount == 0) {
            jQuery('ul#RD_Rotator li:last').removeAttr('style').prependTo(jQuery('ul#RD_Rotator'));
            jQuery('ul#RD_Rotator li.first').removeClass('first');
            jQuery('#RD_Info').hide();
        }

        steppingCount += stepping;

        if (steppingCount > 1)
            steppingCount = 1;

        var list = jQuery('ul#RD_Rotator li');

        for (var i = 0; i < list.size(); i++) {
            //Get angle
            var angle = (startOffset + focus - 1 - i + 1 - steppingCount) * (Math.PI * 2 / circleParts);

            //Fade in new item
            if (i == 0)
                ObjectFadeIn(list[i], steppingCount);

            //Fade out old item
            if (i == numItems)
                ObjectFadeOut(list[i], 1 - steppingCount);

            //Move Items
            if (i <= numItems)
                ObjectRender(list[i], angle, i);


        }

        if (steppingCount >= 1) {
            jQuery(list[numItems]).removeAttr('style');
            Stop();
            steps--;
            if (steps > 0)
                Start('RotateCW(' + steps + ')');
            else {

                jQuery('#RD_Info').show();
                jQuery(list[focus - 1]).addClass('first');
            }
        }
    };


    ObjectFadeIn = function (obj, opac) {
        if (opacity)
            jQuery(obj).find('img').css("opacity", opac);
        jQuery(obj).show();
    };
    ObjectFadeOut = function (obj, opac) {
        if (opacity)
            jQuery(obj).find('img').css("opacity", opac);
        else if (!opacity || opac <= 0) {
            jQuery(obj).hide();
        }
    };


    ObjectRender = function (obj, angle, index) {

        var imgs = jQuery(obj).find('img');
        var imgIndex = (jQuery(imgs[0]).attr('width') == '0' ? 1 : 0);
        var oWidth = parseInt(jQuery(imgs[imgIndex]).attr('owidth'));
        var oHeight = parseInt(jQuery(imgs[imgIndex]).attr('oheight'));
        var scale = oHeight / oWidth;

        var sizeMove = ((3 / 2) - (startOffsetMaxWidth / circleParts * 2)) * Math.PI;

        var width = Math.floor(minWidth + (maxWidth - minWidth) / 2 - (Math.sin(angle + sizeMove)) * (maxWidth - minWidth) / 2);
        var height = width * scale;
        var x = xMiddle + Math.cos(angle) * xRadius - width / 2;

        var y = yMiddle - Math.sin(angle) * yRadius - height / 2;


        var zIndex = Math.floor(20 - Math.sin(angle) * 10);

        jQuery(obj).css({ zIndex: zIndex, left: x, top: y });
        jQuery(obj).find('img').css('width', width);

        if (ie6) {
            jQuery(imgs).css('height', Math.round(width * scale));
        }
    };


    LoadInFocus = function (obj) {
        jQuery('.MD_InFocus').html(obj.html());
        jQuery('#RD_RotatorHolder #RD_Info').html(obj.html());
    };

    jQuery('ul#RD_Rotator li:eq(' + (focus - 1) + ')').addClass('first');

    //if (!ie6)
    //    jQuery('ul#RD_Rotator').hide();
    jQuery('ul#RD_Rotator li').each(function (i) {

        if (i >= numItems) return;
        var angle = (startOffset + focus - 1 - i) * (Math.PI * 2 / circleParts);
        jQuery(this).show();
        ObjectRender(this, angle, i);
    });


    jQuery('ul#RD_Rotator img.blurred').click(function () {
        this.blur();
        var obj = jQuery(this).parent();
        var list = jQuery('ul#RD_Rotator li');
        var index;
        for (index = 0; index < list.length; index++) {
            if (list[index].id == obj.attr('id')) break;
        }
        if (focus - index - 1 < 0)
            Start('RotateCCW(' + Math.abs(focus - index - 1) + ')');
        else
            Start('RotateCW(' + Math.abs(focus - index - 1) + ')');
        return false;
    });
    if (!ie6)
        jQuery('ul#RD_Rotator').fadeIn(1000);

    jQuery('a#RD_NavRight').click(function () {
        this.blur();
        if (flipButtons)
            Start('RotateCW(1)');
        else
            Start('RotateCCW(1)');
        return false;

    });
    jQuery('a#RD_NavLeft').click(function () {
        this.blur();
        if (flipButtons)
            Start('RotateCCW(1)');
        else
            Start('RotateCW(1)');
        return false;
    });
    jQuery('#RD_RotatorHolder').hover(function () {
        clearInterval(cycleInterval);
        auto = false;
    }, function () {
        cycleInterval = setInterval('CycleInterval()', rotateTime);
        auto = true;
    });
    cycleInterval = setInterval('CycleInterval()', rotateTime);
    LoadInFocus(jQuery('ul#RD_Rotator li:eq(' + (focus - 1) + ')').find('.RD_InFocus'));
});
