var max_popupwidth = 720;
var max_popupheight = 540;
// floor plan window
var reapitPopup = null;
function showFloorplan(url,w,h){
	if(reapitPopup && !reapitPopup.closed){reapitPopup.close();reapitPopup = null;}
	var oldw = w; var oldh = h;
	if(w>max_popupwidth){
		w = max_popupwidth;
		h = Math.round(h*w/oldw);
	}
	if(h>max_popupheight){
		h = max_popupheight;
		w = Math.round(oldw*h/oldh);
	}
	reapitPopup = window.open(url,'plan','width='+w+',height='+h+',scrollbars=yes,resizable=yes');
}

// virtual tour
function showVTour(tour,w,h){
	w = (w) ? w : 400;
	h = (h) ? h : 300;
	if(reapitPopup && !reapitPopup.closed){reapitPopup.close();reapitPopup = null;}
	reapitPopup = window.open(tour,'vtour','width='+w+',height='+h+',scrollbars=no,resizable=yes');
}

function showSlideshow(url){
	if(reapitPopup && !reapitPopup.closed){reapitPopup.close();reapitPopup = null;}
	reapitPopup = window.open(url,'slideshow','width=750,height=620,scrollbars=no,resizable=yes');
}


function newInstructionSet(){

	this.instructionUrls = new Array();
	this.counter = 0;
	this.activeWrapper = 0;
	this.attributes = new Object();
	this.divsAligned = false;
	this.div0Opacity = 100;
	this.div1Opacity = 0;
	this.transitionStep = 5;
	this.divWidth = 0;
	this.divHeight = 0;
	this.originalDivHeight = 0;
	this.originalModuleHeight = 0;

	this.setAttribute = function(a,v){
		this.attributes[a] = v;
	}

	this.setAttributes = function(){
		for(var i=0;i<arguments.length;i+=2){
			this.attributes[arguments[i]] = arguments[i+1];
		}
	}

	this.getAttribute = function(a){
		return this.attributes[a];
	}

	this.addInstruction = function(url){

		this.instructionUrls.push(url);

	}

	this.changeInstruction = function(){

		if(!this.divsAligned){

			this.alignDivs();

		}

		var nextWrapper = Math.abs(this.activeWrapper-1);

		if(document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_"+nextWrapper)){

			var thisObj = this;

			ajaxLoadContent("newInstructionWrapper"+this.getAttribute("id")+"_"+nextWrapper,this.instructionUrls[this.counter],function(){ thisObj.switchWrappers() });


			window.setTimeout(function(){ thisObj.changeInstruction() },this.getAttribute("delay")*1000);

		}

	}

	this.alignDivs = function(){

		if((div0 = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_0")) && (div1 = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_1"))){

			moduleDiv =  document.getElementById("module"+this.getAttribute("id"));

			var pos = findPos(div0);
			div1.style.top = pos[1]+"px";

			div0.style.height = "auto";
			div1.style.height = "auto";

			if(this.originalDivHeight == 0){
				this.originalDivHeight = div0.offsetHeight;
				this.originalModuleHeight = moduleDiv.offsetHeight;
			}

			if(div0.offsetWidth){
				if(this.divWidth == 0){
					this.divWidth = div0.offsetWidth;
				}
				this.divHeight=Math.max(this.divHeight,div0.offsetHeight,div1.offsetHeight);
			}else if(div0.style.pixelWidth){
				if(this.divWidth == 0){
					this.divWidth = div0.style.pixelWidth;
				}
				this.divHeight=Math.max(this.divHeight,div0.style.pixelHeight,div1.style.pixelHeight);
			}

			/*if(String(navigator.appName).indexOf("Microsoft") != -1){
				div1.style.zIndex = 0;
			}*/
			div0.style.width=this.divWidth+"px"
			div1.style.width=this.divWidth+"px";

			if(this.getAttribute("autoAdjustHeight")){

				moduleDiv.style.height = (this.originalModuleHeight+(this.divHeight-this.originalDivHeight))+"px";

			}

		}

	}

	this.switchWrappers = function(){

		var div;

		// put old div to the back
		if(div = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_"+this.activeWrapper)){
			div.style.zIndex = 0;
			div.style.position = "absolute";
		}

		this.alignDivs();

		this.activeWrapper = Math.abs(this.activeWrapper-1);

		// put new div to the front
		if(div = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_"+this.activeWrapper)){
			div.style.zIndex = 1;
			div.style.position = "absolute";
		}

		this.loadCarousel();

		this.counter++;
		if(this.counter >= this.instructionUrls.length){
			this.counter = 0;
		}



		this.doTransition();
	}


	this.doTransition = function(){

		var div0, div1;

		if((div0 = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_0")) && (div1 = document.getElementById("newInstructionWrapper"+this.getAttribute("id")+"_1"))){

			if(this.activeWrapper == 0){

				this.div0Opacity += this.transitionStep*this.getAttribute("transitionSpeed");

				if(this.div0Opacity >= 100){
					setOpacity(div0,100);
					setOpacity(div1,0);
					return;
				}

			}else{

				this.div0Opacity -= this.transitionStep;

				if(this.div0Opacity <= 0){
					setOpacity(div0,0);
					setOpacity(div1,100);
					return;
				}

			}

			this.div1Opacity = 100 - this.div0Opacity;

			setOpacity(div0,this.div0Opacity);
			setOpacity(div1,this.div1Opacity);

			var thisObj = this;

			window.setTimeout(function(){ thisObj.doTransition() },50);

		}

	}

	this.loadCarousel = function(){

		if(this.getAttribute("useCarousel")){

			var carouselObj = new SWFObject(this.getAttribute("carouselSWF"), "carousel"+this.getAttribute("id")+"_"+this.counter, this.getAttribute("thumbnailWidth"), this.getAttribute("thumbnailHeight"), "6.0.65", "#FFFFFF");
			carouselObj.addParam("quality", "high");
			carouselObj.addParam("scale", "noscale");
			carouselObj.addParam("salign", "LT");
			carouselObj.addParam("flashvars", "xmlfile="+escape(this.getAttribute("cacheFolder")+"carousel-"+this.getAttribute("id")+"_"+this.counter+".xml"));
			carouselObj.addParam("wmode", "transparent");
			carouselObj.write("carousel"+this.getAttribute("id")+"_"+this.counter);

		}

	}
}




