/* Copyright Property Sol */

$(function() {
	$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=directory&place_id=" + $(this).val();
	});
	
	/*

	//if (!$.browser.msie) $(".live-dir").livedir();
	$(".live-dir").breadcrumbs("ajax.php?go=place&do=kids",{onSelect: function() {alert("You selected: " + this.value)}});
	$("#place-suggest").suggest("ajax.php?go=place",{onSelect: function() {
		place_id=this.value.split('~')[0];
		document.location.href ="index.php?go=directory&place_id=" + place_id;
	}});
	*/
	 
    // bind to the form's submit event 
   /* $('#misc-form').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit({beforeSubmit:loading($(this),true),success:loading($(this),false)});
        return false; 
    }); 
	$('#misc-form #quick_autoremove').click(function(){$('#misc-form').submit();});
   */
 	$('.hideable').hideable();	
	$('#msgs p').hideable();
//	$('.drop').drops();
	$('.checkul').each(function(){
		$(this).click(function(){
			var checkul=$(this).attr('id');
			if ($(this).is(':checked')) $('.checkul_'+checkul).show(200);
			else $('.checkul_'+checkul).hide(200);
			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);
		});
	});
});

/*
function adminplaces()
{
	$('.adminplaces li').each(function(){
		el=$(this);
		var id=el.attr('class').split('_')[1];
		$(this).find('a.kids:first').click(function(){
			$.get('index.php?go=places.many.admin&do=kids&placeid='+id,function(d){
				el.append(d);
				adminplaces();
			});
		});
	});
	
}
*/

function checkSearchForm()
{
	if ($('#ts').is(':checked') || $('#tr').is(':checked')) ready=true;
	else ready=false;
	if (ready) $('#search_submit').removeAttr("disabled").val('Search');
	else $('#search_submit').attr('disabled','disabled').val('Sale or Rent!');
}

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);
}