function $(id){
	return document.getElementById(id);
}
	
function advSearchSwitch(){
	var olinks = $("searchPanelSwitch").getElementsByTagName("SPAN");
	if($('object_type')) {
	switch($('object_type').value){
		case 'flat':
			var oForm = $("advancedSearchForm_flat");
		break;
		
		case 'building_complex' :
			var oForm = $("advancedSearchForm_building_complex");
		break;	
	}
	} else {
		var oForm = $("advancedSearchForm");
	}
	if ($('site_cookie_path')) {
		$pathCookie=$('site_cookie_path').value;
	} else {
		$pathCookie=false;
	}
	if(oForm.style.display!="block"){
		oForm.style.display = "block";
		olinks[0].style.display = "none";
		olinks[1].style.display = "";
		setCookie2('filterFormStyle',1,false,'/',$pathCookie);		
	}
	else {
		oForm.style.display = "none";
		olinks[0].style.display = "";
		olinks[1].style.display = "none";
		setCookie2('filterFormStyle',0,false,'/',$pathCookie);		
	}
}	
	
function infoMenuSwitch(o){
	var oC = o.parentNode.parentNode;
	var oT = oC.parentNode.getElementsByTagName("LI")[0];
	while(oT){
		oT.className = "";
		oT = oT.nextSibling;
	} 
	oC.className = "act";
	oC.blur();
}		

	
function commentsFormSwitch(o){
	var oP = o;
	while(oP.className.indexOf('commentsForm')== -1){
		oP = oP.parentNode;
	}
	oP.className = (oP.className.indexOf('commentsFormOpened') != -1)? oP.className.replace('commentsFormOpened','commentsFormClosed') : oP.className.replace('commentsFormClosed','commentsFormOpened');
	o.blur();
}

function blockSwitch(o){
	var oB = o.parentNode;
	do{
		oB = oB.nextSibling;
	}while(oB && oB.className != "expandBlock")
	
	if(!oB){return}
	
	if(o.className == "expandedLink"){
		o.className = "collapsedLink";
		oB.style.display = "none";		
	}
	else {
		o.className = "expandedLink";
		oB.style.display = "block";		
	}
	o.blur();	
}

function compareSwitch(o){
	var oB = o.parentNode.parentNode.getElementsByTagName("TABLE")[0];
	if(!oB){return}
	
	if(o.className == "expandedLink"){
		o.className = "collapsedLink";
		oB.style.display = "none";		
	}
	else {
		o.className = "expandedLink";
		oB.style.display = "";		
	}
	o.blur();	
}

