/* Copyright Property Sol */

$(function() {
	$.place_data=new Array();
	$.place_id=0;
	
	$tabs=$(".switch-tabs > ul").tabs();	
//	$('.sr li').srLi();
	inputTip('#quick_prop_id','Property Reference');
	inputTip('#place-suggest','Find Location');
	inputTip('#user_email','Email Address');
	//inputTip('#user_password','Password');
	$(".lightbox a").lightbox({
		fileLoadingImage: '_src/images/lightbox-loading.gif',
		fileBottomNavCloseImage: '_src/images/lightbox-close.gif'
	});

	$('select[name="place_id"]').bind('change',function(){
		document.location.href ="index.php?go=search&area=" + $(this).val();
	});
	
	$('#searchform').bind('submit',function(){
		return submitSearchForm();
	});
 	$('.hideable').hideable();	
	$('#msgs p').hideable();
	$('#tsr').bind('change',function(){
		checkSearchForm();
		
	});
	
	checkSearchForm();

	$('#place_name').bind('keyup',function(){
		$.get('ajax.php?go=misc&do=getplacenames&current='+$('#place_id_current').val()+'&place_name='+$(this).val(),function(d){
			$('#place_names').html(d);
		});
	});
	
	
	$('#tsr').change(function(){placeData();});
	$('#search_areas select').live('change',function(){
		
		$(this).attr('name','area'); // set last touched to active.
		var thisarea=$(this).val();
		$.place_id=thisarea; //global
		var p=$(this).parent();
		p.find('ul').remove();
		if ($(this).find('option:selected').hasClass('all')) return;
		var loady=$('<span>Loading...</span>');
		loady.appendTo(p);
		//url='index.php?go=place&inc=ajax&do=kids&pid='+$(this).val();
		url='_data/places/'+thisarea+'.json';
		$.getJSON(url,function(d){
			$.place_data[thisarea]=d;
			if (d!=null && d.kids.length>0)
			{
				$(this).attr('name','');
				
				var ul=$('<ul><li><select name="area"><option value="'+thisarea+'" class="all">- All -</option></select></li></ul>');
				$.each(d.kids,function(i,kid){
					if (kid!=null)
					{
						$('<option'+(kid.total==0?' class="none"':'')+' value="'+kid.id+'" total="'+kid.total+'" sale="'+kid.sale+'" rent="'+kid.rent+'">'+kid.name+' ('+kid.total+')</option>').appendTo(ul.find('select'));
					}

				});

				ul.appendTo(p);
				
				//alert (ul.html());
			}
			placeData();
			loady.remove();
		});

		
	});
});


function placeData()
{
	var types;
	if ($('#tsr').val()=='ts') types=$.place_data[$.place_id].types_sale;
	else types=$.place_data[$.place_id].types_rent;
	
	$('#search_areas option').each(function(){
		var count;
		if ($(this).attr('total')!=undefined)
		{
			if ($('#tsr').val()=='ts') count=$(this).attr('sale');
			else count=$(this).attr('rent');
			$(this).text($(this).text().replace(/\(.*\)/,'('+count+')'));
			if (count==0) $(this).addClass('none');
			else $(this).removeClass('none');
		}
	});
	
	$('#search_types span.c').text('');
	$('#search_types label').addClass('off');
	$('#search_types input').attr('disabled','disabled');
	if (types!=null)
	{
		$.each(types,function(i,v){
			var el=$('#search_types label[for="pt'+i+'"]');
			el.find('span.c').text('('+v+')');
			el.find('input').attr('disabled','');
			el.removeClass('off');
		});
	}
}


function checkSearchForm()
{
	$('.checkul').hide(0);
	$('.checkul_'+$('#tsr').val()).show(0);
	
}

function submitSearchForm()
{
	$('select[name=area]:not(:last)').attr('disabled','disabled');
	pts=new Array();
	$('#search_types input[type=checkbox]').each(function(){
		if ($(this).is(':checked')) pts.push($(this).val());
		$(this).attr('disabled','disabled');
	});
	$('<input type="hidden" name="pt" value="'+pts.join(',')+'" />').appendTo('#searchform');
	return true;
	
}


function inputTip (element,tip)
{
	$(element).bind('blur',function(){
		$(element).addClass('disabled');
		if ($(element).val()=='') {$(element).val(tip);}
	}).bind('focus',function(){
		$(element).removeClass('disabled');
		if ($(element).val()==tip) {$(element).val('');}
		else $(element).select();
	});
}

