﻿/*/// <reference path="jquery-1.3.2-vsdoc2.js" />*/
 
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('#main-body');
    /*/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*/
        }
    });
}

MTG.Tooltip = {
    init: function() {

        /* create custom tooltip effect for jQuery Tooltip*/
        $.tools.tooltip.addEffect("fadeX",
        /* opening animation */
	                    function(done) {
	                        if (!$.browser.msie) {
	                            this.getTip().fadeIn(300);
	                        }
	                        else {
	                            this.getTip().show();
	                        }
	                    },

        /* closing animation */
	                    function(done) {
	                        if (!$.browser.msie) {
	                            this.getTip().fadeOut(30, function() {
	                                $(this).hide();
	                                done.call();
	                            });
	                        }
	                        else {
	                            this.getTip().hide();
	                        }
	                    }
                    );

        /*The brands tooltip:*/
        jQuery('#brands a.logolink').tooltip({
            tip: '#tooltip',
            /* http://flowplayer.org/tools/tabs/index.html
            // tweak the position */
            offset: [-55, -10],
            position: 'bottom right',
            /*relative: true,*/
            predelay: 500,
            effect: 'fadeX',
            onBeforeShow: function() {
                MTG.Tooltip.bindTemplate(this);
            }
        });
        $("#list-brands a.logolink").tooltip({
            tip: '#tooltip',
            offset: [-60, -20],
            position: 'bottom right',
            /*relative: true,*/
            predelay: 500,
            effect: 'fadeX',
            onBeforeShow: function() {
                MTG.Tooltip.bindTemplate(this);
            }
        });
    },
    bindTemplate: function(tooltip) {
        var trigger = tooltip.getTrigger();
        var tip = tooltip.getTip();
        var title;
        if ($.data(trigger, 'title') != null)
            title = $.data(trigger, 'title');
        else {
            title = trigger.find('img').attr('title');
            trigger.find('img').attr('title', '');
            $.data(trigger, 'title', title);
        }
        tip.find('h4 a.link span').html(title);
        tip.find('a.link').attr('href', trigger.attr('href'));
        tip.find('p').html(trigger.attr('rel'));
    }
}

MTG.StartpageBrands = {
    init: function() {
        if (jQuery('#startpage #brands ul.tabs').length > 0) {
            /* Display logotypes after pageload to avoid flicker effect (hidden initially by CSS file)*/
            jQuery('#brands div.panes a').css('display', 'block');

            /* setup ul.tabs to work as tabs for each div directly under div.panes */
            $("#brands ul.tabs").tabs("div.panes > div", {
                /*effect: 'fade',
                //fadeInSpeed: 1000,*/
                rotate: false,
                autoplay: true,
                interval: 3000,
                initialIndex: Math.floor(Math.random() * 4)
            })

            $.tools.tabs.addEffect("default", function(tabIndex, done) {

                /* hide all panes and show the one that is clicked*/
                this.getPanes().hide().find('a.logolink').stop().css({ opacity: 1 });
                $currentPane = this.getPanes().eq(tabIndex);
                $currentPane.find('a.logolink').hide().end().show();

                var n = $currentPane.find('a.logolink').size();
                var height = Math.ceil(n / 5) * 68;
                var fadeInSpeed = 12000 / n; /* speed in ms */
                $('#brands .innerpane').animate({ 'height': height + 'px' }, 500)
                $currentPane.find('a.logolink').each(function(index) {
                    $(this).delay(index * (fadeInSpeed / 16)).fadeIn();
                });

                /* the supplied callback must be called after the effect has finished its job */
                done.call();
            });


            /* NO AUTO SLIDESHOW
            //            $tabs = jQuery('#brands ul.tabs');
            //            //starts playing the slideshow
            //            $tabs.data("slideshow").play();

            //            //onhover stop slideshow, onmouseout start slideshow...
            //            jQuery('#brands').hover(function() {
            //                $tabs.data("slideshow").stop();
            //            },
            //            function() {
            //                $tabs.data("slideshow").play();
            //            });
            */
        } /*end brands tabs*/
    }
}
/*
MTG.StartFlowplayer = {
    init: function() {
        if (jQuery('#StartPage a#player').length > 0) {
//            $("#StartPage a#player[rel]").overlay();
        }
    }
}
*/

MTG.DropDownMenu = {
    init: function() {
        if (jQuery('#nav ul li').length > 0) {

            jQuery('#nav ul li').hover(function(e) {

                jQuery(this).addClass('open');
                /*jQuery(this).css('background-color', '#2B2A28');*/

            }, function() {
                jQuery(this).removeClass('open');
                /*jQuery(this).find('.dropdown').css('display', 'none');*/
            });

            /*also hide menu if hover out of div dropdown:*/
            jQuery('#nav ul li').mouseleave(function() {
                jQuery(this).parent().removeClass('open');
                /*jQuery(this).css('display', 'none');*/
            })
        };

        ;
    }
};

MTG.StartNewsSlider = {
    init: function() {
    if ($('#news-slider div').length > 1) {
        $('#news-slider').prev().html('<a id="prev-slide" href="#">Prev</a><ul id="slide-nav"></ul><a id="next-slide" href="#">Next</a>').end()
            .cycle({
                fx: 'scrollHorz',
                pager: '#slide-nav', 
                prev: '#prev-slide',
                next: '#next-slide',
                pause: true,
                pauseOnPagerHover: true,
                timeout: 6000
            });
        }
    }
};

MTG.TopSlider = {
    init: function() {
        if ($('#top-container').length > 0) {
            $('#top-container')
            .cycle({
                fx: 'fade',
                timeout: 4000
            });
        }
    }
};

jQuery(document).ready(function() {

    RegisterToggleEvents();
    MTG.TogglePreview.init();
    MTG.DropDownMenu.init();
    MTG.Tooltip.init();
    MTG.StartpageBrands.init();
    MTG.StartNewsSlider.init();
    MTG.TopSlider.init();
    //MTG.StartFlowplayer.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;
    });
});

