//functions for the resource ratings page
//needs isLoggedIn to be set

$(function(){

	$('.ratingBox').show();
	
	$('.ratingBox').mouseover(function(){
		var myvote = parseInt($(this).attr('myvote'));	//Grab the current vote out
		if (myvote == 0) {
			var ratingid = $(this).attr('ratingid');	//Grab the id out
			var vote = parseInt($(this).attr('vote'));	//Grab the current vote out

			var strRate = '';
			strRate += '<input class="ratestar" type="radio" name="rtn' + ratingid + '" value="1" title="Poor" ' + (vote > 0 ? 'checked="checked"' : '') + ' />';
			strRate += '<input class="ratestar" type="radio" name="rtn' + ratingid + '" value="2" title="Useful" ' + (vote > 1 ? 'checked="checked"' : '') + ' />';
			strRate += '<input class="ratestar" type="radio" name="rtn' + ratingid + '" value="3" title="OK" ' + (vote > 2 ? 'checked="checked"' : '') + ' />';
			strRate += '<input class="ratestar" type="radio" name="rtn' + ratingid + '" value="4" title="Good" ' + (vote > 3 ? 'checked="checked"' : '') + ' />';
			strRate += '<input class="ratestar" type="radio" name="rtn' + ratingid + '" value="5" title="Great" ' + (vote > 4 ? 'checked="checked"' : '') + ' />';
		
			$(this).html(strRate);
			starize();
			$(this).unbind();
		}
	});
	
});

function starize() {
	$('.ratestar').rating({
		callback: function(value, link){
			// 'this' is the hidden form element holding the current value
			// 'value' is the value selected
			// 'element' points to the link element that received the click.
			//alert("The value selected was '" + value + "'\n\nWith this callback function I can automatically submit the form with this code:\nthis.form.submit();");
			
			if (isLoggedIn) {
			
				// make stars readonly!
				$(this).parent().addClass('ratingSetReadonly').find('div').addClass('star_readonly').removeClass('star_live').unbind('mouseover').unbind('mouseout').unbind('click');

				var rateid = $(this).attr('name');
				var url;
				if (location.href.indexOf('/resources/') > 0 ) {
					url = 'resourceRate.aspx';
				} else {
					url = 'resources/resourceRate.aspx';
				}
				$.post(url, { index: rateid.substring(3), rating: value },
					function(data){
						if (data == 'true') { 
							$('#v' + rateid).text( parseInt($('#v' + rateid).text()) + 1);	//update vote count
						} else {
							alert('There was a problem with your vote, please check you are logged in');
						}
					});

			} else {
				doLoginPop(true, 'To place a vote you must log in first');
			}
		}
	});
}

function doGetResourceInfo(strURL, strName, strDescrip, strData) {
	$('#' + strData).text('Getting Info...');
	$.getJSON('resources/resourceAddAuto.aspx?url=' + escape($('#' + strURL).val()),
		function(data){
			$('#' + strName).val(data.title.substring(0,42));
			$('#' + strDescrip).val(data.description.substring(0,80));
			$('#' + strData).text('');
		});
}

//Resource Dialog
function doResourceAdd(strDialog) { 
	var dlg = $('#' + strDialog).dialog({ 
		modal: true,
		title: 'Add a Resource',
		width: 625,
		height: 225,
		resizable: true,
		overlay: { opacity: 0.4, background: '#4A5967'}
	});
	dlg.parent().appendTo($('form:first'));
}