var flashvars = {};
var params = { wmode: "opaque" };
var attributes = {};

var home = {
	
	queryStringArg: function(name)
	{
		var results = new RegExp("[\\?&]" + name + "=([^&#]*)").exec(window.location.href);
		if (!results) { return 0; }
		return results[1] || 0;
	},

	removeFirstParagraphTag: function(html)
	{
		var pos = html.toLowerCase().search(/<p>/);
		if (pos > -1)
		{
			html = html.substring(0, pos) + html.substring(pos + 3);
			pos = html.toLowerCase().search(/<\/p>/);
			if (pos > -1)
			{
				html = html.substring(0, pos) + html.substring(pos + 4);
			}
		}
		return html;
	},
	
	branchAdvanceClick: function()
	{
        return function(event) {
        ajaxContainer = $(this).parent();
        var url;
        url = this.href;

		if (url.length == 0 || url[url.length - 1] == "#") {
			url = "";
		} else if (url.search(/\?/) == -1) {
			url += "?";
		}

		if (url.length > 0 && url[url.length - 1] != "?") {
			url += "&";
		}

		url += ($("#branch1Option").length > 0 && $("#branch1Option")[0].checked ? $("#branch1Option")[0].value : "");
		url += ($("#branch2Option").length > 0 && $("#branch2Option")[0].checked ? $("#branch2Option")[0].value : "");
		url += ($("#branch3Option").length > 0 && $("#branch3Option")[0].checked ? $("#branch3Option")[0].value : "");
		
		var ajaxClause = (url.search(/ajax/) == -1 ? "ajax=true" : null);
		var contentHeaderClause = (url.search(/contentHeader/) == -1 ? "contentHeader=true" : null);
		if (ajaxClause != null || contentHeaderClause != null) {
			url += (url.search(/\?/) > -1 ? "&" : "?")
				+ (ajaxClause != null ? ajaxClause : "")
				+ (contentHeaderClause != null ? (ajaxClause != null ? "&" : "") + contentHeaderClause : "");
		}

		if ($(this).hasClass("replace")) {
			url = url.replace(/ajax=true/, "").replace(/contentHeader=true/, "");
			window.location = url;
		} else {
			$.get(url, function(data) {
				$(ajaxContainer).replaceWith(data);
				$("#branchAdvance").click(home.branchAdvanceClick());
			});
		}

		event.preventDefault();
		}
	}
};

var ajaxContainer;