function saveWebStat(dbtype,rps,pcode,type){

	params = "rps="+rps+"&dbtype="+dbtype+"&pcode="+pcode+"&type=";
	params += (type == null) ? "d" : type;

	jsonServerProcess("savewebstat",params);

}

function shorthandNumber(num,k,unit){
	k = (k==null) ? 1000 : k;
	unit = (unit == null) ? "" : unit;
	suffix_arr = (k == 1024) ? new Array(" "," K"," M"," G"," T") : new Array("","K","M","B");
	var i=0;
	while(num >= k && i<suffix_arr.length){
		num = num/k;
		i++;
	}
	i = (i>=suffix_arr.length) ? suffix_arr.length-1 : i;
	num = Math.round(num*1000)/1000;
	return num+suffix_arr[i]+unit;
}


function populatePriceDropDowns(id,type){// module id, sales|lettings
	var prices, text, selected;
	if((prices = eval(type+"PriceBands"+id)) != null){
		prices = prices.split(",");
		var minSelected = (eval("typeof(minSelected"+type+id+") != 'undefined'")) ? eval("minSelected"+type+id) : "";
		var maxSelected = (eval("typeof(maxSelected"+type+id+") != 'undefined'")) ? eval("maxSelected"+type+id) : "~";
		if((minSelect = window.document.getElementById("minPriceDropDown"+id)) != null && (maxSelect = window.document.getElementById("maxPriceDropDown"+id)) != null){
			clearDropDown(minSelect);
			clearDropDown(maxSelect);
			addToDropDown(minSelect,0,0);
			for(var i=0;i<prices.length;i++){
				if(type == "lettings"){
					if(eval("monthlyRents"+id+" == true")){
						text = numberFormat(prices[i])+"pcm ("+numberFormat(prices[i]*12/52,0)+"pw)";
					}else{
						text = numberFormat(prices[i])+"pw ("+numberFormat(prices[i]*52/12,0)+"pcm)";
					}
				}else{
					text = shorthandNumber(prices[i]);
				}
				selected = (prices[i] == minSelected);
				addToDropDown(minSelect,prices[i],text,selected);
				selected = (prices[i] == maxSelected);
				addToDropDown(maxSelect,prices[i],text,selected);
			}
			selected = (maxSelected == "~" || maxSelected == null);
			addToDropDown(maxSelect,"~","No limit",selected);
			//maxSelect.selectedIndex =   maxSelect.length - 1;
		}
	}
}
