﻿var base_url = 'http://filmyonline.net.pl/';
function Comment(id, type) {
	var req = mint.Request();
	var comment = $("comment").value;
	var div = document.getElementById('content-msg');
	req.method = 'POST';
	req.retryNum = '2';
	req.AddParam("comment", comment); 
	req.AddParam("type", type);
	div.style.display = "";

	if(comment == '') { 
		div.innerHTML = '<span style="color: #ffff00;">Podaj treść komentarza.</span>';
	} else {
		$("import").disabled = true; 

		req.OnSuccess = function() {  
			if(req.responseText == 'Komentarz został dodany poprawnie.') {
				div.innerHTML = '<span style="color: #00ff40;">' + req.responseText + '</span>';
			} else {
				div.innerHTML = '<span style="color: #ffff00;">' + req.responseText + '</span>';
			}
			$("import").disabled = false; 
		}
		
		req.Send(base_url + 'dodaj-komentarz/' + id + '/' + type);
	}
}
function GetFDB() {
	var req = mint.Request();
	var url = $("url").value;
	req.method = 'POST';
	req.retryNum = '2';
	req.AddParam("url", url); 

	if(url == '') { 
		alert('Proszę podać tytuł filmu.'); 
	} else {
		$("import").disabled = true; 

		req.onLoading = $("content-msg").style.display = "";
		req.onLoading = $("content-msg").innerHTML = 'Szukanie filmu...';

		req.OnSuccess = function() {  
		
			if(this.responseJSON[0].title == '') {
				req.onLoading = document.getElementById("content-msg").innerHTML = '<span style="color: #fff;">Proszę podać tytuł w innej formie.</span>';
			} else {
				req.onLoading = document.getElementById("content-msg").innerHTML = '<span style="color: #fff;">Dane filmu zostały wczytane do formularza.</span><br /><strong>' +  this.responseJSON[0].title + '</strong><br /><strong>' + this.responseJSON[3].category + '</strong><br /><img src="' + this.responseJSON[2].poster + '" alt="' +  this.responseJSON[0].title + '" title="' +  this.responseJSON[0].title + '"/>'; 
				$("title").value = this.responseJSON[0].title;
				$("description").value =  this.responseJSON[1].description;
				$("poster").value =  this.responseJSON[2].poster;
				$("category").value =  this.responseJSON[4].categorya;
	
			}
			$("import").disabled = false; 
		}
		
		req.getJSON = true;
		req.Send(base_url + 'fdb');
	}
}
function vote(rating, id, type)	{
		var req = mint.Request();
		var votes = $("yes-c").innerHTML * 1;
		var votes2 = $("no-c").innerHTML * 1;
				
		req.OnSuccess = function() {  
			if(req.responseText == 'vote ok' ) {
				if(rating == 2) {
					$("yes-c").innerHTML = votes + 1;
				} else {
					$("no-c").innerHTML = votes2 + 1;
				}
			}
			if(req.responseText == 'voted' ) {
				alert('Już głosowałeś.');
			}
			if(req.responseText == 'no logged' ) {
				alert('Proszę się zalogować.');
			}
			if(req.responseText == 'error' ) {
				alert('Wystąpił błąd.');
			}
		} 
		req.Send(base_url + 'ocena/' + rating + '/' + id + '/' + type);
}
function report(id, type)	{
		var req = mint.Request();
		
		req.OnSuccess = function() {  
			if(req.responseText == 'report ok' ) {
				alert('Zgłoszenie zostało wysłane.');
			}
			if(req.responseText == 'reported' ) {
				alert('Ten film został już zgłoszony.');
			}
			if(req.responseText == 'no logged' ) {
				alert('Proszę się zalogować.');
			}
			if(req.responseText == 'error' ) {
				alert('Wystąpił błąd.');
			}
		} 
		req.Send(base_url + 'zgloszenie/' + '/' + id + '/' + type);
}
function count(max, what, where) {
	var what = document.getElementById(what);
	var where = document.getElementById(where);
	where.innerHTML = (max - what.value.length);
	if(max - what.value.length < 0) {
		alert('Twoja wiadomość jest za długa. Maksymlanie: ' + max + ' znaków')
	}
}
function favourite(id)	{
		var req = mint.Request();
		var div = document.getElementById('favourite-' + id);

		req.OnSuccess = function() {  
			if(req.responseText == 'ok' ) {
				div.innerHTML = "<a onclick='favourite(" + id + "); return false' title='Usuń z  ulubionych'><img src='" + base_url + "img/rstar.png' /></a>";
			}
			if(req.responseText == 'no' ) {
				div.innerHTML = "<a onclick='favourite(" + id + "); return false' title='Dodaj do ulubionych'><img src='" + base_url + "img/astar.png' /></a>";
			}
		} 
		req.Send(base_url + 'ulubione/' + id);
}

function change(content, id) {
	$(id).value = content;
}
function change2(id, content) {
	$(id).value = $(id).value + ' ' + content;
}
