function $E(tag, props) {
  if (typeof props == 'string')
    props = { style : props };
  if (typeof tag == 'string') {
    var id = tag.match(/#([\w-]+)/);
    var classes = tag.match(/(?:\.[\w-]+)+/);
    tag = tag.replace(/[#.].*/, '');
    props = props || {};
    if (id) props.id = id[1];
    if (classes) props['class'] = classes[0].replace(/\./g, ' ');
  }
  return new Element(tag || 'div', props);
};

var settings = {
	
	defaultPage:'home',
	listingType:'',
	menuType:'',
	headerLoaded: false,
	headerType: '',
	lastsubpage:{},
	curTime: new Date(),
	curFeatured: [],
	fTimer: null,
	myShow: null,
	curAgentID: null,

	hash:{
		val:null,
		interval:null,
		poll:100,
		check:function(){
			var h=window.location.hash;
			if(settings.hash.val!=h){
				settings.hash.val=h;
				if(h){ settings.hash.parse(h); }
				else{ ipm.gotoPage('home'); };
			};
		},
		parse:function(h){
			if( h.substring(0,2)=='#/' ){
				var sh=h.substring(2).split('/');
				/* NEED A HASH VALIDATION CHECK */
				if(sh.length>1){ if(sh[0]=='results') { ipm.gotoResults(sh[1],sh[2]); } else { ipm.gotoPage(sh[0],sh[1]); } }
				else if(sh){ ipm.gotoPage(sh[0]); };
			};
		}
	}

}

var ipm_tween = null;

var ipm = {
	
	interupt:false,
	
	int: function() {
	
		if(settings.menuType=='default') {
			//new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+2), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':settings.menuType,'xhr':'menu'}, update: 'ipm_menu', evalScripts: true}).send();
		}
		ipm.menu();
		
		$('ipm_content').setStyle('min-height',$('ipm_menu').getHeight().toInt() + 'px');
		
		$('ipm_logo').addEvent('click',function() { ipm.gotoPage('home'); });
	
	},
	
	run: function(v,a) {
		
		try { var f = ipm[v]; f(a); } catch (e) { alert(e); }
		
	},
	
	title: function(v) {
		document.title = ((v ? (v + ' - ') : '') + 'My Property');
	},
	
	featured: function() {
		
		$$('a[rel=ipm_featured]').each(function(el) {
		
			el.addEvent('click',function(e) {
				var e = new Event(e).stop();
				$$('a[rel=ipm_featured]').setOpacity(0.5);
				var h = el.getProperty('href');
				new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+1), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'reset','xhr':'results'}, onComplete: function() { window.location.href = h; }}).send();
			});
		
		});
		
	},
	
	home: function() {
		
		ipm.run('title');
		
		clearTimeout(settings.fTimer);
		$('ipm_header_search').removeEvents();
		
		if(settings.headerType!='search.sale') {
			settings.listingType = 'sale';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+1), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'search','subpage':settings.listingType,'xhr':'header'}, update: 'ipm_header_search', evalScripts: true}).send();
		}
		
		if(settings.menuType!='default') {
			settings.menuType = 'default';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+2), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'default','xhr':'menu'}, update: 'ipm_menu', evalScripts: true}).send();
		}
		
		ipm.run('featured');
		
	},
	
	header: function() {
	
		$('click_map').addEvent('click',function() { $('click_map').setStyle('display','none'); });
		$('ipm_map').addEvent('click',function() { $('click_map').setStyle('display','none'); });
		
		clearTimeout(settings.fTimer);
		$('ipm_header_search').removeEvents();
	
	},
	
	getValue: function(field) {
		
		return ($(field) ? $(field).getProperty('value') : false);
		
	},
	
	gotoPage:function(page,subpage){
		var whash=window.location.hash;
		var expectedHash='#/'+($chk(page)?page:'home')+($chk(subpage)?'/'+subpage:'');
		if(window.location.hash!=expectedHash){
			ipm.changeHash(expectedHash);
			return false;
		}; settings.curTime = new Date();
		new Request.HTML({url: '/?xhr=' + settings.curTime.getTime(), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':page,'subpage':subpage,'xhr':'content'}, update: 'ipm_content', evalScripts: true, noCache: true, onRequest: function() { window.scrollTo(0,0); window.removeEvents('ajax-complete'); $('ipm_loader').setStyle('display','block').set('html','Loading'); }, onComplete: function() { $('ipm_loader').setStyle('display','none'); window.fireEvent('ajax-complete'); $('ipm_content').setStyle('min-height',$('ipm_menu').getHeight().toInt() + 'px'); }}).send();
		if(page=='search'&&settings.menuType!='search') {
			//settings.menuType = 'search';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+2), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'search','xhr':'menu','data':subpage}, update: 'ipm_menu', evalScripts: true}).send(); 	
		}
		if((page=='listings'&&settings.menuType!='listings')||(settings.curAgentID!=subpage&&page=='listings')) {
			//settings.menuType = 'listings';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+3), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'listings','xhr':'menu','data':subpage}, update: 'ipm_menu', evalScripts: true}).send(); 	
		}
		if((page=='agent'&&settings.menuType!='agent')||(settings.curAgentID!=subpage&&page=='agent')) {
			//settings.menuType = 'listings';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+4), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'agent','xhr':'menu','data':subpage}, update: 'ipm_menu', evalScripts: true}).send(); 	
		}
		if(page=='results'&&settings.menuType!='results') {
			//settings.menuType = 'results';
			new Request.HTML({url: '/?xhr=' + (settings.curTime.getTime()+5), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'results','xhr':'menu'}, update: 'ipm_menu', evalScripts: true}).send(); 	
		}
		//ipm.debug('New ajax call via XHR');
		$$('.tooltip').dispose();
		return false;
	},
	
	gotoResults:function(sessionid,data){
		var whash=window.location.hash;
		var expectedHash='#/results'+($chk(sessionid)?'/'+sessionid+(data ? '/'+data : ''):'');
		if(window.location.hash!=expectedHash){
			ipm.changeHash(expectedHash);
			return false;
		}; settings.curTime = new Date();
		if(data) {
			new Request.HTML({url: '/?xhr=' + settings.curTime.getTime(), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'results','subpage':sessionid,'xhr':'results','data':data}, update: 'ipm_content', evalScripts: true, noCache: true, onRequest: function() { window.removeEvents('ajax-complete'); $('ipm_loader').setStyle('display','block').set('html','Loading'); }, onComplete: function() { $('ipm_loader').setStyle('display','none'); window.fireEvent('ajax-complete'); try { Slimbox.scanPage(); } catch(err) {} }}).send();	
		} else {
			new Request.HTML({url: '/?xhr=' + settings.curTime.getTime(), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'results','subpage':sessionid,'xhr':'results'}, update: 'ipm_content', evalScripts: true, noCache: true, onRequest: function() { window.removeEvents('ajax-complete'); $('ipm_loader').setStyle('display','block').set('html','Loading'); }, onComplete: function() { $('ipm_loader').setStyle('display','none'); window.fireEvent('ajax-complete'); try { Slimbox.scanPage(); } catch(err) {} }}).send();
		}
		return false;
	},
	
	changeHash:function(hash){
		window.location.replace(hash);
		//ipm.debug('Forced hash replace: '+hash);
	},
	
	debug:function(message){
		return false;
		if($chk($('debug_ul'))){
			var newli=new Element('li',{'html':message,'events':{'dblclick':function(){this.destroy();}}});
			newli.inject($('debug_ul'),'top');
		};
	},
	clearDebug:function(){
		if($chk($('debug_ul'))){ $('debug_ul').empty(); };
		return false;
	},
	
	addOption: function(selectId, txt, val, defSel){
		// if Safari/Konqueror
		//if(!$(selectId)) { console.log(selectId + ' does not exist in DOM Tree!'); return false; }
		if(window.webkit) {	
			var element = $(selectId);
			var newone = new Option(txt,val,defSel);
			element.add(newone,element.options[element.options.length]);
		// if Internet Explorer (any)
		} else if(window.ie) {
			var objOption = new Option(txt, val, defSel, defSel);
			$(selectId).options.add(objOption);
		// if Mozilla/Gecko/Opera
		} else {
			var objOption = new Option(txt, val, defSel);
			$(selectId).options.add(objOption);
		}
	},
	
	clearToolTips: function() {
		
		$$('.tooltip').dispose();
		
	},
	
	menu: function() {
		
		$$('.ipm_hideshow').each(function(el) {
			
			el.addEvent('click',function() {
			
				if($(el.getProperty('rel'))) {
				
					$(el.getProperty('rel')).toggleClass('hide');
					
					if($(el.getProperty('rel')).hasClass('hide')) {
						el.removeClass('on');
						el.addClass('off');
					} else {
						el.addClass('on');
						el.removeClass('off');
					}
				
				}
			
			});
			
		});
		
	},
	
	clearOptionList: function(selectId){
		if($(selectId)) $(selectId).empty();
	},
	
	menuFX: function() {
	
		var offSet = '280';
		if(ipm_tween) ipm_tween.cancel();
		if((parseInt(window.getScrollTop())<=offSet)&&($('ipm_menu').style.top==offSet)) { if(ipm_tween) { ipm_tween.cancel(); }; return false; }
		ipm_tween = new Fx.Tween('ipm_menu',{property: 'top', duration:'long'}).start($('ipm_menu').style.top,((parseInt(window.getScrollTop())<offSet) ? parseInt(offSet) : parseInt(window.getScrollTop())));
	
	},
	
	slideshow: function(data) {
		if(settings.myShow) { settings.myShow.destroy(0); (function(){ settings.myShow = new Slideshow('ipm_show', data, { controller: false, thumbnails: true, linked: true }); }).delay(100); } else { 
		settings.myShow = new Slideshow('ipm_show', data, { controller: false, thumbnails: true, linked: true }); }
	},
	
	runRecentListings: function(id) {
	
		new Request.HTML({url: '/?xhr=' + settings.curTime.getTime(), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'recently','subpage':'sale','xhr':'menu','listingID':id}, update: 'ipm_recent_container', evalScripts: true, noCache: true, onRequest: function() {  }, onComplete: function() {  }}).send();
	
	},
	
	doClick: function(id,aID) {
		
		new Request.HTML({url: '/?xhr=' + settings.curTime.getTime(), method: 'post', data: {'ipm_ssid':ipm_ssid,'page':'do','subpage':'click','xhr':'listings','listingID':id,'agentID':aID}, noCache: true}).send();
		
	}

}

