$j = $.noConflict();

$j(document).ready(function () {

    if ($j.query.get('show-get-quote') == "true") {
        showGetQuote();
    }

    /* Medigap Get Quote button */
    $j('#getquote').click(function () {
        showGetQuote();
    });

    $j('.getquote').click(function () {
        showGetQuote();
    });

    $j('#cancelquote').click(function () {
        hideGetQuote();
    });

    /* Text Resizer */
    $j('#text-plus').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');

    });

    $j('#text-minus').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');

    });

    //add btn orange to calendar nav go button
    $j(".calendarNav .dateSelector .selectorButton:first").addClass("btn orange");

    //leave page qtip's
    var g_LeavePageText = $j('#LeavePageText').text();
    $j('a').each(function (i) {
        if (PH.UrlIsExternal($j(this).attr('href'))) {
            //add the leave page text to the title attribute for qtip
            $j(this).attr('title', g_LeavePageText);
        }
    });
    //before calling qtip, a minor bug fix
    //$j.fn.qtip.defaults.hide.leave = false;
    //qtip them!
    $j('a[title]').qtip({
        position: {
            viewport: $j(window)
        }
    });

    if ($j.query.get('show-brackets') === 'yes') {
        $j.cookie('show-brackets', 'yes', { expires: 365, path: '/' });
    }

    if ($j.query.get('show-brackets') === 'no') {
        $j.cookie('show-brackets', null, { expires: 365, path: '/' });
    }

    // Are we doing anything with brackets?
    if (($j.cookie('show-brackets') === 'yes') && ($j.query.get('show-brackets') != 'no')) {
        $j('.bracketed').each(function () {
            var prehtml = $j(this).html();

            if ($j(this).is('img')) {
                $j(this).wrap('<span class="bracketspan">');
            } else {
                var modhtml = '[' + prehtml + ']';
                $j(this)
                    .html(modhtml)
                    .addClass('showbracket');
            }
        });
        $j('.bracketspan').each(function () {
            $j(this).prepend('[')
            .append(']')
            .addClass('showbracket')
        });
    }

    /* External Link tagging */
    $j('a[href*="enroll.priorityhealth.com"]').addClass('WebTrackingEnabled');
    $j('.WebTrackEnroll').click(function () {
        // WebTrends
        dcsMultiTrack('DCS.dcssip', 'http://medicare-enroll-click.priorityhealth.com', 'DCS.dcsuri', '/referring-page/' + window.location.pathname);
        // Google Analytics
        _gaq.push(['_trackPageview', 'http://medicare-enroll-click.priorityhealth.com/referring-page/' + window.location.pathname]);
    });

    /* Login Box Redirection */
    if ($j('#loginStatus').val() == "Redirect") {
        parent.location = $j('#pageRedirection').val();
    }
});

function showGetQuote() {
    $j('#quote-bar-rate-tool').removeClass('hidden');
    $j("#quote-bar-get").addClass('hidden');
}

function hideGetQuote() {
    $j('#quote-bar-rate-tool').addClass('hidden');
    $j("#quote-bar-get").removeClass('hidden');
}

function BindEvents() {
    $j('a[title]').qtip({
        position: {
            viewport: $j(window)
        }
    });
    $j('#cancelquote').click(function () {
        $j('#quote-bar-rate-tool').addClass('hidden');
        //$j("#quote-bar-cancel").addClass('hidden');
        $j("#quote-bar-get").removeClass('hidden');
    });
}

/* PRIORITYHEALTH JS UTILITIES */
/*jslint debug: true, evil: true, laxbreak: true, forin: true, sub: true, css: true, cap: true, on: true, fragment: true, es5: true, maxerr: 9999 */
/*globals PH, document, jQuery, window*/
var PH = {}; // a container for priorityhealth functions

PH.UrlIsExternal = function (url) {
    "use strict";
    if (typeof url === "undefined") {
        return false;
    }
    url = url.toLowerCase();
    // First check if it's a full length url (has TLD)
    if ((url.indexOf('.com') >= 0) ||
    	(url.indexOf('.net') >= 0) ||
    	(url.indexOf('.org') >= 0) ||
    	(url.indexOf('.gov') >= 0) ||
    	(url.indexOf('.edu') >= 0) ||
    	(url.substring(0, 7) === 'http://')) {
        if (url.indexOf('priorityhealth.com/medicare') >= 0 ||
                url.indexOf('prioritymedicare.com') >= 0 ||
                url.indexOf('enroll.priorityhealth.com') >= 0) {
            return false;
        } else if (url.indexOf('~/media') >= 0) {
            return false;
        } else {
            return true;
        }
    } else if (url.charAt(0) === '/') { // URL is absolute relative to the domain
        if (url.substring(0, 9) === '/medicare') {
            return false;
        } else if (url.indexOf('~/media') >= 0) {
            return false;
        } else {
            return true;
        }
    } else {
        if (url.indexOf('/medicare/') >= 0 || url.indexOf('javascript') <= 0) {
            return false;
        }
        else {
            return true;
        }
    }
};

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 UpdateStatus(controlId) {
    isValid = true;

    $j.each(Page_Validators, function (index, item) {
        if (item.controltovalidate == controlId && item.isvalid == false) {
            isValid = false;
        }
    });

    if (isValid) {
        $j("#" + controlId).removeClass('input-error');
    }
    else {
        $j("#" + controlId).addClass('input-error');
    }
}
