$(document).ready(function(){
    $('a[rel="widget"]').click( function() {
        var widget = window.open( $(this).attr('href'), 'Do512Widget', 'width=730,height=382,scrollbars=0,resizable=0' );
        widget.focus();
        return false;
    });
    $('a[rel="share_widget"]').click( function() {
        var widget = window.open( $(this).attr('href'), 'Do512Widget', 'width=500,height=472,scrollbars=0,resizable=0' );
        widget.focus();
        return false;
    });
    $('a[rel="ms_w"]').click( function() {
        var widget = window.open( $(this).attr('href'), 'Do512Widget', 'width=630,height=545,scrollbars=0,resizable=0' );
        widget.focus();
        return false;
    });
    var current_li = $('li.tmSelected');
    var current_a = $('a.tmSelected');
    var current_sub = $('div.tmSelected');
    $('li.tm').hover(
        function() {
          current_li.removeClass('tmSelected');
          current_a.removeClass('tmSelected');
          current_sub.removeClass('tmSelected');
          $(this).addClass('xhover');
          $(this).find('a.tm').addClass('tmSelected');
        },
        function() {
          $(this).removeClass('xhover');
          $(this).find('a.tm').removeClass('tmSelected');
          current_li.addClass('tmSelected');
          current_a.addClass('tmSelected');
          current_sub.addClass('tmSelected');
        }
    );
  	$('.tmSubSub').hover(
  		function() { $(this).addClass('subHover') },
  		function() { $(this).removeClass('subHover'); }
  	);

  	$('.tmHiddenWhenCal').hover(
  		function() { $(this).parents('li').addClass('IsaidSubHover'); },
  		function() { $(this).parents('li').removeClass('IsaidSubHover');; }
  	);

  	$('.tmSubPopMid ul.2col').each(function() {
  		var li = $(this).children('li');
  		var half = Math.ceil(li.length/2);
  		li.slice(half).css('margin-left',144);
  		li.eq(half).css('margin-top',-16*half);
  		$(this).height(16*half);
  	});

  	$('.tmSubClose').click(function() {
  		$(this).parents('.tmSubSub').removeClass('subHover'); return false;
  	});
    $('#searchSubmit').hover(
        function() { $(this).addClass('xhover'); },
        function() { $(this).removeClass('xhover'); }
    );
    $('.sxsw #sevenDays').hover(
        function() { $(this).addClass('calHover'); },
        function() { $(this).removeClass('calHover'); }
    );

    $('.sxsw_events > ul > li:odd').addClass('rowOdd');
    $('.sxsw_events > ul > li:even').addClass('rowEven');
    $('.sxsw_events > ul > li').hover(
    	function() { $(this).addClass('rowHover'); },
    	function() { $(this).removeClass('rowHover').find('.sxswD').removeClass("sxswDhover") ; }
    );
    $('.sxsw_events > ul > li:last').addClass('rowLast');
  
    $('.sxi').wrapInner('<div class="sxiInner"></div>').prepend('<div class="sxiTop"></div>').append('<div class="sxiBottom"></div>');
    $(".sxswD > a:not(.sxswA)").attr('href','javascript:void(0);');
    $(".sxswD").click(function() {
        var event_id = $(this).find('div.sxi').attr('id').slice(1);
        if($(this).find('a').hasClass('sxswI')) { 
            $(this).find('div.sxiInner').load("/event/"+event_id+"/hbh_info");
        }
        if($(this).find('a').hasClass('sxswV')) {
            $(this).find('div.sxiInner').load("/event/"+event_id+"/hbh_vid");
        }
        if($(this).find('a').hasClass('sxswQ')) {
            $(this).find('div.sxiInner').load("/event/"+event_id+"/hbh_quo");
        }
    	if($(this).hasClass('sxswDhover')) {
                $(this).removeClass("sxswDhover");
    	}
    	else {
                $(this).siblings().removeClass("sxswDhover");
                $(this).addClass("sxswDhover");
    	}
    });

    var popupProfiles = {
     	myspace: { height: 570, width: 630, center: 1, createnew: 0, scrollbars:0, status:0, resizable:0 }
     };
  	
    $(".popup").popupwindow(popupProfiles); 

});

function dateStatusHandler(date) {
  if (date.getFullYear() == 2009 && date.getMonth()    == 2 && date.getDate() >= 13 && date.getDate() <= 24  ) { return false; }
  return true;
};

// 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',
    less: '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);
