var tabs = new Array("cfd_dating","cfd_jobs","cfd_autos","cfd_realestate");
var nowShowing = "";
var loading = false;
var mode;

/* 
 * set all js functions
*/
$(document).ready(function() {
  //doc has loaded, set all JS functions for tabs
  // set tabClicked function for all <li>

  $("#classifiedsTabs").find("ul").find("li").each(function(i){
	  // set a function for each list item that sets the mouseover function
	  $(this).mouseover(function(){
		if(nowShowing != this.id){ //trigger tab only if not already displayed
		  tabTrigger(this.id,mode); 
		  nowShowing = this.id; 
		} 
	  });
  });
   
});

/* 
 * when tab is clicked, turn off all other tabs
 * and turn on selected tab
*/
function tabTrigger(triggered,whatmode){
  whatmode = (whatmode == null) ? "default" : whatmode;
  mode = (mode == null) ? whatmode : mode; 
  
  for(var i = 0; i < tabs.length; i++){
    var tab = tabs[i];
	if(tab == triggered && !loading){
	  // turn tab on
	  $("#"+tab).addClass("on");
	  $("#"+tab).removeClass("off");
	  
	  loading = true;
	   
	  $.post( 
    	"/components/classifieds/classifieds_get.htm", 
    	{ tab_id: tab, mode: mode }, 
   		function(result){ 
	  	  $("#classifiedsContent").html("<div>"+result+"</div>");   
		  loading = false;
		  /*
		   **** this only works in firefox and IE7 - may be usefull later ****
		   *
		   
		  //execute any <script></script> elements included in retrieved content...
		  var d = document.getElementById('classifiedsContent').getElementsByTagName("script");
		  var t = d.length;
		  for (var x=0;x<t;x++){
			var newScript = document.createElement('script');
	        newScript.type = d[x].type;
			newScript.src  = d[x].src;
	        newScript.text = d[x].text;
			document.getElementById('classifiedsContent').appendChild(newScript);
		  } 
		  */

    	});
	  
	  // display loading graphic for slower internet connections
	  $("#classifiedsContent").html("<img src='/components/classifieds/loading.gif' width=16 height=16 style='margin:5px' alt='loading' />");
	  
	}else{
	  // turn tab off
	  $("#"+tab).addClass("off");
	  $("#"+tab).removeClass("on");
	}
  }
  
}

