function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v; }
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

jQuery(function (){
	clearInputs();
	
	// Accordion
	jQuery("#accordion").accordion({ header: "h3", autoHeight: false });
	
	// Tabs
	jQuery('.tabs').tabs();
	
	// Product Page gallery
	initAdGallery('#ad-gallery');
	// Home Gallery
	jQuery('div.gallery').gallery({
		duration: 500,
		effect: 'fade',
		autoRotation: 5000,
		listOfSlides: '> ul.slides > li',
		switcher:'.switcher li',
		creatSwitcher:true
	});
});

function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		_el.data('val', _el.val());
		_el.bind('focus', function(){
			if(_el.val() == _el.data('val')) _el.val('');
		}).bind('blur', function(){
			if(_el.val() == '') _el.val(_el.data('val'));
		});
	});
}

function initAdGallery(context){
	var galBlock = jQuery(context);
	if(galBlock.length){
		var galleries = galBlock.adGallery({
			effect: 'fade',
			loader_image: 'img/loader.gif'
		});
		galBlock.find('.ad-controls .prev').click(function (){
			galBlock.find('.ad-prev').click();
		});
		galBlock.find('.ad-controls .next').click(function (){
			galBlock.find('.ad-next').click();
		});
	}
};

(function($) {
	jQuery.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	function Gallery(context, options) { this.init(context, options); };
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				infinite: false,								//true = infinite gallery
				duration: 700,									//duration of effect it 1000 = 1sec
				slideElement: 1,								//number of elements for a slide
				autoRotation: false,							//false = option is disabled; 1000 = 1sec
				effect: false,									//false = slide; true = fade
				listOfSlides: 'ul > li',						//elements galleries
				switcher: false,								//false = option is disabled; 'ul > li' = elements switcher
				disableBtn: false,								//false = option is disabled; 'hidden' = class adds an buttons "prev" and "next"
				nextBtn: 'a.link-next, a.btn-next, a.next',		//button "next"
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',		//button "prev"
				circle: true,									//true = cyclic gallery; false = not cyclic gallery
				direction: false,								//false = horizontal; true = vertical
				event: 'click',									//event for the buttons and switcher
				IE: false,										//forced off effect it "fade" in IE
				autoHeight: false,								//auto height on fade
				creatSwitcher: false
			}, options || {});
			var _el = jQuery(context).find(this.options.listOfSlides);
			this.count = _el.index(_el.filter(':last'));
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			this.nextBtn = jQuery(context).find(this.options.nextBtn);
			this.prevBtn = jQuery(context).find(this.options.prevBtn);
			if(this.options.switcher) {
				if(this.options.creatSwitcher){
					var _list = "<div class='switcher'><div><ul><li class='active'><a href='#'>"+ 1 + "</a></li>";
					for(var i=2;i<this.count+2;i++){
						_list+="<li>"+ i + "</li>";
					}
					_list+="</ul></div></div>";
					jQuery(_list).appendTo(jQuery(context));
				}
				this.switcher = jQuery(context).find(this.options.switcher);
			}
			
			if (this.options.switcher){
				this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			}
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.anim = '{marginLeft: -(this.woh * this.active)}';
				else this.anim = '{marginTop: -(this.woh * this.active)}';
				eval('this.list.css('+this.anim+')');
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				if(this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
				if(this.options.autoHeight) this.list.parent().css({height: this.list.eq(this.active).outerHeight()});
			}
			this.flag = true;
			if (this.options.infinite){
				this.count++;
				this.active += this.count;
				this.list.append(_el.clone());
				this.list.append(_el.clone());
				eval('this.list.css('+this.anim+')');
			}
			
			this.initEvent(this, this.nextBtn, true);
			this.initEvent(this, this.prevBtn, false);
			if (this.options.disableBtn) this.initDisableBtn();
			if (this.options.autoRotation) this.runTimer(this);
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
		},
		initDisableBtn: function(){
			this.prevBtn.removeClass('prev-'+this.options.disableBtn);
			this.nextBtn.removeClass('next-'+this.options.disableBtn);
			if (this.active == 0 || this.count+1 == this.wrapHolderW) this.prevBtn.addClass('prev-'+this.options.disableBtn);
			if (this.active == 0 && this.count == 1 || this.count+1 <= this.wrapHolderW) this.nextBtn.addClass('next-'+this.options.disableBtn);
			if (this.active == this.rew) this.nextBtn.addClass('next-'+this.options.disableBtn);
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.ceil(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderW--;
		},
		fadeElement: function(){
			if (jQuery.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					jQuery(this).css('opacity','auto');
				}});
			}
			if(this.options.autoHeight) this.list.parent().animate({height: this.list.eq(this.active).outerHeight()}, {queue:false, duration: this.options.duration});
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function($this){
			if (!$this.options.infinite) eval('$this.list.animate('+$this.anim+', {queue:false, duration: $this.options.duration});');
			else eval('$this.list.animate('+$this.anim+', $this.options.duration, function(){ $this.flag = true });');
			if ($this.options.switcher) $this.switcher.removeClass('active').eq($this.active / $this.options.slideElement).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				if ($this.options.infinite) $this.flag = false;
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index(jQuery(this)) * $this.options.slideElement;
				if($this._t) clearTimeout($this._t);
				if ($this.options.disableBtn) $this.initDisableBtn();
				if (!$this.options.effect) $this.scrollElement($this);
				else $this.fadeElement();
				if ($this.options.autoRotation) $this.runTimer($this);
				return false;
			});
		},
		initEvent: function($this, addEventEl, dir){
			addEventEl.bind($this.options.event, function(){
				if ($this.flag){
					if ($this.options.infinite) $this.flag = false;
					if($this._t) clearTimeout($this._t);
					$this.toPrepare($this, dir);
					if ($this.options.autoRotation) $this.runTimer($this);
				}
				return false;
			});
		},
		toPrepare: function($this, side){
			if (!$this.options.infinite){
				if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
				if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
				for (var i = 0; i < $this.options.slideElement; i++){
					if (side) { if ($this.active + 1 <= $this.rew) $this.active++; }
					else { if ($this.active - 1 >= 0) $this.active--; }
				};
			}
			else{
				if ($this.active >= $this.count + $this.count && side) $this.active -= $this.count;
				if ($this.active <= $this.count-1 && !side) $this.active += $this.count;
				eval('$this.list.css('+$this.anim+')');
				if (side) $this.active += $this.options.slideElement;
				else $this.active -= $this.options.slideElement;
			}
			if (this.options.disableBtn) this.initDisableBtn();
			if (!$this.options.effect) $this.scrollElement($this);
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
})(jQuery);

