﻿// -----------------------------------
// Display instant notifications (without reloading the page)
var platformNotificationTimeout = null;

var showPlatformNotification = function(msg, error) 
{
	clearTimeout(platformNotificationTimeout);
	platformNotificationTimeout = null;

	$('#feedback:animated').stop();
	if (error)
	{
		$('#feedback .inner').html(
			"<p><a href=\"#\" style=\"color:red;\" onclick=\"$('#feedback').fadeOut(200)\">" + msg + "</a></p>"
		);
	}
	else
	{
		$('#feedback .inner').html("<p>" + msg + "</p>" );
		platformNotificationTimeout = setTimeout(function() {
			$('#feedback').fadeOut(200);
		}, 1000);
	}

	$('#feedback').fadeIn(200);
};

// -----------------------------------
// Display instant required action (without reloading the page)
var showPlatformRequiredAction = function(title, msg, actionUrl, actionText)
{
	var popupHtml = '';
	popupHtml += '<div id="promo-popup" class="fancybox-popup">';
	popupHtml += '	<h1><span>'+title+'</span></h1>';
	popupHtml += '	<div class="popup-content">';
	popupHtml += '		<p>'+msg+'</p>';
	if (actionUrl)
	{	popupHtml += '		<p><a href="' + actionUrl + '" class="button"><span>' + actionText + '</span></a></p>';	}
	popupHtml += '	</div>';
	popupHtml += '</div>';

	jQuery.fancybox(
		popupHtml,
		{
			width: 560, height: 340, padding:0,
			overlayOpacity: 0.85, opacity: true, overlayColor: '#000',
			showCloseButton: true
		}
	);
}

// Sve Funkcije koje rade sa servisima platforme.
/*
function FavoritesAdd(id) {
	var successCallback = function(result) {
		if (result) {
			showPlatformNotification("Uspješno je dodano u favorite!", false);
		}
	};

	try
	{	MaxTvShowUserFavoriteAddNew(id, successCallback);	}
	catch(e)
	{	showPlatformNotification("Greška prilikom dodavanja u favorite!", true);	}
	return false;
}
*/
function FavoritesAdd(id, li) {
	var successCallback = function(result) {
		if (result) {
			if (typeof li != 'undefined') jQuery(li).addClass('favorite');
			showPlatformNotification("Uspješno je dodano u favorite!", false);

		}
	};

	try
	{ MaxTvShowUserFavoriteAddNew(id, successCallback); }
	catch (e)
	{ showPlatformNotification("Greška prilikom dodavanja u favorite!", true); }
	return false;
}

function FavoritesRemove(id, tr) {
	var successCallback = function(result) {
	if (result) {
			tr.remove();
			showPlatformNotification("Uklonjeno iz favorita", false);
		}
	};

	try
	{	MaxTvShowUserFavoriteRemove(id, successCallback);	}
	catch(e)
	{	showPlatformNotification("Greška prilikom uklanjanja iz favorita", true);	}
	return false;
}

/*
function Record(epgChannelId, progRefNum, dateStart) {
	var successCallback = function(result) {
		if (result == 1) {
			showPlatformNotification("Aktivirano snimanje emisije!", false);
		} else {
			showPlatformNotification("Greška prilikom aktiviranja snimanja!", true);
		}
	};
	
	try
	{	MaxTvEpgUserRecordingRecord(epgChannelId, progRefNum, dateStart, successCallback);	}
	catch(e)
	{	showPlatformNotification("Greška prilikom aktiviranja snimanja!", true);	}
	return false;
}
*/
function Record(epgChannelId, progRefNum, dateStart, li) {
	var successCallback = function(result) {
	if (result == 1) {
			if (typeof li != 'undefined') jQuery(li).addClass('recording');
			showPlatformNotification("Aktivirano snimanje emisije!", false);
		} else {
			showPlatformNotification("Greška prilikom aktiviranja snimanja!", true);
		}
	};

	try
	{ MaxTvEpgUserRecordingRecord(epgChannelId, progRefNum, dateStart, successCallback); }
	catch (e)
	{ showPlatformNotification("Greška prilikom aktiviranja snimanja!", true); }
	return false;
}

function RecordingDelete(progRefNum, li) {
	var deleteSuccess = function(result) {
		if (result == 1) {
			li.remove();
			showPlatformNotification("Emisija obrisana!", false);
		} else {
			showPlatformNotification("Greška prilikom brisanja emisije!", true);
		}
	};

	try
	{ MaxTvEpgUserRecordingDelete(progRefNum, deleteSuccess, null); }
	catch (e)
	{ showPlatformNotification("Greška prilikom brisanja emisije!", true); }
	return false;
}

