//this part ensures that any open dialog boxes will be closed if the user clicks anywhere on the body
$(document).ready(function(){
  $('body').click(function() {
    $('[id^=header_menu]:visible').css("display","none")
  });
});

$(document).ready(function() {

  $("[id^=header_nav]").click(function(event){
   toggle_nav( $(this).attr("id"), event );
  });

  //function toggles the display of all navigation menus
  function toggle_nav( button_id , event )
  {
  	var menu_id=button_id.replace("header_nav","header_menu");
  	if( $("#"+menu_id).css("display")=="block" )
  	{
  		$("[id^=header_menu]").css("display","none");
  	}
  	else
  	{
  		$("[id^=header_menu]").css("display","none");
  		$("#"+menu_id).css("display","block");
  		event.stopPropagation();
  	}
  }

  //this ensures that the body code is halted if the user click on the menu itself
  $("[id^=header_menu]").click(function(event){
  	event.stopPropagation();
	
  });

  $(".brand_control").click(function(){
    var brand_id=$(this).attr("id");
    brand_id=brand_id.replace("brand_control_","");
    //check if we're open or closed
    if( $(this).is(".leftnav_drop_label_off") )
    {
   	  //closed!
      $(this).removeClass('leftnav_drop_label_off');
      $(this).addClass('leftnav_drop_label_on');
      $(".brand_child_"+brand_id).css("display","block");

    }
    else
    {
      //open!
      $(this).removeClass('leftnav_drop_label_on');
      $(this).addClass('leftnav_drop_label_off');
      $(".brand_child_"+brand_id).css("display","none");
    }

  });

  $("#leftnav_post_button").click(function(){
  	//toggle display of the posting buttons
  	if( $("#leftnav_post_control").hasClass("invisible") )
  	{
  	  $("#leftnav_post_control").removeClass("invisible");
  	}
  	else
  	{
  	  $("#leftnav_post_control").addClass("invisible");
  	}
  });

});

