function openHeaderLayer(self, id) {
    var layer = $j("#" + id);
    var btn1 = $j(self); // a
    var btn2 = btn1.parent(); // a..li
    var btn3 = btn2.next("li"); // li -> li
    if (btn1.hasClass("active")) {
        layer.hide();
        btn1.removeClass("active");
        btn2.removeClass("active");
        btn3.removeClass("prev-active");
    } 
        else {
        layer.show();
        btn1.addClass("active");
        btn2.addClass("active");
        btn3.addClass("prev-active");
    }
    layer.onMouseOut = function () {
        alert('wq');
    }
    return false;
}
function openProfileLayer(self) {
    var layer = $j("#layer-profiles");
    layer.toggle();
    return false;
}
function loaded() {}
var $j = jQuery.noConflict();
$j(document).ready(function () {
    $j("#layer-country").bind("mouseleave", function () { openHeaderLayer($j("#changecountry"), "layer-country"); });
    $j("#layer-language").bind("mouseleave", function () { openHeaderLayer($j("#changelanguage"), "layer-language"); });
});
function changeClass(x, levelName) {
    var selectedClass = "selectedRow";
    x.className = (x.className == levelName) ? selectedClass : (x.className == selectedClass) ? levelName : selectedClass;
}
function renderFlash(swfPath, swfWidth, swfHeight, swfMajorVersion, swfMinorVersion, swfRevision, swfFlashvars, isEditMode, flashInstance) {
    var hasRequestedVersion = DetectFlashVer(swfMajorVersion, swfMinorVersion, swfRevision);
    if (hasRequestedVersion) {
        var tuuba = AC_FL_RunContent(
        "src", swfPath,
        "width", swfWidth,
        "height", swfHeight,
        "menu", "false",
        "align", "middle",
        "id", ("teaserFlash" + flashInstance++),
        "quality", "high",
        "bgcolor", "#ffffff",
        "name", ("teaserFlash" + flashInstance++),
        "allowScriptAccess", "always",
        "wmode", "opaque",
        "type", "application/x-shockwave-flash",
        'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
        "pluginspage", "http://www.adobe.com/go/getflashplayer",
        'FlashVars', swfFlashvars
        );

        var layer = $j('#imagecontent' + flashInstance);

        if (isEditMode == '1') {
            layer.show();
        } else {
            layer.hide();
        }
    }
    else {
        var layer = $j('#imagecontent' + flashInstance);
        layer.show();
    }
}
var examDuration;
var resultPagePath;
var setResultPagePath = function (resultpage) {
    resultPagePath = resultpage;
}
var countdown = function (resultPage) {
    Uponor.Web.Services.ExamService.HasTimeLeft(function (timeLeft) {
        if (timeLeft) {
            Uponor.Web.Services.ExamService.TimeRemaining(function (timeRemaining) {
                timeRemaining = Math.ceil(timeRemaining);
                $j("#timeRemaining").text(timeRemaining);
                window.setTimeout("countdown()", 60000);});}
        else {modalPopUp();}});}
