$(document).ready(function(){

	$('#itemContent').expandable();

	// Update likers
	$('.iLikeIt').live("click", function(){

		var art = this['value'];

		$.post('/inc/comments/functions.php', { art: art, f: 'iLikeItUpdate' },  function (data){

			var dataSplitResult = data.split(':');
			
			if (dataSplitResult[0] == 'notLoggedIn') {
				notice(dataSplitResult[1],'red',4);
			} else {
				document.getElementById(dataSplitResult[2]).innerHTML=dataSplitResult[0];
				document.getElementById(dataSplitResult[3]).innerHTML=dataSplitResult[4];
				notice(dataSplitResult[1],'green',2);
				
					if(dataSplitResult[5] == 0 && dataSplitResult[6] == 'usersLikeThisBox') {
						$('.usersLikeThis').hide();
					} else {
						$('.usersLikeThis').fadeIn('slow');
					}
			}
		});
	});
	
	
	// Delet comment post
	$('.deletComment').live("click", function(){
		
		var art = this['value'];
		//jAlert('Ok it is Fine: ' + r, 'Results');
		
		var alertHeader = document.getElementById("commentAlertHeader").value;
		var alertDelet = document.getElementById("commentAlertDelet").value;
		
		jConfirm(alertDelet, alertHeader, function(check) {
		
			if (check == true) {
			
				$.post('/inc/comments/functions.php', { art: art, f: 'deletComment' },  function (data){
				
					// (true/false, noticeMsg, noticeColor)
					var dataSplitResult = data.split(':');
					
					if (dataSplitResult[0] == 'true') {
						$('#commentContentBox_' + art).hide();
						notice(dataSplitResult[1],dataSplitResult[2],4);
					} else {
						notice(dataSplitResult[1],dataSplitResult[2],4);
					}
				});
				
			}
			
		});
		
	});
	
	
	// New comment post
	$('.commentSubmit').live("click", function(){   

		var c = document.getElementById("itemContent").value;
		var b = document.getElementById("basicValue").value;
		
		if (c != '' && c != b) {
		
			var i 			= document.getElementById("itemId").value;
			var t 			= document.getElementById("itemType").value;
			
			$.post('/inc/comments/functions.php', { c: c, i: i, t: t, f: 'newComment' },  function (data){
				
				// (true/false, noticeMsg, noticeColor)
				var dataSplitResult = data.split(':');
				
				if (dataSplitResult[0] == 'true') {
				
					// Set old value
					document.getElementById("itemContent").value = b;
				
					$.post('/templates/comments/comments.php', { i: i, t: t, f: 'reViewComments' },  function (data2){

						document.getElementById('commentsX2').innerHTML = data2;	
						
					});
					
					notice(dataSplitResult[1],dataSplitResult[2],4);	
				}
			});
		
		} else {
			var errorMsg = document.getElementById("errorMsg1").value;		

			notice(errorMsg,"red",4);
		}

	});
	
});