var compareFramework = {
	oArea: null,
	oBar: null,
	
	init: function(idArea) {
		this.oArea = document.getElementById(idArea);
		this.oBar = document.getElementById(idArea+"Bar");
		
		var nCols = this.getColumnNumber();		
		this.fixLayout(nCols-1);		
		var vCols = (nCols > 4) ? 4 : nCols-1;
//		vCols = (nCols < 4) ? 4 : nCols;
		//console.log(nCols);
		//console.log(this.transformColumn);
		
		for(var i=0; i<vCols; i++){
			this.transformColumn("show",i+1);
		}
		this.showBar();			
	}
	,
	
	onDelete: function(o,id) {
		
		o.blur();
		do{
		o = o.parentNode;
		} while(o.tagName != "TD");
	
		var delIndex = o.cellIndex;
	
		var oTds = o.parentNode.cells;		
		
		
		this.transformColumn("delete",delIndex);
		
		for (var i=1; i<oTds.length; i++){
			if(i < delIndex && this.isColHidden(i) && oTds[i+1] && !this.isColHidden(i+1)){
				this.transformColumn("show",i)	
				break;
			}

			if(i >= delIndex && this.isColHidden(i)){
				this.transformColumn("show",i);	
				break;			
			}
		}
		//alert(id);	
		favoritesDel(id,'compare');
		incFavCounter(-1,'compare');		
		this.fixLayout(this.getColumnNumber()-1);		
		this.showBar();		
		if(getCookie2('compare')=='' || getCookie2('compare')==','){
			window.location = window.location;
		}
	}
	,
	
	onNext: function() {
		var nCols = this.getColumnNumber();
		if(nCols<5 || !this.isColHidden(nCols-1)){
			return;
		}
		
		for(var i=1; i<nCols; i++){
			if(!this.isColHidden(i)){
				break;
			}
		}

		this.transformColumn("hide",i);			
		this.transformColumn("show",i+4);			
		
		this.showBar();		
	}
	,
	
	onPrev: function() {
		var nCols = this.getColumnNumber();
		if(nCols<5 || !this.isColHidden(1)){
			return;
		}	
		
		for(var i=nCols-1; i>1; i--){
			if(!this.isColHidden(i)){
				break;
			}
		}
		
		this.transformColumn("hide",i);			
		this.transformColumn("show",i-4);			
		
		this.showBar();		
	}
	,
	
	transformColumn: function(action,index){
		var oTbls = this.oArea.getElementsByTagName("TABLE");
		for (var i in oTbls){
			if(oTbls[i].className != "tblComp"){
				continue;
			}
			var oTrs = oTbls[i].rows;
			for(var k=0; k<oTrs.length; k++){
				
				var oTd = oTrs[k].cells[index];
				switch(action){
					case "show":
						oTd.style.display = (oTd.runtimeStyle)?"block":"table-cell";
						break; 				
					case "hide":
						oTd.style.display = "none";					
						break; 				
					case "delete":
						oTrs[k].deleteCell(index);
						break; 				
				}			
			}
		}
	}
	,
		
	showBar: function(){
		var nCols = this.getColumnNumber();
		if(nCols<5){
			return;
		}
		var fullLen = this.oBar.parentNode.offsetWidth;
		var barLen = Math.round(fullLen*4 / (nCols-1));
	
		for(var i=1; i<nCols; i++){
			if(!this.isColHidden(i)){
				break;
			}
		}
		var barPos = Math.round(fullLen*(i-1) / (nCols-1));		
		
		this.oBar.style.marginLeft = barPos+"px";	
		this.oBar.style.width = barLen+"px";		
	}
	,
	
	fixLayout: function(n){
		var cssClass = "pgCompareCont";	
		switch(n){
			case 1:
			case 2:
				cssClass += " pgCompareContTwoCol";
				break;		
			case 3:
				cssClass += " pgCompareContThreeCol";
				break;		
			case 4:
				cssClass += " pgCompareContFourCol";
				break;		
		}		
		this.oArea.className = cssClass;		
	}
	,
	
	getColumnNumber: function(){
		return this.oArea.getElementsByTagName("TABLE")[0].rows[0].cells.length;
	}
	,
	
	isColHidden : function(index){
		var o = this.oArea.getElementsByTagName("TABLE")[0].rows[0].cells[index];
		return (o.style.display == "none" || o.style.display == "") ? true : false;
	}

}
	

/*

params = {id:"popupID", t:100, r:50, pos:"r", w:500}
id - popup id
t - top popup margin (px)
l - left popup margin (px)
r - right popup margin (px)
pos: "l/r" - popup position - left or right. Default: right
w - width of popup (px)
relEl: id/object -  relative element for set position
*/

var popup = {
	oShim: null,
	oCurrDialog: null,
	
	show: function (params){
		var cDialog = document.getElementById(params.id);
		var cDialogContent = document.getElementById(params.id+"Content");
		var oPopupsOuter = document.getElementById("popupsOuter");
		params.relEl = (params.relEl)? ((typeof(params.relEl)=="string")?document.getElementById(params.relEl):params.relEl) : oPopupsOuter;
		params.t = (params.t)? params.t : 0;
		params.l = (params.l)? params.l : 0;
		params.r = (params.r)? params.r : 0;		
		params.pos = (params.pos)? params.pos : "l";
		/*if(!this.oShim){
			this.oShim = document.createElement('DIV');
			this.oShim.id = 'popupShimOuter';
			this.oShim.innerHTML = '<iframe src="javascript:false" frameBorder="0" scroll="none"></iframe>';
			document.body.appendChild(this.oShim);
			this.oShim.onclick = function(event){popup.autoHide(event)};	
			this.oShim.getElementsByTagName("IFRAME")[0].contentWindow.document.onclick = function(event){window.parent.popup.autoHide(event,1)};
		}
		*/
		cDialog.parentNode.onclick = function(event){popup.autoHide(event)};		
		
		cDialog.style.margin = (this.getPosY(params.relEl)-this.getPosY(oPopupsOuter)+params.t) + "px " + params.r + "px 0 " + params.l + "px";
		
		if(params.w){
			cDialog.style.width = params.w + 'px';
		}	
		this.oCurrDialog = cDialog;
		//this.oShim.style.display = 'block';
		cDialog.style.top = 0;
		cDialog.style.left = 0;		
		
		cDialogContent.innerHTML="Подождите, идет загрузка...&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;&nbsp;<br />";
		Ext.Ajax.request({
			url: '/ajax/regionsmap.php',
			success: function(response, options) {
				var res = Ext.decode(response.responseText);
				if (res && res.html) {
					cDialogContent.innerHTML=res.html+"<br />&nbsp;";
				} else {
					cDialogContent.innerHTML="Ошибка связи. Попробуйте повторить позже.<br />&nbsp;";
				}
			},
			failure:  function(response, options) {
				cDialogContent.innerHTML="Ошибка связи. Попробуйте повторить позже.<br />&nbsp;";
			},
			params: {location: document.location.toString()}
		});
	
	}
	,
	
	hide: function (params){
		document.getElementById('bodyPage').className="";
		var cDialog = document.getElementById(params.id);
		if(this.oShim) {
			this.oShim.style.display = 'none';
		}	
		cDialog.style.top = '-1000em';
		//cDialog.style.left = '-1000em';		
	}
	,
	
	autoHide: function(event,ieFlag) {
		if(this.oCurrDialog){
			if(ieFlag){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;			
				return;
			}
			
			if(!event) var event = window.event;
			var targetElm = (event.target)? event.target : event.srcElement;	
			if(targetElm.id == 'popupShimOuter' || targetElm.id == "popupsOuter"){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;
			}	
		}
	}
	,
	
	getPosY: function(o){
		var y = 0;
			while(o && o.tagName!="BODY"){
				y+= o.offsetTop + (o.clientTop || 0);
				o = o.offsetParent;		
			}
		return y;		
	}	
}