var results = [];
var submitExam = function (resultPage) {
    var questions = $j("div.el_question");
    for (var i = 0; i < questions.length; i++) {
        var answer = new Array();
        answer[0] = $j("input[type='hidden']", questions[i]).val();
        var answers = $j(".answer", questions[i]);
        for (var j = 0; j < answers.length; j++) {
            answer[j + 1] = $j($j(answers).get(j)).attr("checked") == true ? true : false;
        }
        results[i] = answer;
    }
    Uponor.Web.Services.ExamService.PostResults(results, function (results) {
        document.location.href = resultPage;
    })
}
var abortExam = function () {
    Uponor.Web.Services.ExamService.FinalizeExam();
}
var clearInput = function () {
    $j("div#exam_contact textarea").val("");
}
var goToQuestion = function (currentQuestion, requestedQuestion) {
    $j($j("div.el_question").get(currentQuestion - 1)).css("color", "#999");
    $j($j("div.el_question").get(requestedQuestion - 1)).css("color", "#000");
    $j($j("div.el_qnr a").get(currentQuestion - 1)).css("color", "#999");
    $j($j("div.el_qnr a").get(requestedQuestion - 1)).css("color", "#0072B7");
    $j($j("div.slider").get(currentQuestion - 1)).slideToggle();
    $j($j("div.slider").get(requestedQuestion - 1)).slideToggle();
}
var jumpToQuestion = function (requestedQuestion, link) {
    if ($j($j("div.slider").get(requestedQuestion - 1)).css("display") != "block") {
        $j("div.slider:visible").slideToggle();
        $j($j("div.slider").get(requestedQuestion - 1)).slideToggle();
        $j("div.el_qnr a").css("color", "#999");
        $j(link).css("color", "#0072B7");
        $j("div.slider:visible").parent("div.el_question").css("color", "#999");
        $j($j("div.el_question").get(requestedQuestion - 1)).css("color", "#000");
    }
}
var goToMaterial = function (currentMaterial, requestedMaterial) {
    $j($j("div.el_material").get(currentMaterial - 1)).slideToggle();
    $j($j("div.el_material").get(requestedMaterial - 1)).slideToggle();
    $j("div.menuItem a").not(":last").css("color", "#999");
    $j($j("div.menuItem a").get(requestedMaterial - 1)).css("color", "#000");
}
var jumpToMaterial = function (requestedMaterial, link) {
    if ($j($j("div.el_material").get(requestedMaterial - 1)).css("display") != "block") {
        $j("div.el_material:visible").slideToggle();
        $j($j("div.el_material").get(requestedMaterial - 1)).slideToggle();
        $j("div.menuItem a").not(":last").css("color", "#999");
        $j(link).css("color", "#000");
    }
}
var modalPopUp = function () {
    $j("#ModalPopUp").click(function () {
        submitExam(resultPagePath);
    });
    $j("#ModalPopUp").css({ "z-index": "100000", "display": "block" });
    window.setTimeout("submitExam(resultPagePath)", 5000);
}
var modalPopUpError = function () {
    $j("#ModalPopUp").click(function () {
        document.location.href = resultPagePath;
    });
    $j("#ModalPopUp").css({ "z-index": "100000", "display": "block" });
    window.setTimeout("document.location.href = resultPagePath;", 5000);
}
var highliteOpenQuestions = function () {
    if ($j(".tbl_question").length) {
        $j(".el_question").each(function () {
            var wrapper = $j(this);
            var done = false;
            $j("input", wrapper).each(function () {

                if ($j(this).is(":checked")) {
                    done = true;
                }

            });
            if (done) {
                $j(".question", wrapper).css("color", "");
            }
            else {
                $j(".question", wrapper).css("color", "red");
            }

        });
        $j(".el_question").click(function () {
            var wrapper = $j(this);
            var done = false;
            $j("input", wrapper).each(function () {
                if ($j(this).is(":checked")) {
                    done = true;
                }
            });
            if (done) {
                $j(".question", wrapper).css("color", "");
            }
            else {
                $j(".question", wrapper).css("color", "red");
            }
        });
    }
}

// Greeting Card

var previewCard = function () {
    $j(".jqMessageToWrite").text($j(".message").attr("value"));
    $j(".senderNameToWrite").text($j(".senderName").attr("value"));
    $j(".headlineToWrite").text($j(".headline").attr("value"));
}
function PageSurvey(startItem) {
    var head = document.getElementsByTagName('head')[0];
    switch (startItem) {
        case "Sweden":
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/scripts/SitesterSurvey/SitesterSurvey_1512.js';
            head.appendChild(script);
            break;
        case "Germany":
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/scripts/SitesterSurvey/SitesterSurvey_1513.js';
            head.appendChild(script);
            break;
        case "Spain":
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/scripts/SitesterSurvey/SitesterSurvey_1514.js';
            head.appendChild(script);
            break;
        case "Russia":
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/scripts/SitesterSurvey/SitesterSurvey_1515.js';
            head.appendChild(script);
            break;
    }
}

// Product Finder - START
function OpenNewWindow(url) {
    window.open(url);
}
function PageLoadedHandler(sender, args) {
    BlurHandler();
}
function FocusHandler() {
    var lnkTextboxSearch = document.getElementById('<%=lnkTextboxSearch.ClientID %>');
    var lnkTextboxSearchSimple = document.getElementById('<%=lnkTextboxSearchSimple.ClientID %>');
    if (lnkTextboxSearch != null)
    //lnkTextboxSearch.disabled = false;
        disableAnchor(lnkTextboxSearch, 0, "href");
    if (lnkTextboxSearchSimple != null)
    //lnkTextboxSearchSimple.disabled = false;
        disableAnchor(lnkTextboxSearchSimple, 0, "href");
}
function BlurHandler() {
    var txtControl;
    for (var i = 0; i < productFinderBehaviorIds.length; i++) {
        if (document.getElementById(productFinderBehaviorIds[i]).type == "text") {
            txtControl = document.getElementById(productFinderBehaviorIds[i]);
        }
    }
    var lnkTextboxSearch = document.getElementById('<%=lnkTextboxSearch.ClientID %>');
    var lnkTextboxSearchSimple = document.getElementById('<%=lnkTextboxSearchSimple.ClientID %>');
    if (txtControl.value == "") {
        if (lnkTextboxSearch != null) {
            //lnkTextboxSearch.disabled = true;
            disableAnchor(lnkTextboxSearch, 1, "href");
        }
        if (lnkTextboxSearchSimple != null) {
            //lnkTextboxSearchSimple.disabled = true;
            disableAnchor(lnkTextboxSearchSimple, 1, "href");
        }

    }
    else {
        if (lnkTextboxSearch != null)
        //lnkTextboxSearch.disabled = false;
            disableAnchor(lnkTextboxSearch, 0, "href");
        if (lnkTextboxSearchSimple != null)
        //lnkTextboxSearchSimple.disabled = false;
            disableAnchor(lnkTextboxSearchSimple, 0, "href");
    }
}
function disableAnchor(obj, disable, evnt) {

    var href = obj.getAttribute(evnt);
    if (href && href != "" && href != null) {
        obj.setAttribute('href_bak', href);
    }
    if (disable) {
        obj.removeAttribute(evnt);
        var color = document.getElementById('color');
        color.style.color = "gray";
    }
    else {
        obj.setAttribute(evnt, obj.attributes['href_bak'].nodeValue);
        var color = document.getElementById('color');
        color.style.color = "#365200";
    }
}

