﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var Axfood = {};
Axfood.modal = {

    init: function() {
        this.setBehavior();
    },

    setBehavior: function() {
        $('a.modalImage').click(function(e) {
            e.preventDefault();
            $("#ModalContent").modal({ closeClass: 'modalOverlay', overlay: 30 });
            // load the contact form using ajax
            $.get(this.href + '?ajax=true', function(data) {
                // create a modal dialog with the data
                $("#ModalContent").html(data);
            });
        });
    }
};
Axfood.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(); });
        }
    }
};
Axfood.concept = {
    url: '/Templates/Axfood/WebServices/AjaxService.asmx/GetPageContent',
    o_links: null,
    links: null,
    lang: null,
    target: null,
    current: null,

    init: function() {
        this.o_links = jQuery('.concept a');
        this.links = jQuery('.conceptList .ctrig');
        this.lang = jQuery('html').attr('lang');
        this.target = jQuery('#ConceptContainer');
        if (this.target.length === 0 || this.links.lenght === 0) return;
        this.setOverlayBehavior();
        this.setBehavior();
    },

    setOverlayBehavior: function() {
        var inst = this;
        jQuery(this.o_links).click(function(e) {
            e.preventDefault();
            jQuery('#LayerContentArea .ContentArea .concept').animate(
                { 'top': '-=70px', opacity: 0 }, 500);
                
            jQuery('#LayerContentArea').animate({ opacity: 0 }, 500, function() { jQuery(this).css('display', 'none'); });
            var i = jQuery.inArray(this, inst.o_links);
            inst.current = jQuery(inst.links).eq(i);
            jQuery(inst.current).addClass('selected');
            inst.getData(inst, this)
        });
    },

    setBehavior: function() {
        var inst = this;
        jQuery(this.links).click(function(e) {
            e.preventDefault();
            var elem = this;
            if (inst.current != null)
                jQuery(inst.current).removeClass('selected');
            inst.current = this;
            $(inst.current).addClass('selected');
            jQuery(inst.target).animate({ opacity: 0.1 }, 250)
            $(inst.target).queue(function() {
                inst.getData(inst, elem)
                $(this).dequeue();
            });
        });
    },

    getData: function(inst, link) {
        jQuery.ajax({
            type: "POST",
            url: inst.url,
            data: "{'pageid':'" + jQuery(link).attr('rel') + "','lang':'" + inst.lang + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            cache: false,
            success: function(msg, status) {
                jQuery(inst.target).html(msg.d);
                jQuery(inst.target).animate({ opacity: 1 }, 500);
            },
            error: function(xhr, msg, e) {
                alert(msg + ', ' + e); //Error Callback
            }
        });
    }
};
Axfood.shareInfo = {

    load: function(containerId, url) {
        jQuery.getJSON(url, function(data, textStatus) {
            if (textStatus == null || textStatus == "success" || textStatus == "notmodified") {
                jQuery(containerId + " .lastprice").html(data.data[0].lastprice + ' ' + data.data[0].tradecurrency);
                jQuery(containerId + " .change").html(data.data[0].diff + ' ' + data.data[0].tradecurrency);
            }
            else {
                jQuery(containerId + " .lastprice").html("N/A");
                jQuery(containerId + " .change").html("N/A");
            }
        });

    }
};
Axfood.print = {
    init: function() {
        jQuery('.print').show();
        jQuery('.print').click(function(e) {
            e.preventDefault();
            window.print();
            return false;
        });
    }
};
Axfood.comment = {
    target: null,

    init: function() {
    this.target = jQuery('#CommentArea');
    if (this.target.length === 0) return;
        var defaultopen = jQuery(this.target).hasClass('showComments');
        if (this.target.length > 0 && !defaultopen) {
            if (window.location.hash != '#CommentArea')
                jQuery(this.target).hide();
            this.setBehavior();
        }
    },

    setBehavior: function() {
        jQuery('a.comment').click(function() {
            jQuery('#CommentArea').slideToggle('fast');
        });
    }
};
Axfood.history = {
    selected: null,

    init: function() {
        var instance = this;
        this.selected = jQuery('#history-logos a:first');
        if (this.selected.length === 0) return;
        jQuery('#history-map area').hover(function() {
            jQuery('#history-logos a').attr('class', 'off');
            var target = '#' + jQuery(this).attr('title');
            jQuery(target).attr('class', 'hover');
        }, function() { });
        jQuery('#history-logos a').hover(function() { },
            function() {
                jQuery(this).attr('class', 'off');
                if (instance.selected != null)
                    jQuery(instance.selected).attr('class', 'selected');
            });
        jQuery('#history-logos a').click(function(e) {
            instance.selected = this;
            jQuery(this).attr('class', 'selected');
            jQuery('#history-slides div.show').attr('class', 'hide');
            var target = '#slide-' + jQuery(this).attr('id');
            jQuery(target).attr('class', 'show');
        });
    }
};

$(document).ready(function() {
    Axfood.comment.init();
    Axfood.modal.init();
    Axfood.fancy.init();
    Axfood.concept.init();
    Axfood.print.init();
    Axfood.history.init();
});
