
$(function() {
	$("body").addClass('js');
	cufon();
	initGallery();
	initWidth();
	initExternalLinks();
	initRadio();
	initFaq();
});

function initFaq() {
	$("#faq dt").click( function() { $(this).next("dd").slideToggle(150); });
}

function initRadio() {
	$("#radio-box object").after("<p>Popup Player</p>");
	$("#radio-box p").click( function() {
		window.open("/radio.html","Radio",
			"menubar=no,width=188,height=130,toolbar=no,scrollbars=no,location=no,status=no,resizable=no");
	});
}

function initWidth() {
	setMin();
	
	$(window).resize(function() {
		setMin();						  
	});
	
	function setMin() {
		if ($(window).width() > 960) {
			$("body").css({'overflow-x': 'hidden'});
		} else {
			$("body").css({'overflow-x': 'auto'});
		}
	}
}

/* Text replacement
===================================================================================== */
function cufon() {
	Cufon.replace(['h1','h2','h3','#faq dt','#faq dd strong', '#artist h4', 'th', '#tv-box h4', '#radio-box p'], {});
} 

/* Gallery
===================================================================================== */	
function initGallery() {

//#### initilise
	var slideshow = false;	
	var first = $("#gallery #thumbs li:first img");
	var count = $("#gallery #thumbs li").length;	
	
	if (count === 0) return;
	
	var thumbWidth  = $("#gallery #thumbs li").outerWidth();
	var thumbMargin = parseInt($("#gallery #thumbs li").css("margin-left"), 10)
	thumbWidth = thumbWidth + (thumbMargin * 2);	
	var stripWidth   = thumbWidth * count;
	var stripVisible = $("#gallery #thumbs").width();	
		
	
//#### build controls	
	$("#gallery #thumbs ul").css({width: stripWidth +'px'});
	$("#gallery #thumbs li:first").addClass('on');
	
	$("#gallery #thumbs").after('<div id="gal-wrap"><img src="'+ getBig($(first).attr('src')) +'" id="gal-img" /></div>');
	$("#gallery #gal-img").after('<p id="gal-title">&nbsp;</p>');
	$("#gallery #thumbs").before('<img src="/img/gal-left.gif" id="gal-prev" />');
	$("#gallery #thumbs").after('<img src="/img/gal-right.gif" id="gal-next" />');
	//$("#gallery #gal-wrap").after('<h3 id="gal-start">Start Slideshow</h3>');
	//$("#gallery #gal-wrap").after('<h3 id="gal-stop" style="display: none">Stop Slideshow</h3>');
	//Cufon.replace(['#gal-stop','#gal-start'], {});
	
		
//#### attach events
	$('#gallery #gal-prev').click( function() {
		slideThumbs('left');						
	});
	
	$('#gallery #gal-next').click( function() {
		slideThumbs('right');						
	});
	
	$('#gallery #thumbs a').click( function() {
		stopSlideShow();
		$(this).parent('li').showImage();	
		return false;
	});
	
	$('#gallery #gal-start').click( function() {
		startSlideShow();
	});

	$('#gallery #gal-stop').click( function() {
		stopSlideShow();
	});
	

//#### functions
	function slideThumbs(dir) {
		var thumbs = $("#gallery #thumbs ul");
		var left = parseInt($(thumbs).css('left'));
		
		if (dir === 'left') {
			if (left >= 0) return;
			dist = left + stripVisible;
		} else {
			if (left <= (stripVisible - stripWidth)) return;
			dist = left - stripVisible;
		}
		
		$(thumbs).animate({'left': dist}, 200);
	}
	
	function startSlideShow() {	
		$('#gallery #gal-start').hide();	
		$('#gallery #gal-stop').show();
		
		slideshow = true;
		slideShow();	
	}
	
	function stopSlideShow() {		
		$('#gallery #gal-stop').hide();
		$('#gallery #gal-start').show();
		
		slideshow = false;	
	}
		
	function slideShow() {
		if (!slideshow) return;
		
		
		var main  = $("#gallery #gal-img");		
		var nxtThumb = $("#gallery #thumbs li.on").next();		
		if ($(nxtThumb).length === 0) {
			nxtThumb = $("#gallery #thumbs li:first");	
		}	
		
		$(nxtThumb).animate({opacity: 1.0}, 3000,
			function() { 
				$(this).showImage({
					callBack: function() { slideShow(); }
				});
			}
		);
	}
	

//#### helpers	
	function getBig(filename) {
		return filename.replace('/small/', '/big/');
	}
	function getSml(filename) {
		return filename.replace('/big/', '/small/');
	}	
		
	jQuery.fn.showImage = function(options) {
		var defaults = { callBack: function() {} }
		var settings = $.extend(true, {}, defaults, options);
		
		var main  = $("#gallery #gal-img");	
		var filename = $(this).find('img').attr('src');	
		var title = $(this).find('img').attr('alt');
		filename = getBig(filename);
		
		$(this).siblings().removeClass('on');
		$(this).addClass('on');
		
		$(main).unbind('load');
		
		$(main).fadeOut(800, function() {			
			$(main).load( function() {		
				var h = $(main).height();
				$(main).css({marginTop: -Math.ceil(h / 2)});
				$("#gal-title").text(title);
				$(main).fadeIn(500, function() { 				
					settings.callBack.call(this); 
				});
			}).attr('src', filename);
		});
	}
}


/* External Links and PDF Links
===================================================================================== */
function initExternalLinks() {
    var h = window.location.host.toLowerCase();
    $("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "']), a[href$='.pdf']").attr("target", "_blank");
}



function debug(msg) {
	$('#wrap').after('<p>'+msg+'</p>');	
}