﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

/*$.fn.makeAbsolute = function(rebase) { return this.each(function() { var el = $(this); var pos = el.position(); el.css({ position: "absolute", marginLeft: 0, marginTop: 0, top: pos.top, left: pos.left }); if (rebase) el.remove().appendTo("body"); }); }
*/

var Cybercom = {};
Cybercom.itemExpand = {
    items: null,

    init: function() {
        this.items = jQuery('.toggleintro');
        if (this.items.length === 0) { return; }
        this.setBehavior();
    },

    setBehavior: function() {
        jQuery(this.items).click(function() {
            $(this).parent().toggleClass('itemexpanded');
            $(this).parent().toggleClass('itemcollapsed');
        });
    }
};
Cybercom.fileInput = {
    items: null,

    init: function() {
        this.items = jQuery('input.file');
        if (this.items.length === 0) { return; }
        this.setBehavior();
    },

    setBehavior: function() {
        jQuery(this.items).change(function() {
            var textbox = jQuery(this).next().find('input');
            jQuery(textbox).val(this.value);
        });
    }
};
Cybercom.menu = {
    menuList: null,

    init: function() {
        this.menuList = jQuery('#MainMenu ul');
        if (this.menuList.length === 0) { return; }
        jQuery(this.menuList).superfish({ autoArrows: false, onBeforeShow: function() { }, delay: 200 });
    }
};
Cybercom.reportDownload = {
    chapters: null,
    
    init: function() {
            jQuery(".dlbox-list-title").siblings('li').hide();
            this.chapters = jQuery(".dlbox-list-title");
            if (this.chapters.length === 0) return;
            this.setBehavior();
            
    },
    
    setBehavior : function() {
            jQuery(this.chapters).click(function() {
                jQuery(jQuery(this).parent()).toggleClass('expanded');
                jQuery(jQuery(this).siblings('li')).slideToggle('fast'); //{ duration: 500, easing: 'easeOutCubic'});
            });
    }
};
Cybercom.chartTable = {
    scrollToId: 'flashcontent',
    selectedElement: null,
    flashMovie: null,
    table: null,
    tableRows: null,
    flashData: null,
    lang: null,

    init: function() {
        this.flashMovie = document.getElementById('ctl00_ctl00_FormHolder_PageHolder_CenterContentHolder_MainBodyHolder_FlashContainer-Movie');
        this.table = jQuery('#FinancialChartTable');
        this.tableRows = jQuery('#FinancialChartTable tr:has(.rowtitle)');
        this.lang = jQuery("html").attr('lang');
        if (this.flashMovie == null || this.table == null || this.flashMovie.length === 0 || this.tableRows.length === 0) { return; }
        this.setBehavior();
    },

    setBehavior: function() {
        var inst = this;
        jQuery(this.tableRows).click(function() {
            if (inst.selectedElement != null) {
                if (jQuery(inst.selectedElement).index(this) == 0) return;

                var prevElem = inst.selectedElement;
                jQuery(prevElem).removeClass('selected');
            }
            inst.selectedElement = this;
            //jQuery.scrollTo('#' + this.scrollToId, 800, { offset: { top: -50} });
            jQuery(inst.selectedElement).addClass('selected');
            inst.createXml();
            inst.flashMovie.setData(inst.flashData);
        });
    },

    createXml: function() {
        this.flashData = "<chart><series>";
        var headElements = jQuery(this.table).find('tr.header > td.columntitle');
        for (var i = 0; i < headElements.length; i++) {
            this.flashData += "<value xid='" + i + "'>" + $(headElements[i]).html().replace(/<\S*.?\/?>/g, ' ') + "</value>";
        }
        this.flashData += "</series><graphs><graph gid='0'>";
        var dataElements = $(this.selectedElement).children('.data');
        var tl = /,/g;
        var cs = /\./g;
        if (this.lang == "sv"){
            tl = / /g;
            cs = /,/g;
        }
        for (var i = 0; i < dataElements.length; i++) {
            this.flashData += "<value xid='" + i + "'>" + $(dataElements[i]).html().replace(tl, '').replace(cs, '.') + "</value>";
        }
        var title = $(this.selectedElement).find(".rowtitle")[0].innerHTML;
        this.flashData += "</graph></graphs><labels><label><y>10</y><align>center</align><text_color>#000000</text_color><text_size>15</text_size><text><![CDATA[<b>" + title + "</b>]]></text></label></labels></chart>";
    }
};
Cybercom.fancy = {
    images: null,

    init: function() {
        this.images = jQuery("img.fancy");
        if (this.images.length > 0) {
            jQuery(this.images).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(); });
        }
    }
};
Cybercom.LangList = {
    init: function() {
        jQuery('#LocationArea a').click(function(e) {
            e.preventDefault();
            if (jQuery.browser.msie && jQuery('#StartFlash')) {
                jQuery('#StartFlash').hide();
            }
            jQuery('#LocationList').css({ 'opacity': 0, 'display': 'inline' });
            jQuery('#LocationList').animate({ 'opacity': 1 }, 200);
            //jQuery('#LocationList').show();
            return false;
        });
        jQuery('form').click(function(event) {
            if (!jQuery(event.target).parents().is('#LocationList') && !jQuery(event.target).parents().is('#LocationArea')) {
                jQuery('#LocationList').css({ 'opacity': 0, 'display': 'none' });
                if (jQuery.browser.msie && jQuery('#StartFlash'))
                    jQuery('#StartFlash').show();
            }
        });
    }
};

$(document).ready(function() {
    Cybercom.LangList.init();
    Cybercom.itemExpand.init();
    Cybercom.fileInput.init();
    Cybercom.reportDownload.init();
    Cybercom.chartTable.init();
    Cybercom.fancy.init();
    //Cybercom.menu.init();
});

function GetContactInfo(pid) {
    var innerH = jQuery('#InnerArea').height();
    //jQuery('#inner').height(innerH);
    jQuery('#InnerArea').animate({
        opacity: 0
    }, 300, function() {
        //jQuery('.loader').show();
        jQuery.ajax({
            type: "POST",
            url: "/Templates/Cybercom/WebServices/AjaxService.asmx/GetPageContent",
            data: "{'pageid':'" + pid + "','lang':'" + jQuery('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('#InnerArea').html(msg.d);
                //jQuery('#InnerArea').css('top',-100);
                //jQuery('.loader').hide();
                jQuery('#InnerArea').animate({
                    //top: '0',
                    opacity: 1
                }, 500, function() {
                    innerH = jQuery('#InnerArea').height();
                    jQuery('#OuterArea').animate({
                        height: innerH
                    }, 500);
                });
            },
            error: function(xhr, msg, e) {
                alert(msg + ', ' + e); //Error Callback
            },
            complete: function(xhr, status) {
            }
        });
    });
}
