var errRefresh = "Wystąpił błąd\n\n\Odśwież stronę i spróbuj ponownie";
var aSchedule = new Array();

function addPost( sTitle, sText ) {
	if( sTitle.length > 0 && sText.length > 0 ) {
		$.ajax({
			type:'POST',
			url:'/profile-post/',
			data:{title:sTitle,text:sText},
			success:function(txt) {
				switch(txt)
				{
					case 'ok':
						sMsg = '<li><strong>' + sTitle + '</strong><span>10 sekund temu</span>' + sText + '</li>';
						$( sMsg ).insertBefore( $( '#news ul li:first' ) ).hide().show( 1000 );
						break;
					case 'not_logged_in':
						alert( 'Aby dodać notkę, musisz być zalogowany' );
						break;
					default:
						alert( errRefresh );
						break;
				}
			}
		});
	} else {
		alert( 'Musisz podać tytuł i treść wiadomości' );
	}
}

function abuse( iChannelId, iReasonId, sEmail, sText ) {
	if( parseInt( iChannelId ) > 0 && parseInt( iReasonId ) > 0 && sEmail.length > 0 && sText.length > 0 ) {
		$.ajax({
			type:'POST',
			url:'/channel-abuse/',
			data:{id:iChannelId,id_reason:iReasonId, email:sEmail,text:sText},
			success:function(txt) {
				switch(txt)
				{
					case 'ok':
						$( '#abusive' ).text( 'Dziękujemy! Twoje zgłoszenie zostało wysłane' );
						break;
					default:
						alert( errRefresh );
						break;
				}
			}
		});
	} else {
		alert( 'Musisz podać swój email i treść wiadomości' );
	}
}

function refreshSchedule( iVideoId, iTimeout, iChannelId ) {
	setTimeout( 'getActualVideo(' + iChannelId + ');', iTimeout * 1000 );
}

function reloadSchedule( iChannelId ) {
	$.ajax({
		type:'GET',
		url:'/channel-schedule/',
		data:{ id:iChannelId },
		dataType:'html',
		success:function( html ) {
			$( '#schedule' ).html( html );
		}
	});
}
function setActualVideo( iVideoId, iChannelId ){
	
	$( 'table.schedule tr' ).removeClass( 'current' );
	if( $( '#video' + iVideoId ).length ){
		$( '#video' + iVideoId ).addClass( 'current' );	
	}
	else
	{
		reloadSchedule( iChannelId );
	}
	
}
function getActualVideo( iChannelId ) {
	$.ajax({
		type:'GET',
		url:'/video-actual/',
		data:{ id:iChannelId },
		dataType:'json',
		success:function( video ) {
			$( 'table.schedule tr' ).removeClass( 'current' );
			$( '#video' + video.id ).addClass( 'current' );
			refreshSchedule( video.id, video.time_left, iChannelId );
		}
	});
}

function createEvent() {
	iChannelId = $( '#channels_id' ).val();
	sTitle = $( '#title' ).val();
	sDesc = $( '#description' ).val();
	sStartDate = $( 'select.year' ).val() + '-' + $( 'select.month' ).val() + '-' + $( 'select.day' ).val() + ' ' + $( 'select[name=Time_Hour]' ).val() + ':' + $( 'select[name=Time_Minute]' ).val();
	iDuration = $( '#duration' ).val();
	if( parseInt( iChannelId ) <= 0 ) {
		return false;
	}
	if( sTitle.length == 0 ) {
		alert( 'Musisz podać tytuł wydarzenia' );
		return false;
	}
	if( sStartDate.length == 0 ) {
		alert( 'Musisz podać datę wydarzenia' );
		return false;
	}
	addEvent( iChannelId, sTitle, sDesc, sStartDate, iDuration );
}

