/**
 * lh.js
 * @author Lewis Howles
 *
 * Default Niceties.
 */

var lh = {
	
	/*
	 * Set default text for inputs (title attribute)
	 */
	setInputTexts : function(){
	   $("input[type=text], textarea").each(
		   function(){
			   if($(this).val() === "")
				$(this).val($(this).attr('title'));
		   }
	   );
	},
	
	/*
	 * Show / hide text from inputs
	 */
	inputText : function(){
	   $("input, textarea").focus(function(event){
		   if($(this).val() === $(this).attr('title'))
			   $(this).val("");
	   }).blur(function(event){
		   if($(this).val() === "")
			   $(this).val($(this).attr('title'));
	   });
	},
	
	/*
	 * Set target blank on external links
	 */
	externalLinks : function(){
		$('a[rel="external"]')
			.attr('target', '_blank')
	},
	
	isiPhone : function() {
		var agent = navigator.userAgent.toLowerCase();
		return agent.match(/iPhone/i);
	},
	
	isiPad : function() {
		var agent = navigator.userAgent.toLowerCase();
		return agent.match(/iPad/i);
	},
	
	isiDevice : function() {
		return (lh.isiPhone() || lh.isiPad());
	},
	
	replaceImage : function($object, $newImage){
		$object.attr('src', $newImage.children('a').children('img').attr('src').replace('/thumbs/','/HR/'));
	}
}

$(function() {
	lh.setInputTexts();
	lh.inputText();
	lh.externalLinks();
	
	$('#menu li:last').css({'border' : 'none'});
	
	var $images = $('#images');
	
	if ($images.length) {
		$($images).innerfade({
			speed: 1000,
			timeout: 6000,
			containerheight: '394px'
		});
		
		if ($.browser.webkit) {
			$('select','#quick-search').addClass('webkit');
		}
	}
	
	var $breadcrumbs = $('#breadcrumbs');
	
	if ($breadcrumbs.length) {
		$('li:last', $breadcrumbs).addClass('no-crumb');
	}
	
	var $properties = $('#properties');
	
	if ($properties.length) {
		$('li:nth-child(3n+2)', $properties).addClass('middle-child');
	}
	
	var $propertiesText = $('#properties-text');
	var $sidebar = $('#sidebar');
	
	if ($propertiesText.length || $sidebar.length) {
		if (lh.isiDevice()) {
			if ($propertiesText.length) {
				$('li div', $propertiesText).append('<span class="icon-description">View more details about this property</span>').children('.icon-description').show();
			}
			
			if ($sidebar.length) {
				$('#sub-images').insertAfter($('#showcase'));
				$('#main-image, #sub-images, #sidebar').addClass('iDevice');
			}
		}
		else {
			if ($propertiesText.length) {
				$('li div', $propertiesText).append('<span class="icon-description"></span>');
				
				$('.icon').hover(function() {
					$this = $(this);
					$this.next('.icon-description').text($this.attr('title')).fadeIn();
					$title = $this.attr('title');
					$this.attr('title', '');
				}, function() {
					$this.next('.icon-description').fadeOut();
					$this.attr('title', $title);
				});
			}
		}
		
		if ($sidebar.length) {
			$sidebar.append('<span class="icon-description"></span>');
			
			$('.icon').hover(function() {
				$this = $(this);
				$('.icon-description').text($this.attr('title')).fadeIn();
				$title = $this.attr('title');
				$this.attr('title', '');
			}, function() {
				$('.icon-description').hide(); // Prevent quick button hovers causing ugly side effects
				$this.attr('title', $title);
			});
			
			var $showcaseImage = $('#main-image');
			var $subImages = $('#sub-images');
			var $first = $('li:first', $subImages).addClass('active');
			
			var interval = setInterval(function() {
				var $image = $('li.active', $subImages);
				$image.removeClass('active');
				if ($image.next().length) {
					var $next = $image.next().addClass('active');
				}
				else {
					var $next = $first;
				}
				lh.replaceImage($showcaseImage, $next);
			}, 10000);
			
			$('a', $subImages).click(function() {
				var $this = $(this).parent();
				lh.replaceImage($showcaseImage, $this);
				
				$('li.active', $subImages).removeClass('active');
				$this.addClass('active');
				
				return false;
			});
			
			$('#viewing-button, #audio-tour-button, #epcs-button, #street-view-button').fancybox({
				'scrolling' : 'no',
				'titleShow' : false
			});
			
			$('#broadband-button').fancybox({
				'width' : '50%',
				'height' : '70%'
			})
			
			$('#map-button, #room-dimensions-button, #floorplanbutton').fancybox({
				'width' : '80%',
				'height' : '80%'
			});
		}
	}
});
