/**
 * Notes: MX2 class requires prototype.js and jquery.js
 */
var MX2 = {
    TRANS_BG_ID : "transbg",

    welcomeBlock : {
        isWelcomeLoadManual : false,

        load : function() {
            try {
                if (welcomeBlockSO && MX2.welcomeBlock.isWelcomeLoadManual == true) {
                    // If the embed tag doesn't exist, then write to the welcomeBlock div,
                    // else, make the welcomeBlockFlash visible.
                    if (!$('welcomeBlockFlash')) {
                        welcomeBlockSO.write("welcomeBlock");
                    }
                    
                    else {
                        Element.setStyle('welcomeBlockFlash', {visibility : 'visible'});
                    }
                
                    MX2.welcomeBlock.isWelcomeLoadManual = false;
                }
                
            } catch (e) {}
        },

        unload : function() {
            try {
                // Make welcomeBlockFlash invisible.
                if ($('welcomeBlockFlash')) {
                    Element.setStyle('welcomeBlockFlash', {visibility : 'hidden'});
                }
                
                MX2.welcomeBlock.isWelcomeLoadManual = true;
            }
            
            catch (e) {
            }       
        }
    },

    show : function(elementID, isDropShadow) {
        if (isDropShadow && isDropShadow == true) {
            var transBG = document.createElement("div");
            transBG.id = MX2.TRANS_BG_ID;
            document.body.appendChild(transBG);
    
            if (transBG.offsetHeight < document.body.offsetHeight) {
                transBG.style.height = document.body.offsetHeight + "px";
            }
        }
        
        var flyout = $(elementID);

        if ($(elementID).offsetHeight < document.body.offsetHeight)
            $(elementID).style.height = document.body.offsetHeight + "px";
                    
        $(elementID).show();
        MX2.welcomeBlock.unload();
    },
    
    hide : function(elementID) {
        try {
            $(elementID).hide();
            try {
                // Hack for now. I don't like the idea that this assumed this element exists.
                if ($('dropShadow')) {
                    document.body.removeChild($('dropShadow'));
                }
            } catch (e) {
            }

            try {
                if ($(MX2.TRANS_BG_ID)) {
                    document.body.removeChild($(MX2.TRANS_BG_ID));
                }
            } catch (e) {
            }
        }
        
        catch (e) {
        
        }
        
        MX2.welcomeBlock.load();
    },
    
    validateEmail : function(emailStr, onSuccess, onFailure) {  
        // "any chars" + "@" + "any chars" + "." + "any chars"
        var emailRegxp = /^.+@.+\..+$/; 
                
        if (emailRegxp.test(emailStr) && emailRegxp.test(emailStr) == true) {
            if (onSuccess) {
                onSuccess.call();
            }
        }
        
        else {
            if (onFailure) {
                onFailure.call();
            }
        }
    }       
}

function clearText(thefield) {
    if (thefield.defaultValue==thefield.value)
    thefield.value = "";
}

function doPulseGrabber() {
    MX2.validateEmail(jQuery('form#pulseGrabberForm > #grabberEmail')[0].value
        , function() {
            // Ajax call.
            jQuery.get('/newsletter', 
            {
                newsstand:jQuery('form#pulseGrabberForm > #newsstand')[0].value,
                emailAddress:jQuery('form#pulseGrabberForm > #grabberEmail')[0].value,
                registrationSourceCode:jQuery('form#pulseGrabberForm > #registrationSourceCode')[0].value,
                marketingEmail:jQuery('form#pulseGrabberForm > #marketingEmail')[0].value,
                emailCampaignCode:jQuery('form#pulseGrabberForm > #emailCampaignCode')[0].value,
                type:'pulsegrabber'
            });
        	
            // Popup.
            var pulseGrabberDim = jQuery('#pulseGrabber').offset();
            var pulseGrabberWidth = jQuery('#pulseGrabber').width();

            // position popup right on top of the form.
            jQuery('#pulseGrabberPopup').popup('0.6', 
                {
                    top: pulseGrabberDim.top - 300, 
                    left: pulseGrabberDim.left - (pulseGrabberWidth / 2)
                });


            // Hide error block.
            jQuery('#pulseGrabberError').slideUp(250);
                        
            // Clear the text field.
            jQuery('#grabberEmail')[0].value = "";
            jQuery('#grabberEmail')[0].blur();      

        }
        , function() {
            // Show error block.
            jQuery('div#pulseGrabberError').slideDown(250);
    }); 

}

function initCountryFlydown(link,div){
	jQuery(link).mouseover(function(){
		if(jQuery(div).css('display')=='none'){
			jQuery(div).css('display','block')}
		else if(jQuery(div).css('display')=='block'){
			jQuery(div).css('display','none')
			jQuery(link).css('background-color','')}
		})
	jQuery(div).hover(function(){
		jQuery(this).css('display','block')
		},
		function(){
			jQuery(this).css('display','none')
		})}

function MM_jumpMenu(targ,selObj,restore){
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'")
	if(restore)selObj.selectedIndex=0
	}
//The jumpmenu's listed below are required for my Library. Please do not remove

function mylibrary_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='/account/my-library.jsp?sort="+selObj.options[selObj.selectedIndex].value+"'");
  
  //if (restore) selObj.selectedIndex=0;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function mypublication_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='/account/my-library.jsp?productId="+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Init for global newsstand.
jQuery(document).ready(function() {

    // Make sure popup is hidden on document.ready;
    jQuery('div#pulseGrabberPopup').css('display', 'none');

    // These are for the pulse grabber form.
    jQuery('form#pulseGrabberForm').submit(function() {
        doPulseGrabber();
        return false;
    });
    
    jQuery('#grabberSubmit').click(function() {
        doPulseGrabber();
    });
    
    // Close button on the popup.
    jQuery('#pulseGrabberCloseBtn').click(function() {
        jQuery('#pulseGrabberPopup').pushdown();
    });
    
    initCountryFlydown('a#flydownLink','div#flydownChooser')
    initCountryFlydown('a#splashLink','div#splashChooser')
    
	
	jQuery('#safeModeViewOn').hover(function(){
        jQuery('#maturePop').show();
	}, function() {
		  jQuery('#maturePop').hide();
    });

	jQuery('#safeModeViewOff').hover(function(){
        jQuery('#maturePop').show();
	}, function() {
		  jQuery('#maturePop').hide();
    });
    
	jQuery('#safeOn').hover(function(){
        jQuery('#safe-popup').show();
	}, function() {
		  jQuery('#safe-popup').hide();
    });

	jQuery('#safeOff').hover(function(){
        jQuery('#safe-popup').show();
	}, function() {
		  jQuery('#safe-popup').hide();
    });});