/**
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad, should also work with Android mobile phones (not tested yet!)
 * Common usage: wipe images (left and right to show the previous or next image)
 * 
 * @author Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
 * @version 1.0 (15th July 2010)
 */
(function($) { 
   $.fn.touchwipe = function(settings) {
     var config = {
    		min_move_x: 20,
 			wipeLeft: function() { alert("left"); },
 			wipeRight: function() { alert("right"); },
			preventDefaultEvents: true
	 };

     if (settings) $.extend(config, settings);

     this.each(function() {
    	 var startX;
		 var isMoving = false;

    	 function cancelTouch() {
    		 this.removeEventListener('touchmove', onTouchMove);
    		 startX = null;
    		 isMoving = false;
    	 }	

    	 function onTouchMove(e) {
    		 if(config.preventDefaultEvents) {
    			 e.preventDefault();
    		 }
    		 if(isMoving) {
	    		 var x = e.touches[0].pageX;
	    		 var dx = startX - x;
	    		 if(Math.abs(dx) >= config.min_move_x) {
	    			cancelTouch();
	    			if(dx > 0) {
	    				config.wipeLeft();
	    			}
	    			else {
	    				config.wipeRight();
	    			}
	    		 }
    		 }
    	 }

    	 function onTouchStart(e) {
    		 if (e.touches.length == 1) {
    			 startX = e.touches[0].pageX;
    			 isMoving = true;
    			 this.addEventListener('touchmove', onTouchMove, false);
    		 }
    	 }

         if (this.addEventListener) {
	    	 this.addEventListener('touchmove', onTouchMove, false);
    		 this.addEventListener('touchstart', onTouchStart, false);
		 }
     });

     return this;
   };

})(jQuery);

(function($) {
	$(function() {
		var end_url = '.jpg';
		if ($('body#home').length > 0) end_url = '-home.png';
		ffEventHandler.registerEvent('dominsert', function() {
		$('.featured-listing').each(function(el) {
			if ($(this).children('img.dec').length > 0) return;
			$(this).append('<img class="dec" src="' + ffBaseURL + ffBaseTURL + '/img/bg/featured-listing-bottom' + end_url + '">');
			$(this).data('enriched', true);
		});
		$('p.more-info').each(function(el) {
			if ($(this).data('enriched')) return;
			if ($(this).find('a').length == 0) {
				$(this).wrapInner("<span></span>");
			}
			$(this).wrapInner("<span></span>").wrapInner("<span></span>").wrapInner("<span></span>");
			$(this).data('enriched', true);
		}); }, true);
		
	});
	
	window.Slideshow = function() {
		this.itemContainer = "";
		this.itemTotal = 0;
		this.currentItem = 1;
		this.itemWidth = 0;
		this.speed = 300;
		this.wrapperClass = 'window';
		
		// the controls need to have rel="prev" and rel="next"
		// DO NOT EDIT THIS
		this.controlsMarkup = (
			'<p class="slideshow-controls">' + 
			'<a href="#" class="prev" rel="prev">Previous</a>' + 
			'<a href="#" class="next" rel="next">Next</a></p>'
		);
	};
	Slideshow.prototype.init = function() {
		this.itemTotal = parseInt($(this.itemContainer+'>li').length,10);
		if (this.itemTotal <= 1) {
			return;
		}
		$(this.itemContainer).wrap('<div class="'+this.wrapperClass+'"></div>');
		this.itemWidth = this.getItemWidth();
		$($(this.itemContainer).parents()[0]).append(this.controlsMarkup);
		$(this.itemContainer+'>li').width(this.itemWidth+'px');

		this.checkControls();

		var self = this;
		$('.'+this.wrapperClass+' a[rel="prev"]').live('click', function() {
			if ($(this).hasClass('active')) {
				self.moveToItem(self.currentItem-1);
			}
			return false;
		});

		$('.'+this.wrapperClass+' a[rel="next"]').live('click', function() {
			if ($(this).hasClass('active')) {
				self.moveToItem(self.currentItem+1);
			}
			return false;
		});

		$(this.itemContainer+'>li img').touchwipe({
			wipeLeft: function() {
				if (self.currentItem != self.itemTotal) {
					self.moveToItem(self.currentItem+1);
				}
			},
			wipeRight: function() {
				if (self.currentItem > 1) {
					self.moveToItem(self.currentItem-1);
				}
			}
		});

		$(window).resize(function() {
			self.itemWidth = self.getItemWidth();
			$(self.itemContainer+'>li').width(self.itemWidth+'px');
			self.moveToItem(self.currentItem);
		});
	};
	Slideshow.prototype.getItemWidth = function() {
		return $(this.itemContainer).parents('.'+this.wrapperClass).width();
	};
	Slideshow.prototype.moveToItem = function(itemNumber) {
		if (!$(this.itemContainer).parents('.'+this.wrapperClass+' :animated').length) {
			var endpoint = (itemNumber-1) * this.itemWidth * -1;
			$(this.itemContainer).animate({'left':(endpoint+'px')},this.speed);
			this.currentItem = itemNumber;
			this.checkControls();
		}
	};
	Slideshow.prototype.checkControls = function() {
		if (this.currentItem == 1) {
			$('.'+this.wrapperClass+' a[rel="prev"]').removeClass('active');
		} else {
			$('.'+this.wrapperClass+' a[rel="prev"]').addClass('active');
		}
		if (this.currentItem == this.itemTotal) {
			$('.'+this.wrapperClass+' a[rel="next"]').removeClass('active');
		} else {
			$('.'+this.wrapperClass+' a[rel="next"]').addClass('active');
		}
	};
	
	
	
	
	// slidey dropdown area 
	window.DropdownArea = function() {
		this.trigger = null;
		this.target = '';
		this.targetParent = '';
		this.callbackFunction = function(){};
	};
	DropdownArea.prototype.bodyclick = function(e) {
		// this will get fired on click of body, we need to close the dropdown
		if (this.bodyWatching) {
			if (!
				(($(e.target).get(0) == $(this.targetParent).get(0)) ||
				($(e.target).parents(this.targetParent).length))
			) {
				this.callbackFunction();
				$(this.target).slideUp('200');
				$(this.targetParent).removeClass('expanded');
				this.bodyWatching = false;
			}

		}
	}
	DropdownArea.prototype.hide = function() {
		var self = this;
		$(self.target).slideUp('200');
		$(self.targetParent).removeClass('expanded');
		//unbind bodyclick
		self.bodyWatching = false;
	}
	DropdownArea.prototype.init = function() {
		// advanced dropdown 
		$(this.target).hide();
		var self = this;
		if (this.trigger) {
			this.trigger.click( 
				function() {
					if(! $(self.target+':animated').length) {
						if ($(self.target+':visible').length){
							self.callbackFunction();
							$(self.target).slideUp('200', function(){});
							$(self.targetParent).removeClass('expanded');
							//unbind bodyclick
							self.bodyWatching = false;
						} else {
							self.callbackFunction();
							$(self.target).slideDown('200', function(){});
							$(self.targetParent).addClass('expanded');
							self.bodyWatching = true;
						}
					}
					$(self.target).trigger('click');
					return false;
				}
			);
			// if box now showing bind bodyclick
			$('body').bind("click", function(e) {
				self.bodyclick(e);
			});
		}
	};
})(jQuery);