/*
jQuery.fn.extend({
	drops: function(options) {
		return this.each(function(i) {
			$(this).children('li').each(function()
			{
				var whole=$(this);
				var id=$(this).attr('id');
				var body=$(this).children('.bd');
				var classname;
				var cookiename='user-drop-'+id;
				var link=$(this).find('h3 a');
				var url=false;
				var top=false;
				if (link.length>0) {
					realurl=$(link).attr('href');
					url=realurl.replace(/index.php/g,'ajax.php'); // replace index.php link with ajax.php
					top='<p><a href="'+realurl+'"><em>Full view &raquo;</em></a></p>';
					$(this).find('h3').html($(link).html()).dblclick(function(){document.location.href =realurl}); // replace h3>a>contents with h3>contents
					
					//$(link).unbind('click');
				}
				if ($.cookie(cookiename)>0) 
				{
					classname='drop-click-open';
					$(body).show();
					if (url) {
						$(body).html('<p>Loading...</p>');
						$.get(url,function(data){
							$(body).html(top+data);
						});
					}
				}
				else
				{
					classname='drop-click-closed';
					$(body).hide();
				}
				$(whole).prepend(' <span class="drop-click '+classname+'"></span>');
				$(whole).children('h3')
				.attr('title','Click to open/close')
				.css({cursor:'pointer'})
				.click(function(){
					// is it up or down?
					el=$(whole).children('.drop-click');
					if (el.hasClass('drop-click-open'))
					{
						$(el).removeClass('drop-click-open').addClass('drop-click-loading');	
						$.cookie(cookiename, -1);
						$(body).slideUp(250);
						setTimeout(function(){$(el).removeClass('drop-click-loading').addClass('drop-click-closed');},250);	
					}
					else
					{
						$(el).removeClass('drop-click-closed').addClass('drop-click-loading');	
						$.cookie(cookiename, 1);
						if (url) {
							$(body).html('<p>Loading...</p>').show();
							$.get(url,function(data){
								$(body).hide().html(top+data).slideDown(250);
								setTimeout(function(){$(el).removeClass('drop-click-loading').addClass('drop-click-open');},250);	
							});
						}
						else
						{
							$(body).slideDown(250);
							setTimeout(function(){$(el).removeClass('drop-click-loading').addClass('drop-click-open');},250);	
						}
					}
				});
			})
		});
	}
});
*/

jQuery.fn.extend({
	hideable: function(options) {
		return this.each(function(i) {
			var el = this;
			var ele = $(this);
			ele.prepend(' <span class="hideable-link" style="float:right;cursor:pointer;"><span>&lt;</span> <em>Hide</em></span>');
			ele.children('.hideable-link').each(function(){
				$(this).mouseover(function(){ele.css({opacity:'0.4'})})
					.mouseout(function(){ele.css({opacity:1})})
					.click(function(){ele.hide(250);})
					;
			});
		});
	}
});

xfeat=new Array();
jQuery.fn.extend({
	livedir: function() {
		return this.each(function(i) {
			var el = this;
			el.children = $(this).find('span');
			//el.index = i;
			//el.totalChildren = el.children.size();
			//alert(el.totalChildren);
			el.children.each(function() {
				var em = $(this);
				em.html('&nbsp;').addClass('live-dir-dropdown').click(function(){});
				em.click(function(){
					em.addClass('live-dir-dropdown-loading');
					$('#live-dir-results').hide();
					var place_id=$(this).attr('id').split('_')[1];
					var offset = em.offset();
					$.get('ajax.php?go=place&do=kids&pid='+place_id,function(data){
						data=data.replace(/^\s+|\s+$/g,"");
						var tokens = data.split("\n");
						var tokens2='<ul>';
						for (i=0;i<=tokens.length;i++)
						{
							if (tokens[i]) 
							{
								var temp=tokens[i].split('~');
								var token_place_id=temp[0];
								var token_text=temp[1];
								tokens2+='<li><a href="index.php?go=directory&amp;place_id='+token_place_id+'">'+token_text+'</a></li>';
							}
						}
						tokens2+='</ul>';
						$('#live-dir-results')
						.html('<div>'+tokens2+'</div>')
						.css({left:(offset.left)-5})
						.slideDown()
						.removeClass('live-dir-dropdown-loading')
						.addClass('live-dir-dropdown-open')
						.hover(function(){},function(){
							$(this).slideUp();
							$('.live-dir-dropdown').removeClass('live-dir-dropdown-open').removeClass('live-dir-dropdown-loading');
						});
					});
				});
			});
		});
	}
});

