// body onload
$(function(){
	// load the history script for tracking the loaded tab
	$.getScript('/scripts/jquery.history.js', function(){
		$.historyInit(function(hash){
			$('#tabs-area').tabs('select',hash + '-tab');
			fitToScreen();
		});
		// add tab, content, or side link clicks to the history
		$("#tabs-area a,#side a").click(function(e){
			// if this is a reference to a link on the same page, just switch the tab
			var hash_regexp = new RegExp(decodeURI(this.hash).replace('?','\\?')+'.*');
			if (this.hash && decodeURI(document.URL).indexOf(decodeURI(this.href).replace(hash_regexp,'')) > -1){
				// check if it is a tab link or a regular hash link (named anchor, popup, etc)
				if ($(this.hash.split('?')[0].replace('-tab','')+'-tab').length){
					$.historyLoad(this.hash.replace('#','').replace('-tab',''));
				}
				// set webinar if available
				var webinar = this.href.substring(this.href.lastIndexOf('?')+1);
				$('#webinarDateAndLocation').val(decodeURI(webinar));
				return false;
			}
		});
	});
	// initialize the tabs area and focus any form elements
	$('#tabs-area').tabs({
		selected: -1,
		show: function(e,ui){
			$(ui.tab.hash+" :text:visible:first").focus();
			// send Omniture tracking data
			s.pageName = ui.tab.hash;
			s.t();
			// send Eloqua tracking data
			setTimeout("elqFCS(location.href.replace('#',''))",500);
		}
	}).tabs('select',window.location.hash+'-tab');
	// Star rating functionality
	var starFull = '/images/star-full.png';
	var starEmpty = '/images/star-empty.png';
	$('#star-rating').show(1,function(){
		var allowedChars = 250;
		fitToScreen();
		$('#star-rating img').mouseenter(function(){
			$(this).prevAll().andSelf().attr('src',starFull);
			$(this).nextAll().attr('src',starEmpty);
		}).click(function(){
			$('#star-rating').children('img').css('cursor','auto').andSelf().unbind();
			var rating = this.id.split('-')[1]
			s.prop7=rating;
			s.events="event6";
			s.products=";;;;event6="+rating;
			s.t();
		});
		$('#star-rating-comment-link').click(function(){
			$('#star-rating-comment').fadeIn();
			fitToScreen();
			$('#star-rating-comment textarea').val('').focus().siblings('.comment-counter').text(allowedChars);
			return false;
		});
		$('#star-rating-comment input').click(function(){
			$('#star-rating-comment').fadeOut('fast',function(){
				fitToScreen();
			});
			s.prop12=$(this).prevAll('textarea').val().substring(0,allowedChars);
			if (s.prop12) s.t();
			s.prop12='';
		});
		$('#star-rating-comment textarea').keyup(function(){
			var chars = this.value.length;
			var total = allowedChars - chars;
			if (total < 0){
				total = 0;
				$(this).val(this.value.substring(0,allowedChars)).scrollTop(this.scrollHeight);
			}
			$(this).siblings('.comment-counter').text(total);
		});
	}).mouseleave(function(){
		$(this).children('img').attr('src',starEmpty);
	});
});
