﻿var MTG = {};
MTG.Pager = {
    pages: null,
    currentPage: 1,
    pagingControlsContainer: "#pagingControls",
    pagingContainer: null,
    pagingContainerPath: "#content",
    pageSelector: ".innerPage",

    init: function() {
        this.pagingContainer = $(this.pagingContainerPath);
        this.pages = jQuery(this.pageSelector, this.pagingContainer);
        this.showPage(1);
    },

    numPages: function() {

        var numPages = 0;
        if (this.pages != null) {
            numPages = this.pages.length;
        }

        return numPages;
    },

    showPage: function(page) {
        this.currentPage = page;
        var html = "";
        for (var i = (page - 1); i < (page - 1); i++) {
            if (i < this.pages.length) {
                var elem = this.pages.get(i);
                html += "<" + elem.tagName + ">" + elem.innerHTML + "</" + elem.tagName + ">";
            }
        }

        $(this.pagingContainerPath).html(html);

        renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
    },

    renderControls: function(container, currentPage, numPages) {
        var pagingControls = "Page: <ul>";
        for (var i = 1; i <= numPages; i++) {
            if (i != currentPage) {
                pagingControls += "<li><a href='#' onclick='pager.showPage(" + i + "); return false;'>" + i + "</a></li>";
            } else {
                pagingControls += "<li>" + i + "</li>";
            }
        }

        pagingControls += "</ul>";

        $(container).html(pagingControls);
    }
};

MTG.TogglePreview = {
    init: function() {
        if (jQuery('.togglepreview')) {
            jQuery('.togglepreview').click(function(e) {
                e.preventDefault();
                jQuery(this).parents('.BoxContent').toggleClass('expanded');
                jQuery(this).parents('.BoxContent').toggleClass('collapsed');
                return false;
            });
        }
    }
};

function RegisterToggleEvents() {
    jQuery(".toggleintro").click(function() {
        jQuery(this).parent().toggleClass('expanded');
        jQuery(this).parent().toggleClass('collapsed');
    });
};

function GetHistory(pid) {
    var resultcontainer = jQuery('#MainBodyArea');
    //jQuery(resultcontainer).html("<img src=\"/Templates/Public/Images/ajax-loader.gif\" class=\"loadingicon\" alt=\"loading\" />");
    jQuery.ajax({
        beforeSend: function() { jQuery(resultcontainer).hide(); },
        type: "POST",
        url: "/Templates/Public/WebServices/AjaxService.asmx/GetPageContent",
        data: "{'pageid':'" + pid + "','lang':'" + $('html').attr('lang') + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: false,
        cache: false,
        success: function(msg, status) {
            // Replace the div's content with the page method's return.
            $(resultcontainer).html(msg.d);
            $(resultcontainer).fadeIn(1000);
        },
        error: function(xhr, msg, e) {
            alert(msg + ', ' + e); //Error Callback
        }
    });
}

jQuery(document).ready(function() {
    RegisterToggleEvents();
    MTG.TogglePreview.init();
    jQuery(".ImageList li").hover(function() {
        jQuery(this).addClass('itemHover');
    }, function() { jQuery(this).removeClass('itemHover') });

    jQuery('a.modalImage').click(function(e) {
        e.preventDefault();
        jQuery("#ModalContent").modal({ closeClass: 'simplemodal-overlay', opacity: 60, onOpen: function(dialog) {
            dialog.overlay.fadeIn('fast', function() {
                dialog.container.fadeIn('fast');
                dialog.data.show();
            });
        }
        });
        // load using ajax
        jQuery.get(this.href + '?ajax=true', function(data) {
            // create a modal dialog with the data
            jQuery("#ModalContent").html(data);
        });
    });

    jQuery('a.modalContent').click(function(e) {
        e.preventDefault();
        jQuery("#ModalContent").modal({ closeClass: 'simplemodal-overlay', opacity: 50, containerId: 'datamodal-container' });
        // load using ajax
        jQuery.ajax({
            //beforeSend: function() { jQuery(resultcontainer).hide(); },
            type: "POST",
            url: "/Templates/Public/WebServices/AjaxService.asmx/GetPageContentByUrl",
            data: "{'url':'" + this.href + "','lang':'" + $('html').attr('lang') + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            cache: false,
            success: function(msg, status) {
                // Replace the div's content with the page method's return.
                jQuery("#ModalContent").html(msg.d);
            },
            error: function(xhr, msg, e) {
                alert(msg + ', ' + e); //Error Callback
            }
        });
        //jQuery.get(this.href + '?ajax=true', function(data) {
        // create a modal dialog with the data
        //    jQuery("#ModalContent").html(data);
        //});
    });
    jQuery("img.fancy").each(function(i) {
        var src = jQuery(this).attr("src");
        jQuery(this).wrap("<a href=\"" + src + "\"></a>")
    });
    jQuery("a:has(img.fancy)").fancybox({ 'zoomOpacity': true, 'overlayShow': true, 'zoomSpeedIn': 500, 'zoomSpeedOut': 500 })
    .wrap("<div class=\"zoom-holder\"></div>");
    jQuery(".zoom-holder").prepend("<a class=\"zoom\">Förstora</a>");
    jQuery(".zoom").click(function() { jQuery(this).next().click(); });
    //<a href=\"#\" class=\"zoom\">Förstora</a>

    jQuery('input.file').change(function(e) {
        var textbox = jQuery(this).next().find('input');
        //jQuery(textbox).empty();
        jQuery(textbox).val(this.value);
    });
    jQuery('#Print').show();
    jQuery('#Print').click(function(e) {
        e.preventDefault();
        window.print();
        return false;
    });
});