function addOption(selectId, txt, val, defSel) { ipm.addOption(selectId, txt, val, defSel); }
function clearOptionList(selectId) { ipm.clearOptionList(selectId); }

window.addEvent('domready',function(){
	new Asset.css('/css/style.php?c=autocompleter', {id: 'Autocompleter.css', title: 'TextboxList'});
	//new Asset.css('/css/style.php?c=textboxlist', {id: 'TextboxList.css', title: 'TextboxList'});
	//new Asset.css('/css/style.php?c=textboxlist.autocomplete', {id: 'TextboxList.Autocomplete.css', title: 'TextboxList.Autocomplete'});
	new Asset.javascript('/js/GrowingInput.js', { id: 'GrowingInput.js' });
	new Asset.javascript('/js/Observer.js', { id: 'Observer.js' });
	new Asset.javascript('/js/Autocompleter.js', { id: 'Autocompleter.js' });
	new Asset.javascript('/js/Autocompleter.Request.js', { id: 'Autocompleter.Request.js' });
	
	new Asset.javascript('/js/slideitmoo.js', { id: 'slideitmoo.js' });
	/*
	new Asset.javascript('/js/TextboxList.js', { id: 'TextboxList.js' });
	new Asset.javascript('/js/TextboxList.Autocomplete.js', { id: 'TextboxList.Autocomplete.js' });
	new Asset.javascript('/js/TextboxList.Autocomplete.Binary.js', { id: 'TextboxList.Autocomplete.Binary.js' });
	*/
	new Asset.css('/css/style.php?c=debug', {id: 'debug', title: 'debug'});
	//ipm.debug('DOM Ready');
	if( $chk($('interupt')) ){ ipm.interupt=true; };
	if( !ipm.interupt ){
		settings.hash.check();
		settings.hash.interval=setInterval("settings.hash.check()",settings.hash.poll);
	}; ipm.int();
	ipm.menuFX();
	if(!window.ie) { window.addEvent('scroll', ipm.menuFX); } else { window.onscroll=ipm.menuFX; }
	
});
