
/* Extend jQuery */

function customEqualHeights(element) {
	
	var tallest = 0;

	$(element).each(function() {

		var height = $(this).height();

		if(height > tallest) tallest = height;

	});

	$(element).css('min-height', tallest);
	
}

// preloading function of images
$.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
};

(function($) {

	var interval = null;
	var checklist = [];

	$.elementReady = function(id, fn) {
		checklist.push({id: id, fn: fn});
		if (!interval) {
			interval = setInterval(check, $.elementReady.interval_ms);
		}
		return this;
	};

	// Plugin settings
	$.elementReady.interval_ms = 23; // polling interval in ms

	// Private function
	function check() {
		var docReady = $.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
		for (var i = checklist.length - 1; 0 <= i; --i) {
			var el = document.getElementById(checklist[i].id);
			if (el) {
				var fn = checklist[i].fn; // first remove from checklist, then call function
				checklist[i] = checklist[checklist.length - 1];
				checklist.pop();
				fn.apply(el, [$]);
			}
		}
		if (docReady) {
			clearInterval(interval);
			interval = null;
		}
	};

})(jQuery);


$.preloadImages(
	"/assets/ui/nav-bg-1-blue-top.png", 
	"/assets/ui/nav-bg-1-blue-btm.png",
	"/assets/ui/nav-bg-2-blue-top.png", 
	"/assets/ui/nav-bg-2-blue-btm.png",
	"/assets/ui/nav-bg-3-blue-top.png", 
	"/assets/ui/nav-bg-3-blue-btm.png", 
	"/assets/ui/nav-bg-4-blue-top.png", 
	"/assets/ui/nav-bg-4-blue-btm.png", 
	"/assets/ui/nav-bg-5-blue.png", 
	"/assets/ui/nav-bg-5-blue-btm.png", 
	"/assets/ui/nav-bg-6-blue.png", 
	"/assets/ui/nav-bg-6-blue-btm.png", 
	"/assets/ui/nav-bg-7-blue.png", 
	"/assets/ui/nav-bg-7-blue-btm.png"
);

var isEarlyMoz = false;
var network_activity_hover = 'out';
var page_url = window.location.href;

$.elementReady('banner', function(){
	
	if($.browser.mozilla && $.browser.version.substr(0,3) <= 1.8) {
		
		isEarlyMoz = true;
		
	}
	
	var bottle_id = $('h1').attr('id');
	
	$.ajax({

		url: "/data/banner/"+bottle_id+"/",
		processData: false,
		dataType: "xml",
		success: function(data) {
			
			var href = $('banner item', data).attr('href');
			//var price = $('banner item', data).attr('price');
			var momprice = $('banner item', data).attr('momprice');
			
			var price = "";
			
			if (momprice != "") price = ' <span>(£'+momprice+')</span>';
			
			// add trcking page ref
			var ref = 'whisky pages';
			if ($('body#reviews').length) ref = 'review pages';
			
			// add click tracking
			var onclick = '';
			if (href.indexOf('master') > 0 ) onclick = ' onClick="pageTracker._trackEvent(\'Buy links: '+ref+'\', \'Click\', \''+href.substring(36)+'\');"';	
					
			$('#banner').html('<a rel="nofollow" href="'+href+'" title="Buy now"'+onclick+'>Buy now'+price+'</a>'); //<span>(from £'+price+')</span>
			
			// if(!$.browser.msie && !isEarlyMoz && !$.browser.chrome && $('p.cta-button-large, p.link-button-large a').length) {
			// 				
			// 				$('p.cta-button-large a, p.link-button-large a').corners();
			// 			
			// 			}
							
		}
		
	});
	
});

$.elementReady('detail', function(){
	
	var bottlimage = $('div#header div.feature-detail #detail img.bottle-image');

	var heading = $('div#header div.feature-detail #detail');

	if(heading.length > 0 && bottlimage.length > 0) {

		//$(heading).css("opacity", 0);

		var show_count = 0;

		var int = setInterval(function() {

			var width = $(bottlimage).outerWidth();

			if (width > 0) {

				$('div#header div.feature-detail #detail').css('padding-left', width+10);

				$('#whiskey div#header div.feature-detail #detail').css('padding-left', width+30);

				$(heading).css("opacity", 1);

			}

			if(show_count > 50) {

				$(heading).css("opacity", 1);

				clearInterval(int);

			}

			show_count ++;

		},100);	

	}
	
});


