$(document).ready( function () {
	
	$('#slides img').show();
	
	// Expand Panel
	/*
	$("#panelLocationDiv").toggle(
		function(){
			$("#panel").slideDown("slow");
		},
		function(){
			$("#panel").slideUp("slow");
		}
	);
	*/
	
	$('#featuredvenue .eventCount').hide();
	
	//Search
	$('.searchText')
		.focus( function () {
			if($(this).val() == 'search') {
				$(this).val('');
			}
		})
		.blur( function () {
			if($(this).val() == '') {
				$(this).val('search');
			}
		});	
	
	$('.go, .submit, .resultsSubmit, .buy').hover(
		function () {
			$(this).css({'background-position': '0px -30px'});
		},
		function () {
			$(this).css({'background-position': '0px 0px'});
		}
	);
	
	$('.go, .submit').click( function() {
		$('#searchForm').submit();
	});
	$('input.searchText, input.searchFrom, input.searchTo').keypress( function(e) {
		if(e.which == 13) {
			$('#searchForm').submit();
		}
	});
	$('.resultsSubmit').click( function() {
		$('#resultsForm').submit();
	});
	$('input.resultsFrom, input.resultsTo').keypress( function(e) {
		if(e.which == 13) {
			$('#resultsForm').submit();
		}
	});
	
	
	
	$('#toggleLocal, #toggleAll').click( function() {
		$('#resultsForm').submit();
	});
	

	//From
	$('.searchFrom, .resultsFrom')
		.focus( function () {
			$(this).val('');
		})
		.blur( function () {
			if($(this).val() == '') {
				$(this).val('from');
			}
		});
	
	//To
	$('.searchTo, .resultsTo')
		.focus( function () {
			$(this).val("");
		})
		.blur( function () {
			if($(this).val() == '') {
				$(this).val('to');
			}
		});
	
	//Datepicker
	$('.searchFrom, .searchTo, .resultsFrom, .resultsTo').datepicker({showOn: 'both', buttonImage: 'http://fgt-static.s3.amazonaws.com/common/portal/1/i/calendar.png', buttonImageOnly: true, minDate: 0});
	$('#ui-datepicker-div').draggable();
		
	$('.calendar').toggle( 
		function () {
			$('#search').css({'background-position': '0px -50px'});
			$('#advanced').css({'display': 'block'});
			$('.calendar').css({'background-position': '0px -60px'});
		},
		function () {
			$('#search').css({'background-position': '0px 0px'});
			$('#advanced').css({'display': 'none'});
			$('.calendar').css({'background-position': '0px 0px'});
		}
	);

	//Autocomplete
	$('.searchText').autocomplete('/completion/search', {
		minChars: 4,
		matchSubset: 1,
		cacheLength: 10,
		matchContains: 1,
		selectFirst: false,
		scroll: true
	});
	$('#location').focus( function () {
		//$(this).val("");
	});
	$('#location').autocomplete('/completion/location', {
		minChars: 3,
		max: 200,
		scroll: true
	});
	
	$('#spawnLocationDialog, #panelLocationDiv').click( function() {
		$('#setLocationDialog').dialog('open');
	});
	$('#setLocationDialog').dialog({
		autoOpen: false,
		height: 200,
		width: 350,
		minWidth: 350,
		minHeight: 200,
		modal: true,
		buttons: {
			submit: function() {
				$('#setLocation').submit();
				$(this).dialog('close');
			},
			cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$('.spawnSignupDialog').click( function() {
		$('#signupDialog').dialog('open');
	});
	$('#signupDialog').dialog({
		autoOpen: false,
		height: 310,
		width: 350,
		minHeight: 310,
		minWidth: 350,
		modal: true,
		buttons: {
			submit: function() {
				$('#signUp').submit();
				$(this).dialog('close');
			},
			cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	

	$('#nav a:not(.vr)').hover( 
		function () {
			$(this).addClass('highlight');
		},
		function () {
			$(this).removeClass('highlight');
		}
	);
	
	//Highlight featured artists
	$('.bannerNav li').hover( 
		function () {
			$(this).addClass('highlight');
		},
		function () {
			$(this).removeClass('highlight');
		}
	);
			
	//Banner Slides
	$("#controls").jFlow({
		slides: "#slides",
		width: "940px",
		height: "240px",
		duration: 200
	});
			
	//Simple List		
	$('div.ribbon').find('.simple').next().each( function () {		
		var list = $(this).prev().find('ul li');
		if($(this).prev().find('ul').children().size() <= 5) {
			$(this).css({'display': 'none'});
		}
		var current = 0;
		var next = 5;
		$(this).prev().find('ul li').hide();
		$(this).prev().find('ul li:lt('+next+')').show();
		
		$(this).click( function () {
			var count = $(this).prev().find('ul').children().size();
			current = current + 5;
			next = current+5;
			if(current < count) {
				$(this).prev().find('ul li:lt('+next+')').show();
				$(this).prev().find('ul li:lt('+current+')').hide();
				$(this).prev().find('ul li:gt('+next+')').hide();
			} else {
				current = 0;
				next = current+5;
				$(this).prev().find('ul li').hide();
				$(this).prev().find('ul li:lt('+next+')').show();
			}
		});
	});
	
	//Expanded List		
	$('div.ribbon').find('.expanded').next().each( function () {		
		var list = $(this).prev().find('ul li');
		if($(this).prev().find('ul').children().size() <= 5) {
			$(this).css({'display': 'none'});
		}
		var current = 0;
		var next = 5;
		$(this).prev().find('ul li').hide();
		$(this).prev().find('ul li:lt('+next+')').show();
		
		$(this).click( function () {
			var count = $(this).prev().find('ul').children().size();
			current = current + 5;
			next = current+5;
			if(current < count) {
				$(this).prev().find('ul li:lt('+next+')').show();
				$(this).prev().find('ul li:lt('+current+')').hide();
				$(this).prev().find('ul li:gt('+next+')').hide();
			} else {
				current = 0;
				next = current+5;
				$(this).prev().find('ul li').hide();
				$(this).prev().find('ul li:lt('+next+')').show();
			}
		});
	});
	
	//Freeform
	$('div.ribbon').find('.freeform').next().each( function () {
		var list = $(this).prev().find('ul li');	
		var offset = -180;				
		var height = 0;
		$(this).click( function () {
			var totalHeight = $(this).prev().find('ul').height();
			var max = Math.ceil(totalHeight / Math.abs(offset));
			var current = list.offset().top-189.5;
			//alert('totalHeight:'+totalHeight+' max:'+max+' current:'+Math.abs(current));
			if (Math.abs(current) < totalHeight+offset) {			
				current = current+offset;
				list.animate({top: current},"fast")
			} else {
				list.animate({top: 0},"fast")
				current = 0;
			}
		});
	});
	
	$('.more').hover( 
		function () {
			$(this).css({'background-position': 'center -25px'});
		},
		function () {
			$(this).css({'background-position': 'center 0px'});
		}
	);
	
	$('.rss').hover( 
		function () {
			$(this).css({'background-position': 'center -50px'});
		},
		function () {
			$(this).css({'background-position': 'center 0px'});
		}
	);
	
	$('#spawnLocationDialog').hover(
		function () {
			$('.locationIcon').css({'background-position': 'center -30px'});
		},
		function () {
			if($('.locationIcon').hasClass('off')) {
				$('.locationIcon').css({'background-position': 'center -60px'});
			} else {
				$('.locationIcon').css({'background-position': 'center 0px'});
			}
		}
	);
		
	//Category Open/Close
	$('.otherHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -200px'});
			$('#other').css({'display': 'none'});
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -225px'});
			$('#other').css({'display': 'block'});
		}
	);
	$('.festivalHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -150px'});
			$('#festival, #festival-nav').css({'display': 'none'});
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -175px'});
			$('#festival, #festival-nav').css({'display': 'block'});
		}
	);
	$('.artistHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -100px'});
			$('#artist, #artist-nav').css({'display': 'none'});
			
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -125px'});
			$('#artist, #artist-nav').css({'display': 'block'});
		}
	);
	$('.featuredartistHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -100px'});
			$('#featuredartist').css({'display': 'none'});
			
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -125px'});
			$('#featuredartist').css({'display': 'block'});
		}
	);
	$('.venueHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -50px'});
			$('#venue, #venue-nav').css({'display': 'none'});
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -75px'});
			$('#venue, #venue-nav').css({'display': 'block'});
		}
	);
	$('.featuredvenueHeader').toggle( 
		function () {
			$(this).find('.arrow').css({'background-position': 'center -50px'});
			$('#featuredvenue').css({'display': 'none'});
		},
		function () {
			$(this).find('.arrow').css({'background-position': 'center -75px'});
			$('#featuredvenue').css({'display': 'block'});
		}
	);
	
	
	$('.closed').toggle( 
		function () {
			$(this).removeClass('closed');
			$(this).addClass('open');
			$(this).find('.eventTitle .place').hide();
			$(this).parent().find('.event, .eventMore:first').css({'display': 'block'});
			if($(this).parent().find('.event').size() == 0 ) {
				$(this).parent().find('.eventMore:first').trigger('click');
			}
		},
		function () {
			$(this).removeClass('open');
			$(this).addClass('closed');
			$(this).find('.eventTitle .place').show();
			$(this).parent().find('.event, .eventMore').css({'display': 'none'});
		}
	);
	
	$('.resultsMore').click(function() {
		$(this).hide();
		var url = $(this).attr('href');
		//alert(url);
		var current = $(this);
		var response = $.ajax({
		  beforeSend: function() {
			current.parent().find('.loader').show();
		  },
		  url: url,
		  async: false,
		  timeout: 1000,
		  cache: false,
		  success: function(html) {
			current.parent().find('.loader').hide();
			current.prev().prev().after(html);
			current.parent().find('.event').show();
			current.next().show();
		  }
		}).responseText;
		$(this).remove();
	});
	
	$('.eventMore').click(function() {
		$(this).hide();
		//$(this).next().css({'display': 'block'});
	
		// /events/:key/:value/:page/:useLoc/:term/:from/:to
		var url = $(this).attr('href');
		var current = $(this);
		var response = $.ajax({
		  beforeSend: function() {
			current.parent().find('.loader').show();
		  },
		  url: url,
		  async: false,
		  timeout: 1000,
		  cache: false,
		  success: function(html) {
			current.parent().find('.loader').hide();
			current.prev().prev().after(html);
			current.parent().find('.event').show();
			current.next().show();
		  }
		}).responseText;
		$(this).remove();
	});
	
	//Timer Settings
	var myTimer = {};
	myTimer = $.timer(7000, function (timer) {
		$(".slideNext").trigger('click');
	});
	$(".slidePrev,.slideControl,.slideNext,.ribbon,#slides").hover(
		function () {
			//window.status = 'Stopped Banner.';
			myTimer.stop();
		},
		function () {
			//window.status = 'Restarted Banner.';
			myTimer.reset(7000);
		}
	);
	
	var path = '#'+location.pathname.substring(1);
	$(path).each(function() {
        $(this).addClass("selected");
    });
	
});
