/*
 *	(c) TURY.ru
 */
if ( true/*self.trr_common_included == null*/ ) {
self.trr_common_included = true;

var trr_included_modules = new Array ();

function trr_Included ( module_name ) {
	return	false;//trr_included_modules [module_name] != null;
}

function trr_RegisterInclusion ( module_name ) {
	trr_included_modules [module_name] = true;
}

/*
 *  Standard objects extension
 */
String.prototype.trim = function () {
	return	this.replace ( /^\s+|\s+$/g, '' );
}

Object.concat = function ( obj1, obj2 ) {
	var obj = new Object ();
	
	for ( var key in obj1 )
		obj [key] = obj1 [key];
	
	for ( var key in obj2 )
		obj [key] = obj2 [key];
	
	return	obj;
}

Object.search = function ( obj, val ) {
	for ( var key in obj )
		if ( obj [key] == val )
			return	key;
	
	return	-1;
}

Object.duplicate = function ( obj ) {
	var new_obj = new Object ();
	
	for ( var key in obj )
		new_obj [key] = obj [key];
	
	return	new_obj;
}

Object.extend = function ( dst, src ) {
	for ( var key in src )
		if ( dst [key] == undefined )
			dst [key] = src [key];
	
	return	dst;
}

Array.search = function ( arr, val ) {
	for ( var key in arr )
		if ( arr [key] == val )
			return	key;
	
	return	-1;
}

Array.duplicate = function ( arr ) {
	var new_arr = new Array ();
	
	for ( var key in arr )
		new_arr [key] = arr [key];
	
	return	new_arr;
}

/*
 *  static class trr_Common {
 */
function trr_Common () {}

trr_Common.Browser = {
	IE           : !!( window.attachEvent && !window.opera ),
	Opera        : !!window.opera,
	WebKit       : navigator.userAgent.indexOf ( 'AppleWebKit/' ) > -1,
	Gecko        : navigator.userAgent.indexOf ( 'Gecko' ) > -1 && navigator.userAgent.indexOf ( 'KHTML' ) == -1,
	MobileSafari : !!navigator.userAgent.match ( /Apple.*Mobile.*Safari/ )
};

trr_Common.unsetImageSizes = function ( root_node ) {
	if ( root_node ) {
		for ( i = 0 ; i < root_node.childNodes.length ; i++ ) {
			var node = root_node.childNodes [i];
			
			if ( node.tagName == 'IMG' ) {
				node.__orig = new Object ();
				node.__orig.width  = node.style.width;
				node.__orig.height = node.style.height;
				node.style.width  = '';
				node.style.height = '';
			} else if ( typeof ( node.tagName ) == 'string' ) {
				trr_Common.unsetSizes ( node );
			}
		}
	}
}

trr_Common.restoreImageSizes = function ( root_node ) {
	if ( root_node ) {
		for ( i = 0 ; i < root_node.childNodes.length ; i++ ) {
			var node = root_node.childNodes [i];
			
			if ( node.tagName == 'IMG' && typeof ( node.__orig ) == 'object' ) {
				node.style.width  = node.__orig.width;
				node.style.height = node.__orig.height;
			} else if ( typeof ( node.tagName ) == 'string' ) {
				trr_Common.restoreSizes ( node );
			}
		}
	}
}

trr_Common.getElementOffsetXY = function ( elt, offset_point ) {
	if ( elt ) {
		offset_point [0] += elt.offsetLeft;
		offset_point [1] += elt.offsetTop;
		
		trr_Common.getElementOffsetXY ( elt.offsetParent, offset_point );
	}
}

trr_Common.sortNumberComparer = function ( a, b ) {
	return	a - b;
}

trr_Common.getPageSize = function () {
	var xScroll, yScroll;
	
	if ( window.innerHeight && window.scrollMaxY ) {	
		xScroll = window.innerWidth  + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if ( document.body.scrollHeight > document.body.offsetHeight ) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {																// Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if ( self.innerHeight ) {	// all except Explorer
		if ( document.documentElement.clientWidth )
			windowWidth = document.documentElement.clientWidth; 
		else
			windowWidth = self.innerWidth;
		
		windowHeight = self.innerHeight;
	} else if ( document.documentElement && document.documentElement.clientHeight ) { // Explorer 6 Strict Mode
		windowWidth  = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if ( document.body ) {													  // other Explorers
		windowWidth  = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	
	// for small pages with total height less then height of the viewport
	if ( yScroll < windowHeight )
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;
	
	// for small pages with total width less then width of the viewport
	if ( xScroll < windowWidth )
		pageWidth = xScroll;
	else
		pageWidth = windowWidth;
	
	return [pageWidth,pageHeight];
}

trr_Common.getScrollOffsets = function () {
	return	[window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
			 window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop];
}

trr_Common.getViewportDimensions = function () {
	var width  = document.body.clientWidth  ? document.body.clientWidth  : ( self.innerWidth  ? self.innerWidth  : document.documentElement.clientWidth  );
	var height = document.body.clientHeight ? document.body.clientHeight : ( self.innerHeight ? self.innerHeight : document.documentElement.clientHeight );
	
	return	[width, height];
}
/*
 * } // end of class trr_Common
 */
}
if ( !trr_Included ( 'trr_url_utf8' ) ) {
	trr_RegisterInclusion ( 'trr_url_utf8' );
/*
 *  static class trr_UrlUtf8 {
 */
function trr_UrlUtf8 () {}

trr_UrlUtf8.UrlEncode = function ( string ) {
	var utftext = '';
	
	string = string.replace ( /\r\n/g, "\n" );
	
	for ( var n = 0 ; n < string.length ; n++ ) {
		var c = string.charCodeAt ( n );
		
		if ( c < 128 ) {
			utftext += String.fromCharCode ( c );
		} else if ( ( c > 127 ) && ( c < 2048 ) ) {
			utftext += String.fromCharCode ( ( c >> 6 ) | 192 );
			utftext += String.fromCharCode ( ( c & 63 ) | 128 );
		} else {
			utftext += String.fromCharCode ( (   c >> 12 ) | 224 );
			utftext += String.fromCharCode ( ( ( c >>  6 ) & 63  ) | 128 );
			utftext += String.fromCharCode ( (   c  & 63 ) | 128 );
		}
	}
	
	return	escape ( utftext );
}

trr_UrlUtf8.UrlDecode = function ( utftext ) {
	var string = '';
	var i = 0;
	var c = c1 = c2 = 0;
	
	utftext = unescape ( utftext );
	
	while ( i < utftext.length ) {
		c = utftext.charCodeAt ( i );
		
		if ( c < 128 ) {
			string += String.fromCharCode ( c );
			i++;
		} else if ( ( c > 191 ) && ( c < 224 ) ) {
			c2 = utftext.charCodeAt ( i + 1 );
			string += String.fromCharCode ( ( ( c & 31 ) << 6 ) | ( c2 & 63 ) );
			i += 2;
		} else {
			c2 = utftext.charCodeAt ( i + 1 );
			c3 = utftext.charCodeAt ( i + 2 );
			string += String.fromCharCode ( ( ( c & 15 ) << 12 ) | ( ( c2 & 63 ) << 6 ) | ( c3 & 63 ) );
			i += 3;
		}
	}
	
	return	string;
}
/*
 * } // end of class trr_UrlUtf8
 */
}
if ( !trr_Included ( 'trr_url' ) ) {
	trr_RegisterInclusion ( 'trr_url' );
/*
 *  static class trr_Url {
 */
function trr_Url () {}

/*const*/
trr_Url.TRR_ARGUMENT_PREFIX    = '_';
trr_Url.TRR_ARGUMENT_SEPARATOR = '&';

trr_Url.UrlEncode = trr_UrlUtf8.UrlEncode;
trr_Url.UrlDecode = trr_UrlUtf8.UrlDecode;

trr_Url.ParseUrl = function ( url ) {
	var res = url.match ( /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/ );
	
	var info = { 'scheme'    : res [2],
				 'authority' : res [4],
				 'path'      : res [5],
				 'query'     : res [7],
				 'fragment'  : res [9] };
	
	return	info;
}

trr_Url.BuildUrl = function ( scheme, authority, path, query, fragment ) {
	var url = '';
	
	if ( scheme    ) url += scheme + '://';
	if ( authority ) url += authority;
	if ( path      ) url += path;
	if ( query     ) url += '?' + query;
	if ( fragment  ) url += '#' + fragment;
	
	return	url;
}

trr_Url.HttpBuildQuery = function ( data, prefix, sep, key ) {
	prefix = prefix != null ? prefix : trr_Url.TRR_ARGUMENT_PREFIX;
	sep    = sep    != null ? sep    : trr_Url.TRR_ARGUMENT_SEPARATOR;
	key    = key    != null ? key    : null;
	
	var ret = new Array ();
	
	for ( var k in data ) {
		var v = data [k];
		var k_is_numeric = k == parseInt ( k );
		
		k = trr_Url.UrlEncode ( '' + k );
		
		if ( prefix != null && k_is_numeric )
			k = prefix + k;
		
		if ( key != null )
			k = key + '[' + k + ']';
		
		if ( typeof ( v ) == 'object' ) {
			var nested_url = trr_Url.HttpBuildQuery ( v, '', sep, k );
			
			if ( nested_url.length > 0 )
				ret.push ( trr_Url.HttpBuildQuery ( v, '', sep, k ) );
		} else {
			ret.push ( k + '=' + trr_Url.UrlEncode ( '' + v ) );
		}
	}
	
	return	ret.join ( sep );
}

trr_Url.HttpParseQuery = function ( query, sep ) {
	sep = sep != null ? sep : trr_Url.TRR_ARGUMENT_SEPARATOR;
	
	var res   = new Array ();
	var pairs = query.split ( sep );
	
	for ( var pair_key in pairs ) {
		var pair = pairs [pair_key];
		
		if ( pair.length == 0 )
			continue;
		
		var eq_pos, first_brace_pos;
		var arg, val;
		
		if ( -1 == ( eq_pos = pair.indexOf ( '=' ) ) ) {
			arg = pair;
			val = '';
		} else {
			arg = pair.substring ( 0, eq_pos );
			val = trr_Url.UrlDecode ( pair.substring ( eq_pos + 1 ) );
		}
		
		if ( ( -1 != ( first_brace_pos = arg.indexOf ( '[' ) ) ) &&
			 ( -1 != arg.indexOf ( ']' ) ) ) {
			var matches;
			
			if ( null != ( matches = arg.match ( /\[([^#\]]*)\]/gi ) ) ) {
				var path = '';
				var isset, len;
				
				arg = arg.substring ( 0, first_brace_pos );
				eval ( 'isset = res ["' + arg + '"]' + ' != undefined;' );
				
				if ( !isset )
					eval ( 'res ["' + arg + '"]' + ' = new Array ();' );
				
				for ( var key_key in matches ) {
					var key = matches [key_key];
					key = key.substring ( 1, key.length - 1 );
					
					if ( ( key.trim ().length ) == 0 ) {
						eval ( 'len = res ["' + arg + '"]' + path + '.length;' );
						path += '[' + len + ']';
					} else if ( parseInt ( key ) == key ) {
						path += '[' + key + ']';
					} else {
						path += '["' + key + '"]';
					}
					
					eval ( 'isset = res ["' + arg + '"]' + path + ' != undefined;' );
					
					if ( !isset )
						eval ( 'res ["' + arg + '"]' + path + ' = new Array ();' );
				}
				
				val = val.replace ( "'", "\\'" );
				eval ( 'res ["' + arg + '"]' + path + " = '" + val + "';" );
			}
		} else {
			res [arg] = val;
		}
	}
	
	return	res;
}
/*
 * } // end of class trr_Url
 */
}
if ( !trr_Included ( 'trr_ajax_client' ) ) {
	trr_RegisterInclusion ( 'trr_ajax_client' );
/*
 *  static class trr_AjaxClient {
 */
function trr_AjaxClient () {}

trr_AjaxClient.SendGetRequest = function ( uri, async, callback, async_data ) {
	if ( async )
		return	trr_AjaxClient._sendRequest ( uri, 'GET', null, async, callback, async_data );
	else
		return	trr_AjaxClient._sendRequest ( uri, 'GET', null, async, callback );
}

trr_AjaxClient.SendPostRequest = function ( uri, params, async, callback, async_data ) {
	if ( async )
		return	trr_AjaxClient._sendRequest ( uri, 'POST', params, async, callback, async_data );
	else
		return	trr_AjaxClient._sendRequest ( uri, 'POST', params, async, callback );
}

/*private*/
trr_AjaxClient._sendRequest = function ( uri, method, params, async, callback, async_data ) {
	var xmlHttp = trr_AjaxClient._getXmlHttp ();
	xmlHttp.open ( method, uri, async );
	
	if ( async ) {
		xmlHttp.onreadystatechange = function () {
			if ( xmlHttp.readyState == 4 )
				trr_AjaxClient._onLoadFile ( uri, async, callback, xmlHttp, async_data );
		}
	}
	
	if ( method == 'POST' ) {
		xmlHttp.setRequestHeader ( 'Content-Type', 'application/x-www-form-urlencoded' );
		
		if ( params != null )
			xmlHttp.setRequestHeader ( 'Content-Length', params.length.toString () );
	}
	
	xmlHttp.send ( params );
	
	if ( async )
		return	xmlHttp;
	else
		return	trr_AjaxClient._onLoadFile ( uri, async, callback, xmlHttp );
}

/*private*/
trr_AjaxClient._onLoadFile = function ( uri, async, callback, req, async_data ) {
	if ( callback != undefined )
		callback ( req.responseText, req.responseXML, async_data );
	if ( !async )
		return	req.responseXML;
}

/*private*/
trr_AjaxClient._getXmlHttp = function() {
	try {
		if ( window.XMLHttpRequest ) {
			var req = new XMLHttpRequest ();
			// some versions of Moz do not support the readyState property and the onreadystate event so we patch it!
			if ( req.readyState == null ) {
				req.readyState = 1;
				req.addEventListener ( 'load', 
									function () {
										req.readyState = 4;
										
										if ( typeof ( req.onreadystatechange ) == 'function' )
											req.onreadystatechange ();
									},
									false );
			}
			
			return	req;
		}
		
		if ( window.ActiveXObject )
			return	new ActiveXObject ( trr_AjaxClient._getXmlHttpProgID () );
	} catch ( ex ) {}
	
	throw new Error ( 'Your browser does not support XmlHttp objects' );
}

/*private*/
trr_AjaxClient._getXmlHttpProgID = function () {
	if ( trr_AjaxClient._getXmlHttpProgID.progid )
		return	trr_AjaxClient._getXmlHttpProgID.progid;
	
	var progids = ['Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
	var o;
	
	for ( var i = 0; i < progids.length; i++ ) {
		try {
			o = new ActiveXObject ( progids [i] );
			
			return	trr_AjaxClient._getXmlHttpProgID.progid = progids [i];
		} catch ( ex ) {};
	}
	
	throw new Error ( 'Could not find an installed XML parser' );
}
/*
 * } // end of class trr_AjaxClient
 */
}
if ( !trr_Included ( 'best' ) ) {
	trr_RegisterInclusion ( 'best' );
	
	function trr_Best () {}
	
	trr_Best.trr_bestParams = {
		"country"     : '',
		"trr_country" : ''
	};
	
	trr_Best.trr_directLinkVals   = new Array ();
	trr_Best.trr_directLinkActive = false;
	trr_Best.trr_presetParams     = null;
	
	trr_Best.req_loadBest = null;
	
	trr_Best.trr_ParseDirectLink = function () {
		loc = window.location.toString ();
		addr_parts = loc.split ( "#:tour_search" );
		
		if ( addr_parts.length > 1 ) {
			dl_args = ( addr_parts [1] ).split ( '/' );
			
			if ( dl_args.length > 1 ) {
				dl_args = dl_args [1].split ( '?' );
				dl_args = '?' + dl_args [1];
			}
			
			dl_args = dl_args.toString ();
			
			if ( dl_args.substr ( 0, 1 ) == '?' ) {
				dl_args = dl_args.substr ( 1 ).split ( '&' );
				trr_Best.trr_directLinkActive = true;
			} else {
				return	false;
			}
		} else {
			return	false;
		}
		
		trr_Best.trr_directLinkVals = new Array ();
			
		for ( i = 0 ; i < dl_args.length ; i++ ) {
			keyVal = dl_args [i].split ( '=' );
			
			if ( keyVal.length < 2 )
				continue;
			
			var key = keyVal.shift ();
			var val = keyVal.join ( '=' );
			
			if ( trr_Best.trr_directLinkVals [key] != null )
				trr_Best.trr_directLinkVals [key] = new Array ( trr_Best.trr_directLinkVals [key], trr_Url.UrlDecode ( val ) );
			else
				trr_Best.trr_directLinkVals [key] = trr_Url.UrlDecode ( val );
		}
		
		return	true;
	}
	
	trr_Best.trr_LoadFromDirectLink = function ( eltIds ) {
		switch ( typeof ( eltIds ) ) {
		case 'string':
			processElt ( eltIds );
			break;
		case 'object':	// массив
			for ( var key in eltIds )
				processElt ( eltIds, key );
			break;
		}
		
		function processElt ( eltIds, key ) {
			var dlink_vals = null;
			
			if ( trr_Best.trr_directLinkVals [key] != null )
				eltIds [key] = trr_Best.trr_directLinkVals [key];
			else if ( trr_Best.trr_presetParams != null && trr_Best.trr_presetParams [key] != null )
				eltIds [key] = trr_Best.trr_presetParams [key];
		}
	}
	
	trr_Best.trr_InitBestBlock = function ( presetParams ) {
		if ( presetParams != null )
			trr_Best.trr_presetParams = presetParams;
		
		trr_Best.trr_bestOldParams = new Array ();
		
		for ( var key in trr_Best.trr_bestParams )
			trr_Best.trr_bestOldParams [key] = trr_Best.trr_bestParams [key];
		
		if ( trr_Best.trr_ParseDirectLink () || trr_Best.trr_presetParams != null ) {
			trr_Best.trr_LoadFromDirectLink ( trr_Best.trr_bestParams );
			
			var match = true;
			
			if ( trr_Best.trr_bestOldParams ) {
				for ( var key in trr_Best.trr_bestParams ) {
					if ( trr_Best.trr_bestParams [key] != trr_Best.trr_bestOldParams [key] ) {
						match = false;
						break;
					}
				}
			}
			
			if ( !match ) {
				var trr_country = trr_Best.trr_bestParams ['country'] ? trr_Best.trr_bestParams ['country'] : trr_Best.trr_bestParams ['trr_country'];
				
				if ( trr_country )
					trr_Best.trr_LoadBest ( trr_country, false );
				else
					trr_Best.trr_LoadBest ();
			}
		} else {
			trr_Best.trr_LoadBest ();
		}
	}
	
	trr_Best.trr_LoadBest = function ( country, next_func ) {
		if ( targetContainer = document.getElementById ( 'best_content' ) )
			targetContainer.innerHTML = '<div width="100%" align="center">Загрузка...<br /><img src="http://www.oldis.ru/modules/tury.ru/common/img/loading.gif"></div>';
		
		country = country != null ? country : '';
		url = "http://www.oldis.ru/modules/tury.ru/best/wsclient.php/best?country=" + country + '&limit=5' + '&querydate=' + Date ();
		
		if ( trr_Best.req_loadBest != null )
			trr_Best.req_loadBest.abort ();
		
		trr_Best.req_loadBest = trr_AjaxClient.SendGetRequest ( url, true, trr_Best.trr_LoadBestCompleted, next_func );
	}
	
	trr_Best.trr_LoadBestCompleted = function ( text, xml, next_func ) {
		trr_Best.req_loadBest = null;
		var nd = xml.getElementsByTagName ( 'data' );
		
		if ( nd.length != 0 ) {
			var targetContainer = document.getElementById ( 'best_content' );
			
			if ( targetContainer )
				targetContainer.innerHTML = nd [0].childNodes [0].nodeValue;
		}
		
		if ( next_func )
			next_func ();
	}
}

