$(document).ready(function(){
	$(".download-link").click( function () { getDlg( $(this).attr("id") ) } );
	$("#id_country").change( function () { init_opers( $(this).val() ) } );
	
	$("#smsform").dialog({
		autoOpen: false,
		position: ["center","center"],
		modal: true,
		width: 790,
		buttons: { 'Закрыть': function() { $(this).dialog('close'); } },
		close: function() { }
	});
	
	$("#freeload").dialog({
		autoOpen: false,
		position: ["center","center"],
		modal: true,
		width: 500,
		buttons: { 'Закрыть': function() { $(this).dialog('close'); } },
		close: function() { }
	});
	
	$("#smsload").dialog({
		autoOpen: false,
		position: ["center","center"],
		modal: true,
		width: 600,
		buttons: { 'Закрыть': function() { $(this).dialog('close'); } },
		close: function() { }
	});
	
	$("#onlinepayload").dialog({
		autoOpen: false,
		position: ["center","center"],
		modal: true,
		width: 600,
		buttons: { 'Закрыть': function() { $(this).dialog('close'); } },
		close: function() { }
	});

	
	
	
	
	/* проверяем на возможность ставить рейтинг */
	if ( progRating ) {
		if ( $.cookie('rat-prog-' + idItem) ) {
			$("#ratingTip").remove();
			$("#unit-rating").html('<li class="current-rating" style="width:' + progRating + 'px"></li>');
		}
	}
	if ( dllRating ) {
		if ( $.cookie('rat-dll-' + idItem) ) {
			$("#ratingTip").remove();
			$("#unit-rating").html('<li class="current-rating" style="width:' + dllRating + 'px"></li>');
		}
	}
	if ( drvRating ) {
		if ( $.cookie('rat-drv-' + idItem) ) {
			$("#ratingTip").remove();
			$("#unit-rating").html('<li class="current-rating" style="width:' + drvRating + 'px"></li>');
		}
	}
	if ( newsRating ) {
		if ( $.cookie('rat-news-' + idItem) ) {
			$("#ratingTip").remove();
			$("#unit-rating").html('<li class="current-rating" style="width:' + newsRating + 'px"></li>');
		}
	}
	
	
	function checkCommentForm(){
		var errors=0;
		if ( ! $("#acf_title").val().length > 0 ) { $("#messtitle").text('Обязательно для заполнения'); errors++; }
		if ( ! $("#acf_comment").val().length > 0 ) { $("#messcomment").text('Обязательно для заполнения'); errors++; }
		if ( ! registeredUser ) {
			if ( ! $("#acf_name").val().length > 0 ) { $("#messname").text('Обязательно для заполнения'); errors++; }
			if ( ! $("#acf_email").val().length > 0 ) { $("#messemail").text('Обязательно для заполнения'); errors++; }
		}
		return ( errors==0 ) ? true : false ;
	}
	
	$("#addCommentForm").dialog({
		autoOpen: false,
	    position: ["center","center"],
	    modal: true,
	    width: 600,
		buttons: {
			'Готово': function() {
				if ( checkCommentForm() ) {
					switch ( typeContent ) {
						case 'program': url = "/ajax/process.php?module=catalog&action=addComment"; break; 
						case 'dll': url = "/ajax/process.php?module=dll&action=addComment"; break; 
						case 'driver': url = "/ajax/process.php?module=driver&action=addComment"; break; 
						case 'news': url = "/ajax/process.php?module=news&action=addComment"; break; 
					}
					$.post(url, 
						{ 
							id_item:idItem,
							id_account:registeredUser,
							name:$("#acf_name").val(),
							email:$("#acf_email").val(),
							title:$("#acf_title").val(),
							comment:$("#acf_comment").val(),
							rating:$("#acf_rating").val()
						},
						function( data ){ 
							$("#addCommentForm").dialog('close');
							if ( data.status ) {
								$("#addCommentFormResult").text('Ваш отзыв принят. В ближайшее время он будет рассмотрен модератором и опубликован. Большое спасибо.');
							} else {
								$("#addCommentFormResult").text('Сохранить Ваш отзыв не удалось, т.к. возникла ошибка. Попробуйте повторить попытку чуть позже. Спасибо за понимание.');
							}
							$("#addCommentFormResult").dialog('open');
						}, "json");
				}
			},
			'Отмена': function() { $(this).dialog('close'); }
		},
		close:  
			function() {
				$("#messtitle").text('');
				$("#messcomment").text('');
				$("#messname").text('');
				$("#messemail").text('');
				$("#acf_name").val('');
				$("#acf_email").val('');
				$("#acf_title").val('');
				$("#acf_comment").val('');
			}
	});
	
	$("#addCommentFormResult").dialog({
		autoOpen: false,
		position: ["center","center"],
		modal: true,
		width: 300,
		buttons: { 'Закрыть': function() { $(this).dialog('close'); } },
		close: function() { $("#addCommentFormResult").text(''); }
	});
	
	$('#btnAddCommentForm').click(function(){ $("#addCommentForm").dialog('open') });
	
	
	/* выставление рейтинга */
	$("#btnRating1").click( function(){ setRating( idItem, 1 ); } );
	$("#btnRating2").click( function(){ setRating( idItem, 2 ); } );
	$("#btnRating3").click( function(){ setRating( idItem, 3 ); } );
	$("#btnRating4").click( function(){ setRating( idItem, 4 ); } );
	$("#btnRating5").click( function(){ setRating( idItem, 5 ); } );
	
	function setRating( ID, ratingStar ) {
		switch ( typeContent ) {
			case 'program': url = "/ajax/process.php?module=catalog&action=setRating"; break; 
			case 'dll': url = "/ajax/process.php?module=dll&action=setRating"; break; 
			case 'driver': url = "/ajax/process.php?module=drivers&action=setRating"; break; 
			case 'news': url = "/ajax/process.php?module=news&action=setRating"; break; 
		}
		$.post(url, 
			{ 
				id_item:ID,
				rating:ratingStar
			},
			function( data ){
				if ( data.status ) { 
					$("#addCommentFormResult").text('Ваша оценка учтена. Большое спасибо.');
					$("#ratingTip").remove();
					$("#unit-rating").html('<li class="current-rating" style="width:' + data.rating + 'px"></li>');
					switch ( typeContent ) {
						case 'program': $.cookie('rat-prog-' + ID, "1", { expires: 1, path: "/" }); break; 
						case 'dll': $.cookie('rat-dll-' + ID, "1", { expires: 1, path: "/" }); break; 
						case 'driver': $.cookie('rat-drv-' + ID, "1", { expires: 1, path: "/" }); break; 
						case 'news': $.cookie('rat-news-' + ID, "1", { expires: 1, path: "/" }); break; 
					}
				} else {
					$("#addCommentFormResult").text('Сохранить Вашу оценку не удалось, т.к. возникла ошибка. Попробуйте повторить попытку чуть позже. Спасибо за понимание.');
				}
				$("#addCommentFormResult").dialog('open');
			}, "json");
	}
	
});
