
var scrolltimer = null;

$(document).ready(function(){
	
	$('.artist_button a').click(function(e){
		e.preventDefault();
		var artist_id = $(this).attr('data-artist');
		open_artist(artist_id);
	})
	
	$('.artist_content_close a').click(function(e){
		e.preventDefault();
		var id = $(this).attr('data-artist');
		close_artist(id);
	})
	
	$('.top_section_close a').click(function(e){
		e.preventDefault();
		close_top();
	})
	
	$('.news_bar_buttons a').click(function(e){
		e.preventDefault();
		var artist_id = $(this).attr('data-artist');
		var news_id = $(this).attr('data-news');
		var position = $(this).attr('data-position');
		
		$('.artist_section[data-artist="'+artist_id+'"] .news_bar_buttons .selected').removeClass('selected');
		$(this).addClass('selected');
		
		$('.artist_section[data-artist="'+artist_id+'"] .artist_news_bar_inner').stop(true, false).animate({
			top:-26*(position-1)
		}, 300);
		
	})
	
	$('.read_more_link').click(function(e){
		e.preventDefault();
		var artist_id = $(this).attr('data-artist');
		var news_id = $(this).attr('data-news');
		open_artist(artist_id, news_id);
	})
	
	//make all the artist buttons spread out
	var total_width = 0;
	var count = 0;
	$('.artist_button').each(function(){
		total_width += parseInt($(this).width())+parseInt($(this).css('padding-left'))+parseInt($(this).css('padding-right'));
		count++;
	})
	
	var left = 940 - total_width;
	var add_each = Math.floor(left/count);
	
	$('.artist_button').each(function(){
		$(this).width($(this).width()+add_each);
	})
	
	
	$('#aboutlink').click(function(e){
		e.preventDefault();
		open_about();
	})
	
	$('#bloglink').click(function(e){
		e.preventDefault();
		open_news();
	})
	
	//if we should be opening an artist do it here
	
	if(showing_artist){
		if(artist_id==0){
			open_news(news_id);
		}else{
			open_artist(artist_id, news_id);
		}
	}
	
	$('#main_logo').click(function(e){
		e.preventDefault();
		close_all_artists();
		close_top();
		$('body, html').animate({
			scrollTop: 0
		}, 300)
	})
	
	$('.discog_item').hover(function(){
		$(this).find('.discog_over').animate({
			top: 0
		}, 300)
	}, function(){
		$(this).find('.discog_over').animate({
			top: 313
		}, 300)
	})
	
})

