//XOS=top.XOS||{};(function(_){return(!XOS.debug)?_:XOS.debug.call(window,_)})


TQ.ui.FeaturedVenues = function() {
	this.urls = '#featured_venues_nav li a';
	this.timer = {};
	this.interval = 5000;
	this.current = 0;
	this.initEvents();
	
}

TQ.ui.FeaturedVenues.prototype.initEvents = function() {
	var that = this;
	$('#featured_venues_nav li a').bind('click', {inst: that}, that.onClick);
	//$('#featured_venues_list').css({height:$("#featured_venues_list").outerHeight() });
	if($(that.urls).length > 1)
	this.timer = setInterval(function(){
  	that.doSwitch.apply(that)
  }, 5000);
}



TQ.ui.FeaturedVenues.prototype.onClick = function(e){
	var that = e.data.inst;
	e.preventDefault();
	$('#featured_venues_nav li a').removeClass('active');
	$(e.target).addClass('active');
	that.reset();
	that.current = $(that.urls).index($(e.target));
	that.doSwitch(e.target.href);
}

TQ.ui.FeaturedVenues.prototype.getNext = function(){
	if (this.current++ < $(this.urls).length - 1) {
  	return this.current;
  }
  else {
  	this.current = 0;
  	return this.current;
  }
}

TQ.ui.FeaturedVenues.prototype.reset = function() {
	clearInterval(this.timer);
	var that = this;
	this.timer = setInterval(function(){
  	that.doSwitch.apply(that)
  }, 5000);
}

TQ.ui.FeaturedVenues.prototype.doSwitch = function(url) {
	
	
	var url = url || $(this.urls)[this.getNext()].href;
	
	if(arguments.length == 0){
		$(this.urls).removeClass('active');
		$($(this.urls)[this.current]).addClass('active');
	}
	
	$("#featured_venues_list li").css({position: 'absolute'});
	var featured_venue_template = new TQ.ui.JsonLoader();
	$.get(url, function(req){
		var obj = $.secureEvalJSON(req).venue;
		if (obj.assets.length > 0 && obj.assets[0].image) {
			obj.asset_id = obj.assets[0].image.id;
			obj.attachment_file_name = obj.assets[0].image.attachment_file_name;
		}
		obj.description = obj.description.split(" ", 20).join(" ") + '...';
		$('#featured_venues_list').append($('<li>' + featured_venue_template.eval(obj) + '</li>').css({position: 'absolute', opacity: 0}));
		($($('#featured_venues_list .featured_venue_photo')[0])).animate({opacity: 0});
		($($('#featured_venues_list li .pad_fix_left_right p')[0])).animate({opacity: 0});
		($($('#featured_venues_list li')[1])).animate({opacity: 1}, function() {
			$($('#featured_venues_list li')[0]).remove();
		});
		//$('#featured_venues_list').css({height:$("#featured_venues_list li").outerHeight() })
		
	});
} 

TQ.ui.EventsNearYour = function() {
	var events_near_you = '<li>';
		events_near_you += '<div class="photo">';
		events_near_you += '<a href="/events/#{id}"><span class="overlay"></span><img src="#{photo_path}" /></a>';
		events_near_you += '</div>';
		events_near_you += '<div class="details">';
		events_near_you += '<p class="no_margin"></p>';
		events_near_you += '<p class="dashed_bottom">#{description} <a href="/events/#{id}">#{title}</a></p>';
		events_near_you += '<p>Posted by <a href="/users/#{user_id}">#{user_name}</a></p>';
		events_near_you += '</div>';
		events_near_you += '</li>';
	this.template = events_near_you;
	this.initEvents();
}

TQ.ui.EventsNearYour.prototype.initEvents = function() {
	var that = this;
	$('#sort_box li a').bind('click', {inst: that}, that.onClick);
}

TQ.ui.EventsNearYour.prototype.onClick = function(e){
	var that = e.data.inst;
	$('#sort_box li a').removeClass("active");
	$(e.target).addClass("active");
	$.template('events_near_you', that.template);
	e.preventDefault();
	var list = '';
	var date = $(e.target).attr('title');
	
	$.get(e.target.href + '&response=json', function(req){
		var obj = $.secureEvalJSON(req);
		var counter = 0;
		for (var item in obj) {
			if (counter < 3) {
				//alert(obj[item])
		obj[item].event.photo_path = obj[item].event.event_photo_id ? "http://s3.amazonaws.com/thequeerist/attachments/" + obj[item].event.event_photo_id + "/bw_medium/" + obj[item].event.event_photo_file_name : "/images/medium/noeventpic.jpg";
	  	list += $.template('events_near_you', (obj[item].event));
	  }
			counter ++;
		}
		if(list == '')
			list = '<p>No events near you ' + date + '.</p>'
		$('.events_near_you_list').html(list);
		//console.log($.template('events_near_you', obj));
		
	});
}

$((function(){
  	$.getScript('/javascripts/jquery.json-1.2.js', function(){
  		new TQ.ui.FeaturedVenues();
		new TQ.ui.EventsNearYour();
  	});
  }).delay(2000));
