﻿var visualhotels_xml = null;

var visualhotels_touractive = false;

var visualhotels_words = {
	'ru': ['Виртуальный тур', 'Смотреть виртуальный тур', 'Все права защищены', 'На весь экран', 'Обычный вид', 'Закрыть'],
	'en': ['Virtual tour', 'Watch a virtual tour', 'All rights reserved', 'Full screen', 'Normal', 'Close'],
	'gr': ['Virtual tour', 'Watch a virtual tour', 'All rights reserved', 'Full screen', 'Normal', 'Close']
};

var visualhotels_virtualtour = [];
var visualhotels_previews = [];
var visualhotels_hoteldata = {'country':'','region':'','city':'','name':'','stars':''};

var visualhotels_panonum = 0;

var visualhotels_panoramawidth = 480;
var visualhotels_panoramaheight = 360;

var visualhotels_section = null;
var visualhotels_subsection = null;
var visualhotels_panorama = null;

var visualhotels_previewscount;
var visualhotels_previewsinrow;

var visualhotels_isfull = false;

var visualhotels_currentsrc = '';

function VH_FullScreen(a)
{
	if ( !visualhotels_isfull )
	{
		visualhotels_isfull = true;
		try {
			var vnormal = visualhotels_words.ru[4];
			a.innerHTML = vnormal;
		}
		catch (e){}
		
		var ml = document.getElementById('visualhotels-virtual-tour');
		var ic = document.getElementById('visualhotels-inner-container');
		var pc = document.getElementById('panorama-container');
		
		ml.style.top = '0px';
		ml.style.left = '0px';
		
		var width = VH_GetW();
		var height = VH_GetH();
		
		visualhotels_panoramawidth = width - 222;
		visualhotels_panoramaheight = height - 75;
		
		ic.style.width = (width-20)+'px';
		ic.style.height = (height-20)+'px';
		
		pc.style.width = (width - 222)+'px';
		pc.style.height = (height - 75) + 'px';
		pc.innerHTML = VH_GetFlash(visualhotels_currentsrc);
	}
	else
	{
		var vfullscreen = visualhotels_words.ru[3];
		visualhotels_isfull = false;
		a.innerHTML = vfullscreen;
		
		var ml = document.getElementById('visualhotels-virtual-tour');
		var ic = document.getElementById('visualhotels-inner-container');
		var pc = document.getElementById('panorama-container');
		
		var width = VH_GetW();
		var height = VH_GetH();
		
		visualhotels_panoramawidth = 480;
		visualhotels_panoramaheight = 360;
		
		var left = parseInt((width - 698) / 2);
		var top = parseInt((height - 424) / 2);
		
		ml.style.top = top+'px';
		ml.style.left = left+'px';
		
		ic.style.width = '682px';
		ic.style.height = '416px';
		
		pc.style.width = '480px';
		pc.style.height = '360px';
		pc.innerHTML = VH_GetFlash(visualhotels_currentsrc);
	}
}


function VH_OnResize()
{
	if ( visualhotels_isfull ) // full screen
	{
		var ml = document.getElementById('visualhotels-virtual-tour');
		var ic = document.getElementById('visualhotels-inner-container');
		var pc = document.getElementById('panorama-container');
		
		ml.style.top = '0px';
		ml.style.left = '0px';
		
		var width = VH_GetW();
		var height = VH_GetH();
		
		visualhotels_panoramawidth = width - 222;
		visualhotels_panoramaheight = height - 75;
		
		ic.style.width = (width-20)+'px';
		ic.style.height = (height-20)+'px';
		
		pc.style.width = (width - 222)+'px';
		pc.style.height = (height - 75) + 'px';
		pc.innerHTML = VH_GetFlash(visualhotels_currentsrc);
	}
	else // not full screen
	{
		var ml = document.getElementById('visualhotels-virtual-tour');
		
		var width = VH_GetW();
		var height = VH_GetH();
		
		var left = parseInt((width - 698) / 2);
		var top = parseInt((height - 424) / 2);
		
		ml.style.top = top+'px';
		ml.style.left = left+'px';
	}
}