// Search results LI elements
/*
jQuery.fn.extend({
	srLi: function() {
		return this.each(function(i) {
			var prop_id=$(this).attr('id').split('_')[1];
			$('#prop_'+prop_id+' .fav .add').click(function(){addFav(prop_id)}).attr({title:'Add to Favourites'});
			$('#prop_'+prop_id+' .fav .rem').click(function(){remFav(prop_id)}).attr({title:'Remove from Favourites'});
			$('#prop_'+prop_id+' .view .add').click(function(){addView(prop_id)}).attr({title:'Add to Viewing List'});
			$('#prop_'+prop_id+' .view .rem').click(function(){remView(prop_id)}).attr({title:'Remove from Viewing List'});
			$('#prop_'+prop_id+' .quick .add').click(function(){addQuick(prop_id)}).attr({title:'Add to Quick List'});
			$('#prop_'+prop_id+' .quick .rem').click(function(){remQuick(prop_id)}).attr({title:'Remove from Quick List'});
			$('#prop_'+prop_id+' span').css("cursor","pointer");
		});
	}
});
function remFromSr(prop_id){$('#prop_'+prop_id+'').animate({opacity:0},500).animate({width:0,height:0,border:0,padding:0,margin:0,display:'none'},500)}
//function undoRemFromSr(prop_id){$('#li_'+prop_id+'').animate({height:'show',opacity:1},1000);}
function addFav(prop_id) {addToUserList(prop_id,'fav','favourites')}
function remFav(prop_id) {remFromUserList(prop_id,'fav','favourites')}
function addQuick(prop_id) {addToUserList(prop_id,'quick','quick list')}
function remQuick(prop_id) {remFromUserList(prop_id,'quick','quick list')}
function addView(prop_id) {addToUserList(prop_id,'view','viewing list')}
function remView(prop_id) {remFromUserList(prop_id,'view','viewing list')}

function addToUserList(prop_id,action,something)
{
	$.get('ajax.php?go=misc&do='+action+'&prop_id='+prop_id,function(data){
		if (data!='' && data!='error')
		{
		//	$('#menu-'+action+' a').html(data);
			$('#'+action+'-count').html(data);
			$('#prop_'+prop_id+' .'+action+' .add').css({display:'none'});
			$('#prop_'+prop_id+' .'+action+' .rem').css({display:'block'});
		}
		else
		{
			error('Could not add to '+something+'.');
		}
	});
}
function remFromUserList(prop_id,action,something)
{
	$.get('ajax.php?go=misc&do='+action+'-rem&prop_id='+prop_id,function(data){
		$('#'+action+'-count').html(data);
		if (data!='' && data!='error')
		{
			$('#prop_'+prop_id+' .'+action+' .add').css({display:'block'});
			$('#prop_'+prop_id+' .'+action+' .rem').css({display:'none'});
			if ($('#jinfo').html()==action)
			{
				remFromSr(prop_id);
			}
		}
		else
		{
			error('Could not remove from '+something+'.');
		}
	});
}
*/

// * * * * * * 

function loading(element,bool)
{
	if (bool) {element.addClass('loading');}
	else {element.removeClass('loading');}
}

function homepageXfeat()
{
	// load a different one
	var url='ajax.php?go=home&do=xfeat&not='+xfeat[0];
	$(document).ready(function() {
		$.get(url, function(data){
			//alert("Data Loaded: " + data);
			switchXfeat(data);
		});
	});
	
}

function switchXfeat(data)
{
	// previous?
	if (xfeat[0]!=undefined)
	{
	    timeout=2000;
		$('#xfeat .t').animate({opacity: 'hide'}, 900);
	    setTimeout("$('#xfeat .i').animate({ width: 'hide' }, 900);",1000);
	}
	else
	{
		timeout=5;
		$('#xfeat .t').animate({opacity: 'hide'}, 1);
		$('#xfeat .i').animate({opacity: 'hide'}, 1); // make sure it's hidden
	}
	xfeat=data.split("**"); // 0=ref, 1=src, 2=title, 3=text

	//alert(xfeat);
	setTimeout("$('#xfeat .i img').attr('src','"+xfeat[4]+"');",timeout);
	$('#xfeat').click(function(){document.location.href ="index.php?go=property&id="+xfeat[1];});
    setTimeout(function(){$('#xfeat .i').animate({width: 'show'}, 1000);},timeout+100);
    setTimeout(function(){$('#xfeat .t h3 em').html(xfeat[2]);},timeout+1100);
    setTimeout(function(){$('#xfeat .t p em').html(xfeat[3]);},timeout+1100);
	setTimeout(function(){$('#xfeat .t').animate({opacity: 'show'}, 1000);},timeout+1200);
	setTimeout(function(){homepageXfeat();},10000);
}

function error(msg)
{
	$('#msgs').append('<div class="msg error">'+msg+'</div>').slideDown(1000);
}