jQuery(function($) {
	//	Add the class "hasJS" to the body element.
	$('body').addClass('hasJS');

	
	// input box in the header to have disapearing title
	$('input.title-switch').each(function(){
		if ($(this).attr('title') != ""){
			// if the title exists
			if($(this).val() == "") {
				// if on load the value is blank
				$(this).val($(this).attr('title'));
			}	
			$(this).blur(function(){
				if($(this).val() == "") {
					$(this).val($(this).attr('title'));
				}
			});	
			$(this).click(function(){
				if($(this).val() == $(this).attr('title')) {
					$(this).val("");
				}
			})
		}
	});
	
	
	// A special slideshow that updates the teaser 'selected' list item
	function SpecialSlideshow() {
		Slideshow.call(this);
	}
	SpecialSlideshow.prototype = new Slideshow();
	SpecialSlideshow.prototype.moveToItem = function(itemNumber) {
		Slideshow.prototype.moveToItem.call(this, itemNumber);
		$('.slideshow ul.items li').removeClass('selected');
		$('.slideshow ul.items li').eq(itemNumber - 1).addClass('selected');
	};
	
	
	// slideshow
	var slideshowSlider = new SpecialSlideshow();
	// set up variables for object
	slideshowSlider.itemContainer = ".slideshow ul.slideshow-content";
	// initiate
	slideshowSlider.init();
	
	
	if(slideshowSlider.itemTotal > 1) {
		// insert nav
		$('.slideshow').append('<ul class="items"></ul>');
		$('.slideshow ul.items').css('margin-left', '-' + parseInt(4*slideshowSlider.itemTotal) + 'px');
		// add links
		for(i=0;i<slideshowSlider.itemTotal;i++) {
			$('.slideshow ul.items').append('<li><a href="#">view item '+(i+1)+'</a></li>')
		}
		
	}
	
	
	var slideshowSliderItems = $('.slideshow ul.items li a');
	
	slideshowSliderItems.click(function() {
		slideshowSliderItems.parent('li').removeClass('selected');
		$(this).parent('li').addClass('selected');
		slideshowSlider.moveToItem(slideshowSliderItems.index(this) + 1);
		return false;
	});
	
	// Select the first one
	$('.slideshow ul.items li').eq(0).addClass('selected');
	
	function addPrintFooter() {
	  var p = document.createElement('p')
	  p.className = 'print-footer';
	  p.innerHTML = window.location.href;
	  document.body.appendChild(p);
	}
	addPrintFooter();

});

jQuery(window).load(function() {
	// Crazyweird fix lets us style abbr using CSS in IE 
	// - do NOT run onDomReady, must be onload
	document.createElement('abbr');
});

