$(document).ready(function(){
	// Document is ready.
	$('#open_audio').click(function(){
		if ($('#audio_player').hasClass('opened_player')) {
			$('#audio_player').animate({top: "-140"}, {duration:'fast'});			
			$('#mainwrap').animate({top: "0"}, {duration:'fast'});						
			$('#audio_player').removeClass('opened_player');
			stopMusic();
			$('#arrows').animate({top: "164px"}, {duration: 'fast'});
			$('#home').animate({backgroundPosition:"(50% -111px)"}, {duration:'fast'});			
		} else {
			$('#audio_player').animate({top: "0"},{duration:'fast'});
			$('#mainwrap').animate({top: "140"}, {duration:'fast'});
			$('#audio_player').addClass('opened_player');
			startMusic();			
			$('#arrows').animate({top: "304px"}, {duration: 'fast'});
			$('#home').animate({backgroundPosition:"(50% 35px)"}, {duration:'fast'});

		}
		return false;
	});
	
	$('.closed').hide();
	
	$('.nTitle').click(function(){
		if (!$(this).children().hasClass('active')) {
			var inner_content = $(this).parent().children('.inner_content');
			var parent = $(this).parent().parent().parent();
			var allInners = $(this).parent().parent().parent().children().children().children('.inner_content');		
			var showingLinks = $(this).parent().parent().parent().children().children().children().children('.showing');
			allInners.slideUp('fast');
			inner_content.slideDown('fast');
			showingLinks.removeClass('active');
			$(this).children('.showing').addClass('active');
		}
	});
	
	$('#email_field').focus(function(){
		$(this).val('');
	});
	
	$('#scrollvids').jScrollPane();
	
	loadImages();
	minHeight($('#archive'),300);
	minHeight($('#forthcoming'),300);
	minHeight($('#div_links'),300);
	minHeight($('#div_archivecol'),200);	
	showGigInfo();
	showComments();
	
	$(function() {
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});
	});

	$('body#home').append('<div id="arrows">&nbsp;</div>');

	$('.youtube_link').click(function(){
		vidString = $(this).attr('rel');
		loadMedia(vidString, true, true);
		return false;
	});
	
	simple_tooltip("a.youtube_link","tooltip");
	
	$(".popoutaudio").click(function(){
		var array = stopForPopup();
		// alert(array[0]);
	
		settings = {
					height:140, // sets the height in pixels of the window.
					width:500, // sets the width in pixels of the window.
					toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
					scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
					status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
					resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
					left:0, // left position when the window appears.
					top:0, // top position when the window appears.
					center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
					createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
					location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
					menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
				};
				
				parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
				
				window.open(this.href + "?track=" + array[0] + "&seek=" + array[1], name, parameters).focus();
		return false;
	});	
	
	$('#li_store').mouseover(function(){
		$('#ul_store_sub').show();		
		// function(){
		// 	$(this).css('cursor', 'pointer');
		// 		$('#ul_store_sub').show();
		// 	},
		// function(){		
		// 	var fade = setTimeout("$('#ul_store_sub').hide();", 2500);
	});
	
	
	$('#ul_store_sub').mouseout(function(){
		$('#ul_store_sub').hide();
	});
});

function loadImages(){
	$('.view_more').click(function(){
		var href = $(this).attr('href');
		$(this).parent().parent().load(href, function(){
			loadImages();
		});
		return false;
	});
}

function minHeight(div, height){
	// If the div exists lets resize it to min height.
	if (div.size() > 0) {
		// Now check if the divs height is less than what it's minimum is.
		if (div.height() < height ) {
			div.css('height', height + 'px');
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
				DD_belatedPNG.fix('.div_archive, #forthcoming, #archive');
			}
		};
	};
}

function showGigInfo(){
	$('.show_info').click(function(){
		if ($(this).parent().children('.gInfoBox').css('display') == 'none') {
			$(this).text('Hide Info');
			$(this).parent().children('.gInfoBox').slideDown('fast');
		} else {
			$(this).text('Show Info');
			$(this).parent().children('.gInfoBox').slideUp('fast');
		}
		return false;
	});
}

function showComments(){
	$('.show_comments').click(function(){
		var position = $(this).position();
		var top = position.top - 50;			
		
		// Target IE6 tell it the top position cos it's a dumb bastard.
		if ($.browser.msie && $.browser.version == 6 ) 	var top = position.top + 130;			;
			
		$('.comments_wrap').hide();
		$('#comment_holder').hide().css('top' , top + 'px').show();

		$('#comment_holder').load($(this).attr('href'), function(){
			$('.comments_wrap').fadeIn();
			commentSubmit();
		});

		return false;
	});
}

function commentSubmit(){
	$('#post_comment').click(function() {
		var secret = $("input#secret").val();
		var commentbody = $("textarea#commentbody").val();
		var content_id = $("input#content_id").val();	
		var type = $("input#comment_type").val();
		var dataString = 'secret='+ secret + '&body=' + commentbody + '&content_id=' + content_id + '&type=' + type;

		$.ajax({
			type: "POST",
			url: "comments_process.php",
			data: dataString,
			success: function(html) {
				$('.comment_content').replaceWith(html);
				$("textarea#commentbody").val('');
				$("input#secret").val('');

			}
		});
		return false;
	});
}

function errorFlash(){
	$('.error_msg').fadeOut().fadeIn();
}

// Youtube chromeless stuff
function getFlashMovie(movieName) {
	movieName = swfobject.getObjectById(movieName);
	return movieName;
}

function loadMedia(path, youtube, autoStart){
	getFlashMovie("vid_player").loadMedia(path, youtube, autoStart);
}

// Audio player functions
function stopForPopup(){
	var array = getFlashMovie("player").stopForPopup();
	return array;
}

function startMusic(){
	getFlashMovie("player").startMusic();
}

function stopMusic(){
	getFlashMovie("player").stopMusic();
}



function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+" tipper' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){
		$(this).removeAttr("title").mouseover(function(){
						my_tooltip.css({opacity:1.0, display:"none"}).fadeIn(100);
			}).mousemove(function(kmouse){
					var border_top = $(window).scrollTop();
					var border_right = $(window).width();
					var left_pos;
					var top_pos;
					var topoffset = 20;
					var leftoffset = -30;				
					if(border_right - (leftoffset *2) >= my_tooltip.width() + kmouse.pageX){
						left_pos = kmouse.pageX+leftoffset;
						} else{
						left_pos = border_right-my_tooltip.width()-leftoffset;
						}

					if(border_top + (topoffset *2)>= kmouse.pageY - my_tooltip.height()){
						top_pos = border_top +topoffset;
						} else{
						top_pos = kmouse.pageY-my_tooltip.height()-topoffset;
						}	

					my_tooltip.css({left:left_pos, top:top_pos});
			}).mouseout(function(){
					my_tooltip.css({left:"-9999px"});
			});
		}
	});
}

