$(document).ready(function() {
	$('#date-drop').change(function(){
		show_events_for_date();
	});
	show_events_for_date();

	// $('#date-drop').change(function(){
	// 	show_events_list_for_date();
	// });
	// show_events_list_for_date();
	// 
	
	var event_type = $('#event-display').val();
	
	if(event_type == 'event-schedule'){
		$('#tag-drop').change(function(){
			highlight_by_tag();
		});
		highlight_by_tag();
	}else if(event_type == 'event-schedule-list'){
		$('#tag-drop').change(function(){
			highlight_list_by_tag();
		});
		highlight_list_by_tag();
	}

	$('.event').hover(function() {
		get_event_detail($(this));
	},
	function() { 
		hide_event_detail($(this));
	});

});

function show_events_for_date(){
	var dateID = $('#date-drop').val();
	
	//special case for Thursday 
	if(dateID == '20110818') {
		currentRow = $('.hour-row').first();
		currentRow.hide();
		for(var i=0; i < 14; i++) {
			currentRow = currentRow.next();
			currentRow.hide();
		}
		$('.events').css({top: '-466px'});
	} else {
		currentRow = $('.hour-row').first();
		currentRow.show();
		for(var i=0; i < 14; i++) {
			currentRow = currentRow.next();
			currentRow.show();
		}
		$('.events').css({top: '0'});
	}
	
	$('.event').each(function(){
		$(this).hide();
	});
	$('.event.venue_'+dateID).each(function(){
		$(this).show();
	});
}
// 
// function show_events_list_for_date(){
// 	var dateID = $('#date-drop').val();
// 		
// 	$('.event_list').each(function(){
// 		$(this).hide();
// 	});
// 	$('.event_list.venue_'+dateID).each(function(){
// 		$(this).show();
// 	});
// }

function highlight_list_by_tag(){
	// alert('got here');
	var tagName = 'tags_'+$('#tag-drop').val().replace(/\s/,'');
	if(tagName != 'tags_') {
		$('.event_list').each(function(){
			$(this).fadeOut();
		});
		$('.'+tagName).each(function() {
			$(this).fadeIn(100).removeClass('highlight').addClass('inactive');
		});
	} else {
		$('.event_list').each(function(){
			$(this).show();
		});
	}
}

function highlight_by_tag(){
	var tagName = 'tags_'+$('#tag-drop').val().replace(/\s/,'');
	if(tagName != 'tags_') {
		$('.event').each(function(){
			$(this).removeClass('highlight').addClass('inactive');
		});
		$('.'+tagName).each(function() {
			// alert('yo');
			$(this).removeClass('inactive').addClass('highlight');
		});
	} else {
		$('.event').each(function(){
			$(this).removeClass('inactive highlight');
		});
	}
}

var hoveron = false;

function get_event_detail(eventCont) {
	if(hoveron == false){
		
		setTimeout(function(){
			
			if(hoveron == false){
			
				hoveron = true;
			
				eventID = eventCont.attr('id');
				coffset = eventCont.offset();
				if(coffset.left > $(window).width()/2) {
					leftOffset = coffset.left-$('#event_detail').outerWidth();
					arrowStyles = 'style="right: -38px; background-image: url(\'images/right-arrow.png\');';
				} else {
					leftOffset = coffset.left+eventCont.outerWidth();
					arrowStyles = 'style="left: -25px;';
				}
			// alert(eventID);
				var eventHTML = $('#eventFull_'+eventID.replace('e','')).html();
				// alert(eventHTML);
				$('#event_detail').html(eventHTML);
				$('#event_detail').fadeIn().css({display: 'block', left: leftOffset+'px', top: (coffset.top+eventCont.height()/2-$('#event_detail').height()/2)+'px'});
				arrowStyles += ' top: '+($('#event_detail').height()/2-13)+'px;"';
				$('#event_detail').html(eventHTML+'<div class="arrow" '+arrowStyles+'></div>');

			}

		},400);

	}
	
	// $.ajax({
	//  url: "detail.php",
	//  type: 'POST',
	//  dataType: 'json',
	//  data: 'eventID='+eventID.replace(/[^\d]/, ''),
	//  success: function(t){
	// 	var eventLocal = jsArray[eventID.replace(/[^\d]/, '')*1];
	// 	var eventHTML = '';
	// 	if(eventLocal.flyer_mid){
	// 		eventHTML += '<img src="'+eventLocal.flyer_mid+'" alt="Event Image" class="flyer" style="float:right" />';
	// 	}
	// 	eventHTML += '<span class="event-title">'+t.name+'</span><br />\
	// 	<span class="start-date">'+t.start_pretty+'</span>\
	// 	<span class="end-date"> - '+t.end_pretty+'</span>\
	// 	<div class="event-description">'+t.description+'</div>\
	// 	<span class="venue-name">'+t.venue_name+'</span>';
	// 	
	// 	$('#event_detail').html(eventHTML);
	// 	$('#event_detail').css({display: 'block', left: leftOffset+'px', top: (coffset.top+eventCont.height()/2-$('#event_detail').height()/2)+'px'});
	// 	arrowStyles += ' top: '+($('#event_detail').height()/2-13)+'px;"';
	// 	$('#event_detail').html(eventHTML+'<div class="arrow" '+arrowStyles+'></div>');
	// 	
	//   }
	// });	
}
function hide_event_detail(eventCont) {
	$('#event_detail').fadeOut(100);
	hoveron = false;
}










