$(document).ready(function () {
	$("li, tbody, button").click(function(e) {	
		if ($(this).attr("href")) {
			document.location = $(this).attr("href");
		} else if ($(this).attr("action")) {
			$("INPUT[name=action]").val($(this).attr("action"));
		}
	});
	
	$("button.back").click(function() {	
		history.back();
	});
	
	$("h1#logo").click(function() {
		document.location='/';
	});
	
	if ($("form#search-bar")) {
		$("input#searchstring").focus(function() {
			if ($(this).val()=='Enter team name, performance, artist... or just press let it flow') {
				$(this).val('');
			}
		});
	
		$("input#searchstring").blur(function() {
			if ($(this).val()=='') {
				$(this).val('Enter team name, performance, artist... or just press let it flow');
			}
		});
		
		$("button.submit_button").click(function(e) {
			e.preventDefault();
			if ($("input#searchstring").val()!='' && $("input#searchstring").val()!='Enter team name, performance, artist... or just press let it flow') {
				$("form#search-bar").submit();
			}
		});
	
		$("button.letitflow_button").click(function(e) {
			e.preventDefault();
			$("form#search-bar #searchstring").val('');
			$("form#search-bar").submit();
		});
	}
		
	$("#location_selector_holder span.hide a, #date_range_selector_holder span.hide a").click(function(e) {
		e.preventDefault();
		$('#location_selector_holder').slideUp();
		$('#date_range_selector_holder').slideUp();	
	});

	$("#date_range_change_button").click(function() {
		$('#location_selector_holder').hide();
		$('#date_range_selector_holder').slideDown();
	});

	$("#location_change_button").click(function() {
		$('#date_range_selector_holder').slideUp();
		$('#location_selector_holder').slideDown();
	});
	
	$(".rounded").each(function(){
		//$(this).corner();
	});

	$("a.new_window").click(function(e) {
		e.preventDefault();
		window.open($(this).attr("href")); 
		return false;
	});

});

function load_live_ticket_data(hash, event_id, time) {
	div_id = "#live-ticket-data";
	map_id = "#seat-quest-js";
	
	if ( blocker_shown == true )
	{	
		$(div_id).empty();
		$(div_id).html($('#event_loading').html());	
	}
	
	$.ajax({
		url: "/live_ticket_data/",
		data: ({hash: hash, event_id: event_id, time: time}),
		success: function() {
			$.getJSON("/live_ticket_ranges/", {hash: hash, event_id: event_id, time: time, view: global_view},
			function (data) {
	 			global_min_ticket_price = parseInt(Math.round(data.min_price));
	 			global_max_ticket_price = parseInt(Math.round(data.max_price));
	 			
	 			global_min_tickets = parseInt(data.min_tickets);
	 			global_max_tickets = parseInt(data.max_tickets);
	 			
	 			load_sliders();
	 			
	 			min_price = $("#slider-pricerange").slider("values", 0);
				max_price = $("#slider-pricerange").slider("values", 1);
			
				min_tickets = $("#slider-ticketrange").slider("values", 0);
				max_tickets = $("#slider-ticketrange").slider("values", 1);
				
				section = current_sections;
				
				$.get("/live_ticket_view/", 
					{
						hash: hash, 
						event_id: event_id,
						time: time,
						min_price: min_price,
						max_price: max_price,
						min_tickets: min_tickets,
						max_tickets: max_tickets,
						section: [section],
						disabled_sources: [disabled_sources()],
						order_by: global_order_by,
						sort: global_sort_by,
						page: global_page,
						view: global_view
					}, 
				 	function(html) {
				 		$(div_id).empty();
				 		$(div_id).html(html);
				 		$('#event_loading').hide();
						$.unblockUI();						
						$.ajax({
							url: "/update_live_ticket_data/",
							data: ({hash: hash, event_id: event_id})
						});	
					}
					
				);	
				
				$.get("/live_ticket_sections/", 
						{
							hash: hash, 
							event_id: event_id,
							view: global_view
						}, 
					 	function(html) {
					 		$("#section-tags-holder").empty();
					 		$("#section-tags-holder").html(html);
					 		$('#event_loading').hide();

					 		$("#section-tags-holder a").click(function() {							
								if ( $(this).attr("rel") == 'show_all' )
								{		
									if ( global_sections_shown == false )
									{	
										$('#show_all_sections').text('Show Less Sections');										
										$('#section-holder-small').hide();
										$('#section-holder').slideDown();
										global_sections_shown = true;										
									}								
									else
									{
										$('#show_all_sections').text('Show More Sections');
										$('#section-holder').hide();
										$('#section-holder-small').show();										
										global_sections_shown = false;										
									}
									return;
								} 
								else if ( $(this).attr("rel") == 'clear' )
								{
									for ( i in current_sections )
										current_sections.splice(i, current_sections.length);

								}
								else if ( $(this).attr("rel") == 'toggle_tickets' )
								{
									updateView('tickets');
								}
								else if ( $(this).attr("rel") == 'toggle_parking' )
								{
									updateView('parking');
								}								
								else
								{									
									current_sections[current_sections.length] = $(this).attr("rel");									
								}
								update_live_ticket_data();
					 		})
						}
					);	
				if ( seatquest_called == false )
				{
					$.get("/seat_quest/", { event_id: event_id, time: time, hash: hash }, function (data) {
						$(map_id).empty();
						$(map_id).html(data);
					});
					seatquest_called = true;					
				}					
	 		});
		}
	});
}

function get_html_sections() {
	var list = $('#section-tags-holder-no-js').html();
	list = list.split(',');	
	return list;
}

$("a.set_location").click(function(event) {
	event.preventDefault();
	$('#location_selector_holder').slideUp();
	$.get($(this).attr("href"), function(html){
		$("p#location").empty();
		$("p#location").html(html);
	});
});

function set_location(city_id) {
	$('#location_selector_holder').slideUp();
	$.get("/set_location/", { city_id: city_id }, function(html){
		$("p#location").empty();
		$("p#location").html(html);
	});	
}

function reset_location() {
	$("INPUT[name=location]").val("");
	//document.form.submit();
}

function updatePriceSlider() {
	$("#slider-pricerange-amount").val('$' + $("#slider-pricerange").slider("values", 0) + ' - $' + $("#slider-pricerange").slider("values", 1));
}

function updateTicketSlider() {	
	$("#slider-ticket-amount").val($("#slider-ticketrange").slider("values", 0) + ' - ' + $("#slider-ticketrange").slider("values", 1));
}

// populate iggli data
function populateInviteData(venue_title, venue_location, venue_date) {
	//all the possible fields in this json object
	json = {
		partner_id:"72",
		title:venue_title,
		location:venue_location,
		date:venue_date,
		event_url:location.href
	}

	//replace any current invite data using the json object
	inviteButtons[0].setOptions(json);

	//refresh the html to reflect new data
	inviteButtons[0].refresh();

	// launch the invite
	loadInviteInModal(0);
}


function clearAllFilters() {
	// Clear Section Filter
	set_current_section(false);	
	
	// Clear Price Filter
	$("#slider-pricerange").slider("values", 0, 1);
	$("#slider-pricerange").slider("values", 1, maxTicketPrice);	
	update_prices();
	
	// Clear Ticket filter
	$("#slider-ticketrange").slider("values", 1, defaultMaxTicket);
	update_tickets();
}