jQuery(document).ready(function(){
	// infinite gallery
	jQuery('div.gallery').gallery({
		infinite: true,
		duration: 500,
		autoRotation: 5000,
		listOfSlides: '> div.image > ul > li'
	});
	// slide gallery
	var gal1 = jQuery('div.gallery1').gallery({
		duration: 500,
		autoRotation: 5000,
		listOfSlides: 'div > ul > li'
	});
	// fade gallery
	var gal2 = jQuery('div.gallery2').gallery({
		duration: 700,
		autoRotation: 4000,
		listOfSlides: 'div > ul.foto > li',
		switcher: 'ul.switcher > li',
		effect: true
	});
	// turn off auto rotation gal1
	gal1.stop();
	// turn on auto rotation gal2
	gal2.stop();
});

jQuery(document).ready(function(){ 
	
	jQuery('.buyrent').change(function() {
 
		
		if(jQuery(this).val() == 'b') { 
		
			jQuery('.rentsearch').hide();
			jQuery('.propsearch').show();
			jQuery('.renttown').hide();
			jQuery('.proptown').show();
			
		} else {
			
			jQuery('.rentsearch').show();
			jQuery('.propsearch').hide();
			jQuery('.renttown').show();
			jQuery('.proptown').hide();
		}
		
	});
	

	
	if ("#map_canvas" && jQuery.isFunction(window.initialize)) {
		
		initialize();	 
	}
	
	 
	
});

	jQuery.fn.exists = function(){return jQuery(this).length>0;}

function validate(form) {
var mes = "";
 
if (document.form1.yourname.value == false) {
mes = mes+"\nPlease enter your name"; 
}
if ( verifyEmail(document.form1.email.value) == false ) {
mes = mes+"\nPlease enter your valid email address";

}
if (document.form1.terms.checked == false) {
mes = mes+"\nPlease tick the terms box"; 
} 

if (mes) {
	 
		alert(mes+"\n\nPlease complete before continuing.");
		return false;
		
	}  else {
		return true;
	}

} 


function validate2(form2) {
var mes = "";
 
if (document.form2.my_name.value == false) {
mes = mes+"\nPlease enter your name"; 
}
if ( verifyEmail(document.form2.my_email.value) == false ) {
mes = mes+"\nPlease enter your valid email address";

}
if (document.form2.to_name.value == false) {
mes = mes+"\nPlease enter your recipient's name"; 
}
if ( verifyEmail(document.form2.to_email.value) == false ) {
mes = mes+"\nPlease enter a valid recipient email address";

} 

if (mes) {
	 
		alert(mes+"\n\nPlease complete before continuing.");
		return false;
		
	}  else {
		return true;
	}

} 

function verifyEmail(checkEmail) {

if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))) 
{
return false;
} 




} 
