var ajax_response;
function ajax(requestType, url, param, callbackFunc, divId) {  
var xmlHttp;
try {    // Firefox, Opera 8.0+, Safari    
		xmlHttp=new XMLHttpRequest();    
	}  
	catch (e) {    // Internet Explorer    
		try	{      
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
   		 catch (e) {      
		 	try {        
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
      		catch (e) {        
				//alert("Your browser does not support AJAX!");        
				return false;       
			 }
		}
	} 	 
	 xmlHttp.onreadystatechange=function()
	  {
	  if(xmlHttp.readyState==4)
	    {
	   	 	// Get the data from the server's response
			ajax_response=xmlHttp.responseText;
			switch(callbackFunc){
				case 'replaceDivContent':
					replaceDivContent(ajax_response, divId);
					break;
				case 'displayLodgingData':
					displayLodgingData(ajax_response, divId);
					break;
				case 'doNothing':
					doNothing(ajax_response);
					break;
				default:
					doNothing(ajax_response);
			}		
	    }
	  }
	if(requestType == 'POST'){
		// Post Request
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlHttp.setRequestHeader("Content-length", param.length);
	    xmlHttp.setRequestHeader("Connection", "close");
	    xmlHttp.send(param);
	} else {
		// Get Request
		var geturl = url + '?' + param;
		xmlHttp.open("GET", geturl, true);
		xmlHttp.send(null);
	}
 }

 function doNothing(ajax_response){
 	// empty function
	 //alert(ajax_response);
 }
 
 function replaceDivContent(resp, divId){
 	if(resp != ''){
 		document.getElementById(divId).innerHTML = resp;
	}
 	document.getElementById(divId).style.display = 'block';
 }
 
 function showPhoneNumber(thing_id, divId){
 	var url;
	var param = 'thing_id=' + thing_id + '&click_type=phone';
	url = '/utah_logs/thing_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//document.write(url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function showTollFreeNumber(thing_id, divId){
 	var url;
	var param = 'thing_id=' + thing_id + '&click_type=tollphone';
	url = '/utah_logs/thing_click_log.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//document.write(url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function displayVideoListingPhoto(place_id, activity_id, video_thing, divId){
 	var url;
	var param = 'place_id=' + place_id + '&activity_id=' + activity_id + '&video_thing=' + escape(video_thing);
	url = '/video/getVideoListingPhoto.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, 'replaceDivContent', divId);
 }
 
 function displayVideoListing(video_id){
 	var content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="406" height="600" id="video_interface" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="flashVars" value="videoId=' + video_id + '" /><param name="movie" value="video_interface_embedded2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="video_interface_embedded2.swf" flashVars="videoId=' + video_id + '" quality="high" bgcolor="#000000" width="406" height="600" name="video_interface" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	replaceDivContent(content, 'photoDiv')
 }
 
 function displayVideoListing2(video_id){
 	var content = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="406" height="600" id="video_interface" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="flashVars" value="videoId=' + video_id + '" /><param name="movie" value="/video/video_interface_embedded2.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" /><embed src="/video/video_interface_embedded2.swf" flashVars="videoId=' + video_id + '" quality="high" bgcolor="#000000" width="406" height="600" name="video_interface" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	replaceDivContent(content, 'video-shell')
 }
 
 function logVideoImpression(video_id, ad_video_id, player_type){
  	var url;
	var param = 'video_id=' + video_id + '&ad_video_id=' + ad_video_id + '&player_type=' + player_type + '&imp=1';
	url = '/video/logVideoImpression.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert(url + '?' + param);
	ajax('POST', url, param, 'doNothing', '');
 }
 
 function logVideoClick(video_id, ad_video_id){
  	var url;
	var param = 'video_id=' + video_id + '&ad_video_id=' + ad_video_id + '&clk=1';
	url = '/video/logVideoImpression.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, 'doNothing', '');
 }
 
 var replaced = new Array();
 
 function restoreReplaced(cur_id){
 	// Loop through all divs
	var listingDivs = document.getElementById("lodginglist").getElementsByTagName("DIV");
	for(var i=0; i < listingDivs.length; i++){
		id = listingDivs[i].id.replace("content_","");
		if((replaced[id] && replaced[id] != 'empty') && id != cur_id){
			listingDivs[i].innerHTML = replaced[id];
 			listingDivs[i].style.display = 'block';
		} else {
			if(id && id != cur_id && replaced[id]){
				listingDivs[i].innerHTML = '';
 				listingDivs[i].style.display = 'block';
			}
		}
	}
 }
 
 function getLodgingData(id, type){
 	restoreReplaced(id); 
 	var url;
	var param = 'id=' + id + '&type=' + type;
	url = '/database/getLodgingData.php';
	var ucdate = new Date();
	var timestamp = Math.random();
	param = param + '&time=' + timestamp;
	//alert('Div: ' + divId + ' ' + url + '?' + param);
	ajax('POST', url, param, 'displayLodgingData', id);
 }
 
 function displayLodgingData(resp, id){
 	divId = "content_" + id;
 	if(resp != ''){
		if(replaced[id] == undefined){
			var html = document.getElementById(divId).innerHTML;
			if(html != ''){
				replaced[id] = html;
			} else {
				replaced[id] = 'empty';
			}
		}
 		document.getElementById(divId).innerHTML = resp;
	}
 	document.getElementById(divId).style.display = 'block';
 }