jQuery.extend(jQuery.expr[':'], { Contains :"jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0"});

$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 


/* Initiate on DOM ready */

$(document).ready( function() {


	/* General
     *******************************************************************/

	if($.browser.msie) {

		document.execCommand("BackgroundImageCache",false,true);
		
	}
		
	if($.browser.mozilla && $.browser.version.substr(0,3) <= 1.8) {
		
		isEarlyMoz = true;
		
	}
	
	$("body").addClass("js");
	
	if ($('input.share-code-field').length) {
		
		$('input.share-code-field').click(function() {
			
			$(this).select();
			
		})
		
	}
	
	// var anchor = parseUri(page_url).anchor;
	// 	
	// 	if(anchor) {
	// 		$.scrollTo($('#'+anchor), 2000);
	// 	}
	
	
	$("ul.list-content li:odd").addClass("odd");
	
	var searchtext = $('#home-search input.field').val();
	
	$('#home-search input.field').focus(function() {
	
		if($(this).val() == searchtext || $(this).val() == 'No whiskies found.') {
			$(this).val('');
		}
	
	});
	
	
	$('#home-search input.field').blur(function() {
	
		if($(this).val() == '') {
			$(this).val(searchtext);
		}
		
	})
	
	if($('.alert-holder').length) {
		
		setTimeout(function() {
			
			if ($.browser.msie) {
				$('.alert-content').fadeOut(1000, function() {
					$(this).remove();
				});
			} else {
				$('.alert-holder').slideUp(1000);
				$('.alert-content').fadeOut(1000);
			}			

		},5000);	
			
	} 
	
	
	/* Browse whiskies
     *******************************************************************/
	
	var letter;
	
	var az_html = $('#az-holder').html();
	
	if($('ol.list-az').length) {
		
		$('ol.list-az li a').live("click", function() {
		
			letter = '#' + $(this).text();
		
			$.scrollTo($(letter.toLowerCase()), 800, {
			
				offset: { top: -30 },
				onAfter: function() { 
						
						$(letter.toLowerCase()).before($(az_html).css('display','none').fadeIn(1500));
					
					}
			
			});
			
			$(this).parents('ol').remove();		
		
			return false;
		
		});

	}
	
	if($("div#browse-distillers").length) {
	
		$("div#browse-distillers a.image-holder").each(function() {
		
			var src = $(this).attr('href');
		
			$(this).after('<a href="'+$(this).parent().find('h3 a').attr('href')+'"><img src="'+src+'" height="124" width="95" /></a>');
		
			$(this).remove();
		
		})
		
	} 
	
	
	/* Jump to
     *******************************************************************/

	if($("#jump-to").length) {
		
		$.ajax({
	
			url: "/data/jump-to/",
			processData: false,
			dataType: "html",
			success: function(data) {
				
				$("#jump-to select").append(data);
				
			}
			
		});
		
		$("#jump-to select").change(function() {
			
			if($(this).val()) window.location.href = $(this).val();
			
		})
		
		
		var jump_to_open = false;
		var jump_width_closed = $("#jump-to").width();
		var jump_width_open = $("#jump-to form").width() + jump_width_closed + 10;
		
		$("#jump-to").mouseover(function() {
			$(this).css('opacity', 1);
		})
		
		$("#jump-to").mouseout(function() {
			if (!jump_to_open) $(this).css('opacity', 0.8);
		})
		
		$("#jump-to").click(function() {
			
			if(!jump_to_open) {
				
				jump_to_open = true;

				$(this).animate({
				    width: jump_width_open
				  }, 500, function() {
				    $('form', this).css('display', 'inline')
				  });
				
			} 
			
		})
		
		$("#jump-to p").click(function() {
			
			if(jump_to_open) {

				$("#jump-to form").css('display', 'none')
				$("#jump-to").animate({
				    width: jump_width_closed
				  }, 500, function() {

				  });
				
				jump_to_open = false;
				
				return false;
				
			} 
			
		})
		
		
		
	}
	
	
	
	/* Home page search
     *******************************************************************/
	
	if($("#home-search-input").length) {
		
		$("#home-search-input").autocomplete(
				"/data/distiller-search/text/path/",
				{
					delay: 0,
					minChars: 2,
					matchSubset: 0,
					matchContains: 1,
					cacheLength: 50,
					onItemSelect:selectItem,
					onFindValue:findValue,
					autoFill: false,
					maxItemsToShow: 15,
					width: 200
				}
			);
		
	}
	
	if($('#home').length) {
	
		customEqualHeights($('.home-row-1 .content-block'));
		customEqualHeights($('.home-row-2 .content-block'));
		
	}
	
	if ($('.equal-heights-1')) {
		customEqualHeights($('.equal-heights-1'));
	};
	
	
	
	if($('#network-activity').length) {
		
		$.ajax({
	
			url: "/data/activity/cached/all/all/all/25/",
			processData: false,
			dataType: "html",
			success: function(data) {
			
				var activity = data;
				var length = $('li', activity).length;
				var count = length-1;
				
				$('#network-activity ul.list-activity').append($('li', activity));
				$('#network-activity ul.list-activity li').hide();
				
				$('#network-activity .content-loading').removeClass('content-loading');
				
				var first_item = $('#network-activity ul.list-activity li:eq('+count+')');
				
				if($.browser.msie && $.browser.version.substr(0,3)<="7.0") {
					$(first_item).show();
				} else {
					$(first_item).slideDown(500);
				}
				
				count--;
				
				var ticker = setInterval(function() {
							
					var item = $('#network-activity ul.list-activity li:eq('+count+')');
					
					if(network_activity_hover == 'over') return false;
					
					if($.browser.msie && $.browser.version.substr(0,3)<="7.0") {
						$(item).show();
					} else {
						$(item).slideDown(500);
					}
				
					if (count == 0) clearInterval(ticker);
					
					count --;
				
				}, 2000);
				
				$('#network-activity .block').mouseover(function() {
					if(length - count < 8) return false;
					network_activity_hover = 'over';
					$('#network-activity h2').html('Right now on Connosr... <span>(Paused)</span>')
				})
				
				$('#network-activity .block').mouseout(function() {
					if(length - count < 8) return false;
					network_activity_hover = 'out';
					$('#network-activity h2').html('Right now on Connosr...')
					
				})
				
				
			}
			
		});
	}
	
	
	/* Tag Cloud
     *******************************************************************/
	
	/*
	if($('#tag-cloud').length){
			
			$.ajax({

				url: "/data/tag-cloud/",
				processData: false,
				dataType: "xml",
				success: function(data) {
									
					$("tag", data).each(function() {
							
						$('#tag-cloud').append('tag! ');
						
					});
					
				}
				
			});
			
			
		}*/
	

	
	/* Whisky Tags list
     *******************************************************************/
	
	
	if($("div#info-panel ul.tag-list li, div#about-whisky ul.tag-list li").length) {
		
		$('div#info-panel span.comma, div#about-whisky span.comma').remove();
		
		$("div#info-panel ul.tag-list li span, div#about-whisky ul.tag-list li span").each(function() {
			
			$(this).wrap("<a href='/whisky/browse/tags/"+$(this).attr('rel')+"/'>"+$(this).text()+"</a>");
			
			$(this).remove();
			
		});
		
		
		$('div#info-panel ul.tag-list, div#about-whisky ul.tag-list').append('<li class="more-tags"><a href="#TB_inline?height=300&amp;width=450&amp;inlineId=tags&amp;KeepThis=true" class="thickbox">More tags...</a></li>');
		
	}
	
	
	/* Home Tags Cloud
     *******************************************************************/
	
	if($('#tag-cloud').length > 0) {
		
		$.ajax({
	
			url: "/whisky/tag-cloud/",
			processData: false,
			dataType: "html",
			success: function(data) {
			
				var tags = data;
			
				$('#tag-cloud').append($('.tag-list', tags));
				
				$('#tag-cloud').css('background-image', 'none');
				
				customEqualHeights($('.home-row-3 .content-block'));
				
			}
			
		});
		
	}
	
	
	/* Latest dicussions
     *******************************************************************/

	if($('#load-latest-discussions').length > 0) {

		$.ajax({

			url: "/data/recent-discussions/",
			processData: false,
			dataType: "html",
			success: function(data) {
				
				$('#load-latest-discussions').append(data);
				
				listLinks();
				
				customEqualHeights($('.home-row-4 .content-block'));

			}

		});
		
	}
	
	
	/* Get embed code
     *******************************************************************/
	
	if($('#share').length > 0) {
		
		$.ajax({
	
			url: "/share/widget-embed-code/" + '?rnd=' + Math.random(),
			processData: false,
			dataType: "html",
			success: function(data) {
			
				var source = data;
				var small = $('#small', source).html();
				var large = $('#large', source).html();
			
				$('.share-code-small').val(small.normalize_space());
				$('.share-code-large').val(large.normalize_space());
				
				
				
			}
			
		});
		
	}
	
	/* Bottle list
     *******************************************************************/
	
	if($('.list-links').length > 0) {
		
		listLinks();
		
	}
	
	
	/* Thickbox
     *******************************************************************/
    
	
	//http://connosr/map/?KeepThis=true&TB_iframe=true&height=600&width=772
	
	$('.tag-list li').each(function(i) {
		
		if (i < 16) {
			
			$(this).css('display', 'inline');
		}
		
	});
	
	
	
	/* Corners
     *******************************************************************/
	
	//var iecheck = $.browser.msie + $.browser.version.substr(0,3);

	
	// if(!$.browser.msie && !isEarlyMoz && !$.browser.chrome) {
	// 		
	// 		if($('p.link-button').length) {
	// 			$('p.link-button a').corners();
	// 		}
	// 		
	// 		if($('p.link-button-large').length) {
	// 			$('p.link-button-large a').corners();
	// 		}
	// 		
	// 		if($('p.content-button').length) {
	// 			$('p.content-button a').corners();
	// 		}
	// 	
	// 		if($('p.cta-button-large').length) {
	// 			$('p.cta-button-large a').corners();
	// 		}
	// 	
	// 		if($('ul.block-options li a').length) {
	// 			$('ul.block-options li a').corners();
	// 		}
	// 	
	// 		if($('p.permalink a').length) {
	// 			$('p.permalink a').corners();
	// 		}
	// 		
	// 		if($('.mini-submit').length) {
	// 			$('.mini-submit').corners();
	// 		}
	// 		
	// 		if($('#add-wall-post .submit').length) {
	// 			$('#add-wall-post .submit').corners();
	// 		}
	// 		
	// 		if($('#navigation li').length) {
	// 			// use nice rounded corners :-(
	// 			//$('#navigation li.menu').corners('transparent');		
	// 		}
	// 		
	// 		if($('.link-more').length) {
	// 			$('.link-more a').corners();
	// 		}
	// 	
	// 	}
		
	if($.browser.msie && $.browser.version.substr(0,3)<="7.0") {
		// move the nav to fix an irritating IE z-index bug
		$('body').prepend('<div id="nav-ie"></div>')
		$('#nav-ie').append($('#navigation').html());
		$('#navigation ul').remove();

	} 
	
	$('li.menu').mouseover(function() {
		
		$(this).addClass('hover');
		$('ul', this).show();

		
	});
	
	$('li.menu').mouseout(function() {
		
		$(this).removeClass('hover');
		$('ul', this).hide();
		
	});
	
	if($('#date-1, #date-2').length) {
		
		$('#date-1, #date-2').datePicker({

			clickInput: true, 
			createButton: false

		});
		
	}
	
	
	$('.time-input').focus(function(){
		
		if($(this).val() == 'HH' || $(this).val() == 'MM') {
			
			$(this).val('');
		}
		
	})
	
	if($('.bottle-results').length) {
		
		$('#bottle-search input[name="q"]').liveSearch({
			url:'/data/search/bottles/',
			duration: 500, 
			typeDelay: 500
		});
		
		$('#bottle-search form').submit(function() {
			
			return false;
			
		})
		
	}
	
	if($('.review-results').length) {
		
		$('#review-search input[name="q"]').liveSearch({
			url:'/data/search/reviews/',
			duration: 500, 
			typeDelay: 500
		});
		
		$('#review-search form').submit(function() {
			
			return false;
			
		})
		
	}
	
	if($('#comments .comment').length) {
		
		$('#comments .comment p').each(function() {
			
			$(this).html(atlinking.clean(linkify($(this).text())));
		
		}) 
		
		$("#comments .comment:odd").addClass("odd");
		
	}
	
	if ($('#reviews-az').length) {
		
		$('.group-2, .group-3').hide();
		
		$('#reviews-az .tabs li a').click(function() {
			
			$('.group-1, .group-2, .group-3').hide();
			$('.' + $(this).attr('rel')).show();

			$('.tab-group-1, .tab-group-2, .tab-group-3').removeClass('active');
			$(this).parent().addClass('active');
			
			$('#reviews-az h2 span').text('('+$(this).text()+')')

			return false;
			
		})
		
		if($('#reviews-az ul.list-content li ul li').length > 0) {
			
			$('.group-1, .group-2, .group-3').hide();
			
			var group = $('#reviews-az ul.list-content li ul li').parents('li').attr('rel');
					
			$('.'+group).show();
			$('.tab-group-1, .tab-group-2, .tab-group-3').removeClass('active');
			$('.tab-'+group).addClass('active');
			
			$('#reviews-az h2 span').text('('+$('.tab-'+group).text()+')')
			
			
		}
		
	}
	
	// review detail tabs
	
	if($('#review-detail-tabs').length) {
				
		$('#review-detail-tabs.tabs li a').click(function() {
			
			$('.review-panel-1, .review-panel-2, .review-panel-3').hide().removeClass('accessibility');
			$('.' + $(this).attr('rel')).show();

			$('.tab-review-panel-1, .tab-review-panel-2, .tab-review-panel-3').removeClass('active');
			$(this).parent().addClass('active');
			
			return false;
			
		})
		
	}
	
	// bottlers review dropdown
	
	if ($('#bottler-review-select').length) {
		
		$('#bottler-review-select').submit(function() {
			
			var url = $('select', this).val();
			
			if (url != "") window.location.href = url;
			else alert("Please select a bottle from the dropdown list");
			
			return false;
			
		})
		
	} 
	
	if($('.ajax-comment').length) {
		
		$('.ajax-comment').submit(function() {
			
			if ($('.textarea', this).val() == '') return false;
			
			$('input.redirect', this).remove();
			
			$('.submit', this).addClass('submit-loading');
			var button = $('.submit', this);
			var comment_data = $(this).serialize();
			var textarea = $('textarea', this);
			$(textarea).attr('disabled','disabled');
			
			$.ajax({
				type: "post",
				url: "/data/ajax/add-comment/",
				dataType: "html",
				data: comment_data, 
				success: function(data) {
					
					$('.no-comments').remove();
					
					if($('.top-loader').length) {
												
						var odd = ($('.comment-holder .comment:first').hasClass('odd')) ? false : true;
						$('.comment-holder').prepend($(data).hide());
						if (odd) $('.comment-holder .comment:first').addClass('odd'); 
						$('.comment-holder .comment:first').slideDown(500).show();
						if ($('.comment-holder .comment').length > 3) scrollToComments();
					} else {
						var odd = ($('.comment-holder .comment:last').hasClass('odd')) ? false : true;
						$('.comment-holder').append($(data).hide());
						if (odd) $('.comment-holder .comment:last').addClass('odd'); 
						$('.comment-holder .comment:last').slideDown(500).show();
					}
					
					$(button).removeClass('submit-loading');
					$(textarea).attr('disabled','');
					$(textarea).val('');
					
				}
			});

			
						
			return false;
		
		});
		
	} 
	
	if ($('#load-activity').length) {
		
		var activity_range = 7200*6;
		var activity_current_timestamp = $('.list-activity li:last a').attr('rel');
		var activity_from;
		var activity_to;
		var activity_feed;
		
		$('#load-activity').click(function() {
			
			if ($(this).hasClass('more-loading')) return false;
			
			$(this).addClass('more-loading');
			var button = $(this);	
			
			activity_from = activity_current_timestamp - activity_range;
			activity_to = activity_current_timestamp;
			activity_feed = "/data/activity/" + activity_from + "/" + activity_to + "/all/";
			
			$.ajax({

				url: activity_feed,
				processData: false,
				dataType: "html",
				success: function(data) {

					$('#activity .list-activity').append($('.list-activity li', data));
					
					activity_current_timestamp = activity_from;
					
					$(button).removeClass('more-loading');

				}
			});
			
			return false;
			
		})
						
	};
	
	
	// show more comments 
	
	if ($('#more-comments').length) {
		
		var comment_page = 1;
		var comment_subject = $('h1').attr('id');
		
		$('#more-comments').click(function() {
			
			if ($(this).hasClass('more-loading')) return false;
			
			$(this).addClass('more-loading');
			
			var button = $(this);
			var comment_container = $('#comments .comment-holder')	
			comment_page ++;
				
			$.ajax({

				url: '/data/comments/' + comment_subject + '/8/' + comment_page + '/profile/',
				processData: false,
				dataType: "html",
				success: function(data) {
					
					if($(comment_container).hasClass('top-loader')) {
						
						$(comment_container).append($(data));
					
					} else {
						
						$(comment_container).prepend($(data));
						
					}
					
					$("#comments .comment").removeClass("odd");
					$("#comments .comment:odd").addClass("odd");
									
					$(button).removeClass('more-loading');

				}
			});
			
			return false;
			
		})
						
	};
	
	
	// remove comments
	
	if ($('form.remove-comment').length) {
		
		$('form.remove-comment').live('click', function() {
			
			var parent = $(this).parents('div.comment');
			
			var answer = confirm("Remove this comment?");
			
			if (answer){
				
				$.ajax({

					type: 'post',
					url: '/data/comments/update/',
					processData: false,
					data: $(this).serialize(),
					dataType: "xml",
					success: function(data) {

						var result = $('result', data).text();

						if (result == 'success') {

							$(parent).slideUp('2000', function() {
								$(this).remove();
								$("#comments .comment").removeClass("odd");
								$("#comments .comment:odd").addClass("odd");
							})
							
							

						};

					}
				});
				
			}
			
			return false;
			
		})
						
	};
	
	var what_is_distilled = false;
	
	$('#what-is-dstilled-link').click(function() {
		
		if (!what_is_distilled) {
			$('#what-is-distilled').slideDown(300);
			what_is_distilled = true;
		} else {
			$('#what-is-distilled').slideUp(300);
			what_is_distilled = false;
		}
		
		return false;
	})
	
	var newsletter = false;
	
	$('#newsletter-subscribe-link').click(function() {
		
		if (!newsletter) {
			$('.newsletter-form form').slideDown(300);
			newsletter = true;
		} else {
			$('.newsletter-form form').slideUp(300);
			newsletter = false;
		}
		
		return false;
	})
	
	
	if ($('.enter-competition').length) {
		
		$(".enter-competition").validate();
		
		$('.enter-competition').submit(function() {
			
			var form = $(this);
				
			$.ajax({

				type: 'post',
				url: $('#comp-path').val(),
				processData: false,
				data: $(this).serialize(),
				dataType: "xml",
				success: function(data) {

					var result = $('result', data).text();
					
					if (result == 'success') {

						$('.comp-entry-form', form).remove();
						
						$(form).append('<fieldset class="fieldset main-fields boost-height-low"><h2>Thanks for entering this competition.</h2></fieldset>')

					};
					
					if (result == 'entry-exists') {

						$('.comp-entry-form', form).remove();
						
						$(form).append('<fieldset class="fieldset main-fields boost-height-low"><h2>You have already entered this competition.</h2></fieldset>')

					};

				}
			});
			
			return false;
			
		})
						
	};
	
	if($('.distilled-updates').length) {
		
		$.ajax({
	
			url: "/data/distilled-ticker/",
			processData: false,
			dataType: "html",
			success: function(data) {
				
				var updates = $(data);
				
				$('.distilled-updates').append('<ul></ul>');
				
				$('li', updates).each(function() {
					
					var li = $(this);
					$('.distilled-updates ul').append(li);
					
				});
				
				$('.distilled-updates ul').innerfade({
					animationtype: 'fade',
					speed: 1000,
					timeout: 5000,
					type: 'sequence',
					containerheight: '1em'
				});
				
			}
			
		});
		
	}
	
	$('.button-medium.like a').click(function() {
				
		var prompt = $(this).parents('.score');
		
		var type = 'video';
		var post_type = 'fields['+type+']';
		
		$.post("/data/ajax/like/", { 'fields[video]' : $(this).attr('rel'), 'action[likes]' : "yes" },
		   
			function(data){
			
				if($('result', data).text() == 'Failure') {
					
					$('.like-prompt').html($('result', data).attr('message'));
					
				}
			
				 if($('result', data).text() == 'Success') {
									
					$('.like-prompt').html($('result', data).attr('message'));
				
				}
			
		   });
		
		return false;
		
	});
	
	if ($('#video-player a.overlay').length) {
		
		var link = $('#video-player a.overlay').attr('rel');
		
		if (link != '') $('#video-player a.overlay').attr('href', link);
		
	};

	// get FB Connect scripts
	
	// $.getScript('http://static.ak.connect.facebook.com/connect.php/en_US', function() {
	// 	console.log('FB');
	// 	FB.init("6e336dd61571d2f923868e0012a75791");
	// });
	
});	


