if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj)
	{
		for(var i = 0; i < this.length; i++)
		{
			if(this[i] == obj)
				return i;
		}
		return -1;
	}
}

$(document).ready(function()
{
	var $selectMonths = $('#select-months-slices');
	var center = {
		x: ($selectMonths.width() / 2),
		y: ($selectMonths.height() / 2)
	};
	$selectMonths.find('img.slice').css('opacity', 0.5).data('opacity', 0.5);
	var selectedMonth = false;
	var selectedMonths = [];
	$selectMonths.mousemove(function(e)
	{
		monthWheelIn(getMonthFromEvent(e));
	});
	$('#select-months').find('a').hover(function()
	{
		monthWheelIn(parseInt($(this).attr('rel')));
	}, function()
	{
		monthWheelOut();
	});
	$selectMonths.mouseout(function()
	{
		monthWheelOut();
	});
	$selectMonths.click(function(e)
	{
		monthWheelClick(getMonthFromEvent(e));
		return false;
	});
	$('#select-months').find('a').click(function()
	{
		monthWheelClick(parseInt($(this).attr('rel')));
	});
	
	function monthWheelIn(month)
	{
		if (month != selectedMonth) {
			if (month) {
				rad = (month-1) * (2*Math.PI / 12) - Math.PI/2 + (Math.PI/12);
				var x = Math.cos(rad)*2;
				var y = Math.sin(rad)*2;
				$('#month-'+month).stop().animate({opacity: 1, left: x+'px', top: y+'px'}, 'fast');
			}
			if (selectedMonth) {
				var o = $('#month-'+selectedMonth).data('opacity');
				var p = $('#month-'+selectedMonth).data('pos');
				if (!p) {
					p = {x: '0px', y: '0px'};
				}
				$('#month-'+selectedMonth).stop().animate({opacity: o, left: p.x, top: p.y}, 'fast');
			}
			selectedMonth = month;
		}
	}
	
	function monthWheelOut()
	{
		if (selectedMonth) {
			var o = $('#month-'+selectedMonth).data('opacity');
			var p = $('#month-'+selectedMonth).data('pos');
			if (!p) {
				p = {x: '0px', y: '0px'};
			}
			$('#month-'+selectedMonth).stop().animate({opacity: o, left: p.x, top: p.y}, 'fast');
			selectedMonth = false;
		}
	}
	
	function monthWheelClick(month)
	{
		if (month) {
			var index = selectedMonths.indexOf(month);
			var $month = $('#month-'+month);
			if (index == -1) {
				if (selectedMonths.length > 0) {
					$('#month-'+selectedMonths[0]).data('opacity', 0.5);
					$('#month-'+selectedMonths[0]).data('pos', {x: '0px', y: '0px'});
					$('#month-'+selectedMonths[0]).stop().animate({opacity: 0.5, left: '0px', top: '0px'}, 'fast');
					selectedMonths = [];
				}
				selectedMonths.push(month);
				$month.data('opacity', 1);
				$month.data('pos', {x: $month.css('left'), y: $month.css('top')});
			} else {
				selectedMonths.splice(index, 1);
				$month.data('opacity', 0.5);
				$month.data('pos', {x: '0px', y: '0px'});
			}
			filterVenues();
		}
	}
	
	$('#select-event-type ul li a').click(function()
	{
		this.blur();
		var $elem = $(this).parents('ul');
		
		if ($(this).attr('class') != 'active') {
			if ($(this).attr('id') == 'all-event-types') {
				if($elem.css('height') != 'auto') {
					$elem.css('height', 'auto');
					$('.arrow').removeClass('down').addClass('up');
				}
			}
			$('#select-event-type ul li a').removeClass('active');
			$(this).addClass('active');
		} else {			
			if ($(this).attr('id') == 'all-event-types') {
				if($elem.css('height') == 'auto') {
					$elem.css('height', '15px');
					$('.arrow').removeClass('up').addClass('down');
				} 
				$(this).removeClass('active');
			} else {
				$(this).removeClass('active');
			}
		}
		filterVenues();
		return false;
	});
	
	$('#select-event-type .arrow').click(function()
	{
		var $elem = $(this).siblings('ul');
		if ($(this).attr('class').split(' ').indexOf('down') == -1) {
			$(this).addClass('down');
			$(this).removeClass('up');
			$elem.css('height', '15px');
		} else {
			$(this).addClass('up');
			$(this).removeClass('down');
			$elem.css('height', 'auto');
		}
		return false;
	});
	
	$('#select-guestcount').change(function()
	{
		filterVenues();
	});
	
	var guestCount;
	$('#select-guestcount').keyup(function()
	{
		var count = $.trim($(this).find('input').val());
		if (count != guestCount) {
			filterVenues();
			guestCount = count;
		}
	});
	
	$('.venue-change-media').click(function(){
		this.blur();
		$id = parseInt($(this).attr('rel'));
		
		$('#visible-media img.visible').removeClass('visible');
		$('#visible-media img[id="media-'+$id+'"]').addClass('visible');
		
		return false;
	});
	
	$('#select-location ul li a').click(function()
	{
		this.blur();
		var $parent = $(this).parent();

		if ($parent.attr('class').indexOf('active') == -1) {
			$('#select-location ul li').removeClass('active');
			$parent.addClass('active');
		} else {
			$parent.removeClass('active');
		}
		filterVenues();
		return false;
	});
	
	$('#clear-filters a').click(function()
	{
		for (var i in selectedMonths) {
			var selectedMonth = selectedMonths[i];
			var $month = $('#month-'+selectedMonth);
			$month.data('opacity', 0.5);
			$month.data('pos', {x: '0px', y: '0px'});
			$month.css({opacity: 0.5, left: '0px', top: '0px'});
		}
		selectedMonths = [];
		
		var $eventType = $('#select-event-type');
		$eventType.find('ul').css('height', '15px');
		$eventType.find('.arrow').removeClass('up').addClass('down');
		$eventType.find('a').removeClass('active');
		
		guestCount = '';
		$('#select-guestcount input').val('');
		
		$('#select-location ul li').removeClass('active');

		filterVenues();
		
		$(this).blur();
		return false;
	});
	
	function getMonthFromEvent(e)
	{
		var x = e.pageX - $selectMonths.offset().left;
		var y = e.pageY - $selectMonths.offset().top;
		var distance = Math.sqrt(Math.pow(x-center.x, 2) + Math.pow(y-center.y, 2));
		if (distance > 50) {
			return false;
		}
		var rad = Math.atan2(y-center.y, x-center.x) + Math.PI/2;
		if (rad < 0) {
			rad += Math.PI*2;
		}
		return (Math.floor(rad / (Math.PI*2) * 12) + 1);
	}
	
	function filterVenues()
	{
		var types = [];
		$('#select-event-type ul li a.active').each(function()
		{
			types.push(parseInt($(this).attr('rel')));
		});

		var guests = parseInt($.trim($('#select-guestcount input').val()));
		if (!guests) guests = 0;
		
		var locations = [];
		$('#select-location ul li.active a').each(function()
		{
			locations.push($(this).attr('rel'));
		});
		
		var events = [];
		var eventVenues = [];

		$('#venues .venue-filter').hide();
		for (var i = 0; i < venues.length; i++) {
			var venue = venues[i];
			var found;

			if (eventVenues.indexOf(venue.Venue.id) != -1) {
				continue;
			}

			if (!venue.show_always) {
				if (selectedMonths.length == 0 && types.length == 0) {
					if (!venue['default']) continue;
				}

				if (selectedMonths.length > 0) {
					found = false;
					for (var j in selectedMonths) {
						if (venue.months.indexOf(selectedMonths[j]) != -1) {
							found = true;
							break;
						}
					}
					if (!found)	continue;
				}

				found = (types.length == 0 || types[0] == 0 || types.indexOf(venue.venue_type_id) != -1);
				if (!found)	continue;
			
				found = (guests <= venue.guests);
				if (!found) continue;

				if (locations.length > 0) {
					found = (locations.indexOf(venue.location) != -1);
					if (!found) continue;
				}
			}

			events.push(venue.id);
			eventVenues.push(venue.Venue.id);
			$('#venue-'+venue.id).show();
		}
		
		$('#page-content').hide();
		$('#venue-right-list').hide();
		$('#venues').show();
		
		$.post(
			'/filter/update',
			{
				'months[]': selectedMonths,
				'types[]': types,
				'guests': guests,
				'locations[]': locations,
				'events[]': events
			}
		);
	}

	
	$('#front-splasher').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '323px'
	});

	$('.send-link').click(function(){
		$('#overlay').show();
		$('#sendlink').show();
		return false;
	})
	
	$('#overlay, #closeDialog').click(function()
	{
		$('#overlay').hide();
		$('#sendlink').hide();
		return false;
	});
	
	$('#recommend-form').submit(function()
	{
		var data = {};
		$(this).find('input,textarea').each(function()
		{
			data[$(this).attr('name')] = $(this).val();
		});
		$(this).find('.errors').html('');
		$.post('/recommend', data, function(response)
		{
			if (response['status'] == 1) {
				window.location = data.link;
			} else {
				for (var field in response.errors) {
					var items = [];
					for (var code in response.errors[field]) {
						var error = response.errors[field][code];
						items.push('<li>'+error+'</li>');
						break;
					}
					$('#recommend-form').find('[name='+field+'] .errors').html(items.join(''));
					$('#recommend-form').find('[name='+field+']').siblings('.errors').html(items.join(''));
				}
			}
		}, 'json');
		return false;
	});
	
	$('.required').prepend('<span style="font-size: 12px; color: #000;">*</span>');
});