function VH_ParseXML()
{
	var xml = visualhotels_xml;
	var sections, section, section_name, section_id;
	var names;
	var panoramas, panorama, panorama_s_swf, panorama_f_swf, panorama_subid;
	var c=0,a=0,b=0,buf_b=-1;
	
	names = xml.getElementsByTagName('Names');
	for ( a=0; a < names.length; a++ )
	{
		switch ( a )
		{
			case 0:// country
				visualhotels_hoteldata.country = names.item(a).firstChild.nodeValue;
				break;
			case 1:// region
				visualhotels_hoteldata.region = names.item(a).firstChild.nodeValue;
				break;
			case 2:// city
				visualhotels_hoteldata.city = names.item(a).firstChild.nodeValue;
				break;
			default:
				break;
		}
	}
	
	visualhotels_hoteldata.name = xml.getElementsByTagName('HotelNames').item(0).firstChild.nodeValue;
	visualhotels_hoteldata.stars = xml.getElementsByTagName('HotelStars').item(0).firstChild.nodeValue;
	
	sections = xml.getElementsByTagName('HotelSection');
	for ( a=0; a < sections.length; a++ )
	{
		section = sections.item(a);
		section_name = section.childNodes.item(0).firstChild.nodeValue;
		section_id = section.getAttribute('ID');
		
		visualhotels_virtualtour[a] = {'id': section_id,'name': section_name,'panoramas': []};
		
		panoramas = section.getElementsByTagName('Panorama');
		
		for ( b=0; b < panoramas.length; b++ )
		{
			panorama = panoramas.item(b);
			panorama_id = panorama.getAttribute('ID');
			try {
				panorama_name = panorama.childNodes.item(0).firstChild.nodeValue;
			}
			catch (e){
				panorama_name = 'name';
			}
			
			switch ( panorama.childNodes.length )
			{
				case 2:
					c = -1;
					buf_b++;
					
					visualhotels_virtualtour[a].panoramas[buf_b] = {
						'id': panorama_id,
						'name': panorama_name,
						'subpanoramas': []
					};
					break;
				
				case 11:
					c++;
					panorama_s_swf = panorama.childNodes.item(4).firstChild.nodeValue;
					panorama_f_swf = panorama.childNodes.item(5).firstChild.nodeValue;
					panorama_subid = panorama.childNodes.item(6).firstChild.nodeValue;
					
					visualhotels_virtualtour[a].panoramas[buf_b].subpanoramas[c] = {
						'id': panorama_id,
						'name': panorama_name,
						'img_preview': VH_GetPreviewPath(panorama_s_swf),
						's_swf': panorama_s_swf,
						'f_swf': panorama_f_swf
					};
					break;
				
				default:
					buf_b++;
					
					panorama_s_swf = panorama.childNodes.item(4).firstChild.nodeValue;
					panorama_f_swf = panorama.childNodes.item(5).firstChild.nodeValue;
					
					visualhotels_virtualtour[a].panoramas[buf_b] = {
						'id': panorama_id,
						'name': panorama_name,
						'img_preview': VH_GetPreviewPath(panorama_s_swf),
						's_swf': panorama_s_swf,
						'f_swf': panorama_f_swf
					};
					break;
			}
		}
	}
}

function VH_GetPreviews()
{
	var tour = '';
	
	visualhotels_previewscount = visualhotels_previewscount == undefined ? 5 : parseInt(visualhotels_previewscount);
	visualhotels_previewsinrow = visualhotels_previewsinrow == undefined ? 5 : parseInt(visualhotels_previewsinrow);
	
	var vtour = visualhotels_words.ru[0];
	var vwatch = visualhotels_words.ru[1];
	
	tour += '<fieldset style="width:'+(visualhotels_previewsinrow*86)+'px;"><legend class="visualhotels-virtualtour">';
	tour += '<table class="visualhotels-previews"><tr>';
	for ( var a = 1; a < visualhotels_previews.length; a++ )
	{
		tour += '<td><img src="'+visualhotels_previews[a]+'" border="1" onclick="javascript:VH_ShowTour('+a+')"></td>';
		
		if ( !(a % visualhotels_previewsinrow) )
		{
			tour += '</tr><tr>';
		}
		
		if ( visualhotels_previewscount == a )
		{
			break;
		}
	}
	tour += '</tr>';
	tour += '<tr><td colspan="'+visualhotels_previewsinrow+'" align="right">';
	tour += '<a href="javascript: VH_ShowTour(1)">'+vwatch+'</a>';
	tour += '</td></tr>';
	tour += '</table>';
	tour += '</fieldset>';
	return tour;
}


function VH_GetW()
{
	return document.compatMode=='CSS1Compat' && !window.opera ?
								document.documentElement.clientWidth : document.body.clientWidth;
}


function VH_GetH()
{
	return document.compatMode=='CSS1Compat' && !window.opera ?
								document.documentElement.clientHeight : document.body.clientHeight;
}


