$(function(){
	// Start of cycles
	
	function startPropertyCycle() {
		slideShowActivated = true;
		$('.property-images .unicycle')
		.after('<ul class="gallery-nav clearfix">')
		.cycle({ 
		   	fx:    'turnDown',
			timeout: 0,
			speed: 0,
		    pager:  '.gallery-nav', 
		
		    // callback fn that creates a thumbnail to use as pager anchor 
			pagerAnchorBuilder: function(idx, slide) {
				var thumbnail = $(slide).find('img').attr("src");
				var title = $(slide).find('img').attr("alt");
		        return '<li class="slide-'+(idx%5)+'"><a href="#"><img src="' + thumbnail + '" title="'+title+'"/></a></li>'; 
		    }
		});
	}
	
	function startPromoCycle() {
		$('.cycle').cycle({ 
			fx:    'fade',
			timeout: 5000,
			speed: 400
		}).css("height", "+=9");
	}
	
	$('.testimonial').cycle({
		fx:    'fade',
		timeout: 5000,
		speed: 400
	});
	
	$('.property-images .unicycle img').imagesLoaded(startPropertyCycle);
	$('.cycle img').imagesLoaded(startPromoCycle);
	
	// end cycles
	
	//$(".property-images ul.cycle").css("position"," static");
	//remove gallery nav if empty to stop padding ot single image.
	if ($(".gallery-nav li").length == 0) {
		$(".gallery-nav").remove();
	}
	
	// Get the first word in the page title and wrap it in a <span>
	$('h1.page-title a').each(function(){
	     var title = $(this);
	     title.html(title.html().replace(/^(\w+)/, '<span>$1</span>'));
	});
	
	// http://ybits.net/2009/10/making-jquery-tablesorter-persistent/
	  $.tablesorter.addWidget({
	    // give the widget an id
	    id: "sortPersist",
	    // format is called when the on init and when a sorting has finished
	    format: function(table) {

	      // Cookie info
	      var cookieName = 'ljpm_tablesort';
	      var cookie = $.cookie(cookieName);
	      var options = {path: '/'};

	      var data = {};
	      var sortList = table.config.sortList;
	      var tableId = $(table).attr('id');
	      var cookieExists = (typeof(cookie) != "undefined" && cookie != null);

	      // If the existing sortList isn't empty, set it into the cookie and get out
	      if (sortList.length > 0) {
	        if (cookieExists) {
	          data = $.evalJSON(cookie);
	        }
	        data[tableId] = sortList;
	        $.cookie(cookieName, $.toJSON(data), options);
	      }

	      // Otherwise...
	      else {
	        if (cookieExists) { 

	          // Get the cookie data
	          var data = $.evalJSON($.cookie(cookieName));

	          // If it exists
	          if (typeof(data[tableId]) != "undefined" && data[tableId] != null) {

	            // Get the list
	            sortList = data[tableId];

	            // And finally, if the list is NOT empty, trigger the sort with the new list
	            if (sortList.length > 0) {
	              $(table).trigger("sorton", [sortList]);
	            }
	          }
	        }
	      }
	    }
	  });
	
	$('table.property-search-results').each(function(){
		var $t = $(this);
		$t.tablesorter({
			cssHeader:"headerSort",
			cssAsc:"headerSortUp",
			cssDesc:"headerSortDown",
			headers:{
				"0":{"sorter":false}
			},
			widgets:['zebra', 'sortPersist']
		});
		if($t.hasClass('filter-open_for_inspection')){
			var sorting = [[4,0]];
			$t.trigger('sorton',[sorting]);
		}
	});
	

	/*Staff Mono Fading*/
	
	$('a.staff-member img.staff-photo-over').fadeTo(0, 0);
	$('a.staff-member img.staff-photo-over').mouseenter(function() {
		$(this).dequeue().fadeTo(300, 1);
	});
	$('a.staff-member img.staff-photo-over').mouseleave(function() {
		$(this).dequeue().fadeTo(300, 0);
	});
	$('a.staff-member img.staff-photo-over').click(function() {
		$(this).dequeue().fadeTo(300, 0);
	});
	
	


	/* Various Fancy image popups */ 
	$("a.fancy-image").attr('rel', 'gallery').fancybox();
	$("a.fancy-inline").fancybox();
	$("a.fancy-staff").fancybox({
		width:780,
		height:'610',
		autoDimensions: false
	});
	$("a.fancy-tube").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				   	 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		return false;
	});
	
	$('.fancy-player').each(function(){
	 	thisId = $(this).attr("id");
		vidHeight = $(this).attr("height");
		vidWidth = $(this).attr("width");
		poster = $(this).attr("poster");
		jwplayer(thisId).setup({
			height: vidHeight,
			width: vidWidth,
			image: poster,
			stretching: "fill",
			screencolor: "ffffff",
			flashplayer: "/themes/site_themes/default/swf/player.swf",
			file: this.src
		});
	});
});

