﻿function photosRotator(p_parent) {

    var images = new Array();
    
    var linkdots = new Array();

    var currentImagesId;
    var maxImagesId;

    var parent;

    var timer;

    var isReady;

    initialize(p_parent);

    function initialize(p_parent) {
        parent = p_parent;
        images = $(p_parent).find("ul li img");
        
        currentImagesId = images.length - 1;
        maxImagesId = currentImagesId;

        isReady = true;

        if (images.length > 1) {
            createArrowButton();
            createLinkDots();
            
            play();   
        }
    }
    
    function createArrowButton(){
        var div = $(parent).find("div#rightButton");
        
        $(div).append("<a href=\"#\" onclick=\"javascript:pr.showNextImages();\"><img src=\"/images/rightArrow.png\" alt=\"next\" /></a>");
        $(div).addClass("buttonBg");

        var div2 = $(parent).find("div#leftButton");
        $(div2).append("<a href=\"#\" onclick=\"javascript:pr.showPreviousImages();\"><img src=\"/images/leftArrow.png\" alt=\"previous\" /></a>");
        $(div2).addClass("buttonBg");
    }

    function createLinkDots() {
        for (var i = 0; i <= maxImagesId; i++) {
            $(parent).append("<a href=\"#\"><div id=\"ld"+i+"\"class=\"imgLinkdots\"></div></a>");
            $("div#ld"+i).css("left", (maxImagesId * 15 - 15 * i)+30);
        }
        linkdots = $(parent).find("div.imgLinkdots");

        $(linkdots[linkdots.length - 1]).addClass("linkdotsOver");

        var _this = this;
        var i = 0;
        $(linkdots).each(
            function() {
                $(this).bind("click", { Id: i }, clickLinkDots);
                i++;
            }
        );
    }

    function clickLinkDots(event) {
        showImagesId(event.data.Id);
    }

    
    function showNextImages() {
        var idToShow = currentImagesId - 1;
        if (idToShow < 0) {
            idToShow = maxImagesId;
        }
        showImagesId(idToShow);
    }
    photosRotator.prototype.showNextImages = function() { showNextImages(); }
    

    function showPreviousImages() {
        var idToShow = currentImagesId + 1;
        if (idToShow > maxImagesId) {
            idToShow = 0;
        }
        showImagesId(idToShow);
    }
    photosRotator.prototype.showPreviousImages = function() { showPreviousImages(); }
    

    function showImagesId(id) {
        if (isReady == true) {
            isReady = false;
            restart();
        
            if (id < currentImagesId) {
                hideImages(id);
            } else {
                showImages(id);
            }
            currentImagesId = id;
            
            hideAllLinkdots();
            $(linkdots[currentImagesId]).addClass("linkdotsOver");
        }
    }
    function hideAllLinkdots() {
        $(linkdots).each(function() {
            $(this).removeClass("linkdotsOver");
        });
    }

    function hideImages(id) {
        hideUnderImage(id);
        $(images[currentImagesId]).stop().animate({ opacity: 0.0 }, { queue: false, duration: 1200 });
    }
    function hideUnderImage(id) {
        for (var i = currentImagesId - 1; i > id; i--) {
            $(images[i]).css("opacity", 0.0);
        }
        isReady = true;
    } 
    

    function showImages(id) {
        $(images[id]).stop().animate({ opacity: 1.0 }, { queue: false, duration: 1200, complete: function() { showUnderImages(id); } });
        
    }
    function showUnderImages(id) {
        for (var i = 0; i < id; i++) {
            $(images[i]).css("opacity", 1.0);
        }
        isReady = true;
    }
    

    function play() {
        timer = setInterval(function() { showNextImages(); }, 5000);
    }

    function stop() {
        clearTimeout(timer);
    }

    function restart() {
        stop();
        play();
    }
}
