﻿var soderhavet = soderhavet || {};

soderhavet = {
    init: function() {
        soderhavet.hijackAjax();
        startFlash();
        if (!soderhavet.tools.isIElt7()) {
            soderhavet.tools.handleResize();
            soderhavet.flash.resize();
        }
        soderhavet.bubble.init();
    },

    bubble: {
        init: function() {
            soderhavet.bubble.buildBubble();
            $("a.bubbleLink").bind("mouseenter", function() {
                oImg = $(this).children("img");
                soderhavet.bubble.imageHover(oImg, true);
                soderhavet.bubble.show(oImg);
            }).bind("mouseleave", function() {
                oImg = $(this).children("img");
                soderhavet.bubble.hide();
                soderhavet.bubble.imageHover(oImg, false);
            });
        },

        buildBubble: function() {
            $('<div id="bubble"><div id="bubbleTop">&nbsp;</div><div id="bubbleMiddle"></div><div id="bubbleBottom">&nbsp;</div></div>').appendTo('body');
        },

        show: function(oImg) {
            oBubble = $("div#bubble");
            $("div#bubbleMiddle").text(oImg.attr("alt"));
            offset = oImg.offset();
            width = oImg.width();
            bubbleHeight = oBubble.height();
            oBubble.css({ left: ((offset.left + (width / 2)) - 131), top: (offset.top - (bubbleHeight - 15)) });
            if ($.browser.mozilla) {
                oBubble.fadeIn(300);
            } else {
                oBubble.show();
            }
        },

        hide: function() {
            oBubble.hide();
        },

        imageHover: function(oImg, state) {
            image = oImg.attr("src");
            if (state) {
                var strExt = image.match(".jpg|.gif|.png");
                imageSrc = image.split(strExt);
                oImg.attr("src", imageSrc[0] + '_1' + strExt);
            } else {
                imageSrc = image.split('_1.');
                oImg.attr("src", imageSrc[0] + '.' + imageSrc[1]);
            }
        }
    },

    hijackAjax: function() {
        $(".thickbox").each(function() {
            strLink = this.href;
            //strLink = strLink.replace(/http.+\//g, ""); 
            //strLink = strLink.replace(/http.+\%2f/g, "");
            if (soderhavet.tools.isIElt7()) {
                strLink = strLink.replace(/http.+\//g, "");
                strLink = escape(strLink);
            } else {
                strLink = strLink.replace(/http.+\%2f/g, "");
            }

            strLink = strLink.replace(/.aspx/g, "");
            //strLink = "/Ajaxnews.aspx?page="+strLink+"&amp;height=485&amp;width=611&amp;modal=true";
            strLink = "/Ajaxnews.aspx?page=" + strLink + "&amp;TB_iframe=true&amp;height=490&amp;width=520&amp;modal=true";
            //strLink = "/Ajaxnews.aspx?page="+strLink+"&amp;height=458&amp;width=480";
            this.href = strLink;
        });
    },

    closeLink: function() {
        $("a#closeLink").bind("click", function(e) {
            e.preventDefault();
            self.parent.tb_remove();
            //tb_remove();
        });
    },

    flash: {
        resize: function() {
            var flashWidth = parseInt((soderhavet.tools.checkWidth(document) - 920) / 2);
            if (flashWidth < 4) {
                $("#leftFlash").css('display', 'none');
                $("#rightFlash").css('display', 'none');
            } else {
                flashWidth = flashWidth + "px";
                $('#rightFlash').css('width', flashWidth);
                $('#flashRight').css('width', flashWidth);
                $('#leftFlash').css('width', flashWidth);
                $('#flashLeft').css('width', flashWidth);
                $("#leftFlash").css('display', 'block');
                $("#rightFlash").css('display', 'block');
            }
        }
    },

    tools: {
        checkWidth: function(oObj) {
            return $(oObj).width();
        },
        checkHeight: function(oObj) {
            return $(oObj).height();
        },
        handleResize: function() {
            $(window).bind("resize", function() {
                soderhavet.flash.resize();
            });
        },
        isIElt7: function() {
            if ($.browser.msie) {
                if (parseInt($.browser.version) < 7) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
}

// Fire it all up, baby!
$(document).ready(function(){
    soderhavet.init();
})