/* Distiller/Bottle dropdown functions */

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0]; 
		
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	
	if (sValue != 0) window.location.href = sValue;

	//alert("The value you selected was: " + sValue);
}


function selectItem(li) {
	
	findValue(li);
	
}

function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
	var oSuggest = $("#home-search-input")[0].autocompleter;

	oSuggest.findValue();

	return false;
}

function listLinks() {
	
	$('ul.list-links li').addClass('link');
	
	$('ul.list-links li a').each(function() {
		
		var link = $(this).attr('href');
		
		$(this).parents('li').click(function() {
			
			window.location.href = link;
			
		})
		
	})
}



function populateBottleDropdown(id, name, type){
			
		//console.log(id);
	
		if(id == null || id == '') {
			
			return false;
			
		}
	
		$('#new-bottle').hide();
		$('#new-bottle').val('');
		$('#bottle-list').show();
		
		name;
		
		var url = "/data/bottles/" + id + "/";
		url = (type == 'bottlers') ? url + 'bottler/' : url;
			
		$.ajax({
	
			url: url,
			processData: false,
			dataType: "xml",
			success: function(data) {
				
				
				bottledata = data;
				
				var total_bottles = $("entry", bottledata).length;
				
				if(total_bottles > 0) {
					
					$('#new-bottle').hide();
					$('#bottle-list').show();
					
					$('#bottle-list').attr('disabled','');
					$('#bottle-list').empty();
					$('#bottle-input').show();
					
					$('#bottle-input label').text(+total_bottles+' bottles found for "'+name+'"');
					$('#bottle-list').append('<option value="" class="choose">Please choose a bottle</option>');
					$('#bottle-list').append('<option value=""></option>');
					$('#bottle-list').append('<optgroup label="'+name+'"></optgroup>');
					$('#bottle-list').append('<option value=""></option>');

					$("entry", bottledata).each(function() {
					
						$('#bottle-list optgroup').append('<option value="'+$(this).attr('id')+'" class="'+$('id', this).text()+'">'+$('name', this).text()+'</option>')
					
					});
					
					$('#bottle-list').append('<optgroup label="Not listed"><option value="not-listed" id="not-listed">The bottle isn\'t listed</option></optgroup>');
					
					$('#bottle-list .choose').attr('selected','selected');
					
				} else {
					
					$('#new-bottle').show();
					$('#bottle-list').hide();
					$('#bottle-list').empty();
					//$('#bottle-input label').text('Enter the bottle name');
					
				}
				
			}
			
		});
	
}


