﻿jQuery(document).ready(function () {
    if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
        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).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='" + this.src + "'");
            jQuery(this).attr('owidth', width);
            jQuery(this).attr('oheight', height);
            this.src = "/Images/Rotator/Clear.gif";
        });
    }
});

var stepping = 0.2;
var steppingCount = 0;
var steppingInterval;
var cycleInterval;
var isStepping = false;
var rOffset = 15;
var circleParts = 16;
var sizeFix = 55;
var maxStep = 2;
var opacity = !(jQuery.browser.msie);
var ie6 = (jQuery.browser.msie && jQuery.browser.version == '6.0');
jQuery(document).ready(function () {
    Stop = function () {
        clearInterval(steppingInterval);
        steppingCount = 0;
        isStepping = false;
        LoadInFocus(jQuery('ul#RD_Rotator li:eq(0)').find('.RD_InFocus'));
    };
    Start = function (handle, speed) {
        if (isStepping) return false;
        steppingCount = 0;
        isStepping = true;
        steppingInterval = setInterval(handle, speed);
        return false;
    };
    CycleInterval = function () {
        //Start('RotateLeft(1)', 30);
    }
    RotateLeft = function (steps) {

        if (steppingCount == 0) {
            jQuery('ul#RD_Rotator li.first').removeClass('first');
            jQuery('#RD_Info').hide();
        }

        var list = jQuery('ul#RD_Rotator li');

        for (var i = 0; i < list.size(); i++) {
            var angle = ((rOffset - i * 2.5 + steppingCount + 0.5) * (Math.PI) / (circleParts));
            if (i == 3)
                ObjectFadeTo(list[i], steppingCount);
            if (i < 4)
                ObjectMove(list[i], angle, i, true);
            if (i == 0) {
                if (opacity)
                    jQuery(list[i]).find('img').css("opacity", (1 - (steppingCount / maxStep)));
                else jQuery(list[i]).hide();
            }
        }

        steppingCount += stepping;
        if (steppingCount >= maxStep) {
            jQuery('ul#RD_Rotator li:first').removeAttr('style').appendTo(jQuery('ul#RD_Rotator'));
            Stop();
            steps--;
            if (steps > 0)
                Start('RotateLeft(' + steps + ')', 30);
            else {
                jQuery('#RD_Info').show();
                jQuery(list[1]).addClass('first');
            }

        }

    };

    RotateRight = 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();
        }

        var list = jQuery('ul#RD_Rotator li');

        for (var i = 0; i < list.size(); i++) {
            var angle = ((rOffset - i * 2.5 + maxStep - steppingCount) * (Math.PI) / (circleParts));
            if (i == 0)
                ObjectFadeTo(list[i], steppingCount);
            if (i < 4)
                ObjectMove(list[i], angle, i, false);
            if (i == 3 && opacity) jQuery(list[i]).find('img').css("opacity", (1 - Math.round((steppingCount / maxStep) * 10) / 10));

        }

        steppingCount += stepping;

        if (steppingCount >= maxStep) {
            jQuery(list[3]).removeAttr('style');
            Stop();
            steps--;
            if (steps > 0)
                Start('RotateRight(' + steps + ')', 30);
            else {

                jQuery('#RD_Info').show();
                jQuery(list[0]).addClass('first');
            }
        }
    };
    ObjectFadeTo = function (obj, stepping) {
        if (opacity)
            jQuery(obj).find('img').css("opacity", Math.round((stepping / maxStep) * 10) / 10);
        jQuery(obj).show();
    };
    ObjectMove = function (obj, angle, index, left) {

        var sizeOffset = Math.round(-(Math.cos(angle) * 110)) < 0 ? 0 : Math.round(-(Math.cos(angle) * 110));

        var size = (300 + Math.abs(sizeOffset)) - (index * sizeFix);

        if (left)
            size = size + sizeFix;
        
        var zind = size / 10;
        
        var yOffset = Math.round(-(Math.sin(angle) * 110)) > 0 ? 0 : Math.round(-(Math.sin(angle) * 110));

        var y = 20 - yOffset * 0.4;

        var x = 950 + Math.cos(angle) * 700 - size / 2;

        jQuery(obj).css({ zIndex: zind, top: y, left: x });
        var cWidth = jQuery(obj).find('img').width();
        jQuery(obj).find('img').css('width', size);
        if (ie6) {
            var imgs = jQuery(obj).find('img');
            var index = (jQuery(imgs[0]).attr('width') == '0' ? 1 : 0);
            var oWidth = parseInt(jQuery(imgs[index]).attr('owidth')) <= 0 ? 1 : parseInt(jQuery(imgs[index]).attr('owidth'));
            var oHeight = parseInt(jQuery(imgs[index]).attr('oheight'));
            var scale = oHeight / oWidth;
            jQuery(imgs).css('height', Math.round(size * scale));
        }
    };
    LoadInFocus = function (obj) {
        jQuery('.MD_InFocus').html(obj.html());
        jQuery('#RD_RotatorHolder #RD_Info').html(obj.html());
    };
    jQuery('#RD_Gradient').height(jQuery('#RD_Gradient').parent().height());
    jQuery('ul#RD_Rotator li:eq(0)').addClass('first');
    if (!ie6)
        jQuery('ul#RD_Rotator').hide();
    jQuery('ul#RD_Rotator li').each(function (i) {

        if (i > 2) return;
        var angle = ((rOffset - i * 2.5) * (Math.PI) / (circleParts));

        var sizeOffset = Math.round(-(Math.cos(angle) * 110)) < 0 ? 0 : Math.round(-(Math.cos(angle) * 110));

        var size = (300 + Math.abs(sizeOffset)) - (i*55);
        var zind = size / 10;

        var yOffset = Math.round(-(Math.sin(angle) * 110)) > 0 ? 0 : Math.round(-(Math.sin(angle) * 110));
        var y = 20 - yOffset * 0.4;

        var x = 950 + Math.cos(angle) * 700 - size / 2;

        jQuery(this).show();
        jQuery(this).css({ zIndex: zind, top: y, left: x });
        jQuery(this).find('img').css('width', size);
        if (ie6) {
            var imgs = jQuery(this).find('img');
            var index = (jQuery(imgs[0]).attr('width') == '0' ? 1 : 0);
            var oWidth = parseInt(jQuery(imgs[index]).attr('owidth')) <= 0 ? 1 : parseInt(jQuery(imgs[index]).attr('owidth'));
            var oHeight = parseInt(jQuery(imgs[index]).attr('oheight'));
            var scale = oHeight / oWidth;
            jQuery(imgs).css('height', Math.round(size * scale));
        }
    });


    jQuery('ul#RD_Rotator img.blurred').click(function () {
        var obj = jQuery(this).parent();
        var list = jQuery('ul#RD_Rotator li');
        var index = 0;
        for (var i = 0; i < list.length; i++) {
            index = i;
            if (list[i].id == obj.attr('id')) break;
        }
        return Start('RotateLeft(' + index + ')', 30);
    });
    if (!ie6)
        jQuery('ul#RD_Rotator').fadeIn(1000);

    jQuery('a#RD_NavRight').click(function () {
        this.blur();
        return Start('RotateLeft(1)', 30);
    });
    jQuery('a#RD_NavLeft').click(function () {
        this.blur();
        return Start('RotateRight(1)', 30);
    });
    jQuery('#RD_RotatorHolder').hover(function () {
        clearInterval(cycleInterval);
    }, function () {
        cycleInterval = setInterval('CycleInterval()', 4000);
    });
    cycleInterval = setInterval('CycleInterval()', 4000);
    LoadInFocus(jQuery('ul#RD_Rotator li:eq(0)').find('.RD_InFocus'));
});