function InitProductFinder(chooseProduct, language) {
    $j.getJSON("/" + language + "/handler/ProductFinder.ashx", function (result) {
        $j.each(result, function (index, item) {
            $j("#finderSolutions").get(0).options[$j("#finderSolutions").get(0).options.length] = new Option(item.Name, item.Value);
        });
    });
    $j("#finderSolutions").change(function () {
        $j('#finderSelButton span').css('color', 'gray');
        $j('#finderSelButton').attr('href', '#');
        $j('#finderSelButton').attr('target', '');
        $j("#finderProducts").get(0).options.length = 0;
        $j("#finderProducts").get(0).options[$j("#finderProducts").get(0).options.length] = new Option(chooseProduct, '');
        var val = $j('#finderSolutions option:selected').val();
        if (val != '') {
            $j.getJSON("/"+ language + "/handler/ProductFinder.ashx", function (result) {
                $j.each(result, function (index, item) {
                    if (item.Value == val) {
                        $j.each(item.Products, function (index, product) {
                            $j("#finderProducts").get(0).options[$j("#finderProducts").get(0).options.length] = new Option(product.Name, product.Value);
                        });
                    }
                });
            });
        }
    });
    $j("#finderProducts").change(function () {
        var val = $j('#finderProducts option:selected').val();
        if (val != '') {
            $j('#finderSelButton span').css('color', '#365200');
            $j('#finderSelButton').attr('href', val);
            $j('#finderSelButton').attr('target', '_blank');
        }
        else {
            $j('#finderSelButton span').css('color', 'gray');
            $j('#finderSelButton').attr('href', '#');
            $j('#finderSelButton').attr('target', '');
        }
    });
    $j("#txtFreeTextSearch").blur(function () {
        var val = $j('#txtFreeTextSearch').val();
        var lnkTextboxSearch = document.getElementById('finderInputButton');
        if (val == "") {
            disableAnchor(lnkTextboxSearch, 1, "href");
        }
        else {
            disableAnchor(lnkTextboxSearch, 0, "href");
        }
    });
    $j("#txtFreeTextSearch").focus(function () {
        var lnkTextboxSearch = document.getElementById('finderInputButton');
        if (lnkTextboxSearch != null)
        disableAnchor(lnkTextboxSearch, 0, "href");
    });
    $j("#finderInputButton").click(function () {
        var val = $j('#txtFreeTextSearch').val();
        if (val != '') {
            OpenNewWindow('http://catalog.uponor.com/?id=36&L=' + language + '&vollText=' + val);
        }
    });
}
// Product Finder - END

// Guestbook
function InitGuestbook() {
    $j('.Entry .EntryHead a').click(function () {
        var entry = $j(this).parent().parent().find('.EntryText');
        if ($j(entry).css('display') == 'block') {
            $j(entry).slideUp();
            $j(entry).parent().find('.ButtonOpend').attr('class', 'ButtonClosed');
        }
        else {
            $j(entry).slideDown();
            $j(entry).parent().find('.ButtonClosed').attr('class', 'ButtonOpend');
        }
    });

    $j('.GBEntryBorderContent .GBEntryRight a.ExpandAll').click(function () {
        $j(this).parent().parent().parent().find('.EntryText').each(function () {
            $j(this).slideDown();
            $j(this).parent().find('.ButtonClosed').attr('class', 'ButtonOpend');
        });
    });

}
//Guestbook - END
