//author Srinivasa Rao Motupall.
//This is for displaying category popup using ajax call.
var is_time_count_started=0;
var popUp_category_div_id='';
var popUp_category_Name='';
var category_popup_timer_obj;
var category_popup_load_time=1000;
var is_popup_generated=false;
function doPopupTimer(category_div_id,category_Name,isOnClick)
{
	//alert("category_div_id is"+category_div_id +" and is_time_count_started is ::"+is_time_count_started);
	//alert("category_Name is"+category_Name);
	try
	{
		if(is_popup_generated == false || isOnClick == 'true')
		{
			if(is_time_count_started==0)
			{
				if(isOnClick=='true')
				{
					//is_time_count_started=1;
					popUp_category_div_id=category_div_id;
					popUp_category_Name = category_Name;
					showCategoryPopUp();
				}
				else
				{
					is_time_count_started=1;
					popUp_category_div_id=category_div_id;
					popUp_category_Name = category_Name;
					category_popup_timer_obj=setTimeout("showCategoryPopUp()",category_popup_load_time);
				}
				
			}
			else
			{
				if(isOnClick=='true')
				{
					//is_time_count_started=1;
					popUp_category_div_id=category_div_id;
					popUp_category_Name = category_Name;
					clearTimeout(category_popup_timer_obj);
					hideOnClick();
					showCategoryPopUp();
				}
				else(popUp_category_div_id!=category_div_id)
				{	
					is_time_count_started=0;
					popUp_category_div_id='';
					popUp_category_Name = '';
					clearTimeout(category_popup_timer_obj);
					//document.getElementById('categoryContent').innerHTML="";
					//document.getElementById('mmaddSense2').innerHTML="";
					hideOnClick();
					doPopupTimer(category_div_id,category_Name,isOnClick);
				}
			}
		}
		else
		{
			var categoryHeader = document.getElementById('categoryHeader').innerHTML;
			//alert("categoryHeader is ::"+categoryHeader +"category_Name is ::: "+category_Name);
			category_Name = category_Name.replace('&','&amp;')
			if(category_Name != categoryHeader)
			{
				is_popup_generated =false;
				doPopupTimer(category_div_id,category_Name,isOnClick);
			}
		}
	}
	catch (err)
	{
		//alert("in doPopupTimer is:"+err.description);
	}

}
function changeCategoryClass(category_div_id)
{
	try
	{
		for(var i=0;i<category_div_ids_array.length;i++)
		{	
			var dividSplit=category_div_ids_array[i].split("_");
			var CCCdivId =category_div_ids_array[i];
			var CCCspanTxt = "spanTxt_"+dividSplit[1] ;
			var CCCspanArrow = "spanArrow_"+dividSplit[1];
			if(category_div_id==category_div_ids_array[i])
			{
				document.getElementById(CCCdivId).className="LeftCat_Sel";
				document.getElementById(CCCspanTxt).className="LeftTxt_Sel";
				document.getElementById(CCCspanArrow).className="LeftSelArrow";
			}
			else
			{
				document.getElementById(CCCdivId).className="LeftCat";
				document.getElementById(CCCspanTxt).className="LeftTxt";
				document.getElementById(CCCspanArrow).className="LeftArrow";
			}
		}
	}catch(e)
	{
	}
}
function showCategoryPopUp()
{
	if(is_time_count_started!=0)
	{
		try
		{
			is_time_count_started=0;
			clearTimeout(category_popup_timer_obj);
		}catch(e)
		{
		}
	}
	if(popUp_category_div_id !='')
	{
		//alert("Inside showCategoryPopUp ::84");
		try
		{
			changeCategoryClass(popUp_category_div_id);
			var tdOffset=getBrowserOffsetLeft();
			var divIdsplit=popUp_category_div_id.split("_");
			var tdId="td_"+divIdsplit[1];
			var tdObj=document.getElementById(tdId);
			var screenX =tdOffset+ tdObj.offsetLeft+164; 
		
			is_popup_generated=true;
			var showDividSplit=popUp_category_div_id.split("_");
			var popupSpanTxt = "spanTxt_"+showDividSplit[1] ;
			var categoryName=document.getElementById(popupSpanTxt).innerHTML;
			var categoryId = showDividSplit[1];
			var selectTag=document.getElementsByTagName("select");
			for(var i=0;i<selectTag.length;i++)
			{
				selectTag[i].style.display='none';
			}
			document.getElementById('categoryContent').innerHTML="";
			document.getElementById('mmaddSense2').innerHTML="";
			document.getElementById('cat_PopUp_div').style.visibility="visible";
			document.getElementById('cat_PopUp_div').style.zIndex="2";
			document.getElementById('cat_PopUp_div').style.left=screenX+"px";
			document.getElementById('categoryHeader').innerHTML=categoryName;
			getCategoryPopUpContent(categoryId,categoryName);
			mm_ad_context=categoryName;
			mm_ad_contexType='category';
			showAdds('mmaddSense2',1);
		}
		catch (err)
		{
			//alert("Inside showCategoryPopUp ::"+err.description);
		}
	}
}

