// EAT CLUB
$(document).ready(function(){

// CALENDAR
	$("#startDate,#endDate").datepicker({     
		minDate: new Date(2001, 1 - 1, 1),     
		maxDate: new Date(2010, 12 - 1, 31),
		beforeShow: readLinked,     
		//beforeShow: customRange, 
		onSelect: updateLinked,     
		//onSelect: updateDropDown,   
		showOn: "both",     
		buttonImage: "/images/icons/calendar.png",
		buttonImageOnly: true 
	}); 
	$("#startday, #startmonth, #startyear").change(checkLinkedDays);

	function customRange(input) {
		return {minDate: (input.id == "endDate" ? $("#startDate").datepicker("getDate") : null), 
        maxDate: (input.id == "startDate" ? $("#endDate").datepicker("getDate") : null)};
	}
	
	function updateDropDown(date,input) {
		if (input.id == "startDate")
		{
			$("#startday").val(date.substring(3, 5));
			$("#startmonth").val(date.substring(0, 2)); 
			$("#startyear").val(date.substring(6, 10));
		};
		if (input.id == "endDate")
		{
			$("#endday").val(date.substring(3, 5));
			$("#endmonth").val(date.substring(0, 2)); 
			$("#endyear").val(date.substring(6, 10));
		};
	}

	function readLinked() { 
	    $("#startDate").val($("#startmonth").val() + "/" + 
	        $("#startday").val() + "/" + $("#startyear").val()); 
	    return {}; 
	} 
	 
	// Update three select controls to match a date picker selection 
	function updateLinked(date) { 
		$("#startday").val(date.substring(3, 5));
	    $("#startmonth").val(date.substring(0, 2)); 
	    $("#startyear").val(date.substring(6, 10)); 
	} 

	// Prevent selection of invalid dates through the select controls 
	function checkLinkedDays() { 
	    var daysInMonth = 32 - new Date($("#startyear").val(), 
	        $("#startmonth").val() - 1, 32).getDate(); 
	    $("#startday option").attr("disabled", ""); 
	    $("#startday option:gt(" + (daysInMonth - 1) +")").attr("disabled", "disabled"); 
	    if ($("#startday").val() > daysInMonth) { 
	        $("#startday").val(daysInMonth); 
	    } 
	}
// END CALENDAR

// AJAX SUGGEST
	$("#venuename").attr("autocomplete","off");
	$("#suggestions").empty();
	$("#venuename").keydown(function() {
		var search;
		search = $("#venuename").val();
		if (search.length == 0) {
			$("#suggestions").hide();
			$("#suggestions").empty();
		}
	});
	$("#venuename").keyup(function() {
		var search;
		search = $("#venuename").val();
		$("#venuename").blur(
			function() {
				setTimeout("$('#suggestions').hide();", 100);
			}
		);
		if (search.length == 0)
		{
			$("#suggestions").hide();
			$("#suggestions").empty();
		}
		else
		{
			$.get("/events/ajax", {venuename: ""+search+""},
			function (data) {
				if(data.length > 0) {
					$("#suggestions").show();
					$("#suggestions").html(data);
				} else {
					$("#suggestions").hide();
					$("#suggestions").empty();
				}
			});
		}
	});

	// On autosuggest click, fill box and make hidden ID
	$("li.autosuggested") 
	    .livequery('click', function(event) { 
			var textboxvenue;
			textboxvenue = $(this).find("span.name").html();
			$("#venuename").val(textboxvenue);
			$("input#venueid").remove();
			$("form#form-add-event").prepend('<input type="hidden" name="venueid" id="venueid" value="'+$(this).attr("id")+'" \/>');
			setTimeout("$('#suggestions').hide();", 200);
			$("#suggestions").empty();
			$("button:contains('Next')").attr("disabled","disabled");
	        //return false; 
    });
// END AJAX SUGGEST

// EXPORT POPUP
	// Prepare popup with class and close link
	$("div#export").hide();
	$("div#export").addClass("export-popup");
	$("div#export h2").append(' <a class="close" href="#">Close</a>');
	$("div#export").appendTo("body");
	// Position popup in middle of the window
	$(window).bind("load resize",
		function() {
			var windowWidth = $(window).width();  
			var windowHeight = $(window).height();
			var popupWidth = $("div#export").width();
			var popupHeight = $("div#export").height();
			var leftCss = ( (windowWidth / 2) - (popupWidth / 2) );
			var topCss = ( (windowHeight / 2) - (popupHeight / 2) );
			$("div#export").css({
			"left": leftCss,
			"top": topCss
			});
		}
	);
	// Show and hide popup 
	$("a.export").click(function() {
			if ($("div#export").is(":hidden")) {
				$("div#export").fadeIn("slow");
			} else {
				$("div#export").fadeOut();
			}
			return false;
		}
	);
	$("a.close")
		.livequery('click', function(event) {
				$("div#export").fadeOut();
				return false;
		}
	);
	$("dl.export dd.ical a").click(function() {
		$("div#export").fadeOut();
	});
// END EXPORT POPUP

// ALLOWED MARKUP
	$("div#allowed-markup").hide();
		$("a.markup").click(function () {
			if ($("div#allowed-markup").is(":hidden")) {
				$("div#allowed-markup").slideDown("slow");
			} else {
				$("div#allowed-markup").slideUp();
			}
			return false;
	    });
// END ALLOWED MARKUP

// CHARACTER COUNT
	$("span#characters").empty();
	$("textarea[name='comment']").charCounter(500, {
		pulse: false,
		format: "%1 characters remaining",
		container: "span#characters"
	});
// END CHARACTER COUNT

	$("span#word-count").empty();
	$("span#word-count").html("0/95-100");
	$("textarea#review").keyup(function() {
		//var count = $("textarea#review").val().split(/\b[\s,\.-:;]*/).length;
		var count = $("textarea#review").val().split(/[\s,-]+/).length;
		var wordcount = count+"/95-100";
		$("span#word-count").empty();
		$("span#word-count").html(wordcount);
	});




// INPUT HINT
	$('input:text').hint();
// ENDINPUT HINT

// AUTO SELECT ALL FLICKR
    $('div.flickr input').focus( function () { 
      $(this).select();
    });
// END AUTO SELECT ALL FLICKR


// STATUS BOX
	$(window).load(function () {
		$("div.status-box").animate( { backgroundColor:"#c0dfb9"} , 200)
		.animate( { backgroundColor:"#99c68e"} , 1500)
		.fadeOut('slow');
	});
// END STATUS BOX


// SOURCES
	$("div.sources dl dd").hide();
	$("div.sources dl dt").empty();
	$("div.sources dl dt").html('<a href="#" class="show-sources">View Sources</a>');
	$("a.show-sources")
		.livequery('click', function(event) {
		
			if ($("div.sources dl dd").is(":hidden")) {
				$("div.sources dl dd").slideDown("slow");
				$("div.sources dl dt").html('<a href="#" class="show-sources">Hide Sources</a>');
			} else {
				$("div.sources dl dd").slideUp();
				$("div.sources dl dt").html('<a href="#" class="show-sources">View Sources</a>');
			}
			return false;
		}
	);

// END SOURCES






/*
if(!window.BlacknWhite)
    var BlacknWhite = {};

BlacknWhite = {
    init     : function(options)
    {
        options = jQuery.extend({minor : 7}, options);

       	if(jQuery.browser.msie && jQuery.browser.version < options.minor)
        	jQuery("html").css("filter","gray");
    }
};

BlacknWhite.init();
*/









});
