/* Common JavaScript for jQuery demonstration pages. */
$(function () {
	//$('head').prepend('<link href="kw-name.rss.xml" type="application/rss+xml" rel="alternate" title="RSS Feed">');
	// Add header links
	var current = location.href.replace(/^.*[\\\/]([^\.]+)\.[^\.]+$/, '$1').toLowerCase();
	
	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();
	});
	$('#download').append(' <img src="img/download.png" alt="" style="">');
	if ($.fn.bookmark) {
		$('#bookmark').bookmark({compact: true, popup: true, icons: 'img/bookmarks.png',
			popupText: '<button>Bookmark this <img src="img/bookmarker.png" alt=""></button>'});
	}
	// Ratings
	showRating(current, '#rating', ': ');
	// Stripe tables
	$('table').each(function() {
		$('tr:odd', this).addClass('alternate');
	});
	// Initialise tabs
	if ($.fn.tabs) {
		$('#tabs').tabs($.fn.tabs.tabProps);
	}
	// Execute example script tags
	$('code.jsdemo').each(function () {
		$(this).removeClass('jsdemo').addClass('js').hide().
			wrap('<div class="showCode"></div>').
			
			text('\n' + $(this).text());
		eval($(this).text().replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
	});
	$('code.css').each(function () {
		$(this).hide().wrap('<div class="showCode"></div>');
			
	});
	$('code.htmldemo').each(function () {
		$(this).removeClass('htmldemo').addClass('html').
			hide().wrap('<div class="showCode"></div>');
			
	});
	$('a.showCode').toggle(function() {
		$(this).text($(this).text().replace(/Show/, 'Hide')).
			parent().css('width', 'auto').find('code').show();
		return false;
	}, function() {
		$(this).text($(this).text().replace(/Hide/, 'Show')).
			parent().css('width', '80px').find('code').hide();
		return false;
	});
	// Code highlighting
	if ($.fn.chili) {
		$('code').chili({recipeFolder: 'js/'});
	}
});

function showRating(name, id, prefix) {
	var rating = {bookmark: [4.0, 8], calculator: [4.0, 4],
		calendars: [5.0, 3], calendarspicker: [5.0, 3], countdown: [4.0, 36],
		dateentry: [4.0, 12], datepick: [5.0, 42],
		flightboard: [5.0, 2], gchart: [4.0, 21],
		gsblogbar: [0.0, 0], gsbookbar: [0.0, 0],
		gsnewsbar: [0.0, 0], gsvideobar: [3.5, 2],
		icalendar: [4.0, 3], imagecube: [4.0, 8],
		keypad: [4.0, 6], labeleffect: [0.0, 0],
		localisation: [3.0, 3], realperson: [5.0, 2],
		relationships: [4.0, 1], svg: [4.0, 47],
		themes: [4.0, 2], timeentry: [4.5, 37]}[name];
	var html = '';
	if (rating) {
		html = (prefix || '') + '<span>';
		for (var i = 0; i < 5; i++) {
			html += '<img src="img/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);
}

function jumpTo(tab, id) {
	$('#tabs').triggerTab(tab);
	setTimeout('scrollTo(0, $("a[name=' + id + ']").offset().top);', 100);
	return false;
}

