function findPos( obj )
{
	var offwidth = offheight = curleft = curtop = 0;
	if (obj.offsetParent)
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		offwidth = obj.offsetWidth;
		offheight = obj.offsetHeight;
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop,offwidth,offheight];
}

function submitMyForm( obj )
{
	var inputs = obj.parentNode.getElementsByTagName('input');
	var select = obj.parentNode.getElementsByTagName('select');
	var inputs_l = inputs.length;
	var select_l = select.length;
	var u = '/';
	for( i=0;i<inputs_l;i++ )
	{
		if( inputs[i].nodeName && ( inputs[i].nodeName.toLowerCase() == 'input' )  )
		{
			if( inputs[i].value != '' )
				u += inputs[i].value.replace( /\//g, '_' )+'/';
		}
	}
	for( i=0;i<select_l;i++ )
	{
		if( select[i].nodeName && ( select[i].nodeName.toLowerCase() == 'select' ) )
		{
			if( select[i].value != '' )
				u += select[i].value.replace( /\//g, '_' )+'/';
		}
	}
	u += '0_search.html';
	window.location = u;
}

function submitMySForm( obj, pageindex )
{
	var inputs = obj.parentNode.getElementsByTagName('input');
	var select = obj.parentNode.getElementsByTagName('select');
	var inputs_l = inputs.length;
	var select_l = select.length;
	var u = '/';
	for( i=0;i<inputs_l;i++ )
	{
		if( inputs[i].nodeName && ( inputs[i].nodeName.toLowerCase() == 'input' )  )
		{
			if( inputs[i].value != '' )
				u += inputs[i].value.replace( /\//g, '_' )+'/';
		}
	}
	for( i=0;i<select_l;i++ )
	{
		if( select[i].nodeName && ( select[i].nodeName.toLowerCase() == 'select' ) )
		{
			if( select[i].value != '' )
				u += select[i].value.replace( /\//g, '_' )+'/';
		}
	}
	u += pageindex+'_search.html';
	window.location = u;
}

/*googlemap*/
var map, box;
var draggable = false, resizable = false;
var mouseX, mouseY, drawnX, drawnY;
var resizedX, resizedY;
// Mouse move listener
function watchMouse(e)
{
	// Include possible scroll values
	var sx = window.scrollX || document.documentElement.scrollLeft|| 0;
	var sy = window.scrollY || document.documentElement.scrollTop|| 0;

	if(!e) e = window.event; // IEs event definition
	mouseX = e.clientX + sx;
	mouseY = e.clientY + sy;

	// How far has the box been resized?
	var deltaX = mouseX - resizedX;
	var deltaY = mouseY - resizedY;
	// Store the difference in global variables
	resizedX = mouseX;
	resizedY = mouseY;

	if(resizable == true)
	{
		// The resize button is being held
		changeMapSize(deltaX, deltaY);
	}

	if(draggable == true)
	{
		// The box is being dragged
		box.style.left= (mouseX - drawnX) + "px";
		box.style.top = (mouseY - drawnY) + "px";
	}
}

function dragstart(e) // Calculate mouse position for dragging
{
	draggable = true;
	/* Stop selecting the content
	* of the box while dragging
	*/
	if(e.cancelable) { e.preventDefault(); }
	if(window.event) { box.onselectstart = new Function("return false"); }

	drawnX = mouseX - parseInt(box.style.left);
	drawnY = mouseY - parseInt(box.style.top);

	// The box is being dropped
	box.onmouseup = function() { draggable = false; }
}

function getObjPos( obj )
{
	return [170,10];
}
var min_max_x_y = new Array();

function pairPanoraminoValues( zoomLevel )
{
	var PSObject = document.getElementById('panoraminoimagesscript');
	var pattern_search_minx = /(minx\=)([\d]+\.[\d]+)/i;
	var pattern_search_miny = /(miny\=)([\d]+\.[\d]+)/i;
	var pattern_search_maxx = /(maxx\=)([\d]+\.[\d]+)/i;
	var pattern_search_maxy = /(maxy\=)([\d]+\.[\d]+)/i;
	min_max_x_y = [PSObject.src.match(pattern_search_minx),PSObject.src.match(pattern_search_miny),PSObject.src.match(pattern_search_maxx),PSObject.src.match(pattern_search_maxy),zoomLevel,PSObject.src];
}

function changePanoraminoScriptSRC( zoomlevel )
{
	var newSrc = min_max_x_y[5];
	var lvl = ( min_max_x_y[4]-zoomlevel )*0.8;
	newSrc = newSrc.replace( new RegExp( min_max_x_y[0][0],'gi' ), min_max_x_y[0][1]+''+( parseFloat( min_max_x_y[0][2] )+lvl ) );
	newSrc = newSrc.replace( new RegExp( min_max_x_y[1][0],'gi' ), min_max_x_y[1][1]+''+( parseFloat( min_max_x_y[1][2] )+lvl ) );
	newSrc = newSrc.replace( new RegExp( min_max_x_y[2][0],'gi' ), min_max_x_y[2][1]+''+( parseFloat( min_max_x_y[2][2] )+lvl ) );
	newSrc = newSrc.replace( new RegExp( min_max_x_y[3][0],'gi' ), min_max_x_y[3][1]+''+( parseFloat( min_max_x_y[3][2] )+lvl ) );
	document.getElementById('panoraminoimagesscript').src = newSrc;
}

function loadMap(lat, lng, name)
{
	//changePanoraminoScriptSRC();
	if( typeof GLatLng == 'undefined' )
		return false;
	else
	{
		if(GBrowserIsCompatible())
		{
			var point = new GLatLng(lat, lng);
			var destObj = document.getElementById("gmapcont");
			var destObj = [destObj,getObjPos( destObj )];
			map = new GMap2( destObj[0] );
//			GEvent.addListener(map,'zoomend',function(){
//				if( typeof min_max_x_y[0] == 'undefined' )
//               	pairPanoraminoValues( this.getZoom() );
//                else
//                	changePanoraminoScriptSRC( this.getZoom() );
//        	});
			map.setCenter(point, 10, G_NORMAL_MAP);
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			//creaate marker
			var markermy = new GMarker(point);
			map.addOverlay(markermy);
			GEvent.addListener(markermy, "click", function()
			{
				markermy.openInfoWindowHtml(name);
			});
			/*panoramino*/
			var icon = new GIcon();
			icon.image = '';
			icon.shadow = '';
			icon.iconSize = new GSize(32, 32);
			icon.shadowSize = new GSize(32, 32);
			icon.iconAnchor = new GPoint(9, 9);
			icon.infoWindowAnchor = new GPoint(9, 0);
			for (var i = 0; i < photos.length; i++)
			{
				var  photo = photos[i];
				var imgclick = [photo.photo_file_url];
				icon.image = photo.photo_file_url;
				var marker_panoramino = new GMarker(new GLatLng(photo.latitude, photo.longitude), icon);
				marker_panoramino.photo = photo;
				map.addOverlay(marker_panoramino, icon);
				//alert(photo.photo_file_url+'---'+i);
				GEvent.addListener( marker_panoramino, 'click', function( photo ){
					var tobj = document.getElementById('bigPanoramImg');
					tobj.innerHTML = '<img src="'+this.photo.photo_file_url+'" alt="'+this.photo.photo_title+'"/><br/><a href="'+this.photo.owner_url+'" target="_blank">owner: '+this.photo.owner_name+' | '+this.photo.photo_title+'</a>';
					tobj.style.zIndex = 100;
					tobj.style.display = 'block';
					tobj.onclick = function(){
						this.style.display = 'none';
						this.innerHTML = '';
					}
				});
			}
			
			/*end panoramino*/
			// Add the self created ResizeControl
			map.addControl(new ResizeControl());
			setImagesStyle( 'gmapcont', 'gmnoprint' );
			bigImageShow = document.createElement("div"); 
			destObj[0].appendChild(bigImageShow);
			bigImageShow.id='bigPanoramImg';
			//box.style.top = destObj[1][0]+'px';
			//box.style.left = destObj[1][1]+'px';
			box.style.visibility = "visible";
		}
	}
}

function setImagesStyle( holder, id_patern )
{
	var divs = document.getElementById( holder ).getElementsByTagName( 'img' );
	var patt = new RegExp("mtgt_");
	for(i in divs )
	{
		if( divs[i].id && ( divs[i].id.length > 1 ) && ( patt.test( divs[i].id ) ) )
		{
			if( !divs[i].onmouseover )
			{
				divs[i].className = 'mypanimg';
				/*css style*/
				divs[i].style.padding = '2px';
				divs[i].style.background = 'white';
				divs[i].style.border = '1px solid #254F78';
				/*end css style*/
				divs[i].onmouseover = function(){
					this.style.zIndex = '1';
					this.onmouseout = function(){
						this.style.zIndex = '-1';
					}
				}
			}
		}
	}
}

function hideMap()
{
	box.style.visibility = "hidden";
	GUnload();
}

function ResizeControl(){}
if( typeof GControl != 'undefined' )
	ResizeControl.prototype= new GControl();

ResizeControl.prototype.initialize=function(map)
{
	var resizeButton = document.createElement("div");
	resizeButton.style.width = "20px";
	resizeButton.style.height = "20px";
	resizeButton.style.backgroundImage = "url(/imgs/map.resize.gif)";
	resizeButton.onmousedown = function() { resizable = true; }
	resizeButton.onmouseup = function() { resizable = false; }
	var container = map.getContainer();
	container.appendChild(resizeButton);

	/* Move the Copyright 25px to the left
	* to make sure that it's fully readable
	*/
	var copyrightdiv = container.firstChild.nextSibling;
	copyrightdiv.style.marginRight = "25px";

	return resizeButton;
}

ResizeControl.prototype.getDefaultPosition=function()
{
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(0,0));
}
if( typeof GMapTypeControl != 'undefined' )
	var typecontrolgmap = new GMapTypeControl(true);

// Resizes the map's width and height by the given increment
function changeMapSize(dx, dy)
{
	var mapdiv = map.getContainer();
	var width = parseInt(mapdiv.style.width);
	var height =  parseInt(mapdiv.style.height);

	/* Take care that the map's width or height do not get
	* a negative value. Unexpected things will happen.
	*/
	if(width < 50) { width = 50; resizable = false; }
	if(height < 50) { height = 50; resizable = false; }

	if(width > 400) map.addControl(typecontrolgmap); // Switch map types

	if(width < 400) map.removeControl(typecontrolgmap);

	mapdiv.style.width = (width + dx) + "px";
	mapdiv.style.height= (height + dy) + "px";
	map.checkResize();
}
/*end googlemap*/
/*panoramino*/
var photos;
function storePhotos(panoramio) { photos = panoramio.photos; }
/*end panoramino*/

function _showHide( id, position_obj )
{
	var obj = document.getElementById( id );
	if( obj.style.display == 'block' )
		obj.style.display = 'none';
	else
	{
		var post_res = findPos( position_obj );
		var bgholder = $('#c_con');
		var pos = windowSize();
		bgholder.css({width:pos[0]+"px",height:pos[1]+"px"});
		bgholder.click( function(){
			$(this).css({width:"0",height:"0"});
			obj.style.display = 'none';
		}); 
		obj.style.top = (post_res[1]+25)+'px';
		obj.style.left = post_res[0]+'px';
		obj.style.display = 'block';
	}
}

function showHideUserComents()
{
	this.showHide = showHide;
	this.IDS = new Array();
}

function showHide( obj )
{
	var field = $( obj ).children();
	$( obj ).hover(
	function(){},
	function(){
		$(field[0]).hide('slow');
	}
	);
	$(field[0]).show('fast');
}

var uc = new showHideUserComents();
var sCommentEl = [];

function setEStyle( obj, style )
{
	$(obj).css(style);
}

function submitComment(obj)
{
	var elements = $(obj).parent().find(".inputel").get();
	var request_url = { time: Math.random() };
	for( i in elements )
	{
		var _val = elements[i].value.replace( /\n/ig, "\\n" );
		eval( 'request_url.'+elements[i].name+"='"+_val+"';" );
		sCommentEl[elements[i].name] = elements[i];
		var o = $(elements[i]);
		setEStyle( o, {background: "#FFFFFF"} );
		setEStyle( o, {color: "#000000"} );
	}
	$.post( '/sendopinion.php', request_url,
		function(data){
			if( data.error == 0 )
			{
				obj.value = obj.value+' - OK';
				obj.disabled = true;
				setEStyle( obj, {color: "#6D8F23"} );
			}
			else
			{
				for( i in data.error )
				{
					var o = $(sCommentEl[i]);
					setEStyle( o, {background: "#C6A8A3"} );
					setEStyle( o, {color: "#FFFFFF"} );
				}
			}
		}
	, "json");
}

function writeDebugTo( text )
{
	if( ( location.hostname == 'www.stepsacross.new' ) || ( location.hostname == 'stepsacross.new' ) )
	{
		var dbinf = $("#debuginfo");
		dbinf.css({border:"3px solid #A82B2B",background:"white"});
		dbinf.html(dbinf.html()+'<div>'+text+'</div>');
		dbinf.click( function(){
			$(this).remove();
		});
	}
}

var hotelRequest = [];
var hlen = 0;
var conted_hotels_text = [];

function onSuccess( data, id )
{
	writeDebugTo( ' write data for ajaxID: '+id );
	$(hotelRequest[id][2]).after(data);
}

function isFinal()
{
	var totalHotels = 0;
	for( i=0;i<hlen;i++ )
	{
		if( hotelRequest[i][1] == false )
		{
			writeDebugTo( 'NOT FINISHED YET[drop:'+i+']' );
			return false;
		}
	}
	writeDebugTo( 'ALL AJAX REQUESTS ARE FINISHED[#'+hlen+']' );
	
	var boxes = $("#left_cont .hotelsaval");
	var cc1 = boxes.length;
	var imageRewrite = [];
	for( i=0;i<cc1;i++ )
	{
		var thisbox = $(boxes[i]);
		var childrens = $(thisbox.children().get(1)).children();
		var childLenght = childrens.length;
		var isFull = false;
		for( ii=0;ii<childLenght;ii++ )
		{
			if( childrens.get(ii).tagName.toLowerCase() == 'a' )
			{
				isFull = true;
			}
			if( childrens.get(ii).tagName.toLowerCase() == 'img' )
				imageRewrite[imageRewrite.length] = childrens.get(ii);
		}
		if( isFull == false )
		{
			$(thisbox.parent().parent().get(0)).hide("slow");
			writeDebugTo( 'hide box: '+i );
			continue;
		}
		writeDebugTo( 'box will not be hidded: '+i );
		totalHotels++;
	}
	var text2head = '';
	if( totalHotels < 1 )
		text2head = conted_hotels_text[0];
	else
		text2head = conted_hotels_text[1].replace(/__num__/,totalHotels);
	$($("#left_cont h2.searchtitle").get(0)).html( text2head );
	var imglen = imageRewrite.length;
	for( i=0;i<imglen;i++ )
	{
		imageRewrite[i].src = '/imgs/dot_empty.gif';
		imageRewrite[i].alt = 'ready';
	}
	writeDebugTo( '############################# END #############################' );
}

function setHotelRequest( url, whereim, extend )
{
	//[hotel url,state(false-working|true- ready),where im - object,provider_text - text ]
	hotelRequest[hotelRequest.length] = [url,false,document.getElementById(whereim),extend];
}

function jsonp_callback()
{}

function callUrl( next )
{
	var uri = hotelRequest[next][0]+'?'+hotelRequest[next][3]+'&jsonCallback=?';
	writeDebugTo( '<a href="'+uri+'" target="_blank">url has been added</a>' );
	$.getJSON( uri, function(data){
		//writeDebugTo( data.result );
		writeDebugTo( 'json reqest to: '+hotelRequest[next][0] );
		hotelRequest[next][1] = true;
		onSuccess( data.result, next );
		isFinal();
	});
		
	$(document).ajaxError( function( event, XMLHttpRequest, ajaxOptions ){
		hotelRequest[next][1] = true;
		isFinal();
	});
}

function attach_aval_req( req )
{
	$(document).ready(function(){
		writeDebugTo( '############################ START ############################' );
		writeDebugTo( 'event onload atached' );
		hlen = hotelRequest.length;
		if( hlen == 0 )
			isFinal();
		else
		{
			for( i=0;i<hlen;i++ )
				callUrl( i );
		}
	});
}
function checkForum( check ){
	$.get( '/forum/user.create.new.forum.php', { "try":check }, function(data){
		eval(data);
		document.location = c;
	});
}