$j = $.noConflict();

$j(document).ready(function () {
    adjustShoppingBoxes();

    $j("#dialog").dialog({
        modal: true,
        autoOpen: false,
        resizable: false,
        position: ['center', 50],
        width: 670,
        close: function (event, ui) {
            jwplayer("videoplayer").stop();
        }
    });
    /* Text Resizer */
    $j('.textSize img').mouseover(function () {
        $j(this).css('border', '1px solid #666666');
    });

    $j('.textSize img').mouseout(function () {
        $j(this).css('border', '1px solid #cecece');
    });

    $j('.textSize img:eq(0)').click(function () {
        var currentFontSize = $j('body').css('font-size');
        currentFontSize = currentFontSize.substring(0, currentFontSize.length - 2);
        currentFontSize = parseInt(currentFontSize) + 1;

        $j('body').css('font-size', currentFontSize + 'px');
        // Make sure the shopping boxes on the homepage stay the same height
        adjustShoppingBoxes();
    });

    $j('.textSize img:eq(1)').click(function () {
        var currentFontSize = $j('body').css('font-size');
        currentFontSize = currentFontSize.substring(0, currentFontSize.length - 2);
        currentFontSize = parseInt(currentFontSize) - 1;

        $j('body').css('font-size', currentFontSize + 'px');
        // Make sure the shopping boxes on the homepage stay the same height
        adjustShoppingBoxes();
    });

    $j(".calendarNav .dateSelector .selectorButton:first").addClass("btn orange");

    if ($j('#loginStatus').val() == "Redirect") {
        parent.location = $j('#pageRedirection').val();
    }

    $j(".autoFieldHint").focus(function () {
        $j(this).removeClass("fieldHint");
    });
    $j(".autoFieldHint").each(function () {
        updateFieldHint($j(this));
    });

    $j(".autoFieldHint").blur(function () {
        updateFieldHint($j(this));
    });
});

function adjustShoppingBoxes() {
    var shoppingHeights = new Array();

    // Gather the heights of the shopping boxes
    $j('.shopping-boxes').each(function () {
        $j(this).attr('style', 'height:auto;');
        shoppingHeights.push($j(this).height());
    });

    function sortArray(valA, valB) {
        return (valB - valA);
    }
    // Sort the heights in ascending order
    shoppingHeights.sort(sortArray);

    // Set the height of all three shopping boxes to the value of the tallest box
    $j('.shopping-boxes').each(function () {
        $j(this).height(shoppingHeights[0]);
    });
}

function displayVideo(videoPath) {
    PriorityHealth.webservices.Video.GetVideo(videoPath, displayVideo_Response);
}

function displayVideo_Response(result) {
    $j("#dialog").dialog("open");
    jwplayer("videoplayer").setup({
        autostart: true,
        controlbar: "bottom",
        flashplayer: "/PriorityHealth/flash/player.swf",
        modes: [
            { type: "flash", src: "/PriorityHealth/flash/player.swf" },
            { type: "html5" }
        ],
        file: result.Url,
        height: 480,
        width: 640
    });

    $j("#dialog").dialog({ title: result.Title });
    $j("#dialog .topContent").html(result.TopContent);
    $j("#dialog .bottomContent").html(result.BottomContent);
}

function toggleSitemap(toggleId, contentId, show) {
    if (show) {
        $j("#" + contentId).show();
        $j("#" + toggleId + " .expanded").hide();
        $j("#" + toggleId + " .collapse").show();
    } else {
        $j("#" + contentId).hide();
        $j("#" + toggleId + " .expanded").show();
        $j("#" + toggleId + " .collapse").hide();
    }
}

function doClick(buttonName, e) {
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;

    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13) {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) { //If we find the button click it
            btn.click();
            event.keyCode = 0
        }
    }
}

function resizeIframe(heightDiv) {
    iHeightDiv = parseInt(heightDiv);
    $j('#iframeLogin', top.document).height(iHeightDiv);
}
function iframeResizePipe() {
    var height = $j(document.body).height();
    var iFrame = $j('#helpframe');
    var path = $j("#txtOrignalPath").val();

    // Cachebuster a precaution here to stop browser caching interfering
    // Also, let's follow proper javascript rules and not hardcode a development hostname
    iFrame.attr('src', window.location.protocol + '//' + window.location.hostname + '/PriorityHealth/Layouts/LoginIFrameHelper.aspx?height=' + height + '&cacheb=' + Math.random());
}

// Tell the parent iframe what height the iframe needs to be
function parentIframeResize() {
    var height = getParam('height');

    parent.parent.resizeIframe(height);
}

// Helper function, parse param from request string
function getParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function updateFieldHint(inputField) {
    if (inputField.val() == "") {
        inputField.addClass("fieldHint");
    }
    else {
        inputField.removeClass("fieldHint");
    }
}
