/* -----------------------------------------------
BASE
----------------------------------------------- */
Cufon.replace('h1, div.plinth-latest h3, div.plinth-nav-secondary h3, div.form label, #Header a');

function init() {
	document.body.className += " loading js";
};

var Body = null;
$(function() {
	
	Body = $(document.body);
	Setup.init();
	Overlay.init();
	FormNewsletterSubscribe.init();

	
	if(Body.hasClass('home')) {Home.init();}
	else if(Body.hasClass('generic')) {
		Generic.init();
		Sounds.init();
	}
	else if(Body.hasClass('classes')) {Classes.init();}
	else if(Body.hasClass('faq')) {Faq.init();}
	else if(Body.hasClass('listing')) {Listing.init();}
});

/*	Setup
----------------------------------------------- */

var Setup = {	
	Body: null,
	init: function() {
		var cc = this;
		Body.addClass('jsReady');
		Body.removeClass('loading');
		
		// utility classes
		$("ul li:first-child").addClass("first");
		$("ul li:last-child").addClass("last");
	}
};

var Overlay = {
	Api: null,
	init: function() {
		var cc = this;
		$("a[rel]=overlay").overlay({
			mask: '#000',
			target: '#Overlay',
			onBeforeLoad: function() {
				var wrap = this.getOverlay().find(".ajax");
				wrap.load(this.getTrigger().attr("href")+'ajax');
			},
			fixed: false
		});
	},
	render: function() {
		$("#Overlay ul.tabs").tabs("#Overlay div.panes > div.content", {
			current: 'selected'
		});
		$("#Overlay .scrollable").scrollable().navigator();
		var navi = $("#Overlay .navi");
		if(navi.length) {
			var item = navi.find('a');
			var width = Math.round((item.length*11)/2);
			navi.css('marginLeft',-width);
		}
		Cufon.replace('#Overlay h1, #Overlay h2, #Overlay q');
	}
}

var Player = {
 	Api: null,
	init: function() {
		var cc = this;
		cc.Api = $("#JQPlayer");
		cc.Api.jPlayer({
			supplied: "mp3",
			swfPath: '/media/'
		});
	},
	play: function(file) {
		var cc = this;
		cc.Api.jPlayer("clearMedia");

		cc.Api.jPlayer( "setMedia", {
			mp3: file,
		});
		
		cc.Api.jPlayer("play");
	},
	stop: function() {
		var cc = this;
		cc.Api.jPlayer("stop");
	}
};

var Sounds = {
	Items: null,
	init: function() {
		var cc = this;
		cc.Items = $('#Content a[href$=".mp3"]');
		if(cc.Items.length) {
			Player.init();
			cc.Items.click(function(link){
				if($(this).hasClass('pause')) {
					$(this).removeClass('pause');
					Player.stop();
				}
				else {
					Player.play($(this).attr('href'));
					$(this).addClass('pause');
				}
				return false;
			});
		}
	}
}

var FormNewsletterSubscribe = {
	Default: null,
	Input: null,
	init: function() {
		var cc = this
		cc.Input = $('#FormNewsletterSubscribe input.text');
		cc.Default = cc.Input.val();
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Input.focus(function(){
			if($(this).val()==cc.Default) {$(this).val('');}
		});
		cc.Input.blur(function(){
			if($(this).val()=="") {$(this).val(cc.Default);}
		});
	}
}


/*	Sections
----------------------------------------------- */

var Home = {
	Song: null,
	Player: null,
	init: function() {
		var cc = this;
		$("#Promotions .scrollable").scrollable({circular: true}).navigator().autoscroll({ autoplay: true, interval: 4000 });
		$("#Flickr .scrollable").scrollable();
		$('#Flickr .item a').lightBox();
		
		cc.Player = $("#JQPlayer");
		cc.Player.jPlayer({
			ready: function() {
				$(this).jPlayer("setMedia", { mp3: "/media/pd.mp3"});
		  },
		  supplied: "mp3",
			swfPath: '/media/'
		});
		
		cc.SongTrigger = $('#PyjamaDramaSong');
		Cufon.replace('#Content h2, #Content q');
		
		cc.SongTrigger.click(function() {
			if($(this).hasClass('active')) {
				$(this).removeClass('active');
				$("#JQPlayer").jPlayer('stop');
			}
			else {
				$(this).addClass('active');
				$("#JQPlayer").jPlayer('play');
			}
			
		});
	}
}

var Generic = {
	init: function() {
		var cc = this;
		Cufon.replace('#Content h2, #Content q');
		$('#Content ol li').wrapInner('<span>');
	}
}

var Classes = {
	init: function() {
		var cc = this;
		$("div#Content a[title]").tooltip({ effect: 'slide'});
	}
}

var Listing = {
	init: function() {
		var cc = this;
		Cufon.replace('#Content h2');
	}
}

var Faq = {	
	Triggers: null,
	Close: null,
	init: function() {
		var cc = this;
		cc.Triggers = $('#FaqItems h2 a');
		cc.Close = $('#FaqItems a.close');

		
		cc.events();	
		cc.render();
	},	
	events: function() {
		var cc = this;
		cc.Triggers.bind('click', function() {
			var item = $(this).parent().parent();
			item.addClass('item-active');
			Cufon.refresh();
			return false;
		});
		cc.Close.bind('click', function() {
			var item = $(this).parent().parent().parent();
			item.removeClass('item-active');
			Cufon.refresh();
			return false;
		});
	},
	render: function() {
		var cc = this;
		Cufon.replace('#FaqItems h2', {
			hover: true
		});
	}
};




/*	Functions
----------------------------------------------- */

function equalHeight(group) {
	tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

function submitRating(score) {
	var form = $('form#new_rating');
	
	if(form.length) {
		$.post(
            '/rate',
            form.serialize(),
            function(data){
            }
        );
	}
}

