/// <reference path="../jquery.js" />
"use strict";



// =====================================================================================

jQuery.fn.hgRotator = function (settings) {
    settings = jQuery.extend({
        animation: "swing",
        duration: 750,
        interval: 6000,
        navigation: true,
        navposition: "sw",
        textduration: 1500
    }, settings);


    // PROPERTIES
    var hgrContainerWidth = $(this).find("img").first().width();
    var hgrContainerHeight = $(this).find("img").first().height();
    var position = 1;
    var maxPosition = $(this).find("div > div").length;
    var animationInProgress = false;
    var $rotator = $(this).children();
    var $navigation;

    // STYLE
    $(this).addClass("hgRotator");
    /*$(this).css("width", hgrContainerWidth);
    $(this).css("height", hgrContainerHeight);*/

    $(this).find("div > div").addClass("hgrImage");
    $(this).children().append($(this).find("div > div:first-child").clone());
	
	$(this).find("img").show();

    var childrenCombinedWidth = 100;
    $(this).find("div > div").each(function () {
        childrenCombinedWidth += $(this).width();
    });
    $rotator.css("width", childrenCombinedWidth.toString() + "px");
    $rotator.css("position", "absolute");

    $(this).find("div > div > a:nth-child(2)").addClass("hgrText");


    // NAVIGATION
    $navigation = $("<div>").addClass("nav").css("opacity", ".3");
    if (settings.navigation) {
        var cs1 = "top";
        var cs2 = "left";
        if (settings.navposition.charAt(0) == "s") cs1 = "bottom";
        if (settings.navposition.charAt(1) == "e") cs1 = "right";
        $navigation.css(cs1, "4px").css(cs2, "6px");

        $(this).append($navigation);
        $navigation.append($("<a>").attr("rel", "prev").addClass("prev"));
        for (var i = 1; i <= maxPosition; i++) {
            var a = $("<a>").attr("rel", i);
            $navigation.append(a);
        }
        $navigation.append($("<a>").attr("rel", "next").addClass("next"));
        $navigation.find("a").click(function () {
            navMove($(this).attr("rel"));
        });
        if (!$.browser.msie || ($.browser.msie && $.browser.version == "9.0")) {
            $(this).hover(function () {
                $navigation.stop(true, true).animate({ opacity: 1 }, 250);
            }, function () {
                $navigation.stop(true, false).animate({ opacity: .3 }, 250);
            });
        }
    }


    // NAVIGATION POSITION
    var setNavigationPosition = function () {
        $navigation.find("a").removeClass("current");
        $navigation.find("a:nth-child(" + (position + 1) + ")").addClass("current");
    }


    // INTERVAL
    var hgrInterval = setInterval(function () {
        move();
    }, settings.interval);


    // NAV MOVE
    var navMove = function (pos) {
        if (!animationInProgress && pos != position) {
            clearInterval(hgrInterval);
            move(pos);
            hgrInterval = setInterval(function () {
                move();
            }, settings.interval);
        }
    }


    // MOVE
    var move = function (pos) {
        if (animationInProgress) return false;
        animationInProgress = true;

        if (!pos) position++;
        else {
            if (pos.toString() == "next") position++;
            else if (pos.toString() == "prev") {
                position--;
                if (position == 0) {
                    position = maxPosition;
                    var l1 = $rotator.find("div:nth-child(" + (maxPosition + 1).toString() + ")").position().left * -1;
                    $rotator.css("left", l1.toString() + "px");
                }
            }
            else { position = parseInt(pos); }
        }

        var l = $rotator.find("div:nth-child(" + position + ")").position().left * -1;
        var resetPos = false;
        if (position > maxPosition) { resetPos = true; position = 1; }

        $rotator.animate({
            left: l
        }, settings.duration, settings.animation, function () {
            animationInProgress = false;
            if (resetPos) { $rotator.css("left", "0px"); }
            $rotator.find("div").not(":nth-child(" + position + ")").find(" a.hgrText").css("right", (hgrContainerWidth * -1).toString() + "px");
        });
        moveCurrentText();
        setNavigationPosition();
    };

    var moveCurrentText = function () {
        var a = $rotator.find("div:nth-child(" + position + ") a.hgrText").animate({
            right: 20
        }, settings.textduration);
    }
    setNavigationPosition();
    moveCurrentText();
};