/*
function RecordSeries(seriesId) {
	var successCallback = function(result) {
		if (result == 1) {
			showPlatformNotification("Aktivirano snimanje serijala!", false);
		} else {
			showPlatformNotification("Greška prilikom aktiviranja snimanja serijala!", true);
		}
	};

	try
	{	MaxTvEpgUserRecordingRecordSeries(seriesId, successCallback);	}
	catch(e)
	{	showPlatformNotification("Greška prilikom aktiviranja snimanja serijala!", true);	}
	return false;
}
*/
function RecordSeries(seriesId, li) {
	var successCallback = function(result) {
	if (result == 1) {
			if (typeof li != 'undefined') jQuery(li).addClass('recording-season');
			showPlatformNotification("Aktivirano snimanje serijala!", false);
		} else {
			showPlatformNotification("Greška prilikom aktiviranja snimanja serijala!", true);
		}
	};

	try
	{ MaxTvEpgUserRecordingRecordSeries(seriesId, successCallback); }
	catch (e)
	{ showPlatformNotification("Greška prilikom aktiviranja snimanja serijala!", true); }
	return false;
}

function RecordingSeriesDelete(seriesId, li) {
	var deleteSuccess = function(result) {
		if (result == 1) {
			li.remove();
			showPlatformNotification("Serija obrisana!", false);
		} else {
			showPlatformNotification("Greška prilikom brisanja serije!", true);
		}
	};

	try
	{ MaxTvEpgUserRecordingDeleteSeries(seriesId, deleteSuccess, null); }
	catch (e)
	{ showPlatformNotification("Greška prilikom brisanja serije!", true); }
	return false;
}

function RecordingShowMore(moreId) {
	jQuery("#more" + moreId).show("fast");
}


/*
Recomendation API functions 
Requires:
/ModuleResources/SnT.Utils.js (for web service ajax calls)
/App_Modules__T-Com.maxtvCms.Modules__TCom.maxtvCms.Modules.Recomendation.RecService.asmx
*/

var MAXtvRecomendation = {};
MAXtvRecomendation.ServiceUrl = '/App_Modules__T-Com.maxtvCms.Modules__TCom.maxtvCms.Modules.Recomendation.RecService.asmx';

MAXtvRecomendation.NotifyActivate = function(userPageUrl) {
	if (document.location.pathname == userPageUrl)
	{ return; }

	var msgHtml = '';
	msgHtml += '<div id="promo-popup" class="fancybox-popup">';
	msgHtml += '<h1><span>MAXtv preporuke</span></h1>';
	msgHtml += '<div class="popup-content">';
	msgHtml += '<p>Nova potpuno besplatna usluga preporuke je dostupna na MAXtv-u.</br>'
	msgHtml += 'Molimo vas odaberite želite li koristiti u uslugu.</p>';
	msgHtml += '<p>';
	msgHtml += '<a href="#" class="button" onclick="MAXtvRecomendation.AllowRecomendation(true);jQuery.fancybox.close()"><span>Da želim</span></a>';
	msgHtml += '&nbsp;&nbsp;&nbsp;';
	msgHtml += '<a href="#" class="button" onclick="MAXtvRecomendation.AllowRecomendation(false);jQuery.fancybox.close()"><span>Ne, ne želim</span></a>';
	msgHtml += '<p>';
	msgHtml += '</div>';
	msgHtml += '</div>';

	jQuery.fancybox(
		msgHtml,
		{
			width: 560, height: 340, padding: 0,
			overlayOpacity: 0.85, opacity: true, overlayColor: '#000',
			showCloseButton: true
		}
	);
}

MAXtvRecomendation.AllowRecomendation = function(allowRec, onSuccess) {

	var msgError = "Nemoguće postaviti dozvolu za preporuke!";

	// Just reload page on success
	var success = function(result) {
		if (result)
		{
			var msgSuccess = 'MAXtv preporuke ' + (allowRec ? 'aktivirane.' : 'isključene.');
			showPlatformNotification(msgSuccess, false)
			//document.location = document.location;
			if(typeof(onSuccess) != 'undefined')
			{	onSuccess(result);	}
		}
		else
		{	showPlatformNotification(msgError, true)	}
	};

	try
	{
		SnTUtils.Ajax(
			this.ServiceUrl + "/AllowRecomendation",
			{ allow: allowRec },
			success,null
		);
	}
	catch (e)
	{	showPlatformNotification(msgError, true);	}
}

MAXtvRecomendation.GradeSet = function(assetId, grade, onSuccess) {

	var success = function(result) {
		//alert(result);
		if (typeof (onSuccess) != 'undefined')
		{ onSuccess(result); }
	};

	try {
		SnTUtils.Ajax(
			this.ServiceUrl + "/GradeSet",
			{ contentItemId: assetId, newGrade: grade },
			success, null
		);
	}
	catch (e)
	{ showPlatformNotification("Greška prilikom postavljanja ocjene!", true); }
}

MAXtvRecomendation.GradeGet = function(assetId, onSuccess) {

	var success = function(result) {
		//alert(result);
		if (typeof (onSuccess) != 'undefined')
		{ onSuccess(result); }
	};

	try {
		SnTUtils.Ajax(
			this.ServiceUrl + "/GradeGet",
			{ contentItemId: assetId },
			success, null
		);
	}
	catch (e)
	{ showPlatformNotification("Greška prilikom dohvata ocjene!", true); }
}

