jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

function capitalize(string)
{
	var s = "" + string 
    return s.charAt(0).toUpperCase() + s.slice(1);
}


function check_expanded(){
	if ($("#file_logo").val() > "" || $("#file_small_logo").val() > "" || $("#speakers").val() > "" || $("#event_url").val() > "" || $("#event_reg_url").val() > "" || $("#event_twittername").val() > "" || $("#event_hashtag").val() > "")
	{
		$("#basic-submit").hide();
		$("#expanded-submit").show();
	}
	else {
		$("#basic-submit").show();
		$("#expanded-submit").hide();
	}

}

$(document).ready(function(){
	
	// DROPDOWN
	$(".menu, .sort_options").hide();
	
	// clicking a menu button
	$(".drop>a, #sort-drop>a").click(function(){
		$(".menu").slideUp('fast');
		$(this).next().slideDown('fast');
	});
	
	// clicking a menu dropdown
	$(".drop .menu li a").click(function(){
		$(this).parents("ul").children("li").children("a").removeClass("current");
		$(this).addClass("current");
		$(".menu").slideUp('fast');
		$(this).parents(".menuwrap").children("a").children("span").children("span").html($(this).html());		
	});
	$("#sort-drop .menu li a").click(function(){
		$(this).parents("ul").children("li").children("a").removeClass("current");
		$(this).addClass("current");
		$(".menu").slideUp('fast');
		// $(this).parents(".menuwrap").children("a").children("span").children("span").html($(this).html());		
	});
	
	
	$("#cat-list li a, #type-list li a, #price-list li a, #location-list li a").click(function(){
		// $.post("/events/search", { category: $("#cat-list .current").attr("name"), type: $("#type-list .current").attr("name"), sort: $("#sort-list .current").attr("id"), price: $("#price-list .current").attr("name"), location: $("#location-list .current").html(), order: $("#event_table th.current a").attr("name")} , null, "script");
		
		$("a.rss").attr("href", function(){
			href = "/feeds/rss.xml"
			href += "?category="+ $("#cat-list .current").attr("name");
			href += "&type=" + $("#type-list .current").attr("name");
			href += "&sort=" + $("#sort-list .current").attr("id");
			href += "&price=" + $("#price-list .current").attr("name");
			href += "&location=" + $("#location-list .current").html();

			return href
		});
		
		href= "/" + $("#type-list .current").attr("name");
		if($("#cat-list .current").attr("name") != "everything"){
			href+= "/" + "on/" + $("#cat-list .current").attr("name");
		}
		href += "/"
		window.location.href= href;
		
	});
	
	// pre-fill dropdowns on page load if URL is set
	$("#type-list, #cat-list").children("li").children("a").removeClass("current");
	$("#type-list li a[name='" + jQuery.url.segment(0) + "']").addClass("current");
	$("#type-list").siblings(".button").children("span").children(".arrow").html(capitalize(jQuery.url.segment(0)));
	
	if(jQuery.url.segment(2) != null){
		$("#cat-list li a[name='" + jQuery.url.segment(2) + "']").addClass("current");
		$("#cat-list").siblings(".button").children("span").children(".arrow").html(capitalize(jQuery.url.segment(2)));
	}
	else{
		$("#cat-list li a[name='everything']").addClass("current");
		// $("#cat-list").siblings(".button").children("span").children(".arrow").html(capitalize(jQuery.url.segment(2)));
	}
	
	
	// these next two make sure that when you mouse off a dropdown, it goes away
	$(".drop .menu").hoverIntent({
		sensitivity: 1, 
	    interval: 100,    
	    over: function () {$(this).show();},    
	    timeout: 500,     
	    out: function () {$(this).slideUp('fast');}
	});
	$(".menuwrap").hoverIntent({
		// whole nav ul
		sensitivity: 1,    
	    interval: 100,    
	    over: function () {}, 
	    timeout: 500,
	    out: function(){$(".menu").slideUp('fast');}
	});
	
	
	

	
	$('.location_item .suggest_input').autocomplete({ 
		serviceUrl:'/suggest/location.ashx',
		delimiter:/(,|\+)\s*/,
		width: 136,
		deferRequestBy: 200, //miliseconds
		// callback function:
		onSelect: function(value, data){
			var art = "in";
			var s = jQuery.url.attr("path");
			var url = new Array();
			url = s.split('/');
			var val = value.toLowerCase();
			var temp = val.split(" ");
			val = temp.join("_");
			
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art){
					url[i+1] = val;
				}
				new_url.push(url[i]);
			}
			if(url.indexOf(art) == -1){
				new_url.pop();
				new_url.push(art);
				new_url.push(val);
				new_url.push("");
			}
			// alert(new_url);
			document.location = new_url.join("/");
		}			
	});
	$('.speaker_item .suggest_input').autocomplete({ 
		serviceUrl:'/suggest/speaker.ashx',
		delimiter:/(,|\+)\s*/,
		width:136,
		deferRequestBy: 200, //miliseconds
		// callback function:
		onSelect: function(value, data){
			var art = "with";
			var s = jQuery.url.attr("path");
			var url = new Array();
			url = s.split('/');
			var val = value.toLowerCase();
			var temp = val.split(" ");
			val = temp.join("_");
			
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art){
					url[i+1] = val;
				}
				new_url.push(url[i]);
			}
			if(url.indexOf(art) == -1){
				new_url.pop();
				new_url.push(art);
				new_url.push(val);
				new_url.push("");
			}
			// alert(new_url);
			document.location = new_url.join("/");
		}		
	
	});
	$('.tag_item .suggest_input').autocomplete({ 
		serviceUrl:'/suggest/tag.ashx',
		delimiter:/(,|\+)\s*/,
		width: 136,
		deferRequestBy: 200, //miliseconds
		// callback function:
		onSelect: function(value, data){
			var art = "about";
			var s = jQuery.url.attr("path");
			var url = new Array();
			url = s.split('/');
			var val = value.toLowerCase();
			var temp = val.split(" ");
			val = temp.join("_");
			
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art){
					url[i+1] = val;
				}
				new_url.push(url[i]);
			}
			if(url.indexOf(art) == -1){
				new_url.pop();
				new_url.push(art);
				new_url.push(val);
				new_url.push("");
			}
			// alert(new_url);
			document.location = new_url.join("/");
		}
	});
	

	
	
 // $('#dates').DatePickerShow();
 // $('#dates').DatePicker({
 // 	flat: true,
 // 	date: ['2008-07-28','2008-07-31'],
 // 	current: '2008-07-31',
 // 	calendars: 3,
 // 	mode: 'range',
 // 	starts: 1
 // 	});



	$('#description textarea').show_char_limit(300, {
	    status_element: '#description .limit',
	    error_element: '#description'
	  } );

	
	// event table <th> sorting
	// $("#public #event_table table th a").click(function(){ 
	// 	$("#event_table th").removeClass("current");
	// 	$(this).parent("th").addClass("current");
	// 	
	// 	$.post("/events/search", {
	// 		 category: $(".category_item .current").attr("name"), 
	// 		 type: $("#type_item .current").attr("name"), 
	// 		 price: $(".price_item .current").attr("name"), 
	// 		 location: $(".location_item .current").html(), 
	// 		 order: $("#event_table th.current a").attr("name")}, 
	// 		 null, "script");
	// 
	// 	
	// 	$("#event_table th.current a").attr("name", function(){
	// 		var temp = this.name.split("asc").join("desc");
	// 		if(this.name == temp){
	// 			temp = this.name.split("desc").join("asc");
	// 		}
	// 		return temp;
	// 	});
	// 	
	// 	if($("#event_table th.current a").hasClass("asc")){
	// 		$(this).removeClass("asc").addClass("desc");
	// 	}
	// 	else{
	// 		$(this).removeClass("desc");
	// 		$(this).addClass("asc");
	// 	}
	// 	
	// });
	
	// diesel event table <th> sorting
	// $("#diesel #event_table table th a").click(function(){ 
	// 	$("#event_table th").removeClass("current");
	// 	$(this).parent("th").addClass("current");
	// 	
	// 	$.post("/diesel/search", {order: $("#event_table th.current a").attr("name")} , null, "script");
	// 	
	// });
	// 
	// // admin event table <th> sorting
	// $("#admin #event_table table th a").click(function(){ 
	// 	$("#event_table th").removeClass("current");
	// 	$(this).parent("th").addClass("current");
	// 	
	// 	$.post("/admin/search", {order: $("#event_table th.current a").attr("name")} , null, "script");
	// 	
	// });
	
	
	$("#event_country").change(function(){
		if($("#event_country option:selected").attr("value") != "US"){
			// $("#currency_info").show();
		}
		else{
			// $("#currency_info").hide();
		}
	});
	
	$("#delete_file").click(function(){
		$("#filename").hide();
		$("#file_fields").show();
	});
	$("#delete_smallfile").click(function(){
		$("#smallfilename").hide();
		$("#smallfile_fields").show();
	});
	
	
	$(".enddate").hide();
	$("#dates_multiday").click(function(){
		// set the enddate to the startdate
		$("#enddates select option").removeAttr("selected");
		$("#event_enddate_3i").val($("#event_startdate_3i").val());
		$("#event_enddate_2i").val($("#event_startdate_2i").val());
		$("#event_enddate_1i").val($("#event_startdate_1i").val());
		
		// show the enddate fields
		$(".enddate").toggle();
	});
	
	
	
