/**
 * EV Common JavaScript
 *
 */

// establish EV namespace
var $EV = window.$EV || {};

/*
 * tooltipInit(): initialize tooltips
 *
 */
$EV.tooltipInit = function() {
	
	jQuery(".inlineTooltip").each(function(){ 
    	var el = jQuery(this); 
	    el.tooltip({ 
	        effect: 'slide',
	        tip : '#' + el.attr("id") + '-content',
	        delay: 0
	    }); 
	}); 
}


/*
 * toggleDetailDivInit():
 * Shows or hides <div> wrapped html elements based upon the state of another element's display: property
 *
 * e.g.: 
 *  <span id="detail-text-show" class="toggleDetailDiv" style="display: inline">text to show when [id]-content is hidden</span>
 *  <span id="detail-text-hide" class="toggleDetailDiv" style="display: none">Text to show when [id]-content is shown</span>
 *  <div id="detail-text-content class="detailDiv">
 *      <p> To display or not to display: that is question.</p>
 *  </div>
 *
 */
$EV.toggleDetailDivInit = function() {
    
    jQuery( ".toggleDetailDiv" ).each( function() {

        var toggleSwitch    = jQuery( this );       // me: I could be id="foo-show" or id="foo-hide"
        var rootIdStr       = new String();         // root 'foo' for id= "foo-show", "foo-hide" or "foo-content"
        rootIdStr           = toggleSwitch.attr( "id" );
        
        // strip off '-show' or '-hide' to get 'foo'
        if( -1 != rootIdStr.lastIndexOf( "-show" )) { rootIdStr = rootIdStr.replace( '-show', "" ); }   // toogleSwitch is id="foo-show"
        else { rootIdStr = rootIdStr.replace( '-hide', "" ); }                                          // toggleSwitch is id="foo-hide"

        // bind a click event to the text
        toggleSwitch.click( function()  {
            toggleSwitch.css( "display", "none" );      // whether you are id="foo-show" or "foo-hide" you get hidden
            if(   'none'  ==  ( jQuery( '#' + rootIdStr + '-content' ).css( "display" ) )    )  {
                jQuery( '#' + rootIdStr + '-content' ).css( "display", "block");    // show id="foo-content"
                jQuery( '#' + rootIdStr + '-hide' ).css( "display", "inline-block");      // show id="foo-hide"
            } else {
                jQuery( '#' + rootIdStr + '-content' ).css( "display", "none");     // hide id="foo-content"
                jQuery( '#' + rootIdStr + '-show' ).css( "display", "inline-block");      // show id="foo-show"
            }
        })
    })
}


/*
 * overlayInit(): initialize overlay
 *
 */
$EV.overlayInit = function() {
	
	jQuery(".inlineOverlay").each(function(){ 
    	var el = jQuery(this); 
	    el.overlay({ 
	    	closeOnClick: true,
	        target : '#' + el.attr("id") + '-content',
	        mask : {
	        	color: '#6F6F6F', 
	        	loadSpeed: 250, 
	        	closeSpeed: 100
	        	}
	    }); 
	}); 
}

/*
 * getUrlParam(): Get a parameter from the querystring
 *
 * @param name the name of the parameter to get
 */
$EV.getUrlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (!results) { return null; }
	return results[1] || null;
}

/*
 * openWindow(): open a new window
 *
 * @param	theURL		string	the URL to open
 * @param	winName		name of the new window
 * @param	features 	optional features of the window
 *
 */
$EV.openWindow = function(theURL,winName,features) {
  window.open(theURL,winName,features);
  return false;
}

jQuery(document).ready( function(){
	$EV.overlayInit();
});