var popupFilter = {
	oShim: null,
	oCurrDialog: null,
	
	show: function (params){
		var cDialog = document.getElementById(params.id);
		var cDialogContent = document.getElementById(params.id+"Content");
		var oPopupsOuter = document.getElementById("popupsOuter");
		params.relEl = (params.relEl)? ((typeof(params.relEl)=="string")?document.getElementById(params.relEl):params.relEl) : oPopupsOuter;
		params.t = (params.t)? params.t : 0;
		params.l = (params.l)? params.l : 0;
		params.r = (params.r)? params.r : 0;		
		params.pos = (params.pos)? params.pos : "l";		
		/*if(!this.oShim){
			this.oShim = document.createElement('DIV');
			this.oShim.id = 'popupShimOuter';
			this.oShim.innerHTML = '<iframe src="javascript:false" frameBorder="0" scroll="none"></iframe>';
			document.body.appendChild(this.oShim);
			this.oShim.onclick = function(event){popup.autoHide(event)};	
			this.oShim.getElementsByTagName("IFRAME")[0].contentWindow.document.onclick = function(event){window.parent.popup.autoHide(event,1)};
		}
		*/
		cDialog.parentNode.onclick = function(event){popup.autoHide(event)};		
		
		cDialog.style.margin = (this.getPosY(params.relEl)-this.getPosY(oPopupsOuter)+params.t) + "px " + params.r + "px 0 " + params.l + "px";
		
		if(params.w){
			cDialog.style.width = params.w + 'px';
		}	
		
		this.oCurrDialog = cDialog;
		
		//this.oShim.style.display = 'block';
		cDialog.style.top = 0;
		cDialog.style.left = 0;		
		
		cDialogContent.innerHTML="&nbsp; &nbsp; &nbsp; Подождите, идет загрузка...<br />&nbsp;";
		Ext.Ajax.request({
			url: '/ajax/regionsForId.php',
			success: function(response, options) {
				var res = Ext.decode(response.responseText);
				if (res && res.regions && res.regions.length>0) {
					if (res.id==1290) {
						printFilterRegionsForeign(res.regions,res.id);
					} else {
						printFilterRegions(res.regions,res.id);
					}
				} else {
					if (!res.regions || res.regions.length<1){
						cDialogContent.innerHTML="&nbsp; &nbsp; &nbsp; Для выбранного региона нет районов.<br />&nbsp;";
					} else {
						cDialogContent.innerHTML="&nbsp; &nbsp; &nbsp; Ошибка связи. Попробуйте повторить позже.<br />&nbsp;";
					}
				}
			},
			failure: function(){
				cDialogContent.innerHTML="&nbsp; &nbsp; &nbsp; Ошибка связи. Попробуйте повторить позже.<br />&nbsp;";
			},
			params: {id: id}
		});
	
	}
	,
	
	hide: function (params){
		var cDialog = document.getElementById(params.id);
		if(this.oShim) {
			this.oShim.style.display = 'none';
		}	
		cDialog.style.top = '-1000em';
		cDialog.style.left = '-1000em';		
	}
	,
	
	autoHide: function(event,ieFlag) {
		if(this.oCurrDialog){
			if(ieFlag){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;			
				return;
			}
			
			if(!event) var event = window.event;
			var targetElm = (event.target)? event.target : event.srcElement;	
			if(targetElm.id == 'popupShimOuter' || targetElm.id == "popupsOuter"){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;
			}	
		}
	}
	,
	
	getPosY: function(o){
		var y = 0;
			while(o && o.tagName!="BODY"){
				y+= o.offsetTop + (o.clientTop || 0);
				o = o.offsetParent;		
			}
		return y;		
	}	
}



