// <![CDATA[
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

	$(function() {
		$('#portfolio div.title a').click(function() {
			if ($('#portfolio div.content').hasClass('shrink')) {
				$('#portfolio div.content').removeClass('shrink').hide();
			}
			if ($('#portfolio div.content').hasClass('open')) {
				$('#portfolio div.content').slideUp('slow').removeClass('open');
				$(this).attr('title', 'Show your portfolio').html('Show portfolio');
			} else {
				$('#portfolio div.content').slideDown('slow').addClass('open');
				$(this).attr('title', 'Hide your portfolio').html('Hide portfolio');
			}
			return false;
		});
		
		$('#portfolio div.content a.remove').livequery('click', function() {
			var $r = $(this).attr('rel');
			$.ajax({
				cache : false, 
				data : 'ajax=1&action=remove&property=' + $r, 
				dataType : 'json', 
				timeout : 5000, 
				type : 'POST', 
				url : '/portfolio.php', 
				error : function($xHR, $tS, $eT) {}, 
				success : function($d, $tS) {
					if ($d.success == true) {
						$('#portfolio div.content li#property-' + $r).hide("puff", {}, 500, function() {
							$(this).remove();
							var $folioCount = $('#portfolio div.content li.property:visible').length.toString();
							$('#portfolio div.title h5 span').html($folioCount);
							if ($folioCount == '0') {
								$('#portfolio div.content ul li.notice').show();
								$('#portfolio div.content a.left, #portfolio div.content a.right').hide();
							}
							var $newWidth = $('#portfolio div.content ul').width() - 198;
							$('#portfolio div.content ul').width($newWidth);
							
							var $newPos = $('#portfolio div.content ul').css('left');
							if ($newPos == 'auto') {
								$newPos = '0px';
							}
							$newPos = parseInt($newPos.substring(0, $newPos.length - 2));
							var $width = $('#portfolio div.content ul').width();
							
							if (($newPos + $width) <= 0) {
								$newLeft = ($('#portfolio div.content ul li:visible').length - 1) * 198;
								$newLeft = 0 - $newLeft;
								$('#portfolio div.content ul').animate( 
									{ 'left': $newLeft + 'px' }, 
									300, 
									function() { 
										$('#portfolio div.content a.right').removeClass('animating');
									});
							}
						});
						$('a.addToPortfolio[rel=' + $r + ']').removeClass('inactive').attr('title', 'Add this property to your portfolio');
					}
					return false;
				}
			});
			return false;
		});
		
		$('a.addToPortfolio').click(function() {
			var $link = $(this); 
			var $r = $(this).attr('rel');
			$.ajax({
				cache : false, 
				data : 'ajax=1&action=add&property=' + $r, 
				dataType : 'json', 
				timeout : 3000, 
				type : 'POST', 
				url : '/portfolio.php', 
				error : function($xHR, $tS, $eT) {}, 
				success : function($d, $tS) {
					if ($d.success == true) {
						$('#portfolio div.content ul li.notice').hide();
						$('#portfolio div.content a.left, #portfolio div.content a.right').show();
						$('img#propertyImage-' + $r + ', div.images div.full').effect('transfer', { to : '#portfolio div.title h5 span' }, 500, function() {
							$('#portfolio div.content ul')
								.createAppend(
									'li', { className : 'property', id : 'property-' + $r }, [
										'a', { className : 'propertyImage', href : '/propertyDetails.php?id=' + $r, title : 'View this property' }, [
											'img', { src : '/img/cache/portfolio/' + $r + '.jpg', width : '155', height : '118', alt : 'Portfolio property thumbnail' }
										], 
										'a', { rel : $r, className : 'remove imgReplace', href : '?remove=' + $r, title : 'Remove this property' }, [
											'span', {}, 'Remove'
										]
									]
								);
								var $newWidth = $('#portfolio div.content ul').width() + 198;
								$('#portfolio div.content ul').width($newWidth);
								$('#portfolio div.title h5 span').html($('#portfolio div.content li.property:visible').length.toString());
			//							$('#portfolio div.content ul li:last').fadeIn('slow');
							$link.addClass('inactive').attr('title', 'This property is in your portfolio');
						});
					}
					return false;
				}
			});
			return false;
		});
		
		$('#portfolio div.content a.left').livequery('click', function() { 
			if ($(this).hasClass('animating')) { return false; }
			
			$(this).addClass('animating');
			var $width = $('#portfolio div.content ul li:visible').length * 198;
			var $newPos = $('#portfolio div.content ul').css('left');
			if ($newPos == 'auto') {
				$newPos = '0px';
			}
			$newPos = parseInt($newPos.substring(0, $newPos.length - 2)) ;
			if ($newPos != 0) {
				$newPos = $newPos + 198;
				$('#portfolio div.content ul').animate( 
					{ 'left': $newPos + 'px' }, 
					300, 
					function() { 
						$('#portfolio div.content a.left').removeClass('animating');
					});
			} else {
				$(this).removeClass('animating');
			}
			return false;
		});
		
		$('#portfolio div.content a.right').livequery('click', function() { 
			if ($(this).hasClass('animating')) { return false; }
			
			$(this).addClass('animating');
			var $width = $('#portfolio div.content ul li:visible').length * 198;
			var $newPos = $('#portfolio div.content ul').css('left');
			if ($newPos == 'auto') {
				$newPos = '0px';
			}
			$newPos = parseInt($newPos.substring(0, $newPos.length - 2));
			
			if ($newPos != (198 - $width)) {
				$newPos = $newPos - 198;
				$('#portfolio div.content ul').animate( 
					{ 'left': $newPos + 'px' }, 
					300, 
					function() { 
						$('#portfolio div.content a.right').removeClass('animating');
					});
			} else {
				$(this).removeClass('animating');
			}
			return false;
		});
		
		$('a.removeFromPortfolio').click(function() {
			var $r = $(this).attr('rel');
			$.ajax({
				cache : false, 
				data : 'ajax=1&action=remove&property=' + $r, 
				dataType : 'json', 
				timeout : 3000, 
				type : 'POST', 
				url : '/portfolio.php', 
				error : function($xHR, $tS, $eT) {}, 
				success : function($d, $tS) {
					if ($d.success == true) {
						$('li#propertyMain-' + $r).css('backgroundColor', '#FF9').fadeOut('slow', function() {
							if ($('ul#propertyList li.property:visible').length == 0) {
								$('.empty').show();
								$('#propertyCount').hide();
							}
						});
					}
				}
		   });
			return false;
		});
	});
// ]]>