/*
 @ChangeDate:	$LastChangedDate: 2009-03-23 22:28:22 +0000 (Mon, 23 Mar 2009) $
 @Revision:		$Rev: 183 $
 @Author: 		$Id: default.js 183 2009-03-23 22:28:22Z philippryce $
*/

var _session = '';

// JSON POST requests
$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};

function include (url) {
	$('<s'+'cript></s'+'cript>').attr({
		type: 'text/javascript',
		src: url
	}).appendTo('body');
};


// Fancy lyric bubbles
$.fn.bubbler = function () {
	var self = this;
	
	function showMessage (bubble, message) {
		length = $('span.inline-rating', bubble).text(message).length;
	}
	
	return this.each(function () {
		var $this = $(this);
		var bubble = $this.find('.lyricBubble')
		var lyric_token = $this.attr('id').replace('lyric_', '');
		
		$this.find('ul.rating').bind('mouseover', function () {
			$('.current-rating', this).hide();
		}).bind('mouseout', function () {
			$('.current-rating', this).show();
		});
		
		$this.find('ul.rating a').bind('click', function ()
    	{
    		bubble.data('rating', this.innerHTML);
    		x = this.innerHTML;
    		
    		url = $(this).attr('href').split('/');
    		session = url[5];
    		
    		showMessage($this, 'working...');
    		$.postJSON('/json/rate', {rating: x, token: lyric_token, session: session}, function (json)
    		{
    			if (json.status != 'OK') {
	    			showMessage($this, json.message);
	    			return false;
    			}
    			showMessage($this, 'Saved!');
    			setTimeout(function () { showMessage($this, 'You rated '+x+' star'+plural(x,'s')+'.'); }, 1500);
    		});
    		return false;
    	});
		
	});
}

// Popup Window
$.popper = function (options) {
	var defaults = {
		title: 'Window Title',
		content: '.content',
		buttons: { 'Ok': { onclick: function () { self.close(); return false; } } }
	};
	
	var opts = $.extend(defaults, options);
	var self = this;
	
	this.close = function ()
	{
		$('.window').fadeOut('fast');
	};
	function focusFirstButton ()
	{
		$('.window button:first').focus();
	}
	
	$('.window div.buttons button').remove();
	for (title in opts.buttons) {
		tmpButton = opts.buttons[title];
		$('<button type="button" class="button"><span class="buttonLabel">' + title +  '</span></button>').bind('click', tmpButton.onclick ).appendTo('.window div.buttons');
	}
	
	$('.window')
		.find('h2')
			.text(opts.title)
			.end();
	$('.window div.content p').html('').html(opts.content);
	
	$('.window').fadeIn('fast');
	
	
	if ($.browser.mozilla) {
		$('.buttonLabel', $('.window button.button')).css('margin', '-2px -3px 0 0');
	}
	
	setTimeout(focusFirstButton, 300);
};

CI = {
	uri: {
		segment: function (i) {
			var url = window.location;
			var segments = url.pathname.split('/');
			if (segments[i]) { return segments[i]; }
			return false;
		}
	}
}