function showCatPopup()
{
	document.getElementById('cat_PopUp_div').style.visibility="visible";
}
function hidePopUp(category_Name)
{
	try
	{	
		var categoryHeader = document.getElementById('categoryHeader').innerHTML;
		if(category_Name != categoryHeader)
		{
			if(is_time_count_started!=0)
			{
				is_time_count_started=0;
				clearTimeout(category_popup_timer_obj);
			}
			if(is_popup_generated==true)
			{
				for(var i=0;i<category_div_ids_array.length;i++)
				{	
					var dividSplit=category_div_ids_array[i].split("_");
					var hidedivId =category_div_ids_array[i];
					var hidespanTxt = "spanTxt_"+dividSplit[1] ;
					var hidespanArrow = "spanArrow_"+dividSplit[1];
					document.getElementById(hidedivId).className="LeftCat";
					document.getElementById(hidespanTxt).className="LeftTxt";
					document.getElementById(hidespanArrow).className="LeftArrow";
					if(two_baners_enabled)
					{
						clearTimeout(two_baners);
					}
				}
				document.getElementById('categoryContent').innerHTML="";
				document.getElementById('mmaddSense2').innerHTML="";
				popUp_category_Name ='';
				//document.getElementById('cat_PopUp_div').style.visibility="hidden";
				//document.getElementById('cat_connector_div').style.visibility="hidden";
			}
		}
	}
	catch(err)
	{
		//alert("Inside hidePopUp ::"+err.description);
	}
}

function hideOnClick()
{
	try
	{
		clearTimeout(category_popup_timer_obj);
		var selectTag=document.getElementsByTagName("select");
		for(var i=0;i<selectTag.length;i++)
		{
			selectTag[i].style.display='block';
		}
		for(var i=0;i<category_div_ids_array.length;i++)
		{	
			var dividSplit=category_div_ids_array[i].split("_");
			var hidedivId =category_div_ids_array[i];
			var hidespanTxt = "spanTxt_"+dividSplit[1] ;
			var hidespanArrow = "spanArrow_"+dividSplit[1];
			document.getElementById(hidedivId).className="LeftCat";
			document.getElementById(hidespanTxt).className="LeftTxt";
			document.getElementById(hidespanArrow).className="LeftArrow";
			if(two_baners_enabled ==1)
			{
				clearTimeout(two_baners);
			}
		}
		document.getElementById('categoryContent').innerHTML="";
		document.getElementById('mmaddSense2').innerHTML="";
		popUp_category_Name ='';
		is_popup_generated = false;
		document.getElementById('cat_PopUp_div').style.visibility="hidden";
	}catch(e)
	{
	}
	//document.getElementById('cat_connector_div').style.visibility="hidden";
}
function getCategoryPopUpContent(categoryId,categoryName)
{
	if(popUp_category_div_id !='')
	{
		try
		{
			var xmlHttp = getXMLHTTP();
			var url = "/business/getCategoryPopUp?ajax_categoryId="+categoryId+"&ajax_categoryName="+categoryName;
			if(xmlHttp) 
			{
				xmlHttp.onreadystatechange = function()
				{
					if (xmlHttp.readyState == 4) 
					{
						if (xmlHttp.status == 200)
						{	
							category_Name = popUp_category_Name.replace('&','&amp;')
							//alert("category_Name is :::"+category_Name +" categoryName is:::"+categoryName)
							if(category_Name == categoryName)
							{							
								document.getElementById('categoryContent').innerHTML=xmlHttp.responseText;
							}
						}
						else 
						{
							//alert("There is a problem while getting data" + xmlHttp.responseText);
						}
					}
				} //end of function
				xmlHttp.open("GET", url , true);
				xmlHttp.send(null);
			}
		}catch(e)
		{
		}
	}
}

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function getCusrsorPositions(event)
{
	if(category_popup_timer_obj)
	{
		var x=parseInt(mouseX(event));
		var y=parseInt(mouseY(event));
		var xpos = parseInt(getBrowserOffsetLeft());
		var ypos = parseInt(getBrowserOffsetTop());
		if(x<(164 + xpos) && y>(355))
		{
			hideOnClick();
		}
		else if(x>(0+xpos) && x<(797+xpos) && y>(170) && y<(553))
		{
			//alert(x);
		}
		else
		{
			hideOnClick();
		}
	}

	//alert("position: ("+x+","+y+")");
}