$(document).ready(function(){
	$("div.myfestivaluser-stats").hide();
	$('a.show:first').addClass('selectedUser');
	$('div.myfestivaluser-stats:first').show("slow");
	$("div#calendar-container a").click(function(){
		$("div#calendar-container a").removeClass("today");
		$(this).addClass("today");
		var linkval = "/cfc/test.cfm"+$(this).attr("href");
		$('#whatsoncontent').fadeOut(500, function(){
			$(this).load(linkval, function(){
				$(this).fadeIn(500);
			});
		});
		return false;
	})
	.end();

	$('a.show_hide').click(function(){
		$('#'+$(this).attr('id')+'-'+$(this).attr('rel')).slideToggle("fast");
		return false;
	});
	
	
	$('a.show').click(function(){
	$("div.myfestivaluser-stats").hide("slow");
	$("div.user-list a").removeClass("selectedUser");
	$(this).addClass("selectedUser");
	$('#'+$(this).attr('id')+'-'+$(this).attr('rel')).show("slow");
		return false;
	});
	
	$('a.close-box').click(function(){
		$(this).parent('div.dl-box').slideToggle("fast");
		return false;
	});
	
	$('a.eventInfoToggle').click(function(){
		$(this).toggleClass('bold');
		$('#'+$(this).attr('rel')).slideToggle("medium");
		return false;
	});
	
	$('#av_container').css('display','none'); // hide av container
	$('#a_container').css('display','none'); // hide av container
	
	$('a.v_trigger').click(function() {
		var event_id = $(this).attr('rel');
		$('#av_container').css({ position: 'absolute', top: '250px', left: '350px' });
		$('#av_container').fadeIn(250);
		$('#av_embedd').empty();
		$('#av_embedd').flash(
			{
				src: 'http://media.perthweb.com.au/festival/2008/film/'+event_id+'.swf', 
				width: 320,
				height: 300
			},
			{ version: 8 }
		);
		//return false;
	});
	
	$('a.a_trigger').click(function() {
		var event_id = $(this).attr('rel');
		$('#a_container').css({ position: 'absolute', top: '250px', left: '350px' });
		$('#a_container').fadeIn(250);
		$('#a_embedd').empty();
		$('#a_embedd').flash(
			{
				src: 'http://media.perthweb.com.au/festival/2008/audio/becks/'+event_id+'.swf', 
				width: 325,
				height: 180
			},
			{ version: 8 }
		);
		//return false;
	});
	
	$('#av_close').click(function(){
		$('#av_container').fadeOut(250);
		$('#av_embedd').empty();
		return false;
	});
	
	$('#a_close').click(function(){
		$('#a_container').fadeOut(250);
		$('#a_embedd').empty();
		return false;
	});

	$('a.venuetrigger').click(function(){
		$('#venue-'+$(this).attr('rel')).slideToggle("medium");
		return false;
	});
	
	/* ---------- Artist Popup ---------- */
	
	$('#artistpopup-close').click(function(){
		$('#artistpopup').fadeOut(250);
		$('#artistpopupcontent').empty();
		return false;
	});
	
	$("a.artistpopup").click(function(){
		linkval = $(this).attr("href");
		scrolled = $(document).scrollTop();
		$('#artistpopup').addClass('artistpopup-active');
		if(scrolled>230) {
			$('#artistpopup').css({ position: 'absolute', top: (scrolled-180)+'px' });
		} else {
			$('#artistpopup').css({ position: 'absolute', top: '50px' });
		}
		$('#artistpopup').fadeOut(100, function(){
			$('#artistpopupcontent').load(linkval, function(){
				$('#artistpopup').fadeIn(300);
			});
		});
		return false;
	});
	
	/* --- Homepage My Festival stuff ---- *
	var numusers=6;
	var scrollstart=0;
	var firstuserid='u'+userlist.DATA[0][0];
	if (userlist.DATA.length < 6){
		numusers=userlist.DATA.length;
	}
	scrollin(scrollstart);		
	populateuser(firstuserid);
	
	$('#next').click(function(){
		if (scrollstart < userlist.DATA.length){
			scrollstart=scrollstart+1;
			$('#innerscroller').fadeOut(400, function () {
				scrollin(scrollstart);			
			});
			$('#innerscroller').fadeIn(400);						  

		}
	})
	
	$('#prev').click(function(){
		if (scrollstart > 0){
			scrollstart=scrollstart-1;
			$('#innerscroller').fadeOut(400, function () {
				scrollin(scrollstart);
			});
			$('#innerscroller').fadeIn(400);						  

		}
	})

	function scrollin(start){
		var output="";
		for (i=start;i<=numusers-1;i++){
			output+='<a href="##"><img src="/files/myfestival/tiny_'+userlist.DATA[i][1]+'" id="u'+userlist.DATA[i][0]+'" class="MFthumb" alt="i" /></a>&nbsp;';
		}	
		$('#innerscroller').html(output);
		
		$('.MFthumb').click(function(){
			var thing=$(this).attr('id');
			
			$('#featured').fadeOut(900, function () {
				populateuser(thing);
			});
			
			
		})
	}
	*/
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	var myfestival_position = 0;
	var usernum = userlist.DATA.length;
	var currentloaded = 0;

	function refillHTML() {
		
		if( usernum<(myfestival_position+10) ) {
			var userstoget = usernum;
		} else {
			var userstoget = (myfestival_position+10);
		}
		
		for (var i=currentloaded;i<userstoget;++i){
			$('#myfestival-users').append('<a href="##"><img src="/files/myfestival/tiny_'+userlist.DATA[i][1]+'" id="u'+userlist.DATA[i][0]+'" class="MFthumb" alt="User Photo" /></a>');
		}
		
		currentloaded = $('#myfestival-users a').length;
		
		$('#myfestival-users').width(currentloaded*30+'px');
		
		$('#id05').html(currentloaded);
		
		$('#myfestival-users .MFthumb').click(function(){
			var thing=$(this).attr('id');
			$('#featured').fadeOut(200, function () {
				populateuser(thing);
			});
			return false;
		});
		
	}
	
	$('a#myfestival-prev').click(function(){
		myfestival_position--;
		doScroll();
		return false;
	});
	
	$('a#myfestival-next').click(function(){
		myfestival_position++;
		doScroll();
		return false;
	});
	
	function doScroll() {
		checkScrollStatus();
		$('#myfestival-users').animate({left: '-'+myfestival_position*30+'px'}, 200);
	}
	
	function checkScrollStatus() {
	
		refillHTML();
	
		var innerWidth = $('#myfestival-users').width();
		var innerOffset = myfestival_position*30;
		var trueOffset = innerOffset;
		var containerWidth = $('#myfestival-scroller').width();
	
		if(innerWidth<=containerWidth) {
			$('a#myfestival-prev').hide();
			$('a#myfestival-next').hide();
		} else {
	
			if(myfestival_position<=0) {
				$('a#myfestival-prev').hide();
			} else {
				$('a#myfestival-prev').show();
			}
			
			if(innerOffset>0) {
				if( ((innerWidth-trueOffset) < (containerWidth+3)) ) {
					$('a#myfestival-next').hide();
				} else {
					$('a#myfestival-next').show();
				}
			} else {
				$('a#myfestival-next').show();
			}
			
		}
		
	}
	
	checkScrollStatus();

	function populateuser(userid){
		$('#featured').load('/_inc/customtags/homepage/myfestivalsingle.cfm?userid='+userid, {limit: 25},function () { 
			$('#featured').fadeIn(500);
		});
	}
	
	$('#myfestival-users .MFthumb:first').click();
	
});
