$(document).ready(function(){
	$('.div_comments_container').hide();
	$('#ul_releases').hide();
	$('#ul_connect').hide();
	openComments();
	externalLinks();
	toggleMenu();
	$(".commentBody").emptyInput();
	like($('.like'));
	commentSubmit($('.comment_submit'));
	// loginSubmit($('.ajax_login'));	
	buttonHover('#ul_updates_filter','88');
	buttonHover('#ul_gigs_filter','67');
	buttonHover('#ul_releases_filter','91');
	buttonHover('#ul_photos_filter','82');
	// buttonHover('#ul_videos_filter','79');
	// $('#ul_updates_filter').css('opacity','0.5');
	// $('#ul_gigs_filter').css('opacity','0.5');
	// $('#ul_releases_filter').css('opacity','0.5');
	// $('#ul_photos_filter').css('opacity','0.5');
	// $('#ul_videos_filter').css('opacity','0.5');
	$('#div_login').hide();
	toggleLogin();	
	
	remixHighlight();
	
	// $('#pagi').infinitescroll({
	//     navSelector  : "div#navholder",
	//     // selector for the paged navigation (it will be hidden)
	//     nextSelector : "a#nextpage",
	//     // selector for the NEXT link (to page 2)
	//     itemSelector : "div.div_update"
	// 	// selector for all items you'll retrieve
	//   	});

	$('.comment_notlogged').hover( 
		function() {
		// 	Alert. You need to be logged in to comment.
		$(this).parent().parent().parent().children('p.nm').append('<span class="login_message"> - login to like &amp; comment.</a>');
		},
		function() {
		// 	Alert. You need to be logged in to comment.
		$('.login_message').remove();
		}
	);

});

// Fade the opacity to fifty on hover.
function buttonHover(button,width){	
	$(button).css('width','' + width + 'px');
	$(button).hover(
		function(){
			$(this).css('width', '405px');
		},
		function(){
			$(this).css('width','' + width + 'px');
		});
}

$.fn.emptyInput = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};

function externalLinks(){
	$('a[rel="external"]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
}

function toggleLogin(){
	$('#a_login').click(function(){		
		$('#div_login').slideToggle(100);
		return false;
	});
}


function toggleMenu(){
	$('.toggleMenu').click(function(){		
		$(this).parent().children('ul').slideToggle(100);
		return false;
	});
}

function openComments(){
	$('span.comment a').click(function(){
		// alert('toggle');
		$(this).parent().parent().parent().children('.div_comments_container').slideToggle(100);
		return false;
	});
}


function like(item){
	$(item).click(function(){
		$(this).parent().children('.like_stuff').load(this.href, function(){
			flasher($(this).parent().children('.like_stuff').children('.like_message'));
		});		
		return false;
	});
}

function flasher(item){
	$(item).fadeIn('slow').fadeOut('slow').fadeIn('slow').animate({opacity: 1.0}, 800, function(){
		$(item).fadeOut('slow');
	});
}

function commentSubmit(btn){
	$(btn).click(function(){		
		var comment = $(this).parent().children("textarea").val(); 
		var contentID = $(this).parent().children("input.contentID").val();
		var contentTypeID = $(this).parent().children("input.contentTypeID").val();
		var subID = $(this).parent().children("input.subID").val();    
		var dataString = 'body='+ comment + '&commentContentID=' + contentID + '&commentTypeID=' + contentTypeID + '&subID=' + subID;
			if (!comment) {
				alert("You need to add a comment before submitting");
				return false;
			} else {
				$.ajax({
				  type: "POST",
				  url: "/comment_process.php",
				  data: dataString,
				  success: function(html){
						$('#div_' + contentID).replaceWith(html);
				  }
				});
			}
	return false;
	});
}

// function loginSubmit(btn){
// 	$(btn).click(function(){		
// 		var subUsername = $("input#subUsername").val(); 
// 		var subPassword = $("input#subPassword").val(); 
// 		var dataString = 'subUsername='+ subUsername + '&subPassword=' + subPassword;
// 		$.ajax({
// 		  type: "POST",
// 		  url: "/ajax_login.php",
// 		  data: dataString,
// 		  success: function(html){
// 			$('#login_wrap').replaceWith(html);
// 			loginSubmit($('.ajax_login'));
// 		  }
// 		});
// 	return false;
// 	});
// }


function UnCryptMailto( s )
    {
        var n = 0;
        var r = "";
        for( var i = 0; i < s.length; i++)
        {
            n = s.charCodeAt( i );
            if( n >= 8364 )
            {
                n = 128;
            }
            r += String.fromCharCode( n - 1 );
        }
        return r;
    }

function linkTo_UnCryptMailto( s )
    {
        location.href=UnCryptMailto( s );
    }


function remixHighlight(){
	if (location.href.indexOf("#") != -1) {
		if ($('#remix_track').size() > 0) {
			var remix = window.location.hash;
			$(remix).css('background','#ebd9e5');
			$(remix).css('border','1px solid #A44083');
		};
    } 
}