var parseUri = function (source) {
	var o = parseUri.options,
		value = o.parser[o.strictMode ? "strict" : "loose"].exec(source);
	
	for (var i = 0, uri = {}; i < 14; i++) {
		uri[o.key[i]] = value[i] || "";
	}
	
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q: {
		name: "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


String.prototype.normalize_space = function() {
// Replace repeated spaces, newlines and tabs with a single space
return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}


var atlinking = function() {
  return {
    "link": function(t) {
      return t.replace(/(^|\s+)(https*\:\/\/\S+[^\.\s+])/g, function(m, m1, link) {
        return m1 + '<a href=' + link + '>' + ((link.length > 25) ? link.substr(0, 24) + '...' : link) + '</a>';
      });
    },
    "at": function(t) {
      return t.replace(/(^|\s+)\@([a-zA-Z0-9@_-]{1,15})/g, function(m, m1, m2) {
        return m1 + '@<a href="/members/' + m2.toLowerCase() + '/">' + m2 + '</a>';
      });
    },
    // "hash": function(t) {
    //       return t.replace(/(^|\s+)\#([a-zA-Z0-9_]+)/g, function(m, m1, m2) {
    //         return m1 + '#<a href="http://search.twitter.com/search?q=%23' + m2 + '">' + m2 + '</a>';
    //       });
    //     },
    "clean": function(post) {
      return this.at(post);
    }
  };
}();

function scrollToComments() {
	$.scrollTo( $('#comments'), 800);
}