function VH_MakeTour()
{
	var vh = visualhotels_virtualtour;
	var layer = document.getElementById('visualhotels_virtualtour');
	var tour = '';
	var section_name;
	var panorama_count=0;
	var panoramas;
	
	var width = VH_GetW();
	var height = VH_GetH();
	var left = parseInt((width - 698) / 2);
	var top = parseInt((height - 424) / 2);
	
	var vcopyright = visualhotels_words.ru[2];
	var vfullscreen = visualhotels_words.ru[3];
	var vclose = visualhotels_words.ru[5];
	
	tour += '<div class="visualhotels" id="visualhotels-virtual-tour" style="display:none; top:'+top+'px; left:'+left+'px;">';
	tour += '<div class="visualhotels2" style="width:682px;" id="visualhotels-inner-container">';
	tour += '<div class="visualhotels-hotelname">';
	tour += '<h1>'+visualhotels_hoteldata.name+' '+visualhotels_hoteldata.stars+'</h1>';
	tour += '</div>';
	tour += '<div class="visualhotels-close">';
	tour += '<a href="javascript:VH_HideLayer(\'visualhotels-virtual-tour\')">'+vclose+'</a>';
	tour += '</div>';
	tour += '<div class="visualhotels-clear">';
	tour += '<div class="visualhotels-panorama" id="panorama-container" style="width:480px; height:360px;"></div>';
	tour += '<div class="visualhotels-sections">';
	tour += '<ul id="visualhotels-sections-list" class="section">';
	
	for ( var a = 0; a < vh.length; a++ )
	{
		tour += '<li class="hided" value="section"><ins>'+vh[a].name+'</ins>';
		tour += '<ul>';
		
		for ( var b = 0; b < vh[a].panoramas.length; b++ )
		{
			if ( vh[a].panoramas[b] )
			{
				if ( vh[a].panoramas[b].subpanoramas )
				{
					tour += '<li class="hided" value="subsection">';
					tour += '<ins>'+vh[a].panoramas[b].name+'</ins>';
					tour += '<ul>';
					
					for ( var c = 0; c < vh[a].panoramas[b].subpanoramas.length; c++ )
					{
						panorama_count++;
						visualhotels_previews[panorama_count] = vh[a].panoramas[b].subpanoramas[c].img_preview;
						tour += '<li class="panorama" value="'+panorama_count+'" id="'+vh[a].panoramas[b].subpanoramas[c].s_swf+'">'+vh[a].panoramas[b].subpanoramas[c].name+'</li>';
					}
					
					tour += '</ul>';
					tour += '</li>';
				}
				else
				{
					panorama_count++;
					visualhotels_previews[panorama_count] = vh[a].panoramas[b].img_preview;
					tour += '<li class="panorama" value="'+panorama_count+'" id="'+vh[a].panoramas[b].s_swf+'">';
					tour += vh[a].panoramas[b].name;
					tour += '</li>';
				}
			}
		}
		
		tour += '</ul>';
		tour += '</li>';
	}
	
	tour += '</ul>';
	tour += '</div>';
	tour += '</div>';
	tour += '<div class="visualhotels-quality">';
	tour += '<a href="javascript:void(0);" onclick="javascript: VH_FullScreen(this);">'+vfullscreen+'</a>';
	tour += '</div>';
	tour += '<div class="visualhotels-clear"></div>';
	tour += '</div>';
	tour += '</div>';
	
	tour += VH_GetPreviews();
	
	layer.innerHTML = tour;
}


function VH_GetPreviewPath(path)
{
	path = VH_StrReplace(path, "http://visualhotels.com/files", "");
	path = VH_StrReplace(path, "_s.swf", "");
	path = 'http://visualhotels.com/preview.php?path=' + path;
	
	return path;
}