$(document).ready(function () {

    $("#subnav a[href$='/news-and-events/safe-rx-awards.aspx']").click(function () {
        var currURL = window.location.href;
        var newUrl = currURL.substring(0, currURL.indexOf("/n"));
        window.open(newUrl + "/saferx.aspx");
        return false;
    });

    $(".open-video").click(function () {
        $("#ci-popup").show();
    });

    $(".open-video").click(function () {
        if ($.browser.msie) {
            //$("#demo-video").attr('autoplay', 'autoplay');
            $(".flowplayer").flowplayer(0).play();
            $("#video-popup").show();
        }
        else {
            var vid = document.getElementById("demo-video");
            $("#video-popup").show();
            vid.play();
        }

    });

    $("input").each(function () {
        if (this.type == "text")
            $(this).addClass("text");
    });

    if ($.browser.msie && $.browser.version < "8.0") {
        $("input[type=text],textarea").focus(function () { $(this).addClass("focus"); });
        $("input[type=text],textarea").blur(function () { $(this).removeClass("focus"); });
    }

    if (typeof ready == "undefined") { }
    else if (typeof ready == "function")
        ready();
    else if (ready.length > 0)
        for (var iReady = 0; iReady < ready.length; iReady++) {
            if (typeof ready[iReady] == "function")
                ready[iReady]();
        }

    $("form").each(function () { $(this).validate() });

    $("#displaylist").change(function () {
        $("#displayform").submit();
    });

    //TODO: this doesn't seem to work
    $("#feature > ul").addClass("roundie");

    SetupAjaxForms();

    SetupAjaxLinks();

    $("#branchAdvance").click(home.branchAdvanceClick());

    $(".accordion").accordion({ autoHeight: false, collapsible: true, active: false });

    infieldlabels();

    $(".btn-slide1").click(function (event) {
        if ($("#panel2").is(":visible")) {
            $(".btn-slide2").toggleClass("active");
            $("#panel2").hide();
            $(".btn-slide1").toggleClass("active");
            $("#panel1").show();
        }
        else {
            if ($("#panel1").is(":visible")) {
                // if panel is visible, collapse first, then change tab back to inactive style
                $("#panel1").slideToggle("slow", function () { $(".btn-slide1").toggleClass("active"); });
            }
            else {
                // if panel is closed, change tab style to active, then open panel
                $(".btn-slide1").toggleClass("active");
                $("#panel1").slideToggle("slow");
            }
        }

        infieldlabels();
        event.preventDefault();
    });

    $(".btn-slide2").click(function (event) {
        if ($("#panel1").is(":visible")) {
            $(".btn-slide1").toggleClass("active");
            $("#panel1").hide();
            $(".btn-slide2").toggleClass("active");
            $("#panel2").show();
        }
        else {
            if ($("#panel2").is(":visible")) {
                // if panel is visible, collapse first, then change tab back to inactive style
                $("#panel2").slideToggle("slow", function () { $(".btn-slide2").toggleClass("active"); });
            }
            else {
                // if panel is closed, change tab style to active, then open panel
                $(".btn-slide2").toggleClass("active");
                $("#panel2").slideToggle("slow");
            }
        }

        infieldlabels();
        event.preventDefault();
    });

    $("#findPhysiciansClose").click(function (event) {
        $("#panel1").slideToggle("slow", function () { $(".btn-slide1").toggleClass("active"); });
        event.preventDefault();
    });

    $("#findPharmaciesClose").click(function (event) {
        $("#panel2").slideToggle("slow", function () { $(".btn-slide2").toggleClass("active"); });
        event.preventDefault();
    });

    if (currentPageId == 1147) {
        //swfobject.embedSWF("flash/home.swf", "feature", "960", "295", "9.0.0", "flash/expressInstall.swf, flashvars, params, attributes");
        $(document).ready(function () {
            $('.1').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 100 });
            $('.2').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 9600 });
            $('.3').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 3100 });
            $('.4').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 6100 });
            $('.5').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 11800 });
            $('.6').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 1600 });
            $('.7').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 4600 });
            $('.8').cycle({ fx: 'zoom', sync: 0, random: 1, timeout: 7500, speed: 2000, delay: 8000 });
        });

        $("#carousel").showcase({
            animation: { type: "fade", interval: 5000, stopOnHover: true },
            css: { width: "440px", height: "300px" },
            images: images,
            navigator: { position: "bottom-left", orientation: "horizontal", showNumber: true, autoHide: false,
                css: { padding: "10px 10px 7px 10px" },
                item: {
                    css: { "-moz-border-radius": "4px", "-webkit-border-radius": "8px", backgroundColor: "#000000", borderColor: "#000000", color: "#FFFFFF", padding: "4px", "font-weight": "bold" },
                    cssHover: { backgroundColor: "#000000" },
                    cssSelected: { backgroundColor: "#FFFFFF", borderColor: "#FFFFFF", color: "#000000" }
                }
            },
            titleBar: { enabled: false }
        });

        //$("div#scroller").smoothDivScroll({ autoScroll: "always", autoScrollDirection: "endlessloop", pauseAutoScroll: "mouseover", autoScrollSpeed: 1, ajaxContentURL: "connected.xml", visibleHotSpots: "always", mouseDownSpeedBooster: 2 });

        $("img.pause").click(function () {
            $("#carousel").pause();
            $("img.pause").hide();
            $("img.play").show();
        });

        $("img.play").click(function () {
            $("#carousel").go();
            $("img.play").hide();
            $("img.pause").show();
        });
    }
});

function SetupAjaxForms() {
    $("form").submit(function(event) {
        if ($(this).hasClass("ajax")) {
            ajaxContainer = this;
            $.post(this.action + (this.action.search(/\?/) > -1 ? "&" : "?") + "ajax=true", $(this).serialize(), function(data) {
                $(ajaxContainer).replaceWith(data);
                SetupAjaxLinks();
                SetupAjaxForms();
            });

            event.preventDefault();
        }
    });
}

function SetupAjaxLinks() {
    $("a.ajax").click(function(event) {
        ajaxContainer = $(this).parent();
        $.get(this.href + (this.href.search(/\?/) > -1 ? "&" : "?") + "ajax=true", function(data) {
            $(ajaxContainer).replaceWith(data);
            SetupAjaxLinks();
            SetupAjaxForms();
        });

        event.preventDefault();
    });
}

function recordEvent(category, action, label) {
    try {
        var pageTracker = _gat._getTracker("UA-7818719-1");
        pageTracker._trackEvent(category, action, label);
    } catch (err) { }
}