// NEW FILTER CODE 

	// hide drops
	$(".filter_item ul, .suggest_item>span").hide();
		
	// hover behavior
	$(".filter_item, .suggest_item").hover(function(){
		$(this).children("a").addClass("on");
	}, 	function(){
			$(this).children("a").removeClass("on");
	});
	
	
	// button click
	$(".filter_item>a").click(function(){
		$(".menu").slideUp('fast');
		$(this).siblings(".menu").css("min-width", $(this).parents(".filter_item").width() + 15);
		$(this).siblings(".menu").slideDown('fast');
	});
	
	// suggest click
	$(".suggest_item>a").click(function(){
		$(this).hide();
		$(this).siblings("span").show();
		$(this).siblings("span").children("input").val($(this).children("span").html());
	});
	
	// remove button click
	
	
	
	$("#anycity").click(function(){
		var art = "in";
		var s = jQuery.url.attr("path");
		var url = new Array();
		url = s.split('/');
		
		if(url.indexOf(art) == -1){
			// do nothing
			// $(this).parents(".suggest_item").remove();
		}
		else {
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art || url[i-1] == art){}
				else{
					new_url.push(url[i]);
				}
			}
			document.location = new_url.join("/");
		}
		
	});
	$(".speaker_item .remove_button").click(function(){
		var art = "with";
		var s = jQuery.url.attr("path");
		var url = new Array();
		url = s.split('/');
		
		if(url.indexOf(art) == -1){
			// do nothing
			$(this).parents(".suggest_item").remove();
		}
		else {
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art || url[i-1] == art){}
				else{
					new_url.push(url[i]);
				}
			}
			document.location = new_url.join("/");
		}
		
	});
	$(".tag_item .remove_button").click(function(){
		var art = "about";
		var s = jQuery.url.attr("path");
		var url = new Array();
		url = s.split('/');
		
		if(url.indexOf(art) == -1){
			// do nothing
			$(this).parents(".suggest_item").remove();
		}
		else {
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art || url[i-1] == art){}
				else{
					new_url.push(url[i]);
				}
			}
			document.location = new_url.join("/");
		}
		
	});
	
	$(".price_item .remove_button").click(function(){
		var art = "under";
		var s = jQuery.url.attr("path");
		var url = new Array();
		url = s.split('/');
		
		if(url.indexOf(art) == -1){
			// do nothing
			$(this).parents(".price_item").remove();
		}
		else {
			var new_url = new Array();
			for(var i = 0; i < url.length; i++){
				if(url[i] == art || url[i-1] == art){}
				else{
					new_url.push(url[i]);
				}
			}
			document.location = new_url.join("/");
		}
		
	});
	
	
	$(".filter_item .menu").hoverIntent({
		sensitivity: 1, 
	    interval: 100,    
	    over: function () {$(this).show();},    
	    timeout: 500,     
	    out: function () {$(this).slideUp('fast');}
	});
	
	$(".filter_item .menu a").click(function(){
		$(this).parents(".menu").slideUp('fast');
	});
	
	
	// dropdown click
	$(".menu li a").click(function(){
		// $(this).parents(".menu").siblings("a").children("span").html($(this).html());
	});
	
	
	// add filter actions
	$("#add_item li a.suggest_link").click(function(){
		$("#hidden_item ." + $(this).attr("name") + "_item").insertBefore("#add_item");
		$("#filters ." + $(this).attr("name") + "_item>a").hide().siblings("span").show();
		$("#filters ." + $(this).attr("name") + "_item .suggest_input").focus();
	});
	$("#add_item li a.filter_link").click(function(){
		$("#hidden_item ." + $(this).attr("name") + "_item").insertBefore("#add_item");
		// $("#filters ." + $(this).attr("name") + "_item>a").hide().siblings("span").show();
		// $("#filters ." + $(this).attr("name") + "_item .suggest_input").focus();
	});

	
