var bioType = '';

$(document).ready(function() {
	var profileId=$('#hidProfileId').val();
	var postURL = "index.php";   

	
	

	/*Start- Map Functions */

	//map Functions
	$('#lnkLargeMap').click(function(){

		document.frmMap.submit();
		return false;
	});

	$('#lnkDirectionMap').click(function(){

		$('#divDirectionMap').show();
		return false;
	});
	$('#lnkCloseDirectionMap').click(function(){
		$('#divDirectionMap').hide();
		return false;
	});

	
	
	//end Map functions
 });/*end document.ready*/
	
	
 function displayMapLinks(latitude,longitude,address){
 	parent.document.getElementById('hidLongitude').value=longitude;
 	parent.document.getElementById('hidLongitudeDirections').value=longitude;
 	parent.document.getElementById('hidLatitude').value=latitude;
 	parent.document.getElementById('hidLatitudeDirections').value=latitude;
 	parent.document.getElementById('hidAdress').value=address;
 	parent.document.getElementById('fromAddress').value=address;
 	parent.document.getElementById('mapLinks').style.display='block';
 }
 
	function swap_direction() {
	
		var source_text = $.trim($('#fromAddress').val());
		var dest_text = $.trim($('#toAddress').val());
		if($('#hidFlagUserAddUsedIn').val()=='from')
		{
			$('#hidFlagUserAddUsedIn').val("to")
			$('#fromAddress').removeAttr("readonly");
			$('#toAddress').attr("readonly", "readonly");
		}else if($('#hidFlagUserAddUsedIn').val()=='to')
		{
			$('#hidFlagUserAddUsedIn').val("from")
			$('#toAddress').removeAttr("readonly");
			$('#fromAddress').attr("readonly", "readonly");
		}
	
		$('#fromAddress').val(dest_text);
		$('#toAddress').val(source_text);
	
	
	}
	
	function chkFrmDir(){
		if(($.trim($('#fromAddress').val()) ) == "" )
		{
			$('#fromAddress').select();
			return false;
		}
		if(($.trim($('#toAddress').val()) ) == "" )
		{
			$('#toAddress').select();
			return false;
		}
	}

	/*End- Map Functions */


	/* Start - Context Menu*/

	var positionX = 0;
	var positionY = 0;
	var blFirst;
	
	function getElement(obj, blF, bUrl){	
		arrPosition = findPosition(obj);
		blFirst = blF;
	//	str = obj.childNodes[1].innerHTML;
	//alert(obj +' -   '+ blF +' -   '+  bUrl);
	//	alert(str.length);
	//	parseInt(arrPosition[0])+(parseInt(str.length)*5)
		positionX = arrPosition[0];
	//	alert('s'+positionX);
		positionY = arrPosition[1]+20;
	
		$('#myMenu').attr("innerHTML",'');
		var strSiteUrl=$.trim($('#hidSiteURL').val());
	 	var postURL = strSiteUrl+"profile/index.php"; //as its called from builtup also
		var beid = obj.id;
	
		if(beid == ""){
			return false;
		}
	
		arrDetail = beid.split('_');
		bioType = arrDetail[0];
	
		var bEleValue = $.trim($(obj).text());
		bEleValue=bEleValue.replace("\n","");
		bEleValue=bEleValue.replace("\n","");
	//	if(bioType=='st')
	//{
	//	alert(bEleValue);
	//}
		if(bioType=='d')
		{
			bEleValue=$('#hidDOB').val();
		}
		if(bioType=='st')//for including house number with street name
		{
			bEleValue=$('#hidStreet').val();
		}
		$('#posx').attr('value',positionX);
		$('#posy').attr('value',positionY);
		$.ajax({
			cache:false,
			async:false,
			type: "POST",
			dataType:"text",
			data: "action=ajaxGetContextMenu&beid=" + arrDetail[1]+"&type="+arrDetail[0]+"&elevalue="+bEleValue,
			url: postURL,
			success: processContextMenu
		});
	}
	
	function processContextMenu(rawXML){
	var strSiteUrl=$.trim($('#hidSiteURL').val());
//		alert(strSiteUrl);
		if(rawXML)	{
		xmlDoc = createXMLDocument(rawXML);
			if(xmlDoc){
				var pId=$('#hidProfileId').val();
				var starSignId = $('#hidStarSign').val();
				var municipalityName = $('#hidMunicipality').val();
				var countyName = $('#hidCounty').val();
				var strSiteUrl=$.trim($('#hidSiteURL').val())+"profile/";
				var contextItemValue = $(xmlDoc).find('biographyElementValue').text();
				var cOptions="";
	
				$(xmlDoc).find('item').each(function(){
					 var contextMenu=$(this).find('contextMenu').text();
					 var isCount=$(this).find('isCount').text();
					 var type=$(this).find('type').text();
					 var cAction=$(this).find('action').text();
					 var rCount=$(this).find('rCount').text();
	
	
					 if(isCount == 1){
						 if(rCount > 0){
		//					itemRow = contextMenu +" ("+rCount+")"; //commented bcuz we are not showing name count
							itemRow = contextMenu;
						 }else{
						 	itemRow = contextMenu;
						 }
					 }else{
					 	itemRow = contextMenu;
					 }
	
		//			 if(isLink == 1){
						if(cAction.length > 1){
//							alert(cAction); 
							if(type == 'f'){ //facebox
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;txtKeyword="+contextItemValue+"&amp;show=popup' rel='facebox' onclick='javascript:closeContextMenu(\"myMenu\");setpos(0,200,200);'>"+itemRow+"</a></em>";
							}else if(type == 'd'){ //DOB
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;bdate="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 's'){ //star sign
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;ssid="+starSignId+"&amp;ss="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 'st'){ //street
//								contextItemValue = 'hello+how+k98'
								arrAddress = contextItemValue.split('+');
								
//								if(isNaN(parseInt(arrAddress[arrAddress.length - 1]))){
//									houseNumber = '';
//								}else{
//									houseNumber = arrAddress[arrAddress.length - 1];									
//								}

								if(isNaN(parseInt(arrAddress[arrAddress.length - 1])) && (arrAddress[arrAddress.length - 1].length == 1)){
									entrance = arrAddress[arrAddress.length - 1];
									houseNumber = arrAddress[arrAddress.length - 2];
									streetLengthArr = arrAddress.length - 2;
								}else{
									entrance='';
									if(!isNaN(parseInt(arrAddress[arrAddress.length - 1]))){
										houseNumber = arrAddress[arrAddress.length - 1];
										streetLengthArr = arrAddress.length - 1;
									}else{
										houseNumber = '';
										streetLengthArr = arrAddress.length;
									}
								}
								
								arrAddress1 = new Array();
								
								for(i=0; i<streetLengthArr; i++ ){
									arrAddress1[i] = arrAddress[i];
								}
								address = arrAddress1.join('+');
								
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;chkCounty[]="+countyName+"&amp;chkMunicipality[]="+municipalityName+"&amp;txtAddress="+address+"&amp;HouseNumber="+houseNumber+"&amp;entrance="+entrance+"'>"+itemRow+"</a></em>";
							}else if(type == 'ln'){ //street
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;chkLastName[]="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 'fn'){ //street
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;chkFirstName[]="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 'mu'){ //street
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;chkMunicipality[]="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 'cn'){ //street
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;chkCounty[]="+contextItemValue+"'>"+itemRow+"</a></em>";
							}else if(type == 'ct'){ //city
								itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;action=advancedSearchFilter&amp;PostalArea="+contextItemValue+"'>"+itemRow+"</a></em>";	
							}else{
					 			itemRow = "<em><a href='"+strSiteUrl+cAction+"&amp;id="+pId+"&amp;txtKeyword="+contextItemValue+"'>"+itemRow+"</a></em>";
							}
					 	}else{
					 		itemRow = "<em class=LinkList>"+itemRow+"</em>";
					 	}
		//			 }else{
		//			 	itemRow = "<em class='linkstyle'>"+itemRow+"</em>";
		//			 }
	
					 cOptions += "<li>"+itemRow+"</li>";
	
		         }); //close each(
	
		         if(cOptions == ''){
					$('#myMenu').hide();
					return false;
		         }else{
		         	
		//         	alert(positionY);
		//         	alert(positionX);
		//			positionX = $('#posx').attr('value');
		//			positionY = $('#posy').attr('value');
			         $("#bioElements > span").showMenu({
		//				opacity:0.9,
		//				top:positionY,
		//				left:positionX,
						query: "#myMenu"
					});
					$('#myMenu').html("<ul class='menu_body'>"+cOptions+"</ul>");
				}
				if(blFirst != 1){
					$('a[rel*=facebox]').facebox();
				}
			}
	
		}
	
	}
	
	
	/* End - Context Menu*/
	function createXMLDocument(rawXML)
	{
		if(rawXML)
		{
			//xmlDoc=rawXML.documentElement;
			try //Internet Explorer
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(rawXML);
				return xmlDoc;
			}
			catch(e)
			{
				try //Firefox, Mozilla, Opera, etc.
				{
					parser=new DOMParser();
					xmlDoc=parser.parseFromString(rawXML,"text/xml");
					return xmlDoc;
				}
				catch(e) {alert(e.message)}
			}
		}
	}