$(document).ready(function() {
	if(arrContentId != null)
	{
		for(i=0;i<arrContentId.length;i++){
			setRatingControl(arrContentId[i]);
		}
	}
	
	$("#dlgFullContent").dialog({
			bgiframe: false,
			modal: true,
			autoOpen:false,
			width:800
		});
	getOtherNews(false);
});


function setRatingControl(content_id){
	$('#rateContainer_'+content_id+' input').rating({ 
			readOnly:true
		});
	initTooltipAll(content_id);
}

function setRatingControlViewFull(content_id){
	$('#rate_container_average input').rating({readOnly:true});
}

function displayFullContent(content_id, category_id){
	if(category_id){
	$('#dlgFullContentContainer').load(LIVING_SITE_URL + "contents/displayFullContent/"+content_id + "/" + category_id + "?time=" + (new Date).getMilliseconds(), function(){
		
		setRatingControlViewFull(content_id);
		$("#dlgFullContent").dialog('open');
		$('#lblContentTitle').html($('#txtContentTitle').val());
		initTooltip(content_id);
	});
	}
	else 
	{
		$('#dlgFullContentContainer').load(LIVING_SITE_URL + "contents/displayFullContent/"+content_id + "?time=" + (new Date).getMilliseconds(), function(){
		setRatingControlViewFull(content_id);
		$("#dlgFullContent").dialog('open');
		$('#lblContentTitle').html($('#txtContentTitle').val());
		initTooltip(content_id);
		});
	}
}

function doSearchContent(search_value, category_id){
	$.post(LIVING_SITE_URL + 'contents/searchContent/' + category_id + "?time=" + (new Date).getMilliseconds() , {search_value:search_value}, function(data){
	
		$('#dataContainer').html(data);
		if(arrContentId != null){
		for(i=0;i<arrContentId.length;i++){
			setRatingControl(arrContentId[i]);
			}
		}
	});
}

function initTooltip(contentId){
	$('a.tips_content').cluetip({
	  cluetipClass: 'jtip', 
	  arrows: true, 
	  ajaxCache:false,
	  dropShadow: false,
	  hoverIntent: false,
	  sticky: true,
	  mouseOutClose: true,
	  closePosition: 'title',
	  closeText: '<img src="'+LIVING_SITE_URL+'img/cross.png" alt="close" />',
	  onShow : function(){
	  		$('#rateContainer' + ' input').rating({ 
			callback: function(value, link){ 
				//Save rate value
				$.post(LIVING_SITE_URL + 'contents/saveRate/' + contentId + "?second=" + (new Date).getMilliseconds(), {value:value});
				}
			});
	  	}
	});
}

function initTooltipAll(contentId){
	$('a.tips_news_'+contentId).cluetip({
	  cluetipClass: 'jtip', 
	  arrows: true, 
	  ajaxCache:false,
	  dropShadow: false,
	  hoverIntent: false,
	  sticky: true,
	  mouseOutClose: true,
	  closePosition: 'title',
	  closeText: '<img src="'+LIVING_SITE_URL+'img/cross.png" alt="close" />',
	  onShow : function(){
	  		$('#rateContainer' + ' input').rating({ 
			callback: function(value, link){ 
				//Save rate value
				$.post(LIVING_SITE_URL + 'contents/saveRate/' + contentId + "?second=" + (new Date).getMilliseconds(), {value:value});
				}
			});
	  	}
	});
}

function getOtherNews(url){
	if(url){
		$('#other_news_container').load(url);
	}
	else {
		var last_id = $('#last_id').val();
		category_id = $('#text_category_id').val();
		$('#other_news_container').load(LIVING_SITE_URL + 'contents/displayOtherNews/' + last_id + '/' + category_id + "?second=" + (new Date).getMilliseconds());
	}
}