// HTML Truncator for jQuery
// by Henrik Nyh <http://henrik.nyh.se> 2008-02-28.
// Free to modify and redistribute with credit.

(function($) {

  var trailing_whitespace = true;

  $.fn.truncate = function(options) {

    var opts = $.extend({}, $.fn.truncate.defaults, options);
    
    $(this).each(function() {

      var content_length = $.trim(squeeze($(this).text())).length;
      if (content_length <= opts.max_length)
        return;  // bail early if not overlong

      var actual_max_length = opts.max_length - opts.more.length - 3;  // 3 for " ()"
      var truncated_node = recursivelyTruncate(this, actual_max_length);
      var full_node = $(this).hide();

      truncated_node.insertAfter(full_node);
      
      findNodeForMore(truncated_node).append(' (<a href="#show more content">'+opts.more+'</a>)');
      findNodeForLess(full_node).append(' (<a href="#show less content">'+opts.less+'</a>)');
      
      truncated_node.find('a:last').click(function() {
        truncated_node.hide(); full_node.show(); return false;
      });
      full_node.find('a:last').click(function() {
        truncated_node.show(); full_node.hide(); return false;
      });

    });
  }

  // Note that the " (â€¦more)" bit counts towards the max length â€“ so a max
  // length of 10 would truncate "1234567890" to "12 (â€¦more)".
  $.fn.truncate.defaults = {
    max_length: 100,
    more: 'more &rarr;',
    less: '&larr; less'
  };

  function recursivelyTruncate(node, max_length) {
    return (node.nodeType == 3) ? truncateText(node, max_length) : truncateNode(node, max_length);
  }

  function truncateNode(node, max_length) {
    var node = $(node);
    var new_node = node.clone().empty();
    var truncatedChild;
    node.contents().each(function() {
      var remaining_length = max_length - new_node.text().length;
      if (remaining_length == 0) return;  // breaks the loop
      truncatedChild = recursivelyTruncate(this, remaining_length);
      if (truncatedChild) new_node.append(truncatedChild);
    });
    return new_node;
  }

  function truncateText(node, max_length) {
    var text = squeeze(node.data);
    if (trailing_whitespace)  // remove initial whitespace if last text
      text = text.replace(/^ /, '');  // node had trailing whitespace.
    trailing_whitespace = !!text.match(/ $/);
    var text = text.slice(0, max_length);
    // Ensure HTML entities are encoded
    // http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb
    text = $('<div/>').text(text).html();
    return text;
  }

  // Collapses a sequence of whitespace into a single space.
  function squeeze(string) {
    return string.replace(/\s+/g, ' ');
  }
  
  // Finds the last, innermost block-level element
  function findNodeForMore(node) {
    var $node = $(node);
    var last_child = $node.children(":last");
    if (!last_child) return node;
    var display = last_child.css('display');
    if (!display || display=='inline') return $node;
    return findNodeForMore(last_child);
  };

  // Finds the last child if it's a p; otherwise the parent
  function findNodeForLess(node) {
    var $node = $(node);
    var last_child = $node.children(":last");
    if (last_child && last_child.is('p')) return last_child;
    return node;
  };

})(jQuery);
