/* Common JavaScript for jQuery demonstration pages. */
$(function () {
	// Add header links
	var current = location.href.replace(/^.*[\\\/]([^\.]+)\.[^\.]+$/, '$1').toLowerCase();
	$('<div style="float: right;">' +
		(current == 'index' ? '' : '<a id="home" href="index.html"><img src="homePage.png" ' +
		'alt="Home page" title="Home page"/></a>&nbsp;') +
		'<img id="crosslink" src="plugins.png" alt="Other plugins" title="Other plugins"/>' +
		'<ul id="crosslinks">' +
		(current == 'bookmark'      ? '' : '<li><a href="bookmark.html">jQuery Bookmark</a></li>') +
		(current == 'calculator'    ? '' : '<li><a href="calculator.html">jQuery Calculator</a></li>') +
		(current == 'countdown'     ? '' : '<li><a href="countdown.html">jQuery Countdown</a></li>') +
		(current == 'datepick'      ? '' : '<li><a href="datepick.html">jQuery Datepicker</a></li>') +
		(current == 'gchart'        ? '' : '<li><a href="gChart.html">jQuery Google Chart</a></li>') +
		(current == 'gsblogbar'     ? '' : '<li><a href="gsblogbar.html">jQuery Google Search Blogbar</a></li>') +
		(current == 'gsbookbar'     ? '' : '<li><a href="gsbookbar.html">jQuery Google Search Bookbar</a></li>') +
		(current == 'gsnewsbar'     ? '' : '<li><a href="gsnewsbar.html">jQuery Google Search Newsbar</a></li>') +
		(current == 'gsvideobar'    ? '' : '<li><a href="gsvideobar.html">jQuery Google Search Videobar</a></li>') +
		(current == 'icalendar'     ? '' : '<li><a href="icalendar.html">jQuery iCalendar</a></li>') +
		(current == 'imagecube'     ? '' : '<li><a href="imageCube.html">jQuery Image Cube</a></li>') +
		(current == 'keypad'        ? '' : '<li><a href="keypad.html">jQuery Keypad</a></li>') +
		(current == 'localisation'  ? '' : '<li><a href="localisation.html">jQuery Localisation</a></li>') +
		(current == 'relationships' ? '' : '<li><a href="relationships.html">jQuery Relationships</a></li>') +
		(current == 'svg'           ? '' : '<li><a href="svg.html">jQuery SVG Integration</a></li>') +
		(current == 'themes'        ? '' : '<li><a href="themes.html">jQuery Themes</a></li>') +
		(current == 'timeentry'     ? '' : '<li><a href="timeEntry.html">jQuery Time Entry</a></li>') +
		'</ul></div>').insertBefore('h1');
	var crosslink = $('#crosslink');
	var offset = crosslink.offset();
	var crosslinks = $('#crosslinks');
	crosslink.toggle(function() {
		crosslinks.css({left: ($.browser.msie || $.browser.opera ?
			offset.left : crosslink[0].offsetLeft) +
			crosslink.width() - crosslinks.width(),
			top: ($.browser.msie || $.browser.opera ? offset.top :
			crosslink[0].offsetTop) + crosslink.height()});
		crosslinks.slideDown();
	}, function() {
		crosslinks.slideUp();
	});
	// Ratings
	showRating(current, '#rating');
	// Stripe tables
	$('table').each(function() {
		$('tr:odd', this).addClass('alternate');
	});
	// Initialise tabs
	if ($.fn.tabs) {
		$('#tabs').tabs();
	}
	// Execute example script tags
	$('code.jsdemo').each(function () {
		$(this).removeClass('jsdemo').addClass('js');
		eval($(this).text().replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
	});
});

function showRating(name, id) {
	var rating = {bookmark: [4.0, 3], calculator: [4.5, 2],
		countdown: [4.0, 13], datepick: [4.0, 6],
		gchart: [4.0, 8], gsblogbar: [0.0, 0],
		gsbookbar: [0.0, 0], gsnewsbar: [0.0, 0],
		gsvideobar: [4.0, 1], icalendar: [4.0, 1],
		imagecube: [3.0, 4], keypad: [4.0, 3],
		localisation: [4.0, 2], relationships: [4.0, 1],
		svg: [4.0, 18], themes: [0.0, 0],
		timeentry: [4.5, 21]}[name];
	var html = '';
	if (rating) {
		html = '<span>';
		for (var i = 0; i < 5; i++) {
			html += '<img src="star' + (i + 0.5 == rating[0] ? '.5' :
				(i < rating[0] ? '1' : '0')) + '.gif"/>';
		}
		html += '</span> (' + rating[1] + ' vote' + (rating[1] != 1 ? 's' : '') + ')';
	}
	$(id).addClass('rating').html(html);
}

