var show_sendPage = function() {
	$('#sendPage').dialog('open'); return false;
};

var closeWindow = function() {
	$('#sendPage').dialog('close'); 
	document.getElementById('sendform').reset();
};

$(document).ready(function() {
	$("#sendPage").dialog({modal:true, autoOpen:false, height:370, width:550, resizable: false});
	$("#send_message").dialog({modal:true, autoOpen:false, buttons:{ "Ok": function() {$(this).dialog("close");}}});
	$("#rating_message").dialog({modal:true, autoOpen:false, buttons:{ "Ok": function() {$(this).dialog("close");}}});
});

function sendPage(messageTitle, errorMessageTitle)
{		
	$.post("/article/sendPage", $("#sendform").serialize(), 
		function(data) {
			var code = data.substr(0,1);
			var info = data.substr(1);
			switch (code) {
				case '1':
					$("#send_message").html(info);
					$("#send_message").dialog('open');
					$("#send_message").data('title.dialog', errorMessageTitle);
					break;
				default:
					closeWindow();
					$("#send_message").html(info);
					$("#send_message").dialog('open');
					$("#send_message").data('title.dialog', messageTitle);
					break;
			}
		}
	);
} 

//used for print article 
function print_open (url,block,pid) {
	//TO DO - generalize with 'block' parameter
    //window.open(url + 'print/'+block+'/'+pid, 'Print', 'status=0,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1,directories=0,height=600,width=660');
	window.open(url + 'print/'+pid+'/', 'Print', 'status=0,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1,directories=0,height=600,width=660');
}

//used for article rating
$(document).ready(function()
{	/*{
		$('.auto-submit-star').rating({
			callback:function(value,link){
				itemId=$('input#rate_item_id').val();
				$.getJSON('/article/rateItem/'+itemId+'/'+value,
					function(json){$('#article_rating span.comments').html(json.stars);
					$('#article_rating_response_message').html(json.message);
						    });
						},
					required:false});
	}
*/
	

	if ($('.auto-submit-star').is('*'))
	{
		$('.auto-submit-star').rating({ 
			callback: function(value,link){		
				itemId = $('input#rate_item_id').val(); 		
				
				$.ajax({
					type: 'POST',
					url: '/article/rateItem/'+itemId+'/'+value+'/',
					success: function(msg) {
						data = $.parseJSON(msg);
						$('#article_rating span.comments').html(data.stars);
						switch (data.code) {
							case '1':
								$("#rating_message").html(data.message);
								$("#rating_message").dialog('open');
								$("#rating_message").data('title.dialog', data.title);
								break;
							default:
								$("#rating_message").html(data.message);
								$("#rating_message").dialog('open');
								break;
						}
					}
				});
			},
			required: false
		});
	}
});

