var connections = 0;
var callUsesWYSIWYG = 0;

function jsDebug(msg){
	if(0){
		document.getElementById("jsDebug").style.display = "block";
		document.getElementById("jsDebug").innerHTML = document.getElementById("jsDebug").innerHTML + "<li>" + msg;
	}
}

function getAHAH(l_args, divID){
   return getAHAHm(l_args, divID, 1); 
}
function getAHAH2(l_args, divID){
	return getAHAHm(l_args, divID, 0); 
}
function getAHAH3(l_args, divID){
	return getAHAHm(l_args, divID, 2); 
}
function getAHAH4(l_args, divID){
	return getAHAHm(l_args, divID, 4); 
}
function getAHAH5(l_args, divID){
	return getAHAHm(l_args, divID, 99); 
}
function getAHAHForm(obj, divID) {
   var l_args = Form.serialize(obj);
	return getAHAHm(l_args, divID, 109); 
}

function getAHAHm(l_args, divID, showLoading)
{
jsDebug("Doing AHAH - loading results to " + divID);
	if(showLoading != 2 && showLoading != 99)
		$(divID).innerHTML = "";

	if(showLoading == 1)
		$(divID).innerHTML = "<center><img src='images/wait2.gif'></center>";	
		//$(divID).innerHTML = "<center><img src='images/loading.gif'><BR>Loading Content, Please Wait...</center>";	

	if(showLoading == 4)
		$(divID).innerHTML = "<img onclick='hideModal();' align='absmiddle' src='/community/images/closebutton.gif'> Cancel Request<hr><center><P><img src='images/wait2.gif'><BR>Loading Content, Please Wait...</center>";	
	
	//TODO:  Process AHAH request here....
	var url = "/community/includes/ahah.php";
	connections++;
	if(connections > 0 && showLoading != 99)
	{
		$('statDivID').style.display = 'block';
	}

	if(showLoading == 109)
	{
		// Going to do a postbody with teh args
		new Ajax.Request(url, {
			method: 'post',
			onSuccess: function(transport) 
							{
								$(divID).innerHTML = transport.responseText;
								connections--;
								if(connections <= 0)
								{
									Element.hide($('statDivID'));
								}
								
								if($('humanornot') != null)
								{
									showRecaptcha('humanornot');
								}
							},
	
			onFailure: function(transport) 
							{
								$(divID).innerHTML = "Error loading content.  If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>";
								connections--;
								if(connections <= 0)
								{
									Element.hide($('statDivID'));
								}
							},
			postBody:  l_args
			}
		); 
	}
	else
	{
		new Ajax.Request(url, {
			method: 'post',
			onSuccess: function(transport) 
			{
				if(divID != "AJAXDIVRESP")
					$(divID).innerHTML = transport.responseText;
				else if(transport.responseText != "")
					document.body.appendChild(makeDiv(transport.responseText));
				
				connections--;
				if(connections <= 0)
				{
					Element.hide($('statDivID'));
				}

				// EXPERIMENTAL
				if(callUsesWYSIWYG == 1)
				{						
					callUsesWYSIWYG = 0;
					tinyMCE.execCommand('mceAddControl', false, 'mceEditorSpot');
				}
				// END EXPERIMENTAL
			},
			onFailure: function(transport) 
			{
				$(divID).innerHTML = "Error loading content.  If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>";
				connections--;
				if(connections <= 0)
				{
					Element.hide($('statDivID'));
				}
			},
			parameters:  'args=' + l_args
							
			}
		); 
	}

	return false;
}


function getAJAX(obj)
{
	return getAJAX_m(obj, 0);
}
function getAJAXcond(obj)
{
	return getAJAX_m(obj, 1);
}

function getAJAX_m(obj, conditional) 
{
	if(callUsesWYSIWYG == 1)
		tinyMCE.triggerSave();
				
   var data = Form.serialize(obj);
	//TODO:  Do AJAX call here...
	var url = "/community/includes/ajax.php";
	connections++;
	if(connections > 0 && conditional == 0)
	{
		$('statDivID').style.display = 'block';
	}

	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport) 
						{
							if(transport.responseText != "false" || conditional == 0)
							{
								var newDiv = makeDiv(transport.responseText);
								document.body.appendChild(newDiv);
							}
							connections--;
							if(connections <= 0)
							{
								Element.hide($('statDivID'));
							}
						},

		onFailure: function(transport) 
						{
							makeDiv("Error loading content.  If errors persist, please contact <a href='mailto:support@pathtags.com'>support@pathtags.com</a>");
							connections--;
							if(connections <= 0)
							{
								Element.hide($('statDivID'));
							}
						},
						
		
		postBody:  data
						
		}
	); 

	return false;
	
	
}

var modalWidth = 800;
var originalModalWidth = 800;
function loadModalSized(ahahLoad, theWidth)
{
	var tempModalWidth = modalWidth;
	modalWidth = theWidth;
	loadModal(ahahLoad);
	modalWidth = tempModalWidth;
}

function loadModal(ahahLoad)
{
	$$('select').invoke('setStyle', {visibility: 'hidden'});

	grayOut(true);
	viewport.getAll();
	modalSize = modalWidth;
	$("modalDiv").innerHTML = "<div id='modalcontent' style='background-color: #ffffff;border: 10px solid #1E62A1;position: relative;padding: 5px;'><center><h2>Loading Content, Please Wait</h2><P><img src='images/loading.gif'></center></div>";		
	//$("modalDiv").style.width = modalSize + "px";
	//$("modalDiv").width = modalSize;
	
	//$("modalDiv").height = originalModalWidth;
	/*
	if((viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) > 0)
		$("modalDiv").style.top = (viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) + "px";
	else
		$("modalDiv").style.top = "5px";
		*/
	$("modalDiv").style.top = viewport.scrollY + 20 + "px";

	$("modalDiv").style.left = (viewport.width/2 - (modalSize/2)) + "px";
	$("modalDiv").style.display = "block";
	getAHAH4(ahahLoad, "modalcontent");
	modalWidth = originalModalWidth;
}	

