$(document).ready(function () {
	$('a[@rel="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
  
  // PNG Fixes for IE <= 6
  
  /*
  
  ( WORK IN PROGRESS - can't work out why the below isn't working? )
  
  (function($,b,l,u,t,a){t=true;u=u;a=navigator.userAgent;b={name:(/(Firefox|Minefield|Chrome|Safari|MSIE|Opera)/.exec(a)||[0,u])[1].toLowerCase()};b.version=b.name.match(/(firefox|minefield)/)?/(Firefox|Minefield)\/([A-Za-z0-9\.]*?)$/.exec(a)[2]:b.name.match(/(safari|chrome|opera)/)?/(Version|Chrome|Opera)\/([A-Za-z0-9\.]*?)\s/.exec(a)[2]:b.name.match(/(msie)/)?/MSIE\s([A-Za-z0-9\.]*?);/.exec(a)[1]:u;l={name:b.name.match(/(firefox|minefield)/)?'gecko':b.name.match(/(chrome|safari)/)?'webkit':b.name.match(/(msie)/)?'trident':b.name.match(/(opera)/)?'presto':u};l.version=b.name.match(/(firefox|minefield)/)?/rv\:([A-Za-z0-9\.]*?)\)/.exec(a)[1]:b.name.match(/(chrome|safari)/)?/AppleWebKit\/([A-Za-z0-9\.]*?)\s/.exec(a)[1]:b.name.match(/(msie)/)?b.version:b.name.match(/(opera)/)?(b.version>9.27)?'futhark':'linear_b':u;b.versionNumber=parseFloat(b.version,10)||1;b.versionX=(b.version+'').substr(0,1);l.versionNumber=parseFloat(l.version,10)||1;l.versionX=(l.versionNumber+'').substr(0,1);b[b.name]=t;l[l.name]=t;b.className=b.name+b.versionX;l.className=l.name+l.versionX;$.browser=$.extend($.browser,b);$.layout=l;$.os={name:(/(Win|Mac|Linux|SunOS|Solaris|iPhone)/.exec(navigator.platform)||[u])[0].toLowerCase().replace('sunos','solaris')};$('html').addClass([$.os.name,$.browser.name,$.browser.className,$.layout.name,$.layout.className].join(' '));})(jQuery);
  
  //if ( $.browser.name == 'msie' && $.browser.versionX <= '6' )
  {
    // Fix Stars
    
    // Fix Letting Agreed Motifs
    $('img.let').each(function()
    {
      var newHTML = "<span style=\"position: absolute; z-index: 9999; top: 10px; left: 10px; width: 51px; height: 51px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src'); + "', sizingMethod='scale');\"></span>";
      $(this).before(newHTML);
    });
  }
  
  */
  
	
	$("div#content.wide form#forgot").hide();
	clearInputs();
	$("div#content.wide div.main form#loginError fieldset button.forgot").click(function(){
		$("div#content.wide form#loginError").fadeOut("slow");
		$("div#content.wide form#forgot").fadeIn("slow");
		$("div#content.wide h2.loginError").fadeOut("slow");
	});
	
	createHelpTips();
	activateHelpTips();
	
	// About image enlarge
	if($("body").attr("id") == 'about' || $("body").attr("id") == 'help-me')
	{
		var root = deriveRoot();
		$("div.main div.about img").attr("title", "Click to Enlarge");
		$("div.main div.about img").css({cursor: 'url("'+root+'media/img/zoom.png"), pointer'});
		
		$("div.main div.about img").toggle(function() {
			$(this).addClass('active');
			$("div.main div.about img").css('display','none');
			$("div.main div.about img.active").css('display','inline');
			$(this).animate({
								width:		"590px"
							}, 500,
							function () {
								$(this).attr("src", ($(this).attr('src')).replace("overview", "full"));
								$("div.main div.about img.active").attr("title", "Click to Restore");
								$("div.main div.about img.active").css({cursor: 'url("'+root+'media/img/zoom-out.png"), pointer'});
							});
			
			hoverState = false;
		},function() {
			$("div.main div.about img.active").animate({
								width:		"142px"
							}, 500,
							function () {
								$("div.main div.about img.active").attr("src", ($(this).attr('src')).replace("full", "overview"));
								$("div.main div.about img").attr("title", "Click to Enlarge");
								$("div.main div.about img").css({cursor: 'url("'+root+'media/img/zoom.png"), pointer'});
								$("div.main div.about img").css('display','inline');
							});
			$("div.main div.about img.active").removeClass("active");
		});
	}
	
	// FIELD FOCUS
	switch($("body").attr("id"))
	{
		case "ref":
			// REFINE SEARCH
			$("select#location").focus();
			break;
		case "contact":
			// ENQUIRY FORM FIELD FOCUS
			if($("input#subject").val() != '') // submitted but failed so focus on captcha
			{
				$("input#captcha").focus();
			}
			else if($("input#forename").val() != '') // Visiting page while logged in so skip prefilled fields and focus on subject
			{
				$("input#subject").focus();
			}
			else // Nothing completed so focus on first field - title
			{
				$("select#title").focus();
			}
			break;
		case "register":
			// REGISTRATION FORM FIELD FOCUS
			$("input#email").focus();
			break;
		case 'login':
			// LOGIN ERROR PAGE
			$("input#username").focus();
			break;
		case "addProperty":
			// ADD PROPERTY PAGES
			if($("select#confirm_location").length > 0) // Step 2
			{
				$("select#confirm_location").focus();
			}
			else if($("select#fk_listing_type_id").length > 0) // Step 1
			{
				//$("select#fk_listing_type_id").focus();
				$("input#search_county").focus();
			}
			if($("input#address_1")) // Step 3
			{
				$("input#address_1").focus();
			}
			break;
		case "editProperty":
			// EDIT PROPERTY PAGES
			if($("input#minimum_price").length > 0)
			{
				$("input#minimum_price").focus();
			}
			else if($("input#deposit").length > 0)
			{
				$("input#deposit").focus();
			}
			else if($("select#fk_listing_type_id").length > 0)
			{
				$("select#fk_listing_type_id").focus();
			}
			else if($("select#confirm_location").length > 0)
			{
				$("select#confirm_location").focus();
			}
			else if($("input#search_query").length > 0)
			{
				$("input#search_query").focus();
			}
			else
			{
				$("input#guide_price").focus();
			}
			break;
		case "completePayment":
			// PAYMENT PAGES
			if($("select#card_type").length > 0)
			{
				$("select#card_type").focus();
			}
			else if($("select#card_options").length > 0)
			{
				$("select#card_options").focus();
			}
			break;
		case "editLandlord":
			if($("input#email").length > 0)
			{
				$("input#email").focus();
			}
			else if($("input#address_1").length > 0)
			{
				$("input#address_1").focus();
			}
			break;
		case "editDescription":
			$("textarea#description").focus();
			break;
		case "changePassword":
			$("input#old_password").focus();
			break;
		case "editTenant":
			$("input#email").focus();
			break;
	}
});

function clearInputs() {
	/*$("input[name='query']").focus(function() {
		$(this).val('');
	});*/
	
	$("input[name='username']").focus(function() {	
		if($(this).val() == 'Email') {
			$(this).val('');
		}
	});
	
	$("input[name='login_username']").focus(function() {	
		if($(this).val() == 'Email') {
			$(this).val('');
		}
	});
	$("input[name='password']").focus(function() {
		$(this).val('');
	});
}

function createHelpTips()
{
  $('div.helpTip').each(function()
  {
    $(this).before('<span class="helpTipReveal">?</span>');
  });
}

function activateHelpTips()
{
  $('span.helpTipReveal').bind(
    'mouseover',function(){
	  if($.browser.ie == false && $(this).parent().find("select").attr("id")) {
		$('div#'+$(this).next().attr("id")).css({right : "-480px;"});
	  }
      $('div#'+$(this).next().attr("id")).show();
	  
	  $('div#profile').hide();
	  $('div#advert').hide();
	  $('div#similiar').hide();
    }
  ).bind(
    'mouseout',function(){
      $('div#'+$(this).next().attr("id")).hide();
	  $('div#profile').show();
	  $('div#advert').show();
	  $('div#similiar').show();
    }
  );
}

function deriveRoot()
{
  var root = window.location.protocol+'//'+window.location.hostname+'/';
  
  if ( ( pos = window.location.pathname.search('public_html') ) != -1 )
  {
    root += window.location.pathname.substring(1,pos+12);
  }
  
  if ( window.location.pathname.search('demo') != -1 )
  {
    root += 'demo/';
  }
  
  return root;
}