// END FILTER CODE

	


// match the state to the dropdown in event>edit
	$("#event_state option[value='"+ $("#state_value").val() + "']").attr('selected', 'selected');
	$("#event_country option[value='"+ $("#country_value").val() + "']").attr('selected', 'selected');
	
// add class last to last row of eventtable
	$("table.eventtable tr:last").addClass("last");
	
	// twitter/online height adjustments
	$("#event-twitter").height($("#event-website").height());
	
	// tumblr feed adjustments
	$(".tumblr_posts li").hide();
	$(".tumblr_posts li:first").show();
	$(".tumblr_posts li:first .tumblr_body p").hide();
	$(".tumblr_posts li:first .tumblr_body p:first").show();
	
	
	// event table hover
	$(".events-index .eventtable tbody tr, .events-facets .eventtable tbody tr").hover(
		function(){$(this).children("td").addClass("td-hover")}, 
		function(){$(this).children("td").removeClass("td-hover")}
	).click(function(){
		document.location = $(this).children("td.last").children("a").attr("href");
		$(this).children("td").removeClass("td-hover");
	});
	
	
	//form button hiding
	check_expanded();
	
	$(".events-new .expanded input, .events-new .expanded textarea, .events-edit .expanded input, .events-edit .expanded textarea, .events-create .expanded input, .events-create .expanded textarea").keyup(function(){
		check_expanded();
	});
	
	//promocode
	$("#promocode").click(function(){
		$("form.purchase").attr("action", "/purchase/promo?event=" + $("input#event").val());
		$("form.purchase").submit();
	});
	
});
