﻿
panelExclusive = new function ($) {

    var $options = { pages: [], interval: 10000 };
    var isInTransition = false;
    var pagesHolder;
    var currentPage;
    var timer;
    var pageStatus = [];

    function init(options) {
        $.extend($options, options);

        pagesHolder = document.getElementById("exclusive_pages");
        currentPage = 1;
        pageStatus[1] = true;

        //set fixed height to pages holder (no blinking when fade in/out)
        jQuery(pagesHolder).css("height", jQuery(pagesHolder).height());

        play();
    };

    function loadPage(page) {
        var $args = {};

        $args.methodName = $options.methodName;
        $args.assembly = $options.assembly;
        $args.methodClass = $options.methodClass;
        $args.postType = "POST";
        $args.async = false;
        $args.onOk = function (text) { pageStatus[page] = true; setPageHtml(text); };

        if (page != null) {
            $args.args = [page];
            zajaxm.call($args);
        }
    };

    function setPageHtml(text) {

        //create holder Element
        var newHolder = document.createElement("div");
        newHolder.innerHTML = text;

        for (i = 0; i < newHolder.childNodes.length; i++) {
            if (newHolder.childNodes[i].nodeType == 1)
                pagesHolder.appendChild(newHolder.childNodes[i]);
        }
    };

    function setPage(page) {

        if (isInTransition) return;

        resetTimer();

        if (!pageStatus[page]) {
            loadPage(page);
            window.setTimeout(function () { setPage(page); }, 2000)
            return;
        }

        fade(currentPage, page);
        currentPage = page;

    }

    function nextPage() {

        if (isInTransition) return;

        resetTimer();
        var nextPage = (currentPage % ($options.pages.length + 1)) + 1;
        setPage(nextPage);
    }

    function prevPage() {

        if (isInTransition) return;
        resetTimer();

        var prevPage;

        if (currentPage == 1)
            prevPage = ($options.pages.length + 1);
        else
            prevPage = currentPage - 1;

        setPage(prevPage);
    }

    function fade(fromPage, toPage) {
        isInTransition = true;
        jQuery("#exclusive_page_" + fromPage.toString()).fadeOut("slow",
            function () {

                jQuery("#pagebt_" + fromPage).removeClass("selected");
                jQuery("#exclusive_page_" + toPage.toString()).fadeIn("slow",
                function () {

                    jQuery("#pagebt_" + toPage).addClass("selected");
                    isInTransition = false;
                });
            }
            );
    }

    function resetTimer() {
        window.clearInterval(timer);
        timer = window.setInterval(function () { nextPage(); }, $options.interval);
    }

    function play() {

        if ($options.pages.length > 0)
            timer = window.setInterval(function () { nextPage(); }, $options.interval);
        //jQuery("#ticker").removeClass("paused");
        //jQuery("#ticker").addClass("playing");
    }

    //    function pause() {
    //        window.clearInterval(timer);
    //        jQuery("#ticker").removeClass("playing");
    //        jQuery("#ticker").addClass("paused");
    //    }

    this.init = init;
    //    this.pause = pause;
    //    this.play = play;
    //this.prevPage = prevPage;
    this.setPage = setPage;
    //this.nextPage = nextPage;

} (jQuery);