$(document).ready(function() {

});






	var xpPanel_slideActive = true;	// Slide down/up active?
	var xpPanel_slideSpeed = 20;	// Speed of slide
	var xpPanel_onlyOneExpandedPane = false;	// Only one pane expanded at a time ?	

	var dhtmlgoodies_xpPane;
	var dhtmlgoodies_paneIndex;	

	var savedActivePane = false;
	var savedActiveSub = false;

	var xpPanel_currentDirection = new Array();
	var cookieNames = new Array();
	var currentlyExpandedPane = false;


	/*
	These cookie functions are downloaded from 
	http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
	*/	

	function Get_Cookie(name) { 

	   var start = document.cookie.indexOf(name+"="); 

	   var len = start+name.length+1; 

	   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 

	   if (start == -1) return null; 

	   var end = document.cookie.indexOf(";",len); 

	   if (end == -1) end = document.cookie.length; 

	   return unescape(document.cookie.substring(len,end)); 

	} 

	// This function has been slightly modified

	function Set_Cookie(name,value,expires,path,domain,secure) { 

		expires = expires * 60*60*24*1000;

		var today = new Date();

		var expires_date = new Date( today.getTime() + (expires) );

	    var cookieString = name + "=" +escape(value) + 

	       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 

	       ( (path) ? ";path=" + path : "") + 

	       ( (domain) ? ";domain=" + domain : "") + 

	       ( (secure) ? ";secure" : ""); 

	    document.cookie = cookieString; 

	}



	function cancelXpWidgetEvent()

	{

		return false;	

		

	}

	

	function showHidePaneContent(e,inputObj)

	{

		if(!inputObj)inputObj = this;

		

		var img = inputObj.getElementsByTagName('IMG')[0];

		var numericId = img.id.replace(/[^0-9]/g,'');

		var obj = document.getElementById('paneContent' + numericId);

		if(img.src.toLowerCase().indexOf('up')>=0){

			currentlyExpandedPane = false;
			img.src = img.src.replace('up','down');
			
			if(xpPanel_slideActive){

				obj.style.display='';

				xpPanel_currentDirection[obj.id] = (xpPanel_slideSpeed*-1);

				slidePane((xpPanel_slideSpeed*-1), obj.id);

			}else{

				obj.style.display='none';

			}

			if(cookieNames[numericId])Set_Cookie(cookieNames[numericId],'0',100000);

		}else{

			if(this){

				if(currentlyExpandedPane && xpPanel_onlyOneExpandedPane)showHidePaneContent(false,currentlyExpandedPane);

				currentlyExpandedPane = this;	

			}else{

				currentlyExpandedPane = false;

			}

			img.src = img.src.replace('down','up');

			if(xpPanel_slideActive){

				if(document.all){

					obj.style.display='block';

					//obj.style.height = '1px';

				}

				xpPanel_currentDirection[obj.id] = xpPanel_slideSpeed;

				slidePane(xpPanel_slideSpeed,obj.id);

			}else{

				obj.style.display='block';

				subDiv = obj.getElementsByTagName('DIV')[0];

				obj.style.height = subDiv.offsetHeight + 'px';

			}

			if(cookieNames[numericId])Set_Cookie(cookieNames[numericId],'1',100000);

		}	

		return true;	

	}

	

	

	

	function slidePane(slideValue,id)

	{

		if(slideValue!=xpPanel_currentDirection[id]){

			return false;

		}

		var activePane = document.getElementById(id);

		if(activePane==savedActivePane){

			var subDiv = savedActiveSub;

		}else{

			var subDiv = activePane.getElementsByTagName('DIV')[0];

		}

		savedActivePane = activePane;

		savedActiveSub = subDiv;

		

		var height = activePane.offsetHeight;

		var innerHeight = subDiv.offsetHeight;

		height+=slideValue;

		if(height<0)height=0;

		if(height>innerHeight)height = innerHeight;
		

		if(slideValue<0){			

			activePane.style.height = height + 'px';

			subDiv.style.top = height - subDiv.offsetHeight + 'px';

			if(height>0){

				setTimeout('slidePane(' + slideValue + ',"' + id + '")',10);

			}else{

				if(document.all)activePane.style.display='none';

			}

		}else{			

			subDiv.style.top = height - subDiv.offsetHeight + 'px';

			activePane.style.height = height + 'px';

			if(height<innerHeight){

				setTimeout('slidePane(' + slideValue + ',"' + id + '")',10);				

			}		

		}								

	}

	

	function mouseoverTopbar()

	{

		var img = this.getElementsByTagName('IMG')[0];

		var src = img.src;

		img.src = img.src.replace('../.gif','_over.gif');

		var span = this.getElementsByTagName('SPAN')[0];

		span.style.color='#000000';		

		

	}

	function mouseoutTopbar()

	{

		var img = this.getElementsByTagName('IMG')[0];

		var src = img.src;

		img.src = img.src.replace('../_over.gif','.gif');		

		

		var span = this.getElementsByTagName('SPAN')[0];

		span.style.color='';						

	}

		

	function initDhtmlgoodies_xpPane(panelTitles,panelDisplayed,cookieArray)

	{

	
		dhtmlgoodies_xpPane = document.getElementById('dhtmlgoodies_xpPane');

		var divs = dhtmlgoodies_xpPane.getElementsByTagName('DIV');

		dhtmlgoodies_paneIndex=0;

		cookieNames = cookieArray;

		for(var no=0;no<divs.length;no++){

			if(divs[no].className=='dhtmlgoodies_panel'){

				

				var outerContentDiv = document.createElement('DIV');	

				var contentDiv = divs[no].getElementsByTagName('DIV')[0];

				outerContentDiv.appendChild(contentDiv);	

			

				outerContentDiv.id = 'paneContent' + dhtmlgoodies_paneIndex;

				outerContentDiv.className = 'panelContent';

				var topBar = document.createElement('DIV');

				topBar.onselectstart = cancelXpWidgetEvent;

				var span = document.createElement('SPAN');				

				span.innerHTML = panelTitles[dhtmlgoodies_paneIndex];

				topBar.appendChild(span);

				//alert(panelTitles[dhtmlgoodies_paneIndex]);
				 topBar.onclick = showHidePaneContent;
				

				if(document.all)topBar.ondblclick = showHidePaneContent;

				topBar.onmouseover = mouseoverTopbar;

				topBar.onmouseout = mouseoutTopbar;

				topBar.style.position = 'relative';



				var img = document.createElement('IMG');

				img.id = 'showHideButton' + dhtmlgoodies_paneIndex;


				img.src = 'images/up-arrow.png';		

				img.width='7';

				img.height='4';
				if(panelTitles[dhtmlgoodies_paneIndex]!='LOGOUT') 
				  topBar.appendChild(img);

				

				if(cookieArray[dhtmlgoodies_paneIndex]){

					cookieValue = Get_Cookie(cookieArray[dhtmlgoodies_paneIndex]);

					if(cookieValue)panelDisplayed[dhtmlgoodies_paneIndex] = cookieValue==1?true:false;

					

				}

				

				if(!panelDisplayed[dhtmlgoodies_paneIndex]){

					outerContentDiv.style.height = '0px';

					contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';

					if(document.all)outerContentDiv.style.display='none';

					img.src = 'images/down-arrow.png';

					img.width='7';

					img.height='4';

				}

								

				topBar.className='topBar';

				divs[no].appendChild(topBar);				

				divs[no].appendChild(outerContentDiv);	

				dhtmlgoodies_paneIndex++;			

			}			

		}

	}


function select_all(){
		 elmlen = document.frmform.elements.length;
		
		  for(var j=0;j<elmlen;j++)
		  {
			if(document.frmform.elements[j].type == "checkbox")
			{	
				document.frmform.elements[j].checked = true;
			}
		 }		
   }
function deleteAll()
	{
		  var id_str = ''; 
		  elmlen = document.frmform.elements.length;
		  for(var j=0;j<elmlen;j++)
		  {
				if(document.frmform.elements[j].type == "checkbox")
				{	
					if(document.frmform.elements[j].checked)
					{
						if(id_str=='')
							id_str = document.frmform.elements[j].value;
						else
							id_str = id_str + ',' + document.frmform.elements[j].value;
					}
				}
		  }
		  id_str='3,4'; /* Remove while actual implemantation*/
		  if(id_str=='')
		  {
				alert('Please select category(s)');	
				return false;
		  }		  
		  else
		  {
				if(confirm("Are you sure you want to delete this category(s)?"))
				{
					document.frmform.action='manage_provider.php?msg=3'; /* change while actual implemantation*/
					document.frmform.submit();
				}		  	
		  }
	}

	$(".topBar").live('click',function(e)
	{
		/*var obj = $(this);
		
		alert( $(this).next().find(".panelContent").css("height") );
				
		if( $(this).next().attr('height') == 0 )
		{
			var action = 'show';
		}
		else
		{
			var action = 'hide';
		}
		alert(action);return false;
		$(".topBar").each(function()
		{ 
			$(this).next().fadeOut();
		});
		//
		if( action == 'hide' )
		{
			obj.removeClass('hiddenbtn');
			obj.addClass('showbtn');
			obj.next().slideUp(100);
		}
		else
		{
			obj.removeClass('showbtn');
			obj.addClass('hiddenbtn');
			obj.next().slideDown(100);
		}*/
		
		//pest
	});