function VH_StrReplace(haystack, needle, replacement)
{
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

/*
function VH_Show()
{
		
	var xml = '{HOTELXML}';
	
	try
	{
		visualhotels_xml = new ActiveXObject("Microsoft.XMLDOM");
		visualhotels_xml.async="false";
		visualhotels_xml.loadXML(xml.toString());
	}
	catch(e)
	{
		parser = new DOMParser();
		visualhotels_xml = parser.parseFromString(xml.toString(), "text/xml");
	}
	

	VH_ParseXML();
	VH_MakeTour();
}
*/

function VH_GetFlash(src)
{
	visualhotels_currentsrc = src;
	if ( visualhotels_isfull )
	{
		src = VH_StrReplace(src, "_s.swf", "_f.swf");
	}
	
	return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="flag" width="'+visualhotels_panoramawidth+'" height="'+visualhotels_panoramaheight+'"><param name="allowScriptAccess" value="sameDomain"><param name="movie" value="'+src+'"><param name="quality" value="high"><param name="scale" value="noscale"><param name="salign" value="rt"><param name="wmode" value="transparent"><embed type="application/x-shockwave-flash" src="'+src+'" pluginspage="http://www.apple.com/quicktime/download/" autoplay="true" bgcolor="white" align="middle" width="'+visualhotels_panoramawidth+'" height="'+visualhotels_panoramaheight+'"></object>';
}

function VH_ShowLayer(id)
{
	document.getElementById(id).style.display = 'block';
}

function VH_HideLayer(id)
{
	visualhotels_section.className = 'hided';
	if ( visualhotels_subsection != null ) visualhotels_subsection.className = 'hided';
	visualhotels_panorama.className = 'panorama';
	
	visualhotels_touractive = false;
	document.getElementById(id).style.display = 'none';
}

function VH_ShowTour(panorama_num)
{
	if ( !visualhotels_touractive )
	{
		visualhotels_touractive = true;
		VH_ShowLayer('visualhotels-virtual-tour');
		VH_InitSectionsList(panorama_num);
	}
}

function VH_InitSectionsList(panorama_num)
{
	var section = subsection = li = active_panorama = null;
	var s_swf = '';
	
	var sections_id = 'visualhotels-sections-list';
	var sections_list = document.getElementById(sections_id);
	var sections_elements = sections_list.getElementsByTagName('li');
	
	var panorama = document.getElementById('panorama-container');
	
	for ( var i = 0; i < sections_elements.length; i++ )
	{
		li = sections_elements[i];
		
		switch ( li.getAttribute('value') )
		{
			case 'section':
				li.className = 'hided';
				section = li;
				subsection = null;
				break;
			
			case 'subsection':
				li.className = 'hided';
				subsection = li;
				break;
			
			default:
				break;
		}
		
		if ( (li.className == 'panorama') )
		{
			li.onclick = function(li)
			{
				if ( li.getAttribute('value') == panorama_num )
				{
					visualhotels_section = section;
					visualhotels_subsection = subsection;
					
					visualhotels_section.className = '';
					if ( visualhotels_subsection != null ) visualhotels_subsection.className = '';
					
					var d_li = visualhotels_section.getElementsByTagName('li');
					
					for ( var y = 0; y < d_li.length; y++ )
					{
						if ( d_li[y].getAttribute('value') == panorama_num )
						{
							s_swf = d_li[y].getAttribute('id');
							visualhotels_panonum = d_li[y].getAttribute('value');
							panorama.innerHTML = VH_GetFlash(s_swf);
							if ( active_panorama != null ) active_panorama.className = 'panorama';
							
							d_li[y].className = 'panorama selected';
							active_panorama = d_li[y];
							
							visualhotels_panorama = active_panorama;
							break;
						}
					}
				}
				
				return function()
				{
					s_swf = li.getAttribute('id');
					visualhotels_panonum = li.getAttribute('value');
					panorama.innerHTML = VH_GetFlash(s_swf);
					
					if ( active_panorama != null ) active_panorama.className = 'panorama';
					
					li.className = 'panorama selected';
					active_panorama = li;
					
					visualhotels_panorama = active_panorama;
				}
			} (li)
		}
		else
		{
			li.firstChild.onclick = function(li)
			{
				return function()
				{
					var to_break = false;
					
					if ( (li.className=='hided') && ((li==visualhotels_section)||(li==visualhotels_subsection)) )
					{
						li.className = '';
					}
					else if ( li.className == 'hided' )
					{
						li.className = '';
						
						var _lis = li.getElementsByTagName('li');
						for ( var y = 0; y < _lis.length; y++ )
						{
							switch ( _lis[y].className )
							{
								case 'panorama':
									s_swf = _lis[y].getAttribute('id');
									visualhotels_panonum = _lis[y].getAttribute('value');
									panorama.innerHTML = VH_GetFlash(s_swf);
									
									if ( active_panorama != null ) active_panorama.className = 'panorama';
									
									_lis[y].className = 'panorama selected';
									active_panorama = _lis[y];
									to_break = true;
									
									var li_first = _lis[y].parentNode.parentNode;
									
									switch ( li_first.getAttribute('value') )
									{
										case 'section':
											visualhotels_section.className = 'hided';
											if ( visualhotels_subsection != null ) visualhotels_subsection.className = 'hided';
											
											visualhotels_section = li_first;
											visualhotels_subsection = null;
											break;
										
										case 'subsection':
											if ( visualhotels_section != li_first.parentNode.parentNode ) visualhotels_section.className = 'hided';
											if ( visualhotels_subsection != null ) visualhotels_subsection.className = 'hided';
											
											visualhotels_subsection = li_first;
											visualhotels_section = li_first.parentNode.parentNode;
											break;
										
										default:
											break;
									}
									
									break;
								
								case 'hided':
									_lis[y].className = '';
									break;
								
								default:
									break;
							}
							
							if ( to_break ) break;
						}
					}
					else
					{
						li.className = 'hided';
					}
				}
			} (li)
		}
	}
}