function changeLabelForRecordEvent(value) {
    var returnvalue;
    returnvalue = value; //initialize
    if (value == 'EPrescribing') {
        returnvalue = 'eprescribing';
    }
    if (value == 'PharmacyManagement') {
        returnvalue = 'pharmmgmt';
    }
    if (value == 'PBM') {
        returnvalue = 'pbm';
    }
    if (value == 'AcuteCare') {
        returnvalue = 'acutecare';
    }
    if (value == 'Provider or Physician') {
        returnvalue = 'physician';
    }
    if (value == 'HIE, EHR, IDN') {
        returnvalue = 'HIE';
    }
    if (value == 'Prescriber') {
        returnvalue = 'prescriber';
    }
    if (value == 'Pharmacy') {
        returnvalue = 'pharmacy';
    }
    if (value == 'Payer') {
        returnvalue = 'payer';
    }
    if (value == 'Pharmacist') {
        returnvalue = 'pharmacist';
    }
    if (value == 'TechVendor') {
        returnvalue = 'techvendor';
    }
}

var htmlForPrint;
var htmlForPrintState;

function beginGetHtmlForPrint()
{
	htmlForPrintState = -1;
}

function getHtmlForPrint()
{
	if (typeof getHtmlForPrintPage != "undefined")
		return getHtmlForPrintPage();
	return null;
}

function htmlEscape(val)
{
	return val.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
}

function infieldlabels()
{
	$("form.infieldlabels input.infieldlabel,form.infieldlabels textarea.infieldlabel").each(function(index, input){
		$(input).prev("br").remove();

		var label = $(input).prev("label");

		label.css("width", $(input).innerWidth());
		label.css("height", $(input).innerHeight() + 1);
		
		if (input.tagName == "TEXTAREA")
			setInterval(function() {
					label.css("width", $(input).innerWidth());
					label.css("height", $(input).innerHeight() + 1);
				}, 50);

		setTimeout(function() {
				var left = input.offsetLeft;
				var top = input.offsetTop;
				
				if ($.browser.msie && $.browser.version < "8.0")
					left -= $(input).parents("form")[0].offsetLeft;

				label.css("left", left);
				label.css("top", top);
			}, 10);

		label.text(label.text().replace(/:/, ""));
		setLabelClass(label, input);
		label.addClass("infieldlabel");

		input.onfocus = function(){
			label.addClass("focus");
			setLabelClass(label, this);

			if ($.browser.msie && $.browser.version < "8.0")
				$(this).addClass("focus");
		}

		input.onblur = function(){
			label.removeClass("focus");
			setLabelClass(label, this);

			if ($.browser.msie && $.browser.version < "8.0")
				$(this).removeClass("focus");
		}

		input.onchange = function(){
			setLabelClass(label, this);
		}

		input.onkeypress = function(){
			setLabelClass(label, this);
		};
	});

	$("form.infieldlabels").addClass("processed");
}

function setLabelClass(label, input)
{
	if (input.value != "")
		label.addClass("hastext");
	else
		label.removeClass("hastext");
}

function printArea(el) {
    iframe = document.createElement('IFRAME');
    var doc = null;
    iframe.style.height = '0px';
    iframe.style.width = '0px';
    document.body.appendChild(iframe);
    doc = iframe.contentWindow.document;
    var innerhtml = document.getElementById(el).innerHTML;
    doc.write('<html><body><div>' + innerhtml + '</div></body></html>');
    doc.close();
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
    setTimeout("document.body.removeChild(iframe)", 5000);
}

$(document).ready(function(){
    $("#software dl").show();
	$("#software a").mouseover(function(){
	    $("#network dl").hide();
	    $("#software dl").fadeIn("fast");
	});
	$("#mailorder a").mouseover(function(){
	    $("#network dl").hide();
	    $("#mailorder dl").fadeIn("fast");
	});
	$("#medicaid a").mouseover(function(){
	    $("#network dl").hide();
	    $("#medicaid dl").fadeIn("fast");
	});
	$("#prescribers a").mouseover(function(){
	    $("#network dl").hide();
	    $("#prescribers dl").fadeIn("fast");
	});
	$("#hospitals a").mouseover(function(){
	    $("#network dl").hide();
	    $("#hospitals dl").fadeIn("fast");
	});
	$("#healthplans a").mouseover(function(){
	    $("#network dl").hide();
	    $("#healthplans dl").fadeIn("fast");
	});
	$("#independent a").mouseover(function(){
	    $("#network dl").hide();
	    $("#independent dl").fadeIn("fast");
	});
	$("#deptofhealth a").mouseover(function(){
	    $("#network dl").hide();
	    $("#deptofhealth dl").fadeIn("fast");
	});
	$("#vendor a").mouseover(function(){
	    $("#network dl").hide();
	    $("#vendor dl").fadeIn("fast");
	});
});
