$(document).ready(function(){

var is_playing=0;

// Make Stuff Pretty





$(".song").corner("round");
$(".login").corner("bl cc:#000000");
$(".aroundComment").corner("round");
$(".note").corner("round");

$(".event").corner("bl tl");
$(".buyOnAmazon").corner("tl tr ");
$("#intro").corner("bl br ");
// $(".acomment").corner("round");

if (!($.cookie("sessionid"))) //use request.session.set_test_cookie() instead, and then don't delete it.
$('#flashy')
    // .fadeIn()
    .animate({opacity: '+=0'}, 3000)   // Does nothing for 2000ms
    .animate( {width: "+90%"})
    .animate( {fontSize: "+=3px"}, 1000)
	// .animate( {color: 'black'})
	.animate( {backgroundColor: 'black'});



//   Player expander
$("#player-o").hoverIntent(
     function () {
		makeplayerbig()
     }, 
     function () {
		makeplayersmall()
		
     }
);

var makeplayerbig =function()
{
// alert(this);
	$("#player-o").animate({height: "40px"}, 250);
	$("#control-wrap").animate({fontSize: "1.5em", paddingTop:"4px"}, 250);
	$(".player-button").animate({paddingBottom: "10px"}, 250);
	$(".playerOnHover").fadeIn();
}

var makeplayersmall =function()
{
// alert(this);
	$("#player-o").animate({height: "20px"}, 250);
	$("#control-wrap").animate({fontSize: "1em", paddingTop:"0px"}, 250);
		$(".player-button").animate({paddingBottom: "4px"}, 250);
	$(".playerOnHover").fadeOut();
}




// Show and hide inPostEvent-all .
$(".showAllCities").click(function(ev)
{
	
	$(this).parent().find(".inPostEvent-all").slideToggle();

	return false;
})

// Loads rest of post if not a review
$(".loadAfterJumpLink").click(function(ev)
{
	$(this).parent().find(".hiddenAfterJumpText").slideToggle();
	$(this).text("...");
	return false;
})


//  Genneralerized Hide Behavior (Started with the intro, but it's a good idea)

$(".hideButton").click(function(ev)
{
	
	$(this).parent().slideUp();
	return false;
})

// Intro Play button


$("#introPlayButton").click(function(ev)
{
	$(this).parent().slideUp();
	makeplayerbig();
	setTimeout( function()
	      {
	       makeplayersmall();
	      }, 2000);
	return false;
})




//  Ajax for Adding shows to your list IM GOING

$(".imGoing").click(function(ev)
{
	if ($(this).hasClass("isGoing"))
		return false;
	if ($(".login").hasClass("loggedin"))
		{	
			var target = $(this).attr("href");
			$.post(target, function(ev){});
			$(this).text("ADDED");	
			$(this).animate({ backgroundColor: "black" }, 1000).addClass("isGoing");
			var numberAttending = 0; 
			numberAttending= $(this).prev().find(".numberAttending").text();
			numberAttending ++;
			if (numberAttending==1)
				{
					$(this).prev().find(".replace-me").text(numberAttending + ' PERSON ');
					return false;
				}
			$(this).prev().find(".numberAttending").text(numberAttending);
			return false;
		}
	else
		return True;
})


//post comment swapping between general and event

// var foundeventcomments = $(".eventSpecificComments").size();
if ($(".eventSpecificComments").size())
	$(".generalPostComments").hide();


$(".showGeneralPostComments").click(function(){
	$(".eventSpecificComments").hide('normal');
	$(".generalPostComments").show('normal');
	return false;
})

$(".showEventSpecificComments").click(function(){
	$(".generalPostComments").hide('normal');
	$(".eventSpecificComments").show('normal');
	return false;
	
})



//http://static.zazerr.webfactional.com/eml_site_media


// Highlights navs, sort of a hack
a = $("#where-we-are").text();
$("." + a).addClass("bactive");


var toggleCities =function (){
		$("#cityList").slideToggle(200);
		$("#eventTitle").toggleClass("eventTitleActive");
			// .find("#flashy").animate( {backgroundColor: 'black'}, 100); 
}
$("#cityList").hide();
// setTimeout(toggleCities, 2000);


$("#eventTitle").click(toggleCities);

// in post song behavior

$(".song").hover(
     function () {
       if($(this).hasClass("active-song")!=1)
			{$(this).find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/blackplay.gif");}
     }, 
     function () {
		if($(this).hasClass("active-song")!=1)
       		{$(this).find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/aniplay.gif");}
     }
);




//
//
//  PLAYER CONTROLS

//  orlastsong=1 is for next song, -1 is for previous song on page
var change_song = function(orlastsong)
{
	index= $(".song").index($(".active-song"));
	next= index+ orlastsong;
	// alert($(".song")[next])
	$(".active-song").removeClass("active-song").find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/aniplay.gif");
	
	$(".song").eq(next).addClass("active-song").find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/pointer.gif");
	
	var song_url= ($(".active-song")).attr("href");
	var song_text = ($(".active-song")).text();
	
	$("#player-text").text(song_text);
	$("#bottomPlay").load(song_url);
	// alert(song_text);
	is_playing =1;
	
	return 1;
}

// initialize the player

// $("#bottomPlay").player("http://127.0.0.1:8000/site_media/player/music.swf",
$("#bottomPlay").player("http://static.zazerr.webfactional.com/eml_site_media/player/music.swf",
	{next_song_callback: function(){change_song(1)}}
);

// in post song click handeling
$(".song").click(function(ev){
	
	// this song is already playing, so pause it
	if ($(this).hasClass("active-song") && (is_playing==1))
		{
			$("#bottomPlay").toggle("pause");
			$("#play-pause").text("PLAY");
			is_playing=2;
			$(this).find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/blackplay.gif");
			return false;
		}
	
	if ($(this).hasClass("active-song") && (is_playing==2))
		{
			$("#bottomPlay").toggle("play");
			$("#play-pause").text("PAUSE");
			is_playing=1;
			$(this).find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/pointer.gif");
			
			return false;
		}
		
	// not already playing, or another song is playing
	song_url= $(this).attr("href");
	song_text = $(this).text();
	$(".active-song").removeClass("active-song").find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/aniplay.gif");
	
	$(this).addClass('active-song').find(".songIndicator").attr("src", "http://static.zazerr.webfactional.com/eml_site_media/site_images/pointer.gif");
	
	$("#play-pause").text("PAUSE");
	
	$("#bottomPlay").load(song_url);
	
	//alert('about to load');
	
	$("#player-text").text(song_text);
	
	makeplayerbig();
	setTimeout( function()
	      {
	       makeplayersmall();
	      }, 2000);
	
	
	is_playing =1;
	return false;
})

// controls
// $("#play-pause").click(function(ev){
$(".playButtons").click(function(ev){
	volume = $("#bottomPlay").volume(); //wait why is this here? Delete and test.
	if (is_playing)
	{
		if (is_playing==1) 
		{
			$("#bottomPlay").toggle("pause");
			$(this).text("PLAY");
			is_playing =2;
			// alert('lets play')
		}
		else if (is_playing ==2) //2 is paused
		{
			$("#bottomPlay").toggle("play");
			$(this).text("PAUSE");
			is_playing = 1;
			// alert('lets pause')
			
		}


	}
	else
	{
		change_song(1);
			$("#bottomPlay").toggle("play");
			$(this).text("PAUSE");
			is_playing = 1;
			// alert('lets pause')
			

		
		// first_song_url = $(".song").eq(0).attr("href");
		// 		$("#bottomPlay").load(first_song_url);
	}
});

$("#next").click(function(ev){
	change_song(1);	
});

$("#last").click(function(ev){
	change_song(-1);	
});


$("#plusMin").click(function(ev){
	var time = $("#bottomPlay").time();
	$("#bottomPlay").time(time+60);
});

$("#goTo").click(function(ev){
	$.scrollTo(".active-song", 300);
});



// More Facebook connect
$(".fb_login_not_logged_in").click(function(){
	$("#recent #fb-connect-privacy-note").show();
});


// PHOTOS

$("img").lazyload({ threshold : 400, placeholder : "http://static.zazerr.webfactional.com/eml_site_media/images/lightbox-blank.gif" });


$(".photoNext").fadeTo('fast', .4);
$(".photoBox").hover(function(){
	$(".photoNext").fadeTo('fast', 1).animate( {color: 'orange'}, 100);
},
function(){
	$(".photoNext").fadeTo('medium', .4).animate( {color: 'white'}, 100);
})




// The problem is the order. It's one behind, because it's getting the next_photo_el when you click, it doesn't have it already.
// Problem 2 is that you don't use load() dope -- NO, the problem is normal load doesn't work anymore? use bind("load", somethignhere)
$(".photo, .photoNext").click(function(ev){
	
	$(this).parent().find(".photoNext").html("<img src='http://static.zazerr.webfactional.com/eml_site_media/site_images/orangeLoad.gif'>" )
	next_photo_el = $(this).parent().contents().filter(".activePhotoLink").next().next();
	
	// alert('a');
	if (!($(next_photo_el).hasClass("thumbLink")))
		{
					next_photo_el = $(this).parent().contents().filter(".thumbLink").eq(0);
					
		}

	full_photo_url= $(next_photo_el).next().attr("href");
	$(this).parent().find(".seePhotosBigger").attr("href", full_photo_url);
	next_photo_url= $(next_photo_el).attr("href");

	$(this).parent().contents().filter(".activePhotoLink").removeClass("activePhotoLink");
	$(next_photo_el).addClass("activePhotoLink");
	
	

	// $(this).parent().find(".photo").attr("src", next_photo_url).load(function(){
	$(this).parent().find(".photo").attr("src", next_photo_url).bind("load", function(){

			$(this).parent().find(".photoNext").html("&rarr;" );
			// alert("loaded");
			// $(".photo").hide();
			// console.log("loaded");
			
		});
		
		
});





$('a.fullPhotoLink').lightBox({
		fixedNavigation:true,
		imageLoading: 'http://static.zazerr.webfactional.com/eml_site_media/images/lightbox-btn-loading.gif',
		imageBtnClose: 'http://static.zazerr.webfactional.com/eml_site_media/images/lightbox-btn-close.gif',
		imageBtnPrev: 'http://static.zazerr.webfactional.com/eml_site_media/images/lightbox-btn-prev.gif',
		imageBtnNext: 'http://static.zazerr.webfactional.com/eml_site_media/images/lightbox-btn-next.gif',
		});


	}); // end docready





// FACEBOOK CONNECT
	function update_user_box() { 
		var user_box = document.getElementById("user"); // add in some XFBML. note that we set useyou=false so it doesn't display "you" 
		user_box.innerHTML = "<span>" + "<fb:profile-pic uid='loggedinuser' facebook-logo='false'></fb:profile-pic>" + "Hi, <fb:name uid='loggedinuser' useyou='false'></fb:name>. (Signed in with Facebook.)" + "</span>"; // because this is XFBML, we need to tell Facebook to re-process the document 
		FB.XFBML.Host.parseDomTree(); 
		$("#fb_login_image").hide();
		alert('hi');
		
		}