function addEvent( iChannelId, sTitle, sDesc, sStartDate, iDuration ) {
	$.ajax({
		type:'POST',
		url:'/event-add/',
		data:{channels_id:iChannelId,title:sTitle, description:sDesc,start_date:sStartDate,duration:iDuration},
		success:function(txt) {
			switch(txt)
			{
				case 'ok':
					$( '#eventAddForm' ).html( '<div class="message info">Wydarzenie zostało dodane do kalendarium kanału</div>' );
					setTimeout( '$( "#eventAddForm" ).jqmHide();', 1000 );
					reloadEvents( iChannelId );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
}

function deleteEvent( iEventId, iChannelId ) {
	if( iEventId <= 0 || iChannelId <= 0 ) {
		return false;
	}
	$.ajax({
		type:'POST',
		url:'/event-delete/',
		data:{events_id:iEventId,channels_id:iChannelId},
		success:function(txt) {
			switch(txt)
			{
				case 'ok':
					setTimeout( '$( "#eventAddForm" ).jqmHide();', 1000 );
					reloadEvents( iChannelId );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
}

function reloadEvents( iChannelId ) {
	if( typeof $( '#eventsList' ) == 'undefined' ) {
		console.log( 'eventsList undefined' );
		return false;
	}
	$.ajax({
		type:'GET',
		url:'/event-channel/',
		data:{ id:iChannelId },
		dataType:'html',
		success:function( html ) {
			$( '#eventsList' ).html( html );
		}
	});
}

function addFavorite( iObjectId, iTypeId ) {
	$.ajax({
		type:'POST',
		url:'/favorite-add/',
		data:{object_id:iObjectId,type:iTypeId},
		dataType: 'json',
		success:function(json) {
			switch(json.txt)
			{
				case 'ok':
					$.jGrowl( 'Właśnie zostałeś fanem tego kanału!' );
					$( 'a.addfavorite' ).addClass( 'delfavorite' ).removeClass( 'addfavorite' ).attr( 'id', json.id ).text( 'Usuń ulubiony' ).unbind( 'click' ).bind( 'click', function() {
						delFavorite( json.id );
						return false;
					} );
					break;
				case 'exists':
					$.jGrowl( 'Jesteś już fanem tego kanału!' );
					break;
				case 'not_logged':
					$.jGrowl( 'Aby zostać fanem tego kanału, musisz być zalogowany' );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
}

function delFavorite( iFavoriteId ) {
	$.ajax({
		type:'POST',
		url:'/favorite-delete/',
		data:{id:iFavoriteId},
		success:function(txt) {
			switch(txt)
			{
				case 'ok':
					$.jGrowl( 'Usunąłeś ten kanał ze swoich ulubionych!' );
					$( 'a.delfavorite' ).addClass( 'addfavorite' ).removeClass( 'delfavorite' ).text( 'Zostań fanem' ).unbind( 'click' ).bind( 'click', function() {
						addFavorite( $( '#channels_id' ).val(), 1 );
						return false;
					} );
					break;
				case 'not_logged':
					$.jGrowl( 'Aby usunąć ten kanał ze swoich ulubionych, musisz być zalogowany' );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
}

function deleteFavorite( iFavoriteId ) {
	$.ajax({
		type:'POST',
		url:'/favorite-delete/',
		data:{id:iFavoriteId},
		success:function(txt) {
			switch(txt)
			{
				case 'ok':
					$.jGrowl( 'Usunąłeś ten kanał ze swoich ulubionych!' );
					$( 'li#fav' + iFavoriteId ).fadeOut( 1500 ).remove();
					break;
				case 'not_logged':
					$.jGrowl( 'Aby usunąć ten kanał ze swoich ulubionych, musisz być zalogowany' );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
}

function voteChannel( iChannelId, iVote ) {
	if( iChannelId > 0 && iVote > 0 ) {
	$.ajax({
		type:'POST',
		url:'/channel-vote/',
		data:{id:iChannelId,vote:iVote},
		dataType:'json',
		success:function( json ) {
			switch( json.txt ) {
				case 'ok':
					$.jGrowl( 'Dziękujemy za Twoją ocenę!' );
					$( '.star-rating .current-rating' ).width( json.data.width );
					$( '.channelrating strong' ).text( json.data.rank );
					$( '.channelrating em' ).text( json.data.vote_count );
					break;
				case 'voted':
					$.jGrowl( 'Ten kanał został już dziś przez Ciebie oceniony' );
					break;
				default:
					alert( errRefresh );
					break;
			}
		}
	});
	} else {
		$.jGrowl( 'Nie udało się zapisać Twojej oceny tego kanału, spróbuj ponownie' );
	}
}

function faqSearch(phrase)
{
	document.getElementById('searchResult').innerHTML ="Trwa wyszukiwanie...";
	$.ajax({
		type: "POST",
		url: "/faq-szukaj/",
		data: "phrase="+phrase,
		success: function(msg){ 
			document.getElementById('searchResult').innerHTML = msg;
		}
		});	
} 

$( document ).ready( function(){
	$( '#introDiv' ).jqm(
		{
			ajax: '@href',
			trigger: '#navbar #s a',
			toTop: true,
			onShow: function(hash){ $( '#introDiv').css( {
					left: ( ( $( window ).width() - $( '#introDiv' ).width() ) / 2 ) + 'px',
					top: ( ( $( window ).height() - $( '#introDiv' ).height() ) / 2 ) + 'px',
					height: '450px'
				} ); hash.w.css('opacity',0.88).show();
			}
		}
	);
});