var ie6block = (function () {
	var userAgent = navigator.userAgent.toLowerCase();
	var ie = userAgent.indexOf('msie') > -1;
	var version = 0;
	version = parseFloat( userAgent.substring( userAgent.indexOf('msie ') + 5 ) );
	var css = '.ie6overlay{position:absolute;top:0;bottom:0;width:100%;height:100%;background-color:#fff;z-index:999;text-align:left;}.ie6overlay .box{z-index:999;color:#2d3436;position:absolute;top:100px;left:50%;margin:0 0 0 -200px;width:400px;border:1px solid #2d3436;background-color:#eeeeeb;font-size:14px}.ie6overlay .box h2{font-size:17px;font-weight:bold;text-align:center;color:#eeeeeb;margin:0;padding:2px 7px;background-color:#a1292e;border-bottom:1px solid #2d3436}.ie6overlay .box .content{padding:5px 5px 15px 5px;overflow:auto}.ie6overlay .box p{margin:0 6px;padding:0}.ie6overlay .box h3{font-size:16px;font-weight:bold;margin:8px 4px 5px;padding:0}.ie6overlay ul{margin:8px 0}.ie6overlay li{margin:5px 0}.ie6overlay .links{text-align:right;color:#888984;font-size:13px;margin:5px 5px 5px 0}.ie6overlay .links a{color:#888984}';
	var html = $('<div class="ie6overlay">'+
    		'<div class="box">'+
    			'<h2>You\'re using an out-of-date browser!</h2>'+
    			'<div class="content">'+
    				'<p>'+
    					'But its easy to fix! You can upgrade to the latest version by clicking '+
    					'<a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">here</a>.'+
    				'</p>'+
    				'<h3>Why should I upgrade?</h3>'+
    				'<ul><li><strong>Websites load faster</strong>, often double the speed of this older version</li>'+
    					'<li><strong>Websites look better</strong> with more web standards compliance</li>'+
    					'<li><strong>Tabs</strong> let you view multiple sites in one window</li>'+
    					'<li><strong>Safer browsing</strong> with phishing protection</li>'+
    					'<li><strong>Convenient Printing</strong> with fit-to-page capability</li></ul>'+
    				'<div class="links"><a class="remindme" href="javascript:ie6block.close();">Remind me later...</a> &bull; '+
    				'<a class="ignore" href="javascript:ie6block.ignore();">Ignore this warning</a></div>'+
    			'</div>'+
    		'</div>'+
    	'</div>');
    function isCookieSet() {
		if (document.cookie.length > 0) {
			var i = document.cookie.indexOf("ie6block=");
			return (i != -1);
		}
		return false;
	}
    return {
    	version: version,
    	check: function () {
    		if (!ie) { return false; }
    		if (version != 6) { return false; }
    		if (isCookieSet()) { return false; }
    		ie6block.show();
    	},
    	show: function () {
    		$('head').append('<style type="text/css">'+css+'</style>');
    		html.prependTo('body');
    		$('a.ignore', $('.ie6overlay')).bind('click', ie6block.ignore);
    		$('a.remindme', $('.ie6overlay')).bind('click', ie6block.close);
    	},
    	ignore: function () {
    		var exp = new Date();
			exp.setTime(exp.getTime()+(7*24*3600000));
			document.cookie = "ie6block=blocked; expires="+exp.toUTCString();
			return ie6block.close();
    	},
    	close: function () {
    		$('.ie6overlay').remove();
    		return false;
    	}
    };
})();


// On Ready
$(function ()
{
	$('noscript').remove();
    $('body').addClass('javascript');
    
    _session = $('meta[name="X-AuthData"]').attr('content');
    
    var browser = false;
    
    if (jQuery.browser.safari) { browser = 'safari'; }
    if (jQuery.browser.opera) { browser = 'opera'; }
    // IE is special and gets it version added to the variable
    if (jQuery.browser.msie) {
    	var userAgent = navigator.userAgent.toLowerCase();
    	browser = 'ie'+parseFloat(userAgent.substring(userAgent.indexOf('msie ')+5));
    }
    if (jQuery.browser.mozilla) { browser = 'firefox'; }
    
    if (browser) { $('body').addClass(browser); }
    
    ie6block.check();
    
    // Button fixes
	$('.button', $('body.safari, body.firefox, body.opera, body.ie8')).addClass('buttonOn');
    $('.buttonLabel', $('.firefox button.button')).css('margin', '-2px -3px 0 0');
    
    $('div.lyricUI').bubbler();
    
    $("span.loader").ajaxStop(function(){
		setTimeout(fadeAJAXLoaderOut, 500);
	}).ajaxStart(function(){
		$(this).stop().fadeIn('fast');
	});
});

function fadeAJAXLoaderOut() 
{
	$('span.loader').stop().fadeOut('slow');
}

function plural (x, string)
{
    if (x == 1) {return '';}
    else {return string;}
}
