
var formValidation = {
    fieldFocusID: null,
    validateField: function (arrValidatorIDs, mapIDs) {
        var isValid = true;
        if (arrValidatorIDs != undefined) {
            for (var i = 0; i < arrValidatorIDs.length; i++) {
                var validator = document.getElementById(arrValidatorIDs[i]);
                if (validator.enabled == undefined || validator.enabled == true) {
                    ValidatorValidate(validator);
                    if (validator.isvalid == false) {
                        isValid = false;
                        if (this.fieldFocusID == null) {
                            if (validator.controltovalidate != undefined) {
                                if ($("#"+validator.controltovalidate).get(0).tagName.toLowerCase() != "input") {
                                    this.fieldFocusID = $("#" + validator.controltovalidate).find("input:first").attr("id");
                                } else {
                                    this.fieldFocusID = validator.controltovalidate;
                                }
                            } else if (validator.controltofocusonvalidationfailed != undefined) {
                                this.fieldFocusID = validator.controltofocusonvalidationfailed;
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (mapIDs != undefined) {
            if (mapIDs.fieldID != undefined) {
                if (isValid) {
                    $("#" + mapIDs.fieldID).removeClass("error");
                } else {
                    $("#" + mapIDs.fieldID).addClass("error");
                }
            }
        }
        return isValid;
    },
    validate: function (mapValidator) {
        var isValid = true;
        this.fieldFocusID = null;
        for (var memName in mapValidator) {
            var memObj = mapValidator[memName];
            if (typeof (memObj) == "function") {
                var flag = memObj();
                if (flag == false)
                    isValid = false;
            }
        }
        if (!isValid) {
            $("#" + this.fieldFocusID).focus();
        }
        return isValid;
    }
};

function initMainNavHover() {
	$('ul#mainNav li').hover( function() {
		$(this).children('ul.mainSubNav').show();
		$(this).children('a').addClass('liHover');
	}, function() {
		$(this).children('ul.mainSubNav').hide();
		$(this).children('a').removeClass('liHover');
	});	
}

function initAlignTipNote() {
    if ($('.alignTip').length > 0) {
        var tipPos = $('.alignTip').position();
        var tipMar = parseInt($('.alignTip').css('margin-top'));
        var tipPad = parseInt($('.alignTip').css('padding-top'));    
        var tipTop = tipPos.top + tipMar + tipPad;
        $('div.tip').css("top", tipTop + "px");
    }
    
    if ($('.alignNote').length > 0) {
        var notePos = $('.alignNote').position();
        var noteMar = parseInt($('.alignNote').css('margin-top'));
        var notePad = parseInt($('.alignNote').css('padding-top'));    
        var noteTop = notePos.top + noteMar + notePad;
        $('div.note').css("top", noteTop + "px");
    }
}

function initCtaArrows() {     
    if (window.location.pathname.indexOf('/hcp/') != -1) {
        var imgDir = commonProps.appRoot + 'images/hcp/';        
    } else {
        var imgDir = commonProps.appRoot + 'images/consumer/';        
    }

    var arrowGalHtml = '<img src="' + imgDir + 'callouts/arrow_callout_gallery.gif" class="linkArrow" alt="" runat="server" />';
    $('div.calloutsGallery a, div.galleryMain a')
        .not('a.noArrow')
        .append(arrowGalHtml);
        
    var arrowCallHtml = '<img src="' + imgDir + 'callouts/arrow_callout.gif" class="linkArrow" alt="" runat="server" />';        
    $('div.callouts_tall span.hdr, div.callouts span.hdr, div.callouts div.callout a').append(arrowCallHtml);
}

function initLightbox() {
  $('#videoLightbox').prepend('<a href="#" class="btnLightboxClose" onclick="hideLightbox(); return false;">Close</a>');  
  $('#chartWearTimeLightbox').prepend('<a href="#" class="btnLightboxClose" onclick="hideLightbox(); return false;">Close</a>');  
  
  $('#chartWearTimeThumb').click( function() {        
    jQuery.modalDialog.show('#chartWearTimeLightbox', document.forms[0]);    
  });
  
  $('.chartLarge').prepend('<div id="chartClose"><a href="#" onclick="hideLightbox(); return false;">Close</a></div>');
  
  
  $('.external').click(showIntLightbox);  
}

function showIntLightbox(e) {
    e.preventDefault();
    url = this.href;
    
	  $('div#interstitialLightbox a.btn_cancel').click(hideLightbox);
	  $('div#interstitialLightbox a.btn_continue').click(function()
	  {	
        hideLightbox();	
   		  window.open (url,"mywindow");
	  });
      
    jQuery.modalDialog.show('#interstitialLightbox', document.forms[0]);
}

var flashDiv = 'videoLightboxFlash';
var flashDivHolder = 'videoLightboxFlashHolder';
var destroyFlashDiv = function() {
  swfobject.removeSWF(flashDiv);
  $('#'+flashDivHolder).html('<div id="'+ flashDiv +'"></div>'); //recreates the flash holder DOM object that was destroyed by removeSWF
}

function launchVideo() {
		var flashvars = {
			xmlUrl:"../flash/xml/config.xml"
		};
		var params = {
			//menu: "false"
			scale: "noscale",
			allowScriptAccess: "sameDomain",
			wmode: "window",
			quality: "high",
			allowFullScreen: "false"
		};
		var attributes = {};

    attributes.id = flashDiv;
    swfobject.embedSWF("../flash/mediaPlayer.swf", "videoLightboxFlash", "567", "400", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

    jQuery.modalDialog.show('#videoLightbox', document.forms[0], { onHide: destroyFlashDiv } );
}

function hideLightbox() {
  jQuery.modalDialog.hide(); 
  //destroyFlashDiv();
}

function mainInit() {
  initMainNavHover();
  initAlignTipNote();
  initCtaArrows();
  initLightbox();
}
$(document).ready(mainInit);
