	function RunVirtualTour(start_position_val){				
		$("img.advancedpanorama").panorama({
		                auto_start: vtour_autostart,
	                    start_position: start_position_val,
	                    mode_360: true,
	                    viewport_width: vtour_viewportwidth,
             			speed: $("img.advancedpanorama").attr("width")*7,
             			direction: 'left',
             			control_display: vtour_control_display
		         });		      
	}
	
	function StartSelection(){
		$('img.advancedpanorama').imgAreaSelect({				
     			outerOpacity: .4,
     			selectionOpacity: .1,
      			handles: true,
				onSelectEnd: ProcessSelection,				
				onSelectChange: RemoveButton
		});
	}
	function RemoveButton(img, selection){
		var need_correlate = $("#use_non_correlation").val();
		$("div#add_photo_link").remove();		
		$("div#add_panorama_link").remove();
		$("div#add_swf_link").remove();
		if (need_correlate!=0){
			$("div#correlate_link").remove();
		}
	}
	
	function ProcessSelection(img, selection){
		$("#x1").val(selection.x1);
		$("#x2").val(selection.x2);
		$("#y1").val(selection.y1);
		$("#y2").val(selection.y2);
		
		var left_pos = $("#selected_area").offset().left;	   			
		var top_pos = $("#selected_area").offset().top;
		var need_correlate = $("#use_non_correlation").val();				
		if( !$("div#add_photo_link").length){				
   			$("body").append('<div id="add_photo_link" style="padding:5px; z-index:5; position:absolute; left:'+left_pos+'px; top:'+top_pos+'px;"><table><tr><td><div class="fileinputs"><form method="POST" enctype="multipart/form-data" onsubmit="return false;"><input type="file" name="photo" id="photo" class="file" style="margin-left: -117px;"><div class="fakefile"><input type="button"  class="btn_upload" value="'+add_photo_text+'"></div></form></div></td><td style="padding-left:20px;"><span id="load_status" class="error" style="color:white;"></span></td></tr></table></div>');   			
   			$("body").append('<div id="add_panorama_link" style="padding:5px; z-index:5; position:absolute; left:'+left_pos+'px; top:'+(top_pos+20)+'px;"><table><tr><td><div class="fileinputs"><form method="POST" enctype="multipart/form-data" onsubmit="return false;"><input type="file" name="panorama" id="panorama" class="file"  style="margin-left: -117px;"><div class="fakefile"><input type="button"  class="btn_upload" value="'+new_panorama_text+'"></div></form></div></td><td style="padding-left:20px;"><span id="panorama_load_status" class="error" style="color:white;"></span></td></tr></table></div>');
   			$("body").append('<div id="add_swf_link" style="padding:5px; z-index:5; position:absolute; left:'+left_pos+'px; top:'+(top_pos+40)+'px;"><table><tr><td><div class="fileinputs"><form method="POST" enctype="multipart/form-data" onsubmit="return false;"><input type="file" name="swf" id="swf" class="file"  style="margin-left: -117px;"><div class="fakefile"><input type="button"  class="btn_upload" value="'+new_swf_file+'"></div></form></div></td><td style="padding-left:20px;"><span id="swf_load_status" class="error" style="color:white;"></span></td></tr></table></div>');
   			if (need_correlate != 0){
				$("body").append('<div id="correlate_link" style="padding:5px; z-index:5; position:absolute; left:'+left_pos+'px; top:'+(top_pos+60)+'px;"><table><tr><td><input type="button" id="correlate"  class="btn_upload" value="'+correlate_text+'"></td></tr></table></div>');
				$("#correlate").bind("click", function(){
	   				Correlate();
	   			});		
			}
			
   			$("#photo").bind("change", function(){
   				UploadFile('upload_vt_photo', this.form.photo, id_ad, 'load_status', $("#panorama_pic_id").val());
   			});	
   			$("#panorama").bind("change", function(){
   				UploadFile('upload_new_panorama', this.form.panorama, id_ad, 'panorama_load_status', 0);
   			});		   			
   			$("#swf").bind("change", function(){
   				UploadFile('upload_swf', this.form.swf, id_ad, 'swf_load_status', $("#panorama_pic_id").val());
   			});	
		}		
		if (jQuery.browser.opera){
			document.getElementById("selected_area").style.opacity = ".1";
		}		
	}
	
	function Correlate(){
		var id_panorama_from = $(".advancedpanorama").attr("id");
		var id_panorama_to = $("#id_panorama_from").val();				
		$.ajax({
	   type: "POST",
	   url: site_root + "/rentals.php",
	   data: "sel=correlate&id_ad="+id_ad+"&id_panorama_from="+id_panorama_from+"&id_panorama_to="+id_panorama_to+"&x1="+$("#x1").val()+"&x2="+$("#x2").val()+"&y1="+$("#y1").val()+"&y2="+$("#y2").val(),
	   success: function(msg){	   		
	   		UpdatePanoramaPreview(id_ad, id_panorama_from, "virtual_tour_section", 2, 0);        		
	   	}
		});
	}
	function UploadFile(sel, value, id_ad, result_id, id_change) {	
	var id_panorama = $(".advancedpanorama").attr("id");	
    var req = new JsHttpRequest();    
    destination = $('#'+result_id);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {       
        	if (req.responseText.substr(0,11) == '||success||'){ 	
        		destination.html(req.responseText.substr(11,req.responseText.length)); 
        		if ((sel == "upload_panorama" || sel == "upload_swf")){			    	
			    	UpdatePanoramaPreview(id_ad, 0, "virtual_tour_section", 2, 0);        		
			    }else if(sel == "upload_new_panorama"){			    	   		
			    	UpdatePanoramaPreview(id_ad, 0, "virtual_tour_section", 3, 0);			    	
			    }else if(sel == "upload_swf_panorama"){			    	   		
			    	UpdatePanoramaPreview(id_ad, 0, "virtual_tour_section", 4, 0);			    	
			    }else{			    	
        			UpdatePanoramaPreview(id_ad, id_panorama, "panorama_preview", 1, 0);        		
			    }
        	}else{
        		destination.html(req.responseText);        	
        	}
        }else{        	
        	destination.html(indicator+'&nbsp;');
        }
    }    
    if (sel == "upload_panorama" || sel == "upload_swf_panorama"){    	
    	req.open(null, site_root + '/rentals.php?sel='+sel+'&id_ad='+id_ad+"&id_change="+id_change, true);
    }else if(sel == "upload_vt_photo" || sel == "upload_swf"){    	
    	req.open(null, site_root + '/rentals.php?sel='+sel+'&id_ad='+id_ad+"&id_change="+id_change+"&x1="+$("#x1").val()+"&x2="+$("#x2").val()+"&y1="+$("#y1").val()+"&y2="+$("#y2").val(), true);
    }else if(sel == "upload_new_panorama"){
    	req.open(null, site_root + '/rentals.php?sel='+sel+'&id_ad='+id_ad+"&id_change="+id_change+"&id_from="+id_panorama+"&x1="+$("#x1").val()+"&x2="+$("#x2").val()+"&y1="+$("#y1").val()+"&y2="+$("#y2").val(), true);
    }
    req.send( { q: value } );
}
	
	function UpdatePanoramaPreview(id_ad, id_panorama, div_id, mode, id_panorama_from){
		if (with_icon){
			url_ = site_root + "/rentals.php";
		}else{
			url_ = site_root + "/viewprofile.php";
		}		
		$.ajax({
	   type: "POST",
	   url: url_,
	   data: "sel=update_preview&mode="+mode+"&id_ad="+id_ad+"&id_panorama="+id_panorama+"&id_panorama_from="+id_panorama_from,	   
	   success: function(msg){	   		
	   		var msg_arr = msg.split('__');	   		
	   		if (mode != 4){
				if ($("#vt_map") && id_panorama_from == 0 && id_panorama !=0){
					position = 0 - $(".panorama-container").css("margin-left").replace("px","");						
				}else if (id_panorama==0){
					position = Number(msg_arr[0])/3;
					if (msg_arr[0]<1000){
						position = 0;
					}
				}else{
					position = Number(msg_arr[0])-Math.round($(".panorama-viewport").width()/2);				
				}			
	   		}
			$("#"+div_id).css("display","inline");
	   		$("#"+div_id).html(msg_arr[1]);			
			$("div").each(function () {
			 if( $(this).css("position") == "absolute" && $(this).attr("class") != "fakefile") {
			  $(this).remove();			  
			 }
			});						
			
			if (mode != 4){
				if (position < 0){
					position = 0  - position;
				}
		   		RunVirtualTour(position);	   		
		   		$(".panorama-viewport").advancedpanorama();	   		
		   		tb_init('a.thickbox, area.thickbox, input.thickbox');
		   		if (with_icon){
		   			StartSelection();	   		
		   			CreateIcon();
		   		}
		   		CreateCorrelation(1);
		   		$(".panorama-area-2").each(function () {	   				   			
						 if($(this).css("left") == (Number(msg_arr[0]) - Math.round($(this).width()/2))+"px") {
						  $(this).css("background-color", "red");
						 }
						});	
			}else{				
				$('#swf_panorama').flashembed($('#swf_panorama').attr("title"));
				CreatePreviewButton();
			}
	   }
	   });   
	}
	
	function LoadAlt(type, id, alt_id, status_id, id_ad, msg_success){
		var id_panorama = $(".advancedpanorama").attr("id");
		if (status_id){
			$("#"+status_id).html(indicator);
		}
		$.ajax({
	   type: "POST",
	   url: site_root + "/rentals.php",
	   data: "sel=update_alt&type="+type+"&photo_id="+id+"&alt="+$("#"+alt_id).val()+"&id_ad="+id_ad,	   
	   success: function(msg){
	   		if (msg_success){
	   			$("#"+status_id).html(msg_success);		   		
	   		}
	   		UpdatePanoramaPreview(id_ad, id_panorama, "panorama_preview", 1, 0);
	   }
	   });   
	}
	
	function DeletePhoto(type, id, id_ad){
	   var id_panorama = $(".advancedpanorama").attr("id");	   
	   $.ajax({
	   type: "POST",
	   url: site_root + "/rentals.php",
	   data: "sel=delete_photo&type="+type+"&photo_id="+id+"&id_ad="+id_ad,	   
	   success: function(msg){	   		
	   		switch (type){
	   			case "vt_photo":
	   				UpdatePanoramaPreview(id_ad,id_panorama, "panorama_preview", 1, 0);
	   			break;	
	   			case "panorama":
		   			if (id_panorama != id){
		   				UpdatePanoramaPreview(id_ad,id_panorama, "panorama_preview", 1, 0);
		   			}else{		   				
	   					if (use_link_modifications == 1){	   					   				
	   						document.location = site_root + '/my_listings/'+id_ad+'/tour/';		   			
	   					}else{
	   						document.location = site_root + '/rentals.php?sel=virtual_tour&id_ad='+id_ad;		   			
	   					}
		   			}
	   			break;	
	   			case "swf_panorama":	
	   					if (use_link_modifications == 1){	   					   				
	   						document.location = site_root + '/my_listings/'+id_ad+'/tour/';		   			
	   					}else{
	   						document.location = site_root + '/rentals.php?sel=virtual_tour&id_ad='+id_ad;		   			
	   					}
	   			break;	
	   		}
	   		
	   }
	   });   
	}
	
	function CreateIcon(){
		global_left_pos = $(".panorama-container").offset().left;
   		global_top_pos = $(".panorama-container").offset().top;  
   		var need_correlate = $("#use_non_correlation").val();
   		
   		if (need_correlate==0){
   			corner_left_pos = $(".panorama-viewport").offset().left;
   			corner_top_pos = $(".panorama-viewport").offset().top;     		 		
   			$("body").append("<div style=' z-index:4; position:absolute; left:"+corner_left_pos+"px; top:"+corner_top_pos+"px;'><input id='preview_button' class='btn_small' type='button' value='"+preview_text+"'></div>");   			
   			$("#preview_button").click(function(){document.location = site_root+ '/viewprofile.php?view=vtour&id='+id_ad;});
   		}
   		
   		$("a[class^='panorama-area']").each(function() {
   			var id = $(this).attr("id").replace("vt_photo_","");
   			if (id.substr(0,3) == "cor"){
   				var id_panorama_to_arr = new Array();
				id_panorama_to_arr = $(this).attr("href").split("/");			
				var id_panorama_to = id_panorama_to_arr[id_panorama_to_arr.length-1];					
   			}
   			var left_pos = $(this).offset().left - global_left_pos +5;	   			
			var top_pos = $(this).offset().top - global_top_pos +5;	
   			$(".panorama-container").append("<div style=' z-index:4; position:absolute; left:"+left_pos+"px; top:"+top_pos+"px;'><a><img style='height:12px;' src='"+delete_png_src+"' id='icon_delete_"+$(this).attr("id")+"'></a></div>");
   			$(".panorama-container").append("<div style=' z-index:4; position:absolute; left:"+(left_pos+16)+"px; top:"+top_pos+"px;'><a><img style='height:12px;' src='"+edit_png_src+"' id='icon_edit_"+$(this).attr("id")+"'></a></div>");
   			$("#icon_delete_"+$(this).attr("id")).bind("mousemove", function(){
   				$(this).css("height", "16px");}).bind("mouseout", function(){
   				$(this).css("height", "12px");}).attr("alt", alt_delete).attr("title", alt_delete);
   			
   			if (id.substr(0,3) == "cor"){
   				$("#icon_delete_"+$(this).attr("id")).bind("click", function(){
   				if (confirm(sure_text)){   					   					
		   				DeletePhoto("panorama", id_panorama_to, id_ad);   				   			
   				}
   				});
   			}else{
   				$("#icon_delete_"+$(this).attr("id")).bind("click", function(){
   				if (confirm(sure_text)){   					
		   				DeletePhoto("vt_photo", id, id_ad);
   				}
   				});
   			}   				
   			
   			$("#icon_edit_"+$(this).attr("id")).bind("mousemove", function(){
   				$(this).css("height", "16px");}).bind("mouseout", function(){
   				$(this).css("height", "12px");}).bind("click", function(){   				
   				if ($("#edit_title_div_"+id).length > 0){
   					$("#edit_title_div_"+id).remove();
   				}else{
   					$(".panorama-container").append("<div id='edit_title_div_"+id+"' style=' z-index:4; position:absolute; left:"+(left_pos+16)+"px; top:"+(top_pos +16)+"px; margin-right:5px;'><div style='float:left;'><input type='text' class='str' style='width:200px;' id='edit_title_"+id+"' value='"+$("#vt_photo_"+id).attr("title")+"'></div><div id='save_box_"+id+"'><input type='button' class='btn_small' style='height:18px;' value='"+save_text+"' id='save_title_"+id+"'></div></div>");
   					if (id.substr(0,3) == "cor"){
		   				$("#save_title_"+id).bind("click", function(){LoadAlt("correlation", id.replace("cor_",""), "edit_title_"+id, "save_box_"+id, id_ad, '')});   				
		   			}else{
   						$("#save_title_"+id).bind("click", function(){LoadAlt("vt_photo", id, "edit_title_"+id, "save_box_"+id, id_ad, '')});
		   			} 
		   			$("#edit_title_"+id).bind("keypress", function(e){if (e.keyCode == 13) $("#save_title_"+id).click();});  					
   				}
   				
   			}).attr("alt", alt_edit_title).attr("title", alt_edit_title);   			
   		});
	}
	
	function CreateCorrelation(ajax){		
		var id_panorama_from = $(".advancedpanorama").attr("id");		
		$("a[class^='panorama-area-2']").each(function() {
			var id_panorama_to_arr = new Array();
			id_panorama_to_arr = $(this).attr("href").split("/");			
			var id_panorama_to = id_panorama_to_arr[id_panorama_to_arr.length-1];					
			if (ajax){
				$(this).removeAttr("href","");
				$(this).bind("click", function(){
					if (with_icon){						
						UpdatePanoramaPreview(id_ad, id_panorama_to, "virtual_tour_section", 2, id_panorama_from);			
					}else{
						vtour_autostart = 1;						
						UpdatePanoramaPreview(id_ad, id_panorama_to, "panorama_preview", 1, id_panorama_from);
					}
				});
			}else{
				$(this).attr("href", site_root + "/rentals.php?sel=virtual_tour&id_ad="+id_ad+"&id_panorama="+id_panorama_to);
			}			
		});
	}
	
	function CreatePreviewButton(){
		var left_pos = $("#swf_panorama").offset().left + $("#swf_panorama").width()-63;
   		var corner_top_pos = $("#swf_panorama").offset().top - 20;     		 		
   		$("body").append("<div style=' z-index:4; position:absolute; left:"+left_pos+"px; top:"+corner_top_pos+"px;'><input id='preview_button' class='btn_small' type='button' value='"+preview_text+"'></div>");   			
   		$("#preview_button").click(function(){
   			if (use_link_modifications){
   				document.location = site_root+ '/'+id_ad+'/vtour/';
   			}else{
   				document.location = site_root+ '/viewprofile.php?view=vtour&id='+id_ad;
   			}
   		});	
	}