(function($){
    $.fn.extend({         
		// just returns a unique id based on time
		uniqueId : function() {
			var newDate = new Date;
			return 'more_' + newDate.getTime();
		},

        // crops the chosen content areas according to the start_height property on each div
        cropContent: function(options) {
			
            // default values
            var defaults = {
				more_classes : '',
				show_text : 'more',
				hide_text : 'less'
            }
			
            var options =  $.extend(defaults, options);
 
            return this.each(function() {
				var o = options;
                
				var current_content_div = $(this);
				var more_id = $(this).uniqueId();
				
				var more_button_html = '<a href="#" id="' + more_id + '" class="see_more ' + o.more_classes + '">' + o.show_text + '</a>';
				
				var start_height = $(this).attr('start_height');
				var max_height = $(this).height();
				
				if(current_content_div.height() > start_height) {	
					$(this).after(more_button_html);
					$(this).css('height', start_height);

					$('#' + more_id).toggle(
						function() { 
							$(current_content_div).animate({'height': max_height}, {duration: 'slow'});
							$('#' + more_id).text(o.hide_text);
						},
						function(){
							$(current_content_div).animate({'height': start_height}, {duration: 'slow' });
							$('#' + more_id).text(o.show_text);
						}
					);
				}
				
			});
		}
		
	});
})(jQuery);

function hideFlashMessage() {
	$('.flash-message').fadeOut(2600, "linear");
}

$(document).ready(function() { 
	// Tooltips on fan blocks
	if($('.fan_block img').length > 0) {
		$('.fan_block img').tipsy({gravity:'s'});
	}
	
	// Subheader Dropdown Menu
	
	 $("ul.dropdown li").hover(function(){

        $(this).addClass("hover");
        $('ul:first',this).css('visibility', 'visible');

    }, function(){

        $(this).removeClass("hover");
        $('ul:first',this).css('visibility', 'hidden');

    });

    $("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");
	
	// Header Search and Login dropdowns
	
	$('#sub_header_content ul.dropdown li.dropdown-search').click(function() {
	  if ($('#sub_header_search').is(':visible')) {
	    $('#sub_header_search').hide();
	  } else {
	$('#sub_header_search').slideDown();
	$('#sub_header_search input.search').focus();
	  }
	});
	
	$('#top_login').click(function(){
		$('#login_box').slideToggle();
		$('#login_box #Email').focus();
	}); 
	
	// Set cropped content containers
	$('.cropped_content').cropContent();
	//$('.comments_container').cropContent({more_classes: 'show_comments_section', show_text:'show', hide_text:'hide'});
	
	// Menu effects
	
	// Arrow menu
	/* - not working as expected, so it's been simplified a little bit.
	$('ul.red_right_arrow_menu li a').hover(
		function() {
			$(this).prepend('<span class="tail"></span>');
			$(this).append('<span class="hover"></span>');
		}, function() {
			$('.hover').remove();
			$('.tail').remove();
		}
	);
	*/
	
	$('ul.red_right_arrow_menu li').click(function() {
		$('ul.red_right_arrow_menu li').removeClass('current');
		$(this).addClass('current');
		$('.selected_tail').remove();
		$('.selected').remove();
		
		$('a', this).prepend('<span class="selected_tail"></span>');
		$('a', this).append('<span class="selected"></span>');
	});
	
	$('ul.red_right_arrow_menu li.current').hover(function() {$('.hover').remove();$('.tail').remove();}, function() {$('.hover').remove();$('.tail').remove();});
	
	// Profile menu
	$('.profile_main_menu ul li a').click(function() {
		var section = this.hash.substr(1);
		
		$('.profile_content div').hide();
		$('.profile_' + section).show();
	});
	
	// Artist profile menu
	$('.artist_profile_main_menu ul li a').click(function() {
		var section = this.hash.substr(1);
		
		$('.artist_profile_content div').hide();
		$('.artist_profile_' + section).show();
	});
	
	// Drop down menus
	$('#section_menu li').hover(
		function () {
			//show its submenu
			$('ul', this).slideDown(100);
		}, 
		function () {
			//hide its submenu
			$('ul', this).slideUp(100);         
		}
	);
	
	// Follow Us Widget
	$('#close_follow_us').click(function() { 
		$('#follow_us_menu').removeClass('active');
		$('#follow_us').hide();
	});
	
	$('#follow_us_menu').toggle(
		function() {
			$('#follow_us_menu').addClass('active');
			$('#follow_us').show();
			var position = $('#follow_us_menu').offset().left - 1;
			$('#follow_us').css('left', position + 'px');
		},
		function() {
			$('#follow_us_menu').removeClass('active');
			$('#follow_us').hide();
		}
	);
		
	// Flash Messages
	$('#flash_messages').click(function() {
		hideFlashMessage();
	});
	
	setTimeout('hideFlashMessage()', 4000);
	
});