function open_artist(id, news_id){
	//target an artist_section
	
	if($('.artist_section[data-artist="'+id+'"]').hasClass('open')){
		$('body, html').animate({
			scrollTop: $('.artist_section[data-artist="'+id+'"] .artist_content').offset().top-80-113
		}, 300)
		return;
	}
	
	if(!news_id){
		news_id = 0;
	}
	
	$('.artist_section[data-artist="'+id+'"]').each(function(){
		
		var artist_section = this;
		$(this).addClass('open');
		$(artist_section).find('.artist_content_loading').height(50);
		$(this).find('.artist_news_bar').slideUp(300);
		$(this).find('.artist_content').animate({
			height:50
		}, 300, function(){
			//we are now showing the loading graphic
			//do the ajax
			
			$.post('get_content.php?action=full_section', {artist_id: id, news_id: news_id}, function(data){
				//put it in the inner container
				var inner_content = $(artist_section).find('.artist_content_inner');
				$(inner_content).html(data);
				//work out the height required
				var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
				//hide the loading graphic and show the content
				$(artist_section).find('.artist_content').animate({
					height:height+25
				}, 300);
				
				$(artist_section).find('.artist_content_loading').animate({
					height:0
				}, 300);
				
				$('body, html').animate({
					scrollTop: $(artist_section).find('.artist_content').offset().top-80-113
				}, 300)
				
				//make the releases scrollers work
				$(artist_section).find('.releases_scroller a').click(function(e){
					e.preventDefault();
					
					var pos = $(this).attr('data-position');
					
					$(artist_section).find('.releases_scroller a').removeClass('selected');
					$(this).addClass('selected');
					
					$(artist_section).find('.artist_content_releases_inner').stop(true, false).animate({
						left:(pos-1)*-440
					}, 300);
				})
				
				$(artist_section).find('.pagination_list a').click(function(e){
					e.preventDefault();
					
					var id = $(this).attr('data-news');
					
					$(artist_section).find('.pagination_list a').removeClass('selected');
					$(this).addClass('selected');
					
					$.post('update_news.php', {id:id}, function(data){
						$(artist_section).find('.artist_content_news').html(data);
						
						//resize the content box
						var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
						//hide the loading graphic and show the content
						$(artist_section).find('.artist_content').animate({
							height:height+25
						}, 300);
						
						
					})
					
				})
				
				$(artist_section).find('.artist_news_pag_left_arrow').mousedown(function(e){
					e.preventDefault();
					var artist = $(artist_section).find('.artist_news_pag_inner').attr('data-artist');
					var element = $('.artist_news_pag_inner[data-artist="'+artist+'"]');
					if(parseInt(element.css('left'))<0){
						element.animate({
							left: '+=30'
						}, 300)
					}
				})
				
				$(artist_section).find('.artist_news_pag_right_arrow').mousedown(function(e){
					e.preventDefault();
					var artist = $(artist_section).find('.artist_news_pag_inner').attr('data-artist');
					var element = $('.artist_news_pag_inner[data-artist="'+artist+'"]');
					if(parseInt(element.width())>400&&parseInt(element.css('left'))>-(parseInt(element.width())-400)){
						element.animate({
							left: '-=30'
						}, 300)
					}
				})				
				
				$(artist_section).find('.video_list_left_button').mousedown(function(e){
					e.preventDefault();
					var artist = $(artist_section).find('.video_list_center_inner').attr('data-artist');
					var element = $('.video_list_center_inner[data-artist="'+artist+'"]');
					if(parseInt(element.css('left'))<0){
						element.animate({
							left: '+=140'
						}, 300)
					}
				})
				
				$(artist_section).find('.video_list_right_button').click(function(e){
					e.preventDefault();
					var artist = $(artist_section).find('.video_list_center_inner').attr('data-artist');
					var element = $('.video_list_center_inner[data-artist="'+artist+'"]');
					if(parseInt(element.width())>400&&parseInt(element.css('left'))>-(parseInt(element.width())-400)){
						element.animate({
							left: '-=140'
						}, 300)
					}
				})
				
				
				
				$(artist_section).find('.video_thumb').click(function(e){
					e.preventDefault();
					var youtube = $(this).attr('data-id');
					var title = $(this).attr('data-title');
					$(artist_section).find('.first_vid').html('<object width="440" height="253"><param name="movie" value="http://www.youtube.com/v/'+youtube+'?version=3&amp;hl=en_GB&amp;rel=0"></param><param name="wmode" value="opaque"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+youtube+'?version=3&amp;hl=en_GB&amp;rel=0" type="application/x-shockwave-flash" width="440" height="253" allowscriptaccess="always" wmode="opaque" allowfullscreen="true"></embed></object>');
					$(artist_section).find('.first_vid_title').html(title);
				})
				
			})
			
		});
	})
}


function close_all_artists(){
	$('.artist_section').each(function(){
		var artist_section = this;
		if($(this).hasClass('open')){
			$(this).removeClass('open');
			$(artist_section).find('.artist_content').animate({
				height:0
			}, 300);
			$(this).find('.artist_news_bar').slideDown(300);
		}
	});
}

function close_artist(id){
	
	$('.artist_section[data-artist="'+id+'"]').each(function(){
		var artist_section = this;
		if($(this).hasClass('open')){
			$(this).removeClass('open');
			$(artist_section).find('.artist_content').animate({
				height:0
			}, 300);
			$(this).find('.artist_news_bar').slideDown(300);
		}
	});
}

function open_news(news_id){
	
	if(!news_id){
		news_id = 0;
	}
	
	if($('#top_section').hasClass('open')){
		$('.top_section_loading').animate({
			height: 50
		}, 300);
		$('#top_section').animate({
			height:50
		}, 300, function(){
			$.post('get_top_content.php?action=news', {news_id: news_id}, function(data){
				//put it in the inner container
				var inner_content = $('.top_section_inner');
				$(inner_content).html(data);
				//work out the height required
				var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
				//hide the loading graphic and show the content
				$('#top_section').animate({
					height:height+25
				}, 300);
				
				$('.top_section_loading').animate({
					height:0
				}, 300);
				
				$('body, html').animate({
					scrollTop: 0
				}, 300)
				
				link_news_buttons();
				
			})
		});
	}else{
		$('#top_section').addClass('open');
		$('.top_section_loading').height(50);
		$('#top_section').animate({
			height:50
		}, 300, function(){
			$.post('get_top_content.php?action=news', {news_id: news_id}, function(data){
				//put it in the inner container
				var inner_content = $('.top_section_inner');
				$(inner_content).html(data);
				//work out the height required
				var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
				//hide the loading graphic and show the content
				$('#top_section').animate({
					height:height+25
				}, 300);
				
				$('.top_section_loading').animate({
					height:0
				}, 300);
				
				$('body, html').animate({
					scrollTop: 0
				}, 300)
				
				link_news_buttons();
				
			})
		});
	}
	
}


function open_about(){
	
	if($('#top_section').hasClass('open')){
		$('.top_section_loading').animate({
			height: 50
		}, 300);
		$('#top_section').animate({
			height:50
		}, 300, function(){
			$.post('get_top_content.php?action=about', {}, function(data){
				//put it in the inner container
				var inner_content = $('.top_section_inner');
				$(inner_content).html(data);
				//work out the height required
				var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
				//hide the loading graphic and show the content
				$('#top_section').animate({
					height:height+25
				}, 300);
				
				$('.top_section_loading').animate({
					height:0
				}, 300);
				
				$('body, html').animate({
					scrollTop: 0
				}, 300)				
			})
		});
	}else{
		$('#top_section').addClass('open');
		$('.top_section_loading').height(50);
		$('#top_section').animate({
			height:50
		}, 300, function(){
			$.post('get_top_content.php?action=about', {}, function(data){
				//put it in the inner container
				var inner_content = $('.top_section_inner');
				$(inner_content).html(data);
				//work out the height required
				var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
				//hide the loading graphic and show the content
				$('#top_section').animate({
					height:height+25
				}, 300);
				
				$('.top_section_loading').animate({
					height:0
				}, 300);
				
				$('body, html').animate({
					scrollTop: 0
				}, 300)				
			})
		});
	}
}



function close_top(){
	if($('#top_section').hasClass('open')){
		$('#top_section').removeClass('open');
		$('#top_section').animate({
			height:0
		}, 300);
	}
}

function link_news_buttons(){
	$('.top_readmore').click(function(e){
		e.preventDefault();
		
		$('.selected_entry').removeClass('selected_entry');
		$(this).parent().addClass('selected_entry');
		
		var new_news = $(this).attr('data-news');
		$.post('get_top_content.php?action=update_news', {id:new_news}, function(data){
			$('.top_section_inner .artist_content_col1').html(data);
			var inner_content = $('.top_section_inner');
			
			var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
			$('#top_section').animate({
				height:height+25
			}, 300);
		});
	});
	
	$('.top_left_pagination, .top_right_pagination').click(function(e){
		e.preventDefault();
		var next_page = $(this).attr('data-page');
		
		$.post('get_top_content.php?action=update_archive', {page:next_page}, function(data){
			$('.top_section_inner .artist_content_col2').html(data);
				
			var inner_content = $('.top_section_inner');
			
			var height = $(inner_content).height()+parseInt($(inner_content).css('padding-top'))*2;
			$('#top_section').animate({
				height:height+25
			}, 300);
			
			link_news_buttons();
		});
	})
	
}
















