// JavaScript Document

// Preload
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


// Accordion
var divs_height = new Array();
var speed = 350;
var accord_active = false;
function accord_collapse(last_div_id)
{
	// Close old div
	$('#'+last_div_id+' .collapse').animate({
								  height: "0px"
								  }, speed);
	
	$('#'+last_div_id+' .collapse_slide').animate({
								  marginTop: "-"+divs_height[last_div_id]+"px"
								  }, speed);	
}

function accord_expand(div_id)
{
	// Open new div
	$('#'+div_id+' .collapse').animate({
								  height: divs_height[div_id]
								  }, speed);
	
	$('#'+div_id+' .collapse_slide').animate({
								  marginTop: "0px"
								  }, speed);	
}

function accord_init()
{
	var last_div_id;
	// Store heights and collapse divs
	$('#accordion ul li .collapse_slide').each(function(index){
												  var div_id = $(this).parent().parent().attr('id');
												  var div_class = $(this).parent().parent().attr('class');
												  // Store heights
												  divs_height[div_id] = $('#'+div_id+' .collapse_slide').outerHeight();
												  
												  // Collapse all divs
												  if(div_class != 'open')
												  {
													  $('#'+div_id+' .collapse').css({
																					  height: '0px'
																					  });
													  $('#'+div_id+' .collapse_slide').css({
																						   marginTop: "-"+divs_height[div_id]+"px"
																						   });
												  }
												  else
												  { last_div_id = div_id; }
												  });
	
	$('#accordion').css({
						height: $('#accordion').outerHeight()+'px'
						});

	$('#accordion ul li .trigger').mouseover(function(){
												  var div_id = $(this).parent().attr('id');
												  
												  if(last_div_id != div_id && !accord_active)
												  {
														accord_active = true;
														setTimeout('accord_active = false', speed);
														accord_collapse(last_div_id);
														accord_expand(div_id);
														last_div_id = div_id;
												  }
												  
												  });
}


function get_favourite_total()
{
	$('#favourite_total').load('ajax.php?function=get_favourites_total');
}


function playlist_switch(pid)
{
	$('p#playlist_switch').load('ajax.php?function=playlist_switch',{ pid : pid });
	return false;
}


function aj(qstring,func,container)
{
	$(".ajax_loader").show();
	
	$.get("ajax.php?", qstring + "&function=" + func , function(data){
		$(container).html(data);
		$(".ajax_loader").hide();
	});
	return false;
}


// Once DOM has loaded
$(document).ready(function()
{
	// Autoexpand textareas
	$('textarea.expanding').autogrow();
	
	// Disable submit buttons on click
	$('input[type=submit]').click(function(){
		$(this).hide().clone().insertAfter(this).show().attr('disabled',true).attr('value','Please wait...');
	});
	
	// Sub header tray
	var sub_header_toggle = false;
	$('#sub_header_close').click(function(){
		$('#sub_header').slideUp(function() {
				sub_header_toggle = false;
				$('#header .shadow').css("background-position","0 0");
				//$('#sub_header_open').removeClass('active');
				});
	});
	$('#sub_header_open').click(function(){
		if(!sub_header_toggle)
		{
			sub_header_toggle = true;
			$('#header .shadow').css("background-position","0 -16px");
			$('#sub_header').slideDown();
			//$('#sub_header_open').addClass('active');
		}
		else
		{
			sub_header_toggle = false;
			$('#sub_header').slideUp(function() {
					$('#header .shadow').css("background-position","0 0");
					//$('#sub_header_open').removeClass('active');
					});
		}
	});


	// Search functions
	var search_text = document.getElementById('top_search').defaultValue;
	//$('#top_search').attr('value',search_text);
	
	$('#top_search').focus(function() {
		$('#top_search').removeClass('pre');
		if($('#top_search').attr('value') == search_text)
		{
			$('#top_search').attr('value','');
		}
	});
	$('#top_search').blur(function() {
		
		if($('#top_search').attr('value') == '' || $('#top_search').attr('value') == search_text)
		{
			$('#top_search').addClass('pre').attr('value',search_text);
		}
	});
	
	
	// Tabs box
/*	$('#content .tab_box .tabs .tab').click(function() {
		$('#content .tab_box .tabs .tab').removeClass('open');
		$('#' + $(this).attr('id')).addClass('open');

		//$('#content .tab_box .tabs .tab').animate({ height: "36px" }, 200);
		//$('#' + $(this).attr('id')).animate({ height: "41px" }, 200);
		
		$('#content .tab_box .tab_content_outer .tab_content').removeClass('show');
		$('#' + $(this).attr('id') + '_content').addClass('show');
	}); */
	
	
	// Set max height
	//$('#scrollable .items .scroll_news').each(function(){
//													   current_height = $(this).outerHeight() + 2;
//													   if(current_height > max_height)
//													   { max_height = current_height; }
//													   });
	//$('#scrollable div.items').css('visibility','visible').animate({height : max_height+"px"},300);
	//$('#scrollable div.items').css('visibility','visible').animate({height : "400px"},300);

});


function scroll_init()
{
		// Scrollable
	$('#scrollable').scrollable({
			items:".items",
			size:1,
			horizontal:true
			});
	
	// Scrollable - load feeds
	var max_height = 100;
	var current_height;
	$('#scrollable div.items').css({height : max_height+'px'});
	
	$('#scrollable .items .scroll_news').each(function(){
													current_height = $(this).outerHeight() + 2;
													if(current_height > max_height)
													{ max_height = current_height; }
												});
	$('#scrollable div.items').css('visibility','visible').animate({height : max_height+'px'},300);
	

	$('#scrollable .ajax_feed').each(function(){
						  var fid = $(this).attr('title');
						  $(this).load("loadfeed.php",{ fid: fid },function(){
										current_height = $(this).outerHeight() + 2;
										if(current_height > max_height)
										{
											max_height = current_height;
											$('#scrollable div.items').animate({height : max_height+"px"},300);
										}
										$(this).attr('title','');
									});
						  });
}

var scroll_interval;
var scrolling = false;
var init_delay = 3000;
var short_delay = 7000;
var click_delay = 12000;
function auto_scroll()
{
	if(!scrolling)
	{
		scroll_interval = setInterval('jQuery("#scrollable").scrollable("auto_scroll");',click_delay);
		scrolling = true;
	}
}


// Once entire page has loaded
$(window).load(function()
{
	// Accordion
	accord_init();
	
	// Scroll init
	scroll_init();
	
	// Set auto scroll
	setTimeout('auto_scroll();',init_delay);
	$('#scrollable .next, #scrollable .prev, #scrollable .navi').click(function(){
								clearInterval(scroll_interval);
								scrolling = false;
								setTimeout('auto_scroll();',short_delay);
							});
	
	// Preload subhead images
	$.preloadImages('assets/images/subhead_bg.jpg','assets/images/subhead_top.jpg','assets/images/subhead_link_bg.gif','assets/images/subhead_close.gif');

});