var photoGallery = {
	images: new Array(),
	current: 0,
	position: 0,
	length: 0,
	frame: 4,
	ads: 0,
	
	add: function (pathSmall,pathLarge){
		this.images[this.length] = new Array();
		if (this.length<9) {
			this.images[this.length]['small'] = new Image();
			this.images[this.length]['large'] = new Image();
		} else {
			this.images[this.length]['small'] = new Object();
			this.images[this.length]['large'] = new Object();
		}
		this.images[this.length]['small'].src = pathSmall;
		this.images[this.length]['large'].src = pathLarge;
		this.length++;
	},
	
	print: function(){
		html = '<div class="photos">';
		html += '<div class="photo"><a href="#"><img id="photoGalleryImgLarge" onclick="photoGallery.openPopup(); return false"  src="'+this.images[0]['large'].src+'" width="294" height="220" alt=""></a></div>';
		html += '<div class="galleryScope"><table><tr>';
		html += '<td><a id="photoGalleryLinkPrev" onclick="photoGallery.prev(); return false" href="#" class="prev" style="visibility:hidden"></a></td>';
		for (i = 0; i<this.length && i<this.frame; i++) {
			html += '<td><a onclick="photoGallery.setActPosition('+i+'); return false" href="#" class="pic"><img '+((i==0)?'class="act"':'')+' id="photoGalleryImgSmall_'+i+'" src="'+this.images[i]['small'].src+'" width="72" height="55" alt=""></a></td>';
		}	
		html += '<td><a id="photoGalleryLinkNext" onclick="photoGallery.next(); return false" href="#" class="next" '+((this.length<5)?' style="visibility:hidden"':'')+'></a></td>';							
		html += '</tr></table></div>';
		html += '<div class="link"><a href="#" onclick="photoGallery.openPopup(); return false">'+this.length+' фотографи'+((this.length%10==1)?'я':((this.length%10<5)?'и':'й'))+'</a></div>';									
		html += '</div>';
		document.write(html);
	},
	
	next: function(){
		if (this.current<this.length-1){
			if (this.position<this.frame-1) {
				this.setActPosition(this.position+1);
			} else {
				this.current++;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
	},
	
	prev: function(){
		if (this.current>0){
			if (this.position>0) {
				this.setActPosition(this.position-1);
			} else {
				this.current--;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
		
	},
	
	setActPosition: function(position){
		$('photoGalleryImgSmall_'+this.position).className='';
		oldPosition = this.position;
		this.position = position;
		$('photoGalleryImgSmall_'+this.position).className='act';
		this.current=this.current-oldPosition+this.position;
		this.showCurrentImgLarge();
	},

	refreshFrame: function(){
		startFramePos=this.current-this.position;
		for (i=0; i<this.frame && i<this.length; i++){
			$('photoGalleryImgSmall_'+i).src=this.images[i+startFramePos]['small'].src;
		}
	},
		
	correctArrows: function(){
		if (this.current==0) {
			$('photoGalleryLinkPrev').style.visibility='hidden';			
		} else {
			$('photoGalleryLinkPrev').style.visibility='visible';			
		}
		if (this.current==this.length-1) {
			$('photoGalleryLinkNext').style.visibility='hidden';			
		} else {
			$('photoGalleryLinkNext').style.visibility='visible';			
		}
	},
		
	showCurrentImgLarge: function(){
		$('photoGalleryImgLarge').src=this.images[this.current]['large'].src;
		this.correctArrows();
	},
	
	openPopup: function(){
		popup = window.open('/powerSellers/photos/?ads='+this.ads+'&n='+this.current,'popup', 'width=620,height=520');
		popup.focus();	
	}
}

var photoGalleryBig = {
	images: new Array(),
	current: 0,
	position: 0,
	length: 0,
	frame: 7,
	
	add: function (pathSmall,pathLarge,pathOrigin){
		this.images[this.length] = new Array();
		if (this.length<9) {
			this.images[this.length]['small'] = new Image();
		} else {
			this.images[this.length]['small'] = new Object();
		}
		this.images[this.length]['small'].src = pathSmall;
		this.images[this.length]['large'] = new Object();
		this.images[this.length]['large'].src = pathLarge;
		this.images[this.length]['origin'] = new Object();
		this.images[this.length]['origin'].src = pathOrigin;
		this.length++;
	},
	
	print: function(){
		html = '<div class="dlgHeader">\
			<div class="logo"></div>\
			<div class="scope">фотография <span id="photoGalleryCounter">1</span> из '+this.length+'</div>\
		</div>\
		<div class="photos">\
			<div class="galleryPhoto">\
				<table>\
				<tr>\
					<td><a href="#" class="prevN" id="photoGalleryLargeLinkPrev" onclick="photoGalleryBig.prev(); return false"></a></td>\
					<td class="pic"><img id="photoGalleryImgLarge" src="'+this.images[0]['origin'].src+'" height="280" alt=""></td>\
					<td><a href="#" class="next" id="photoGalleryLargeLinkNext" onclick="photoGalleryBig.next(); return false"></a></td>\
				</tr>\
				</table>\
			</div>\
		</div>\
		<div class="galleryScope">\
			<table>\
			<tr>\
				<td><a href="#" class="firstN" id="photoGalleryLinkFirst" onclick="photoGalleryBig.first(); return false"></a></td>\
				<td><a href="#" class="prevN" id="photoGalleryLinkPrev" onclick="photoGalleryBig.prev(); return false"></a></td>\
		';
		for (i = 0; i<this.length && i<this.frame; i++) {
			html += '<td><a onclick="photoGalleryBig.setActPosition('+i+'); return false" href="#" class="pic"><img '+((i==0)?'class="act"':'')+' id="photoGalleryImgSmall_'+i+'" src="'+this.images[i]['small'].src+'" width="72" height="55" alt=""></a></td>';
		}
		html += '\
				<td><a href="#" class="next" id="photoGalleryLinkNext" onclick="photoGalleryBig.next(); return false"></a></td>\
				<td><a href="#" class="last" id="photoGalleryLinkLast" onclick="photoGalleryBig.last(); return false"></a></td>\
			</tr>\
			</table>\
		</div>';		

		document.write(html);
	},
	
	next: function(){
		if (this.current<this.length-1){
			if (this.position<this.frame-1) {
				this.setActPosition(this.position+1);
			} else {
				this.current++;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
	},
	
	prev: function(){
		if (this.current>0){
			if (this.position>0) {
				this.setActPosition(this.position-1);
			} else {
				this.current--;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
		
	},
	
	setActPosition: function(position){
		$('photoGalleryImgSmall_'+this.position).className='';
		oldPosition = this.position;
		this.position = position;
		$('photoGalleryImgSmall_'+this.position).className='act';
		this.current=this.current-oldPosition+this.position;
		this.showCurrentImgLarge();
	},

	setCurrent: function(current){
		if (current<this.length && current>-1){
			this.current=current;
			$('photoGalleryImgSmall_'+this.position).className='';
			if (this.length>this.frame){
				if (this.current<=this.length-this.frame) {
					this.position=0;
				} else {
					this.position=this.frame-(this.length-this.current);
				}
			} else {
				this.position=this.current;
			}
			$('photoGalleryImgSmall_'+this.position).className='act';
			this.refreshFrame();
			this.showCurrentImgLarge();
		}
	},
	
	first: function(){
		this.setCurrent(0);
	},

	last: function(){
		this.setCurrent(this.length-1);
	},
	
	refreshFrame: function(){
		startFramePos=this.current-this.position;
		for (i=0; i<this.frame && i<this.length; i++){
			$('photoGalleryImgSmall_'+i).src=this.images[i+startFramePos]['small'].src;
		}
	},
		
	correctArrows: function(){
		if (this.current==0) {
			$('photoGalleryLinkPrev').className='prevN';
			$('photoGalleryLinkFirst').className='firstN';
			$('photoGalleryLargeLinkPrev').className='prevN';
		} else {
			$('photoGalleryLinkPrev').className='prev';
			$('photoGalleryLinkFirst').className='first';
			$('photoGalleryLargeLinkPrev').className='prev';				
		}
		if (this.current==this.length-1) {
			$('photoGalleryLinkNext').className='nextN';
			$('photoGalleryLinkLast').className='lastN';
			$('photoGalleryLargeLinkNext').className='nextN';			
		} else {
			$('photoGalleryLinkNext').className='next';
			$('photoGalleryLinkLast').className='last';
			$('photoGalleryLargeLinkNext').className='next';				
		}
	},
		
	showCurrentImgLarge: function(){
		$('photoGalleryImgLarge').src=this.images[this.current]['origin'].src;
		$('photoGalleryCounter').innerHTML=this.current+1;
		this.correctArrows();
		
	}
}	

var photoBuildingGallery = {
	images: new Array(),
	current: 0,
	position: 0,
	length: 0,
	frame: 8,
	ads: 0,
    maxlen: 9,
	
	add: function (pathSmall){
		this.images[this.length] = new Array();
		if (this.length < this.maxlen) {
			this.images[this.length]['small'] = new Image();
		} else {
			this.images[this.length]['small'] = new Object();
		}
		this.images[this.length]['small'].src = pathSmall;
		this.length++;
	},
	
	print: function(){
		html = '<div class="photos">';
		html += '<h3>Фотографии дома <span style="color: #757575; font-weight: normal">('+this.length+')</span></h3>';
		//html += '<div class="photo"><a href="#"><img id="photoGalleryImgLarge" onclick="photoBuildingGallery.openPopup(); return false"  src="'+this.images[0]['large'].src+'" width="294" height="220" alt=""></a></div>';
		html += '<div class="galleryScope"><table><tr>';
		html += '<td><a id="photoGalleryLinkPrev" onclick="photoBuildingGallery.prev(); return false" href="#" class="prev" style="visibility:hidden"></a></td>';
		for (i = 0; i<this.length && i<this.frame; i++) {
			html += '<td><a onclick="photoBuildingGallery.setActPosition('+i+'); photoBuildingGallery.openPopup(); return false" href="#" class="pic"><img '+((i==0)?'class="act"':'')+' id="photoGalleryImgSmall_'+i+'" src="'+this.images[i]['small'].src+'" width="72" height="55" alt=""></a></td>';
		}	
		html += '<td><a id="photoGalleryLinkNext" onclick="photoBuildingGallery.next(); return false" href="#" class="next" '+((this.length<5)?' style="visibility:hidden"':'')+'></a></td>';							
		html += '</tr></table></div>';
		html += '<div class="buildingPhotosNotice"><span>Кликните на картинку для увеличения</span></div>';									
		html += '</div>';
		document.write(html);
	},
	
	next: function(){
		if (this.current<this.length-1){
			if (this.position<this.frame-1) {
				this.setActPosition(this.position+1);
			} else {
				this.current++;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
	},
	
	prev: function(){
		if (this.current>0){
			if (this.position>0) {
				this.setActPosition(this.position-1);
			} else {
				this.current--;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
		
	},
	
	setActPosition: function(position){
		$('photoGalleryImgSmall_'+this.position).className='';
		oldPosition = this.position;
		this.position = position;
		$('photoGalleryImgSmall_'+this.position).className='act';
		this.current=this.current-oldPosition+this.position;
		this.showCurrentImgLarge();
	},

	refreshFrame: function(){
		startFramePos=this.current-this.position;
		for (i=0; i<this.frame && i<this.length; i++){
			$('photoGalleryImgSmall_'+i).src=this.images[i+startFramePos]['small'].src;
		}
	},
		
	correctArrows: function(){
		if (this.current==0) {
			$('photoGalleryLinkPrev').style.visibility='hidden';			
		} else {
			$('photoGalleryLinkPrev').style.visibility='visible';			
		}
		if (this.current==this.length-1) {
			$('photoGalleryLinkNext').style.visibility='hidden';			
		} else {
			$('photoGalleryLinkNext').style.visibility='visible';			
		}
	},
		
	showCurrentImgLarge: function(){
			this.correctArrows();
	},
	
	openPopup: function(){
		popup = window.open('/photos/building/?ads='+this.ads+'&n='+this.current,'popup', 'width=620,height=520');
		popup.focus();	
	}
}

var photoBuildingGalleryBig = {
	images: new Array(),
	current: 0,
	position: 0,
	length: 0,
	frame: 7,
	ad_id: 0,
    maxlen: 9,
	   
	add: function (pathSmall,image_id){
		this.images[this.length] = new Array();
		if (this.length < this.maxlen) {
			this.images[this.length]['small'] = new Image();
		} else {
			this.images[this.length]['small'] = new Object();
		}
		this.images[this.length]['small'].src = pathSmall;
		this.images[this.length]['small'].image_id = image_id;
		this.length++;
	},
	
	print: function(){
		html = '<div class="dlgHeader">\
			<div class="logo"></div>\
			<div class="scope">фотография <span id="photoGalleryCounter">1</span> из '+this.length+'</div>\
		</div>\
		<div class="photos">\
			<div class="galleryPhoto">\
				<table>\
				<tr>\
					<td><a href="#" class="prevN" id="photoGalleryLargeLinkPrev" onclick="photoBuildingGalleryBig.prev(); return false"></a></td>\
					<td class="pic"><img id="photoGalleryImgLarge" height="280" alt=""></td>\
					<td><a href="#" class="next" id="photoGalleryLargeLinkNext" onclick="photoBuildingGalleryBig.next(); return false"></a></td>\
				</tr>\
				</table>\
			</div>\
		</div>\
		<div class="galleryScope">\
			<table>\
			<tr>\
				<td><a href="#" class="firstN" id="photoGalleryLinkFirst" onclick="photoBuildingGalleryBig.first(); return false"></a></td>\
				<td><a href="#" class="prevN" id="photoGalleryLinkPrev" onclick="photoBuildingGalleryBig.prev(); return false"></a></td>\
		';
		for (i = 0; i<this.length && i<this.frame; i++) {
			html += '<td><a onclick="photoBuildingGalleryBig.setActPosition('+i+'); return false" href="#" class="pic"><img '+((i==0)?'class="act"':'')+' id="photoGalleryImgSmall_'+i+'" src="'+this.images[i]['small'].src+'" width="72" height="55" alt=""></a></td>';
		}
		html += '\
				<td><a href="#" class="next" id="photoGalleryLinkNext" onclick="photoBuildingGalleryBig.next(); return false"></a></td>\
				<td><a href="#" class="last" id="photoGalleryLinkLast" onclick="photoBuildingGalleryBig.last(); return false"></a></td>\
			</tr>\
			</table>\
			<div class="buildingPhotoGalleryCopyright">&copy; Informap, 2009</div>\
		</div>';		

		document.write(html);
	},
	
	next: function(){
		if (this.current<this.length-1){
			if (this.position<this.frame-1) {
				this.setActPosition(this.position+1);
			} else {
				this.current++;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
	},
	
	prev: function(){
		if (this.current>0){
			if (this.position>0) {
				this.setActPosition(this.position-1);
			} else {
				this.current--;
				this.refreshFrame();
				this.showCurrentImgLarge();
			}
		}
		
	},
	
	setActPosition: function(position){
		$('photoGalleryImgSmall_'+this.position).className='';
		oldPosition = this.position;
		this.position = position;
		$('photoGalleryImgSmall_'+this.position).className='act';
		this.current=this.current-oldPosition+this.position;
		this.showCurrentImgLarge();
	},

	setCurrent: function(current){
		if (current<this.length && current>-1){
			this.current=current;
			$('photoGalleryImgSmall_'+this.position).className='';
			if (this.length>this.frame){
				if (this.current<=this.length-this.frame) {
					this.position=0;
				} else {
					this.position=this.frame-(this.length-this.current);
				}
			} else {
				this.position=this.current;
			}
			$('photoGalleryImgSmall_'+this.position).className='act';
			this.refreshFrame();
			this.showCurrentImgLarge();
		}
	},
	
	first: function(){
		this.setCurrent(0);
	},

	last: function(){
		this.setCurrent(this.length-1);
	},
	
	refreshFrame: function(){
		startFramePos=this.current-this.position;
		for (i=0; i<this.frame && i<this.length; i++){
			$('photoGalleryImgSmall_'+i).src=this.images[i+startFramePos]['small'].src;
		}
	},
		
	correctArrows: function(){
		if (this.current==0) {
			$('photoGalleryLinkPrev').className='prevN';
			$('photoGalleryLinkFirst').className='firstN';
			$('photoGalleryLargeLinkPrev').className='prevN';
		} else {
			$('photoGalleryLinkPrev').className='prev';
			$('photoGalleryLinkFirst').className='first';
			$('photoGalleryLargeLinkPrev').className='prev';				
		}
		if (this.current==this.length-1) {
			$('photoGalleryLinkNext').className='nextN';
			$('photoGalleryLinkLast').className='lastN';
			$('photoGalleryLargeLinkNext').className='nextN';			
		} else {
			$('photoGalleryLinkNext').className='next';
			$('photoGalleryLinkLast').className='last';
			$('photoGalleryLargeLinkNext').className='next';				
		}
	},
		
	showCurrentImgLarge: function(){
	
	Ext.Ajax.request({
		url: '/ajax/getBuildingPhoto.php',
		success: function(response, options) {
				  var res = Ext.decode(response.responseText);
				  $('photoGalleryImgLarge').src = res.url;
				  $('photoGalleryCounter').innerHTML=photoBuildingGalleryBig.current+1;
				  photoBuildingGalleryBig.correctArrows();
				},
		failure: function(){},
		headers: {},
		params: {
			ad_id: this.ad_id,
			photo_id: this.images[this.current]['small'].image_id
		}
	});		
		
	}
}	


/* --new popup ---*/
var multi_popup = {
	oShim: null,
	oCurrDialog: null,
	
	show: function (params){
		$('building_complex_content').innerHTML = '';
	    Ext.Ajax.request({
			url: '/ajax/load_building_complex.php',
			success: function(response, options) {
				var res = Ext.decode(response.responseText);
				$('building_complex_content').innerHTML = res.text;
				multi_popup.showPopup(params);	
					},
			failure:  function(response, options) {
				
			},
			params: {id: params.multi_id}
		});
			
		
	}
	,
	
	
	showPopup: function(params) {
		var Dialog = document.getElementById(params.id);
		var oPopupsOuter = document.getElementById("popupsOuter");
		var h = Dialog.offsetHeight;
		params.relEl = (params.relEl)? ((typeof(params.relEl)=="string")?document.getElementById(params.relEl):params.relEl) : oPopupsOuter;
		params.t = (params.t)? params.t : 0;
		params.l = (params.l)? params.l : 0;
		params.r = (params.r)? params.r : 0;		
		params.pos = (params.pos)? params.pos : "l";	
		if(!this.oShim){
			this.oShim = document.createElement('DIV');
			this.oShim.id = 'popupShimOuter';
			this.oShim.innerHTML = '<iframe src="javascript:false" frameBorder="0" scroll="none"></iframe>';
			document.body.appendChild(this.oShim);
			this.oShim.onclick = function(event){popup.autoHide(event)};	
			this.oShim.getElementsByTagName("IFRAME")[0].contentWindow.document.onclick = function(event){window.parent.popup.autoHide(event,1)};
		}
		
		Dialog.parentNode.onclick = function(event){popup.autoHide(event)};		
		
		Dialog.style.margin = (this.getPosY(params.relEl)-this.getPosY(oPopupsOuter)+params.t)-(h+4) + "px " + params.r + "px 0 " + params.l + "px";
		
		if(params.w){
			Dialog.style.width = params.w + 'px';
		}	
		
		this.oCurrDialog = Dialog;
		
		this.oShim.style.display = 'block';
		Dialog.style.top = 0;
		Dialog.style.left = 0;
	}
	
	,
	
	hide: function (params){
		var Dialog = document.getElementById(params.id);
		if(this.oShim) {
			this.oShim.style.display = 'none';
		}	
		Dialog.style.top = '-1000em';
		Dialog.style.left = '-1000em';		
	}
	,
	
	autoHide: function(event,ieFlag) {
		if(this.oCurrDialog){
			if(ieFlag){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;			
				return;
			}
			
			if(!event) var event = window.event;
			var targetElm = (event.target)? event.target : event.srcElement;	
			if(targetElm.id == 'popupShimOuter' || targetElm.id == "popupsOuter"){
				this.hide({id:this.oCurrDialog.id});
				this.oCurrDialog = null;
			}	
		}
	}
	,
	
	getPosY: function(o){
		var y = 0;
			while(o && o.tagName!="BODY"){
				y+= o.offsetTop + (o.clientTop || 0);
				o = o.offsetParent;		
			}
		return y;		
	}	
}

function showHideFullBlock(elementId){
    var oTd = document.getElementById(elementId);
    if (oTd.style.display != 'none'){
        oTd.style.display = "none";
           document.getElementById('ico'+elementId).className = "ico-set ico-plus";
    } else{
        oTd.style.display = "block";
        document.getElementById('ico'+elementId).className = "ico-set ico-minus";
    }
}

