// Set default base url
var __appPath = "";  //  // "/Bayer_KenticoCMS";
var __pagePath = window.location.pathname.replace(".aspx", "").replace(__appPath, "");
var __caPath = __appPath + "/Bayercontour/click_action.aspx?ai=";

var __pollRecorded = false;

try { document.execCommand('BackgroundImageCache', false, true); } catch (e) { } 


$(document).ready(function() {
    // Set the 'on' state for the navigation
    setNavigationOnState();



    // Search field watermark
    $(".searchfield").Watermark("SEARCH");

    // Home page tout box handling
    $(".feature")
	    .mouseover(function() { $(this).children("h3").addClass("over"); })
	    .mouseout(function() { $(this).children("h3").removeClass("over"); })
	    .click(function() { document.location = $(this).find("a").attr("href"); });

    $(".feature").addClass("middle");
    $(".feature:first").removeClass("middle").addClass("first");
    $(".feature:last").removeClass("middle").addClass("last");


    // Sidebar information rollover
    $(".sidebar img.information").mouseover(function() { $(this).next().css("display", "block"); });
    $(".sidebar div.information-balloon").mouseout(function() { $(this).css("display", "none"); });


    // Badge block handling
    $(".badge, ul.stack-item-listing li, ul.side-item-listing li")
        .addClass("hyperlinkAppearance")
        .each(function(index, element) {
            var $element = $(element);

            var href = "";
            var click_action_tag_val = "";

            var $a = $element.find("a:first");

            href = $a.attr("href");
            // $a.attr("href", "#");

            if ($a.attr("click_action_tag") != undefined && $a.attr("click_action_tag") != "") {
                click_action_tag_val = $a.attr("click_action_tag");
                $a.removeAttr("click_action_tag");
                $a.click(function(event) { event.preventDefault(); });
            }

            $element.click(function() {
                if (click_action_tag_val != "") {
                    $.ajax({
                        url: __caPath + escape(__pagePath) + "|" + click_action_tag_val.replace(" ", "-"),
                        data: "",
                        success: function(data) {
                            // redirect link
                            document.location = href;
                        },
                        dataType: "text"
                    });
                }
                else {
                    document.location = href;
                }
            });

        });


    // Poll click-track event handling
    $(".PollFooter input[type=image]")
        .click(function(event) {
            if (!__pollRecorded) {
                event.preventDefault();
                $.ajax({
                    url: __caPath + escape(__pagePath) + "|POLL",
                    data: "",
                    success: function(data) {
                        __pollRecorded = true;
                        $(".PollFooter input[type=image]").trigger("click");
                    },
                    dataType: "text"
                });
            }
        });


    // Attach a call to any tag that has a title attribute
    $("a[click_action_tag]").each(
        function(index, element) {
            $(element).click(function(event) {
                event.preventDefault();

                // DEBUG LINE
                // alert(__caPath + escape(__pagePath) + "|" + $(element).attr("click_action_tag").replace(" ", "-"));


                $.ajax({
                    url: __caPath + escape(__pagePath) + "|" + $(element).attr("click_action_tag").replace(" ", "-"),
                    data: "",
                    success: function(data) {
                        // perform logic on how to respond after recording

                        // DEBUG LINE
                        // alert($(element).attr("href") + "\n" + $(element).attr("target"));


                        if ($(element).attr("target") != undefined & $(element).attr("target") != "") {
                            newWindow($(element).attr("href"), "_blank", 550, 400, "resizable,scrollbars,toolbar,location,status, menubar");
                        }
                        else if ($(element).attr("href") != undefined && $(element).attr("href").substr(0, 10).toLowerCase() == "javascript") {
                            eval($(element).attr("href").replace("javascript:", ""));
                        }
                        else {
                            // redirect link
                            window.location.href = $(element).attr("href");
                        }
                    },
                    dataType: "text"
                });
            });
        }
    );

});


function setNavigationOnState() {
    var aSplitPath = __pagePath.split("/");
    
    if (aSplitPath[1].toLowerCase() == "default" || aSplitPath[1].toLowerCase() == "") {
        $("#topnav li a:contains('HOME')").css("color", "#66cc33");
    }
    else if (aSplitPath[1].toLowerCase() == "products") {
        $("#topnav li a:contains('PRODUCTS')").css("color", "#66cc33");
    }
    else if (aSplitPath[1].toLowerCase() == "patient-education") {
        $("#topnav li a:contains('PATIENT EDUCATION')").css("color", "#66cc33");
    }
    else if (aSplitPath[1].toLowerCase() == "resources") {
        $("#topnav li a:contains('RESOURCES')").css("color", "#66cc33");
    }
    else if (aSplitPath[1].toLowerCase() == "community") {
        $("#topnav li a:contains('COMMUNITY')").css("color", "#66cc33");
    }
}



/* Utility functions */
function newWindow(mypage, myname, w, h, features) {
    var winl = (screen.width - w) - 40;
    var wint = (screen.height - h) - 50;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += features;
    win = window.open(mypage, myname, settings);
}

