jQuery().ready(function(){

	$('a.external, a[href^="http://"]').attr("target", "_blank");

	/* applies the webkit.css file if viewing in Safari or Chrome */
	if ($.browser.safari) {
		$("head").append("<link rel=\"stylesheet\" href=\"_inc/css/webkit.css\" type=\"text/css\" />");
	}

	$('a.nyi').live('click', function() {
		alert('Not yet implemented. Coming soon...');
		return false;
	});
	/* initialize dropdowns */
	$('.replace').hide();
	$('.dropdown').show();
	$('.dropdown a.select').next().hide();

	$('.dropdown a.select').live('click', function() {
		$(this).next().toggle();
		$(this).toggleClass('on');
		return false;
	});

	$('.dropdown ul a').live('click', function() { $(this).parent().parent().hide(); $(this).parent().parent().prev().toggleClass('on'); });

		$('#search_text').attr('autocomplete','off');
		jQuery.fn.setSearchFocus = function($display, $value) {
			$('#selected_search').html($display);
			$('#search_section').val($value);
			$('#faux_search').hide();
			$('#search_text').focus().select();
		};

		$('div#main_search_select a#wines_search').click(function() {
			$(this).setSearchFocus('Wines', 'AllWines');
			return false;
		});

		$('div#main_search_select a#notes_search').click(function() {
			$(this).setSearchFocus('Tasting Notes', 'AllNotes');
			return false;
		});

		$('div#main_search_select a#people_search').click(function() {
			$(this).setSearchFocus('People', 'People');
			return false;
		});

		$('div#main_search_select a#forum_search').click(function() {
			$(this).setSearchFocus('Discussions', 'Discussions');
			return false;
		});

		$('div#main_search_select a#articles_search').click(function() {
			$(this).setSearchFocus('Articles', 'Articles');
			return false;
		});

		$('div#main_search_select a#food_search').click(function() {
			$(this).setSearchFocus('Food Pairing Tags', 'Food');
			return false;
		});

		$('div#main_search_select a#help_search').click(function() {
			$(this).setSearchFocus('Help', 'Help');
			return false;
		});

		$('div#main_search_select a#mywish_search').click(function() {
			$(this).setSearchFocus('My Wish List', 'MyWish');
			return false;
		});

		$('div#main_search_select a#mynotes_search').click(function() {
			$(this).setSearchFocus('My Tasting Notes', 'MyNotes');
			return false;
		});

		$('div#main_search_select a#mycellar_search').click(function() {
			$(this).setSearchFocus('My Cellar', 'MyCellar');
			return false;
		});

	$('a.saved_search').attr('href','popup/savedsearch_form.asp?URL=' + encodeURIComponent(window.location.href));
	$('a.remove_search').live('click', function() {
		if (confirm('Are you sure you want to remove this saved search?')) {
			$.get($(this).attr('href'), function() {
				$('div#actionbox').hide();
				$('div#actionbox span#copy').children().remove();
			});
		}
		return false;
	});

	$('#saved_search_form').live('submit', function() {
		$.get('savesearch.asp?' + $('#saved_search_form').serialize(), function() {
			$('div#actionbox').hide();
			$('div#actionbox span#copy').children().remove();
		});
		return false;
	});

	$('#saved_search_commit').live('click', function() {
		$.get('savesearch.asp?' + $('#saved_search_form').serialize(), function() {
			$('div#actionbox').hide();
			$('div#actionbox span#copy').children().remove();
		});
		return false;
	});

		jQuery.fn.setColumnHeight = function() {
			if ($('div.colD').attr('offsetHeight') > $('div.colABC').attr('offsetHeight')) {
				$('div.colD_yellow').css('height', $('div.colD').attr('offsetHeight'));
			}
			else {
				$('div.colD_yellow').css('height', '');
			}
		};

	/* normal action boxes */
		jQuery.fn.showActionBox = function() {
			var $this = $(this);
			var click_offset = $this.offset();
			var $width;
			if ($(this).attr('class').indexOf('action_exwide') != -1) {
				$width = 640;
			} else if ($(this).attr('class').indexOf('action_wide') != -1) {
				$width = 400;
			} else {
				$width = 250;
			}
			if ($this.attr('id') != 'login') {
				var click_target = $this.attr('href');
				if (click_offset.left==0 && click_offset.top==0) {
					var click_offset = $this.parent().parent().parent().offset();
				}
				$('div#actionbox').hide().css('left',click_offset.left - 50).css('top',click_offset.top + 35).show();
				if ($('div#actionbox').offset().left <= 0) {
					$('div#actionbox').css('left', 7 + $(window).scrollLeft());
				}
				else if (($('div#actionbox').offset().left + $width) > ($(window).width())) {
					$('div#actionbox').css('left', $(window).width() - $width - 10);
				}
			} else {
				var click_target = 'popup/login.asp';
				$('div#actionbox').hide().css('left',click_offset.left - 350).css('top',click_offset.top + 35).show(); 
			}
			$('div#actionbox, div#actionbox div.body, div#actionbox div.shadow').css('width',$width);
			$('div#actionbox span#copy').load(click_target, {}, function() {
				$('#popup_new_item').focus().select();
				$('#popup_search_item').focus();
				$('a.saved_search').attr('href','popup/savedsearch_form.asp?URL=' + encodeURIComponent(window.location.href)).attr('class','action_wide');
				if (($('div#actionbox').offset().top + $('div#actionbox').height()) > ($(window).height() + $(window).scrollTop())) {
					if ($(window).scrollLeft() > 0) {
						$('div#actionbox').css('top', $(window).height() + $(window).scrollTop() - $('div#actionbox').height() - 30);
					}
					else {
						$('div#actionbox').css('top', $(window).height() + $(window).scrollTop() - $('div#actionbox').height() - 5);
					}
				}
			});
			$('div#actionbox').draggable();
			return false;
		};

	/* when logged in as a guest force login popup for links that require authentication */
	$('a.auth_required').live('click', function() {
		var $this = $(this);
		if ($this.attr('class').indexOf('action_wide') == -1) {
			$this.addClass('action_wide');
		}
		$this.attr('href', 'popup/login.asp?Referrer=' + encodeURIComponent($this.attr('href')));
		$this.showActionBox();
		return false;
	});

		$('a.action, a.action_wide, a.action_exwide').live('click', function() {
			var $this = $(this);
			$this.showActionBox();
			return false;
		});

	/* close action boxes on escape or click-x */
		$(document).keydown(function(checkKey) {
			if (checkKey.keyCode == 27) {
				$('div#actionbox').hide();
				$('div#helpbox').hide();
				$('div#actionbox span#copy').children().remove();
				$('div#wine_popup').hide();
			}
		});
		$('div#actionbox a.close').click(function() {
			$('div#actionbox').hide();
			$('div#actionbox span#copy').children().remove();
			return false;
		});

	/* initialize rightnav */
	$('div.colD').scrollFollow( {
		speed: 300,
		delay: 150
	});

	$('#right_nav li a.bar').next().hide();

	$('#right_nav li a.on').next().show();

	$(this).setColumnHeight();
	$('#right_nav li a.bar').live('click', function() {
		$(this).next().toggle();
		$(this).toggleClass('on');
		$(this).setColumnHeight();
		return false;
	});

	/* LikeIt AJAX */
	$('a.likelink').live('click', function() { 
		$('#likeit').load($(this).attr('href') + '&iAJAX=1', {}, function() {
			$(this).setColumnHeight();
		});
		return false;
	});

	$('#bulk_popup_form').live('submit', function() {
		var $tag_page = $('#bulk_popup_form').attr('action') + '?' + $('#bulk_popup_form').serialize();
		$.get($tag_page, function() {
			window.location.reload(true);
		});
		return false;
	});

	$('#bulk_submit').live('click', function() {
		var $tag_page = $('#bulk_popup_form').attr('action') + '?' + $('#bulk_popup_form').serialize();
		$.get($tag_page, function() {
			window.location.reload(true);
		});
		return false;
	});

	/* initialize additional options */
	$('#additional_options h2 a').click(function() {
		$('#additional_options h2').toggleClass('on');
		$('#additional_options_inner').toggle();
		$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_AdvancedTnOptions", GS_AdvancedTnOptions: $('#additional_options h2.on').length } );
		$(this).setColumnHeight();
		return false;
	});

		$('#note_display').live('submit', function() {
			$.get('settings.asp?' + $('#note_display').serialize(), function() {
				window.location.reload(true);
			});
			return false;
		});
		$('#note_display_submit').live('click', function() {
			$.get('settings.asp?' + $('#note_display').serialize(), function() {
				window.location.reload(true);
			});
			return false;
		});
		jQuery.fn.setNoteSort = function(fPersonal, cSort) {
			var $url = 'ajaxnotes.asp' + window.location.href.substring(window.location.href.indexOf('?'))
			if (window.location.href.indexOf('/notes.asp') != -1) {
				$url += '&List=true';
			}
			if (window.location.href.indexOf('/mynotes.asp') != -1) {
				$url += '&Personal=true';
			}
			if (fPersonal==true) {
				$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_NoteSortPersonal", GS_NoteSortPersonal: cSort }, function() {
					$('#update_ajax').load($url, {}, function() {
						$('#update_ajax .dropdown').show();
						$('#update_ajax .dropdown a.select').next().hide();
					});
				});
			}
			else {
				$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_NoteSortCommunity", GS_NoteSortCommunity: cSort }, function() {
					$('#update_ajax').load($url, {}, function() {
						$('#update_ajax .dropdown').show();
						$('#update_ajax .dropdown a.select').next().hide();
					});
				});
			}
		};

		$('#my_sort li a').live('click', function() {
			$(this).setNoteSort(true, $(this).parent().prevAll().length);
			return false;
		});
		$('#community_sort li a').live('click', function() {
			$(this).setNoteSort(false, $(this).parent().prevAll().length);
			return false;
		});
		$('#hide_like_blurb').live('click', function() {
			$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_ShowCommunityLike", GS_ShowCommunityLike: "0" }, function() {
				$('span#like_blurb').load('ajaxlikeblurb.asp?' + $('#wine_form').serialize());
			});
			return false;
		});
		$('#show_like_blurb').live('click', function() {
			$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_ShowCommunityLike", GS_ShowCommunityLike: "1" }, function() {
				$('span#like_blurb').load('ajaxlikeblurb.asp?' + $('#wine_form').serialize());
			});
			return false;
		});
		$('#hide_average_blurb').live('click', function() {
			$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_ShowCommunityAverage", GS_ShowCommunityAverage: "0" }, function() {
				$('span#average_blurb').load('ajaxaverageblurb.asp?' + $('#wine_form').serialize());
			});
			return false;
		});
		$('#show_average_blurb').live('click', function() {
			$.get("settings.asp", { Confirmed: "TRUE", Setting: "GS_ShowCommunityAverage", GS_ShowCommunityAverage: "1" }, function() {
				$('span#average_blurb').load('ajaxaverageblurb.asp?' + $('#wine_form').serialize());
			});
			return false;
		});
	/* handle disabling for defective wines */
		$('#bottle_defective').change(function(){
			if ($('#bottle_defective').is(':checked')) {
				$('#my_rating').attr('disabled', true);
				$('#like_yes').attr('disabled', true);
				$('#like_no').attr('disabled', true);
				$('#my_rating').val('');
			}
			else
			{
				$('#my_rating').removeAttr('disabled');
				$('#like_yes').removeAttr('disabled');
				$('#like_no').removeAttr('disabled');
			}
		});

	/* turn on tasting date picker */
	$('#tasting_date').datepicker();

	$('#publish_note').click(function() { 
		$(this).attr('href','postnote.asp?' + $('#post_note').serialize());
		return false;
	})
	/* initialize lightbox */
	if($.browser.msie && $.browser.version != "8.0"){
		/* windows are 5px shorter and narrower in IE, due to border display issues */
		$('#wine_rating_assist').colorbox({contentWidth:"730px", contentHeight:"348px", bgOpacity: .55, contentIframe: true});
		$('#tasting_note_assist').colorbox({contentWidth:"730px", contentHeight:"465px", bgOpacity: .55, contentIframe: true});
		$('#publish_note').colorbox({contentWidth:"740px", contentHeight:"615px", bgOpacity: .55, contentIframe: true});
	}else{
		$('#wine_rating_assist').colorbox({contentWidth:"755px", contentHeight:"373px", bgOpacity: .55, contentIframe: true});
		$('#tasting_note_assist').colorbox({contentWidth:"755px", contentHeight:"490px", bgOpacity: .55, contentIframe: true});
		$('#publish_note').colorbox({contentWidth:"745px", contentHeight:"620px", bgOpacity: .55, contentIframe: true});
	}

	colorbox_close = function() {
		$('#bottle_defective').trigger('change');
		$('#modalClose').click();
	};
	/* initialize help tip */
	$('a.help').live('click', function() { 
		var click_offset = $(this).offset();
		var click_target = $(this).attr('href');
		$('div#helpbox').hide().css('left',click_offset.left + 25).css('top',click_offset.top + 15).show();
		$('div#helpbox span#helpcopy').load(click_target);
		return false;
	})
	$('div#helpbox').draggable();
	$('div#helpbox a.close').click(function() { $('div#helpbox').hide(); return false; })

	/* Vote AJAX */
	$('a.votelink', this).live('click', function() { 
		$.get($(this).attr('href') + '&iAJAX=1', function(data) {
		$('#update_ajax').html(data); $('.replace').hide(); $('.dropdown').show(); $('.dropdown a.select').next().hide(); }, 'script');
		return false;
	});

});
