// HIL Theme v9.1
// ------------------------------------------------------------------

$(document).ready(function() {
	
	// ------------------------------------------------------------------
	// Handle scrolling to the top of the page when we arrive
	window.scrollTo(0,0);
	//console.log('scroll to top');
});
	

// ------------------------------------------------------------------
// Handle the performance list toggling

// Close with an anchor click
$('.perf a.close').live('click', function(e) {
	e.preventDefault();
	var perf = $(this).parents('.perf');
	togglePerformance('',$(this).parents('.perf'));
});

// Open only when moused over a div.perf that's minimized
$('.perf').live('mouseover', function() {
	if ( $(this).find('.min-view').is(':visible') && !$(this).hasClass('keep-open') ) {
		$(this).click(function(){
			togglePerformance('max',$(this));
			$(this).unbind('click');
		});	
	}
});

// Unbind the div.perf click event when not moused over
$('.perf').live('mouseout', function() {
	$(this).unbind('click');
});

// ------------------------------------------------------------------
// Prevent the click of an unopened performance title from doing anything.
$('.perf .min-view .default-title a').live('click',function(e){
	e.preventDefault();
});

// ------------------------------------------------------------------
// The function that does the opening and closing
function togglePerformance(maxMin,obj){
	if ( maxMin == 'max') {
		obj.find('.max-view').slideToggle(500);
		obj.find('.min-view').hide();
		obj.css('cursor','default');
	} else {
		obj.find('.max-view').slideToggle();
		obj.find('.min-view').slideToggle(500);
		obj.css('cursor','pointer');
	}
}

// ------------------------------------------------------------------
// Performance list item interactions
$('.perf a.more-info').live('click', function(e){
	e.preventDefault();
	var moreInfo = $(this);
	moreInfo.parent().find('.event-description').toggle('slow',function(){
		( $(this).is(':visible') ) ? moreInfo.html('X more info') : moreInfo.html('more info &gt;&gt;');
	});
})

// ------------------------------------------------------------------
// Add a pop-up for items that need authentication when clicked
// $('#featured #filters').delegate('.cal-cover,.logged-out-thumb-cover,.loggedout-cover-image,.add-to-cal', 'click', function(){
// 	if($(this).next().hasClass('popup')){
// 		$(this).next().slideUp(function(){
// 			$(this).remove();
// 		});
// 	} else {
// 		var popup = $('<div>').addClass('popup').hide().html("You need a Hear It Local to use this feature. <a href='/#!/as_account-manager'>Click here to create one now.</a>");
// 		$(this).after(popup.slideDown(300, function(){
// 			popup.show(100);
// 		}));
// 	}
// });
// 
// $('.add-to-cal').live('click',function(){
// 	var parent = $(this).parent();
// 	var popup = $('<div>').addClass('popup').hide().html("You need a Hear It Local to use this feature. <a href='/#!/as_account-manager'>Click here to create one now.</a>");
// 	$(this).after(popup.slideDown(300, function(){
// 		popup.show(100);
// 	}));	
// });

// ------------------------------------------------------------------
// Open and close an artist flyout within a performance
$('.perf a.flyout-open').live('click', function(e){
	e.preventDefault();
	$(this).parents('.featured-player').find('.performance-flyout').slideToggle(500);
});

$('.perf a.flyout-close').live('click', function(e){
	e.preventDefault();
	$(this).parents('.performance-flyout').slideToggle(500);
});


// ------------------------------------------------------------------
// Warn people who are not logged in.
$('.share-this img, .share-this a.sign-up, #share_this img, #share_this a.sign-up').live('click', function(e){
	e.preventDefault();
	
	// Clicking a 'sign up' link will take the user to the sign-up/login page
	if ( $(this).hasClass('sign-up') ) location.href = $(this).attr('href');
	
	// Simplify the 'share this' container
	var obj = $(this).parents('.share-this');
	
	// If we're in Omniplayer, share_this is an ID, not a class
	if ( obj.length == 0 ) obj = $(this).parents('#share_this');
	
	// Handle logged out behavior
	if ( !isMemberLoggedIn() ) {
		obj.prepend('<div class="logged-out-message shadow_2">This functionality requires a Hear It Local account. <a href="/#!/as_member" class="sign-up">Click here</a> to log in or create your free account.</div>');
		obj.find('.logged-out-message').css({
			'padding' : '5px',
			'border' : '1px solid #ccc',
			'position' : 'absolute',
			'z-index' : 100,
			'font-size' : '.7em',
			'width' : '150px',
			'top' : '-45px',
			'right' : '0px',
			'background-color' : '#fff'
		});
		
		setTimeout(function(){
			obj.find('.logged-out-message').fadeOut(function(){
				$(this).remove();
			});
		},3000);
	}
});


// ------------------------------------------------------------------
// Close the logged out messaging pop-up on click
$('.logged-out-message').live('click',function(){
	$(this).remove();
});

// ------------------------------------------------------------------
// Grab the logged-in/logged-out image. If it's 1px X 1px, we're logged in.
function isMemberLoggedIn() {
	//console.log('width=',$('#memberLoginImage').width());
	($('#memberLoginImage').width() > 1) ? $('div.share-this').addClass('logged-out') : $('div.share-this').removeClass('logged-out');
	return ($('#memberLoginImage').width() <= 1) ? true : false;
}


$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook',
		deeplinking: false
	});
	$("a.video-trigger[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook',
		deeplinking: false,
		callback: function(){ omniPlay();}
	});
//_________ /pretty photo _________\