function loadModalFromForm(obj)
{
	$$('select').invoke('setStyle', {visibility: 'hidden'});

	grayOut(true);
	viewport.getAll();
	modalSize = modalWidth;
	$("modalDiv").innerHTML = "<div id='modalcontent' style='background-color: #ffffff;border: 10px solid #1E62A1;position: relative;padding: 5px;'><center><h2>Loading Content, Please Wait</h2><P><img src='images/loading.gif'></center></div>";		
	//$("modalDiv").style.width = modalSize + "px";
	//$("modalDiv").width = modalSize;
	
	//$("modalDiv").height = originalModalWidth;
	/*
	if((viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) > 0)
		$("modalDiv").style.top = (viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) + "px";
	else
		$("modalDiv").style.top = "5px";
		*/
	$("modalDiv").style.top = viewport.scrollY + 20 + "px";

	$("modalDiv").style.left = (viewport.width/2 - (modalSize/2)) + "px";
	$("modalDiv").style.display = "block";
	getAHAHForm(obj, "modalcontent");
	modalWidth = originalModalWidth;
	return false;
}	

function loadModalFromForm2(obj)
{
	$$('select').invoke('setStyle', {visibility: 'hidden'});

	grayOut(true);
	viewport.getAll();
	modalSize = modalWidth;
	$("modalDiv").innerHTML = "<center><h2>Loading Content, Please Wait</h2><P><img src='images/loading.gif'></center>";		
	$("modalDiv").style.width = modalSize + "px";
	$("modalDiv").width = modalSize;
	
	$("modalDiv").height = originalModalWidth;
	if((viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) > 0)
		$("modalDiv").style.top = (viewport.scrollY + viewport.height/2 - (originalModalWidth/2)) + "px";
	else
		$("modalDiv").style.top = "5px";

	$("modalDiv").style.left = (viewport.width/2 - (modalSize/2)) + "px";
	$("modalDiv").style.display = "block";
	getAHAHForm(obj, "modalDiv");
	modalWidth = originalModalWidth;
	return false;
}	

function hideModal()
{
	$$('select').invoke('setStyle', {visibility: 'visible'});
	grayOut(false);
	$("modalDiv").style.display = "none";
}
	
var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth) 
  		this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth) 
  		this.width = document.body.clientWidth;
  },
  
  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
  	else if (document.documentElement && document.documentElement.clientHeight) 
  		this.height = document.documentElement.clientHeight;
  	else if (document.body && document.body.clientHeight) 
  		this.height = document.body.clientHeight;
  },
  
  getScrollX: function () {
    this.scrollX = 0;
  	if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
  	else if (document.documentElement && document.documentElement.scrollLeft)
  		this.scrollX = document.documentElement.scrollLeft;
  	else if (document.body && document.body.scrollLeft) 
  		this.scrollX = document.body.scrollLeft; 
  	else if (window.scrollX) this.scrollX = window.scrollX;
  },
  
  getScrollY: function () {
    this.scrollY = 0;    
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
  		this.scrollY = document.documentElement.scrollTop;
  	else if (document.body && document.body.scrollTop) 
  		this.scrollY = document.body.scrollTop; 
  	else if (window.scrollY) this.scrollY = window.scrollY;
  },
  
  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  } 
}

// Vars for auto dive generation
var xPtStat = 0;
var yPtStat = 500;
var xPt = 50;
var yPt = 10;
var divCount = "0";

function makeDiv(text)
{
	var eDiv=document.createElement("DIV");
	eDiv.style.width="350px";
	eDiv.style.minHeight="150px";
	eDiv.style.position="fixed";
	eDiv.style.zindex="100";
	eDiv.style.zIndex="100";        
   eDiv.style.border = "5px solid #FFAA2A"
   eDiv.style.backgroundColor ="#ffffff";
   eDiv.style.padding = "10px";
   eDiv.style.overflow = "auto";

	eDiv.style.top = xPt.toString() + "px";
	eDiv.style.left = yPt.toString() + "px";
	
	xPt += 5;
	yPt += 5;
	
	eDiv.style.fontfamily = "Trebuchet MS, Arial, sans-serif";
	eDiv.style.fontSize = "12px";
	eDiv.style.lineheight = "1.5em";
	//eDiv.setAttribute('onClick', 'this.style.visibility = "hidden";xPt -= 5; yPt -= 5;');
	eDiv.setAttribute('id', 'dyndiv'+divCount);
	eDiv.onclick = function() 
		{ 
			this.style.visibility = "hidden";
			xPt -= 5; 
			yPt -= 5; 
		};
	eDiv.innerHTML = '<img src="/community/images/closebutton.gif" onclick="$(\'dyndiv' + divCount + '\').style.visibility = \'hidden\'" align="right">' + text;
		
	divCount++;
	return eDiv;
}	

function hide(theDiv){
	theDiv.style.visibility = "hidden";	
}
function submitKeyword(keyword){ 
	document.keywordsuggest.keyword.value = keyword;
}
		


function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 85;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}	

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
  return fileref;
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
