﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

(function($) {
    $.fn.autoheight = function() {
        var height = 0,
            highest = 0,
            reset = $.browser.msie ? "1%" : "auto";
        return this.css("height", reset).each(function() {
            height = Math.max(height, this.offsetHeight);
            if (height > highest) {
                highest = height;
                $("*").removeClass("longest");
                $(this).addClass("longest");
            };
        }).css("height", height).each(function() {
            var h = this.offsetHeight;
            if (h > height) {
                $(this).css("height", height - (h - height));
            };
        });
    };
})(jQuery);

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');
        });
    }
};
Axfood.FAQ = {
    init: function() {
        if (jQuery("#faq-list").length > 0) {
            $("#faq-toggle-all").click(function() {
                if ($(this).attr("rel") == "open") {
                    jQuery("#faq-list dl > *").addClass("open");
                    $(this).attr("rel", "");
                }
                else {
                    jQuery("#faq-list dl > *").removeClass("open");
                    $(this).attr("rel", "open");
                }
            });
            jQuery("#faq-list dl dd").removeClass("open");
            jQuery("#faq-list dl dt").click(function() {
                if ($(this).hasClass("open")) {
                    $(this).removeClass("open");
                    $(this).next("dd").removeClass("open");
                }
                else {
                    $(this).addClass("open");
                    $(this).next("dd").addClass("open");
                }
            });
        }
    }
}
Axfood.Tabs = function(selector) {
    var tabs = jQuery(selector);

    $.each(tabs, function(index) {
        jQuery(this).next().find('div:not(.active)').addClass('inactive');
        var currentTab = this;
        jQuery(this).find('ul li a').click(function(e) {
            e.preventDefault();
            if (!jQuery(this).parent().hasClass('selected')) {
                jQuery(currentTab).find('ul li').removeClass('selected');
                jQuery(this).parent().addClass('selected');
                var href = jQuery(this).attr('href');

                jQuery(currentTab).next().find('div').addClass('inactive');
                jQuery(href).removeClass('inactive');
            }
        });
    });
};
Axfood.SetTeaserAutoHeight = function(selector) {
    var tesers = $(selector);
    var rows = tesers.length / 3;
    var rowArray = new Array();
    for (var i = 0; i < rows; i++) {
        tesers.filter(function(index) { return (index >= i * 3 && index < (i + 1) * 3) }).find('.content').autoheight();
    }
};

$(document).ready(function() {
    Axfood.comment.init();
    Axfood.modal.init();
    Axfood.fancy.init();
    Axfood.concept.init();
    Axfood.print.init();
    Axfood.history.init();
    Axfood.FAQ.init();
    Axfood.Tabs('.tabs');
});

