/*** jQuery library is required ***/
/*** jQuery Form Plug-In is required ***/
(function() {

window.mlogic = 
{
   /** show/hide search screen */
   search : function() {
    	var vSearch = $("#searchContainer");
    
    	vSearch.css("top", 0);
    	vSearch.toggle();
   },
   
   /** show/hide section screen */
   section : function() {
	var vSection = $("#sectionContainer");
	
	vSection.css("top", 0);
	vSection.toggle();
   },
   
   /** Inline Content AJAX Type Toolbar.
      tabMenu (required): the menu jQuery Object, the most outter html tag.
      tabItem (required): the menu item jQuery Object.
      tabIndex (required): index of the meun item.
      targetURL (required): url of target page.
      slide (optional): sliding effect.
   */
   ajaxTab : function(tabMenu, tabItem, tabIndex, targetURL, slide) {
   	// check to see if same call is in progress
   	var inProgress = tabItem.attr("in-progress");
   	if(inProgress) {   
   	   setTimeout(function() {
   	   	tabItem.removeAttr("in-progress");
   	   }, 3000);
   	   
   	   return; 
   	}
   
   	// set in-progress to true
   	tabItem.attr("in-progress", "true");
   	
   	// tabMenuContext
   	var tabMenuContext;
   	
   	try {
   	  tabMenuContext = tabMenu.parent(); 
   	}catch(exception) {
   	  tabItem.removeAttr("in-progress");
   	  return;
   	}
   	
   	
   	var tabId = "ajax-tabs-" + tabIndex;
   	var tabPanelId = "ajax-tabs-panel";
   	var tabPanel = $("#" + tabPanelId, tabMenuContext);
   	
   	var currentTabId = tabPanel.attr("tabId");
   	
   	// same tab, hide it
   	if( currentTabId && currentTabId == tabId) {
	  if(slide)
   	    tabPanel.slideUp("normal", function(){ tabPanel.remove() } );
   	  else 
   	    tabPanel.remove();
   	    
   	    tabItem.removeAttr("in-progress");
   	    return; // stop following code execution
   	}

	// ajax call
   	$.ajax( { url: targetURL,  
   		success: sCallback,
   		beforeSend: function() { tabItem.attr("selected", "progress") },
   		complete:  function() { tabItem.removeAttr("selected") },
   	} );
   	
   	// callback function for ajax
   	function sCallback(html) {
   	  // remove existing panel
   	  if(tabPanel.attr("tabId")) {
   	    tabPanel.remove();
   	  } 
   	  
   	  // create panel
  	  tabPanel = $(html);
      	  tabPanel.attr("id", tabPanelId);
      	  tabPanel.attr("tabId", tabId); 
      	  tabPanel.css("display", "none");
	  tabMenu.after(tabPanel); // append tabPanel below tabMenu
   	  
   	  if(slide)
   	    tabPanel.slideDown("normal");
   	  else
   	    tabPanel.show();
   	}
   	
   	// remove progress status
   	tabItem.removeAttr("in-progress");
   },
   
   /** send 2 friend email AJAX type 
      emailForm (required): email form jQuery Object.
   */
   send2friendEmail : function(emailForm) {
   	var submitType = emailForm.attr("method");
   	if(submitType == "ajax") {
   	   submitType = "GET";
   	}
   
   	emailForm.ajaxSubmit({ type : submitType,
   			       beforeSubmit : validate, 
   			       target : emailForm,
   			       success: sCallback 	
 	});
 	
 	function validate() {
 	  var isValid = true;
	  var fromAdr = emailForm.get(0).from.value;
	  var toAdr = emailForm.get(0).to.value;
	  
	  if( !isValidEmailAdr(fromAdr) ) {
	     alert("Invalid From Address");
	     return false;
	  }
	  
	  if( !isValidEmailAdr(toAdr) ) {
	     alert("Invalid To Address");
	     return false;
	  }
	  
 	  return isValid;
 	}
   	
   	function sCallback(data) {
   	   // leave it empty for now.
   	}
   },
   
   /** send sms AJAX type 
    smsForm (required) : sms form jQuery Object.
   */
   sms : function(smsForm) {
      	var submitType = smsForm.attr("method");
      	if(submitType == "ajax") {
      	   submitType = "GET";
      	}
      
      	smsForm.ajaxSubmit({ type : submitType,
      			       beforeSubmit : validate, 
      			       target : smsForm,
      			       success: sCallback 	
    	});
    	
    	function validate() {
    	  var isValid = true;
   	  var phone = smsForm.get(0).to.value;
   	  if(phone.length == 0) {
   	    alert("Please enter phone number.");
   	    return false;
   	  }
   	  
   	  var fromName = smsForm.get(0).fromName.value;
   	  if(fromName.length == 0) {
   	    alert("Please enter name.");
   	    return false;
   	  }
   	  
    	  return isValid;
    	}
      	
      	function sCallback(data) {
      	   // leave it empty for now.
      	}
   },
   
   /** Help to switch toolbar class attribute 
       item (required) : jQuery Object
       offClazz (required) : item off html class value.
       onClazz (required) : item on html class value
       itemContext (optional) : jQuery Object of the most outter toolbar html tag.
       contextOffClazz (optional) : 
       contextOffClazz (optional): 
   */
   toolbarSwitch : function(item, offClazz, onClazz, itemContext, contextOffClazz, contextOnClazz) {
   	//alert(item.attr("class"));
   	if(item.attr("class")) {
   	  if(item.attr("class") == onClazz) {
   	    item.attr("class", offClazz);
   	    
   	    if(itemContext) {
   	    	itemContext.attr("class", contextOffClazz);
   	    }
   	  }
   	  else {
   	    item.attr("class", onClazz);
   	    
   	    if(itemContext) {
	       	itemContext.attr("class", contextOnClazz);
   	    }
   	  }
   	}
   	
   	// turn off all siblings
   	item.siblings().attr("class", offClazz);
   },
   
   
   clearScreen : function() {
       // clear the search screen
       var searchObj =  $("#searchContainer");
       if( searchObj.length > 0 ) {
       	  searchObj.css("display", "none");
       }
       
       // clear the section screen
       var sectionObj = $("#sectionContainer");
       if( sectionObj.length > 0 ) {
       	  sectionObj.css("display", "none");
       }
   },
   
   jumper : function() {
   	var jumper = document.getElementById('jumper');
   	var jumpingOptions = jumper.jumpingOptions;
   	var jumpingTarget = jumpingOptions.options[jumpingOptions.selectedIndex].value;
   	
   	if(jumpingTarget.length > 0) {
   	   window.location.href = jumpingTarget;
   	}
   	
   }
   
   
}; // end of window.mlogic

function isValidEmailAdr(emailAdr) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(emailAdr) == false) {
      return false;
   }
   else {
      return true;
   }
}

addEventListener("click", function(event)
{
    // clear the screen for sliding
    var targetObj = $(event.target);
    if(targetObj.is("a")) {
    	if(targetObj.attr("target") == "_slide") {
    		mlogic.clearScreen();
    	}
    }
}, true);

addEventListener("submit", function (event) {
    mlogic.clearScreen();
}, false);

})();
