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