function showCaseAfterTimeout(caseframe, thisref, activebutton) {
    caseframe.find(".loadingframe").hide();
    caseframe.find(".casebutton").removeClass("activecase");
    caseframe.find(".casebuttons .casebutton[ref='" + thisref + "']").addClass("activecase");
    caseframe.find(".case[ref='" + thisref + "']").fadeIn(300);
}

$(document).ready(function () {
	
    //DO THINGS SPECIFIC FOR IE6
    var browserversion = jQuery.browser.version;
    if ($.browser.msie && browserversion.charAt(0) == "7") {

    }

    $("#searchdomainname").click(function (e) {
        e.preventDefault();
        var domain = $("#domain").attr("value");
        window.location.href = '/bestall/?domain=' + domain;
    });

    $("#loginbutton").click(function (e) {
        e.preventDefault();
        var form = $("#login");
        if (DoLogin(document.getElementById('login')) == true) {
            form.submit();
        }
    });

    // CHANGE APPEREANCE OF SELECT-ELEMENTS
    $("select[class='visualizedropdown']").each(function (e) {
        createdropdown($(this));
    });

    $(".caseframe").each(function (e) {
        var caseframe = $(this);
        //SHOW FIRST TAB OF CASES
        var firstref = caseframe.find(".casebutton:last").attr("ref");
        caseframe.find(".casebuttons .casebutton:last").addClass("activecase");
        caseframe.find(".case[ref='" + firstref + "']").show();
        if (caseframe.find(".case").length == 1) {
            caseframe.find(".casebuttons").remove();
        }

        //CHANGE TAB AND BUTTON-APPEREANCE OF CASE
        caseframe.find(".casebuttons .casebutton").click(function (e) {
            e.preventDefault();
            var activebutton = $(this);
            var thisref = $(this).attr("ref");

            caseframe.find(".case").hide();
            caseframe.find(".loadingframe").show();
            showCaseAfterTimeout(caseframe, thisref);
            //setTimeout("showCaseAfterTimeout(" + caseframe + ",\'" + thisref + "\')", 500);
        });
    });


    //SET TIME FOR SPECIFICATIONTABLE INFO FADEIN AND FADEOUT
    var specificationfadeintime;
    var specificationfadeouttime;
    if ($.browser.msie) { specificationfadeintime = 0; specificationfadeouttime = 0; } else { specificationfadeintime = 200; specificationfadeouttime = 50; }

    //SET CLASSES FÖR DIFFERENT IN TABLE WITH CLASS=SPECIFICATION TABLE
    $(".specificationtable").each(function (e) {
        $(".specificationtable:eq(" + e + ") tr:even").addClass("grayrow");
    });

    //CREATE INFOPOPUP FOR EACH ROW WHERE INFO-ICON HAS ALT ATTRIBUTE
    $('.specificationtable td').each(function () {
        if ($(this).children("img").attr("alt")) {
            $.infoframe = $("<div class='infoframe'></div>");
            $.alttext = $(this).children("img").attr("alt");
            $.infoframe.html("<div class='innerframe'><div class='inner'>" + $.alttext + "<img src='/BluerangeEasy/templates/1003/images/sectioninfoarrow_v2.png' class='specificationarrow' /></div></div>");
            $.infoframe.appendTo($(this));
            $(this).children("img").hover(function () {
                $(this).parent().find(".infoframe").fadeIn(specificationfadeintime);
            }, function () {
                $(this).parent().find(".infoframe").fadeOut(specificationfadeouttime);
            })
        }
    });

    //GO BACK TO TOP OF PAGE
    $('.backtotop').click(function () {
        $('html, body').animate({ scrollTop: 0 }, 'fast');
    });

    //HANDLE ORIGINAL TEXT ON INPUTS AND TEXTAREAS
    // SET ON FOCUS VALUES FOR ALL TEXTFIELDS
    $("input.textfield, textarea").each(function () {
        if ($(this).val() == $(this).attr("alt") || $(this).val() == "") {
            $(this).val($(this).attr("alt"));
        }
    });
    // CLEAR VALUES ON FOCUS FOR ALL TEXTFIELDS
    $("input.textfield, textarea").focus(function () {
        if ($(this).val() == $(this).attr("alt")) {
            $(this).val("");
        }
    });
    // ADD ORIGINAL VALUE IF EMPTY FOR ALL TEXTFIELDS
    $("input.textfield, textarea").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("alt"));
        }
    });

    //SHOW MENUDESCRIPTION AFTER 2 SECOND HOVER
    //$(".signin, .nomenu").hover(function() {
    //$(this).prev(".menudescription").stop(true, true).delay(2000).animate({ opacity: "show", bottom: "53" }, 300);
    //}, function() {
    //$(this).prev(".menudescription").stop(true, true).animate({ opacity: "hide", bottom: "63" }, 100);
    //});
    $(".sendformlink").click(function (e) {
        e.preventDefault();
        try {
            var bate = document.getElementById('__EasyBate');
            var frm = document.getElementById("contactform");
            frm.removeChild(bate);
        } catch (err) { }
        document.getElementById("contactform").submit();
    });


    //HOVER-APPARENCE FOR TOPMENU ITEMS
    $(".signin, .nomenu").hover(function () {
        if (!($(this).hasClass("menu-open"))) {
            $(this).animate({ backgroundColor: "#6f6f6f",
                color: "#ffffff",
                borderTopColor: "#949494"
            }, 300);
        }
    }, function () {
        if (!($(this).hasClass("menu-open"))) {
            $(this).animate({
                backgroundColor: "#575757",
                color: "#dddddd",
                borderTopColor: "#6e6e6d"
            }, 100);
        }
    });


    //SET TIME FOR MENU FADEIN AND FADEOUT
    var menufadeintime;
    var menufadeouttime;
    if ($.browser.msie) { menufadeintime = 0; menufadeouttime = 0; } else { menufadeintime = 500; menufadeouttime = 100; }

    //SHOW FULL MEGAMENU FOR MENUITEM
    $(".signin").click(function (e) {
        e.preventDefault();

        //Load mega menu with an ajax call
        //var menuitemid = $(this).attr("id").split("_")[1];
        //$(this).next(".menuframe").load('/?topmenuid=' + menuitemid + ' #menu', function () { });


        //Fade out any open mega menus
        $("#menucontainer .menuframe").fadeOut(menufadeouttime);

        //Change cssclass of clicked menuitem, if its not selected
        if (!($(this).hasClass("active"))) {
            $(this).toggleClass("menu-open");
        }
        //Finally, fade in the megamenu
        $(this).next(".menuframe").fadeIn(menufadeintime, function () {
            //SET SAME HEIGHT FORM COLUMNS IN MEGA MENUS
            var tallestColumn = 0;
            $(this).find(".signin_menu .menusection").each(function () {
                tallestColumn = 0;
                $(this).find(".column").each(function (e) {
                    columnHeight = $(this).height();
                    if (columnHeight > tallestColumn) {
                        tallestColumn = columnHeight;
                    }
                });
                $(this).find(".column").css("min-height", tallestColumn); //height(tallestColumn);
            });
        });
    });

    //IF MOUSE UP ON THE MENU, DONT HIDE
    $(".menuframe").mouseup(function () {
        return false
    });

    //HIDE MEGA MENU ON MOUSE UP ON DOCUMENT
    $(document).mouseup(function (e) {
        $(".signin").removeClass("menu-open");
        if ($(e.target).parent("a.signin").length == 0) {
            $("#menucontainer .menuframe").fadeOut(menufadeouttime, function () { });
        }
    });


    //SET TIME FOR SERVICE DROPDOWN FADEIN AND FADEOUT
    var servicesfadeintime;
    var servicesfadeouttime;
    if ($.browser.msie) { servicesfadeintime = 0; servicesfadeouttime = 0; } else { servicesfadeintime = 200; servicesfadeouttime = 50; }

    //SET CLASSES ON HOVER FOR SERVICES MENU, IF NOT HAS CLASS="ADD"
    $(".servicesframe .item").hover(function () {
        if (!($(this).hasClass("all"))) { $(this).addClass("active"); }
    }, function () { $(this).removeClass("active"); });

    //SERVICES DROPDOWN CREATION
    $("#servicesarrow, #servicesfield").click(function (e) {
        e.preventDefault();
        $("#servicesmenu").fadeIn(servicesfadeintime);
    });
    $("#servicesmenu").mouseup(function () {
        return false
    });
    $(document).mouseup(function (e) {
        $(".servicedescription").fadeOut(0);
        $("#servicesmenu").fadeOut(servicesfadeouttime, function () { });
    });
    $("#servicesmenu .item").click(function (e) {
        $(".servicedescription").fadeOut(0);
        $("#servicesfield").val($(this).children("span").text());
        $("#servicesmenu").fadeOut(servicesfadeouttime, function () { });
    });


    //SET SAME HEIGHT FOR TEXTCOLUMNS IN "PUFF"-TABLES AND H3-headers
    $(".rowblocktable").each(function () {

        //PUFFARNA
        var tallest = 0;
        $(this).find(".column .text").each(function (e) {
            var thisHeight = $(this).find("p:first").height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        $(this).find(".column .text").each(function (e) {
            $(this).find("p").each(function () {
                if ($(this).html().length < 10) {
                    $(this).remove();
                }
            });
        });
        $(this).find(".column .text").each(function (e) {
            $(this).find("p:first").height(tallest);
        });

        //H3:orna
        $(this).find("h3").next("p").each(function (e) {
            if ($(this).html().length < 10) {
                $(this).remove();
            }
        });

        tallest = 0;
        thisHeight = 0;
        $(this).find("h3").each(function (e) {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        $(this).find("h3").height(tallest);

    });
	


});

function equalHeight(group) {
    var tallest = 0;
    group.each(function (e) {
        alert(e);
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function writeMoreNews(categoryid) {
    var fullurl = "/-nyheter-/?newsview=morenews&xslt=blank.xsl";
    $.ajax({
        url: fullurl,
        success: function (data) {
            var fullcontainer = $("<div></div>").html(data);
            var morenewsdata = fullcontainer.find("#morenewscontainer").html();
            $("#morenews").html(morenewsdata);
        }
    });
}

//------------------ SETTING NEW APPEREANCE FOR SELECT-ELEMENTS ------------------//

function createdropdown(dropdown) {
    var parent = dropdown.parent();

    //create container
    var selectwidth = parseInt(dropdown.outerWidth());
    if (selectwidth == 0) {
        selectwidth = 180;
    }
    var dropdowncontainer = $("<div></div>").addClass("dropdownframe").css("width", selectwidth).css("font-family", dropdown.css("font-family")).css("font-size", dropdown.css("font-size")).css("line-height", dropdown.css("font-size"));
    var inner = $("<div></div>").addClass("dropdown").appendTo(dropdowncontainer);
    $("<span></span>").text(dropdown.children("option:first").text()).appendTo(inner);
    $("<input />").attr("type", "hidden").attr("id", dropdown.attr("id")).attr("name", dropdown.attr("name")).appendTo(dropdowncontainer);

    //create options
    var optionsframe = $("<div></div>").addClass("alloptions").css("width", parseInt(dropdown.outerWidth()) - 2).appendTo(dropdowncontainer); // 6 = padding of the optionsframe * 2
    dropdown.children("option").each(function (e) {
        $("<a></a>").text($(this).text()).attr("ref", $(this).val()).attr("href", "#").appendTo(optionsframe);
    });

    //remove the original select-element, and ad the new one
    dropdown.replaceWith(dropdowncontainer);

    //add the functions needed for the new dropdown to work
    dropdownfunctions(dropdowncontainer.children(".dropdown"));

}
function dropdownfunctions(dropdowncontainer) {

    //change on hover
    dropdowncontainer.hover(function () {
        $(this).parent().addClass("hoverdropdown");
    }, function () {
        $(this).parent().removeClass("hoverdropdown");
    });

    //show the options
    dropdowncontainer.click(function (e) {
        e.preventDefault();
        $(this).parent().addClass("opendropdown");
        $(this).parent().children(".alloptions").fadeIn(300);
    });

    //make sure the options aren't hidden when visitor clicks in the options-frame
    dropdowncontainer.parent().children(".alloptions").mouseup(function () {
        return false;
    });

    //when clicking outside the options-frame, hide the options
    $(document).mouseup(function (e) {
        dropdowncontainer.parent().children(".alloptions").fadeOut(100);
        dropdowncontainer.parent().removeClass("opendropdown");
    });

    //when an option is chosen, set hiddenvalue, change text in the new select, then hide the options-frame
    dropdowncontainer.parent().children(".alloptions").children("a").click(function (e) {
        e.preventDefault();
        dropdowncontainer.children("span").text($(this).text());
        //dropdowncontainer.children("span").hide();
        //dropdowncontainer.children("span").fadeIn(200);
        dropdowncontainer.parent().children("input").val($(this).attr("ref"));
        dropdowncontainer.parent().children(".alloptions").fadeOut(100);
        dropdowncontainer.parent().removeClass("opendropdown");

    });
}

String.prototype.ToJSON = function () {
    if (/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(this.replace(/"(\\.|[^"\\])*"/g, ''))) {
        return eval(this);
    }
}

function GetBlogList() {
    var url = "/-blog-/?blogview=Right_Latest_List&xslt=ajaxfetch.xsl";
    $.ajax({ url: url, success: function (data) {
        var result = $("<div></div>").html(data).find("#result").html();
        var resultJSON = $.parseJSON(result);
        var items = resultJSON.result;

        var month = new Array(12);
        month[1] = "January";
        month[2] = "February";
        month[3] = "March";
        month[4] = "April";
        month[5] = "May";
        month[6] = "June";
        month[7] = "July";
        month[8] = "August";
        month[9] = "September";
        month[10] = "October";
        month[11] = "November";
        month[12] = "December";

        var str = "";
        for (var i = 0; i < items.length; i++) {
            var date = items[i].date;
            var m = date.substring(5, 7);
            var d = date.substring(8);
            var y = date.substring(0, 4);
            str += "<p>" + month[m] + " " + d + ", " + y;
            str += "<br /><a href=" + items[i].url + ">" + items[i].title + "</a></p>";
            //Är detta en korrekt if-sats??
            if (i != (items.length - 1)) {
                str += "<hr />";
            }
        }

        $("#Right_Latest_List").html(str);
    }
    });
}

var timeout = 100;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}

$(document).ready(function () {
    $('#breadcrumb > li').bind('mouseover', jsddm_open);
    $('#breadcrumb > li').bind('mouseout', jsddm_timer);
	
});

document.onclick = jsddm_close;

