/*	Worthing Borough Council On-line, v3.0.

	wbc.content.js - Core Content Page Progressive JavaScript initialisation and enhancements

	Copyright (C)2008-9 Worthing Borough Council
	<webmaster@worthing.gov.uk>
*/

var initContentHeader=function(){

	//$('body').append('run init');
	var lastMod='';

	$('meta').each(function(){
		if($(this).attr('name')=='DC.date.modified'){
			lastMod=$(this).attr('content');
		}
	});

	var last_Modified;
	var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
			
	if (lastMod == '' || !lastMod) {
		last_Modified = '<span style="cursor:help" title="No modification date found, sorry!">Not sure...</span>';
	}
	else{
		lastMod=lastMod.split('-');
		if(lastMod[1].substr(0,1)=='0'){
			lastMod[1]=lastMod[1].substr(1);
		}
		last_Modified=lastMod[2]+' '+monthNames[parseInt(lastMod[1])-1]+' '+lastMod[0];
	}

	//$('#content-Header').append('<div id="content-Header-Info"></div>');
	$('#content-header-date-modified').append('<div class="content-header-field"><span style="cursor:help;" title="This is the date we last changed what this page said">Last Update</span></div><div class="content-header-data"><span class="feint">'+last_Modified+'</span></div>');

	$('#share-zone-help').bind('click',function(){	$('#share-zone-help').hide();
							$('#share-zone-help-text').fadeIn(250);
							return false;
							});

	$('#close-share-help').bind('click',function(){ 
							$('#share-zone-help-text').slideUp(500);
							$('#share-zone-help').fadeIn(250);
							return false;
							});
		
	initContentRating();
}

var setContentRating=function(rating){
	$("input[@value='"+rating+"']").attr('checked','checked');
}
		
var saveContentRating=function(ratingUrl,pageRating){		
	$.ajax({
		dataType:	'html',
		url:		'/lib/wbc/sendRating.asp',
		data:		{
					url:	ratingUrl,
					rating:	pageRating,
					ajaxInit:	'true', 	// tell script it was called from ajax request not form
					dummy: new Date().getTime() // avoid IE cache effect
				},
		cache:		false,
		type:		'get',
		success:	function(newAvgRating){
					$('.star').remove();

					$('#rating-form-new').append('<input title="Very Poor" class="auto-submit-star" type="radio" name="rating" value="1"/>');
					$('#rating-form-new').append('<input title="Poor" class="auto-submit-star" type="radio" name="rating" value="2"/>');
					$('#rating-form-new').append('<input title="Average" class="auto-submit-star" type="radio" name="rating" value="3"/>');
					$('#rating-form-new').append('<input title="Good" class="auto-submit-star" type="radio" name="rating" value="4"/>');
					$('#rating-form-new').append('<input title="Very Good" class="auto-submit-star" type="radio" name="rating" value="5"/>');
					setContentRating(newAvgRating);
					$('.auto-submit-star').rating({
						readOnly:	true
					});
					
					$('#rating-form-label').hide().text('Thanks!').fadeIn(1000);
					
				},
		error:		function(){
					alert('Error saving your rating and calculating the public\'s new average page rating');
				}
	});
}
		
var initContentRating=function(){
	// Obtain page's current rating and display

	var docUrl=document.location.href.replace('http://www.worthing.gov.uk','');
	docUrl=docUrl.split('?')[0];
	docUrl=docUrl.split('#')[0];
	
	$('#rating-form').css({float:	'right'});
	$.ajax({
		dataType:	'html',
		url:		'/lib/wbc/getCurrentRating.asp',
		data:		{
					url:	docUrl,
					dummy: new Date().getTime() // avoid cache effect in IE
				},
		type:		'get',
		success:	function(avgRating){	
					setContentRating(avgRating);
					$('.rating-score-label').remove();
					$('#rating-form-submit').remove();
					$('.auto-submit-star').rating({
						required:true,
						callback: function(value, link){
							// Get chosen value and submit to server with current URL
							docUrl=document.location.href.replace('http://www.worthing.gov.uk','');
							docUrl=docUrl.split('?')[0];
							docUrl=docUrl.split('#')[0];
							saveContentRating(docUrl,value);
						}
					});			
				},
		error:		function(){
					//alert('Error retrieving this page\'s current rating');
				}
	});
}