var comment_page_id = 0;
var comment_page_pk = 0;
var comment_page_nr = 1;

var modal_timer = 10000;
var modal_fade_timer = 400;
var modal_timeout;

$(document).ready(function()
{
	/* Fancy inputs */
	$('label.inlined').unbind('click').click(function()
	{
		$(this).next('input').trigger('focus');
	});
	$('.input-inlined').unbind('focus').focus(function()
	{
		$(this).parent().prev('label').hide();
	});
	$('.input-inlined').unbind('blur').blur(function()
	{
		if( $(this).val() == '' )
		{
			$(this).parent().prev('label').show();
		}
	});
	/* Fancy inputs */
	
	/* Views general */
	$('#settings_lista_view a').click(function()
	{
		var view;
		var cookie_view = $('#settings_lista_view').attr('cookie');
		
		if( !$(this).hasClass('activ') )
		{
			if( $(this).hasClass('list-view') ) view = 'list';
			if( $(this).hasClass('detail-view') ) view = 'detail';
			
			if( view == 'list' || view == 'detail' )
			{
				$.post(
					'libs/kissfm/ajax_requests_general.php',
					{
						act: 'settings_lista_view',
						view: view,
						cookie: cookie_view
					},
					function(data)
					{
						if( data.success )
						{
							if( data.view == 'list' )
							{
								$('#settings_lista_view .detail-view').removeClass('activ');
								$('#settings_lista_view .list-view').addClass('activ');
								
								$('#general_lista_content').removeClass('detail-view').addClass('list-view');
							}
							if( data.view == 'detail' )
							{
								$('#settings_lista_view .list-view').removeClass('activ');
								$('#settings_lista_view .detail-view').addClass('activ');
								
								$('#general_lista_content').removeClass('list-view').addClass('detail-view');
							}
						}
					},
					'json'
				);
			}
		}
	});
	/* Views general */
	
	/* Butonul de close la modal - mesaje */
	$('#modal .close').click(function()
	{
		if( $('#modal').is(':visible') ) modal_hide();
	});
	/* Butonul de close la modal - mesaje */
	
	/* Search Top */
	$('#search-top-submit').click(function()
	{
		if( $(this).hasClass('open') && $('#search-q').val() != '' )
		{
			$('#form-top-search').submit();
		}
	});
	
	$('#search-top-close').click(function()
	{
		if( $('#search-top-submit').hasClass('open') )
		{
			$('#search-top-close').stop().animate(
			{
				right: '0px'
			}, 300);
			
			$('#search-q-fieldset').stop().animate(
			{
				right: '-160px'
			}, 300, function() 
			{
				$('#search-q').val('');
				$('#search-top-submit').removeClass('open');
			});
		}
	});
	
	$('#top-search').hover(function()
	{
		if( !$('#search-top-submit').hasClass('open') )
		{
			// deschidem
			$('#search-top-close').stop().animate(
			{
				right: '-41px'
			}, 300);
			
			$('#search-q-fieldset').stop().animate(
			{
				right: '41px'
			}, 300, function() 
			{
				$('#search-top-submit').addClass('open');
				$('#search-q').focus();
			});
		}
	},
	function()
	{
		// daca are clasa open si e gol .. inchidem
		if( $('#search-top-submit').hasClass('open') && $('#search-q').val() == '' )
		{
			$('#search-top-close').stop().animate(
			{
				right: '0px'
			}, 300);
			
			$('#search-q-fieldset').stop().animate(
			{
				right: '-160px'
			}, 300, function() 
			{
				$('#search-top-submit').removeClass('open');
			});
		}
	});
	/* Search Top */
	
	/* Search general */
	$('#cautare-avansata-btn').click(function()
	{
		if( $(this).hasClass('active') )
		{
			// colapse
			$('#cautare-avansata-container').slideUp(300);
			$(this).removeClass('active');
		}
		else
		{
			// expand
			$('#cautare-avansata-container').slideDown(300);
			$(this).addClass('active');
		}
	});
	$('#search-page-submit').click(function()
	{
		$('#big-search-form').submit();
	});
	
	$('#cautare-avansata-container input.check').change(function()
	{
		var checked = $(this).is(':checked');
		var has_unchecked = 0;
		
		if( !checked )
		{
			has_unchecked = 1;
			if( $(this).hasClass('check_media') )
			{
				// debifam media
				$('#check-media').attr( 'checked', false );
				if( $(this).attr('id') == 'check-media' )
				{
					$('.check_media').each(function()
					{
						$(this).attr('checked', false);
					})
				}
			}
			if( $(this).hasClass('check_stiri') )
			{
				// debifam stiri
				$('#check-stiri').attr( 'checked', false );
				if( $(this).attr('id') == 'check-stiri' )
				{
					$('.check_stiri').each(function()
					{
						$(this).attr('checked', false);
					})
				}
			}
			if( $(this).hasClass('check_concursuri') )
			{
				// debifam concursuri
				$('#check-concursuri').attr( 'checked', false );
				if( $(this).attr('id') == 'check-concursuri' )
				{
					$('.check_concursuri').each(function()
					{
						$(this).attr('checked', false);
					})
				}
			}
			if( $(this).hasClass('check_evenimente') )
			{
				// debifam evenimente
				$('#check-evenimente').attr( 'checked', false );
				if( $(this).attr('id') == 'check-evenimente' )
				{
					$('.check_evenimente').each(function()
					{
						$(this).attr('checked', false);
					})
				}
			}
			if($(this).attr('id') == 'check-tot')
			{
				$('.check').each(function()
				{
					$(this).attr('checked', false);
				});
			}
		}
		else
		{
			var unchecked = 0;
			if( $(this).hasClass('check_media') && $(this).attr('id') != 'check-media' )
			{
				$('.check_media').each(function()
				{
					if( !$(this).is(':checked') )
					{
						if( $(this).attr('id') != 'check-media' )
						{
							unchecked = 1;
							has_unchecked = 1;
						}
					}
				});
				if( unchecked ) $('#check-media').attr('checked', false);
				else $('#check-media').attr('checked', true);
			}
			if( $(this).attr('id') == 'check-media' )
			{
				$('.check_media').each(function()
				{
					$(this).attr('checked', true);
				});
			}
			
			unchecked = 0;
			if( $(this).hasClass('check_stiri') && $(this).attr('id') != 'check-stiri' )
			{
				$('.check_stiri').each(function()
				{
					if( !$(this).is(':checked') )
					{
						if( $(this).attr('id') != 'check-stiri' )
						{
							unchecked = 1;
							has_unchecked = 1;
						}
					}
				});
				if( unchecked ) $('#check-stiri').attr('checked', false);
				else $('#check-stiri').attr('checked', true);
			}
			if( $(this).attr('id') == 'check-stiri' )
			{
				$('.check_stiri').each(function()
				{
					$(this).attr('checked', true);
				});
			}
			
			unchecked = 0;
			if( $(this).hasClass('check_concursuri') && $(this).attr('id') != 'check-concursuri' )
			{
				$('.check_concursuri').each(function()
				{
					if( !$(this).is(':checked') )
					{
						if( $(this).attr('id') != 'check-concursuri' )
						{
							unchecked = 1;
							has_unchecked = 1;
						}
					}
				});
				if( unchecked ) $('#check-concursuri').attr('checked', false);
				else $('#check-concursuri').attr('checked', true);
			}
			if( $(this).attr('id') == 'check-concursuri' )
			{
				$('.check_concursuri').each(function()
				{
					$(this).attr('checked', true);
				});
			}
			
			unchecked = 0;
			if( $(this).hasClass('check_evenimente') && $(this).attr('id') != 'check-evenimente' )
			{
				$('.check_evenimente').each(function()
				{
					if( !$(this).is(':checked') )
					{
						if( $(this).attr('id') != 'check-evenimente' )
						{
							unchecked = 1;
							has_unchecked = 1;
						}
					}
				});
				if( unchecked ) $('#check-evenimente').attr('checked', false);
				else $('#check-evenimente').attr('checked', true);
			}
			if( $(this).attr('id') == 'check-evenimente' )
			{
				$('.check_evenimente').each(function()
				{
					$(this).attr('checked', true);
				});
			}
			
			if($(this).attr('id') == 'check-tot')
			{
				$('.check').each(function()
				{
					$(this).attr('checked', true);
				});
			}
			else
			{
				$('.check').each(function()
				{
					if( !$(this).attr('checked') ) 
					{
						if( $(this).attr('id') != 'check-tot' ) has_unchecked = 1;
					}
				});
			}
		}
		
		if( has_unchecked )
		{
			$('#check-tot').attr('checked', false );
		}
		else
		{
			$('#check-tot').attr('checked', true);
		}
	});
	/* Search general */
});
function show_login( scroll )
{
	$.post(
		'libs/kissfm/ajax_requests_general.php',
		{
			act: 'get_modal_login'
		},
		function( data )
		{
			if( data.success )
			{
				// afisam modal-ul
				if( scroll == 1 )
				{
					$.scrollTo('250px', 300);
				}
				$('#modal_overlay').show();
				$('#modal_container').hide();
				$('#modal_container').html( data.message );
				$('#modal_container').fadeIn();
			}
			else
			{
				// afisam eroare!
				use_modal( 'error', 'Eroare!', data.message )
			}
		},
		'json'
	);
}
function show_profil( scroll )
{
	$.post(
		'libs/kissfm/ajax_requests_general.php',
		{
			act: 'get_modal_profil'
		},
		function( data )
		{
			if( data.success )
			{
				// afisam modal-ul
				if( scroll == 1 )
				{
					$.scrollTo('250px', 300);
				}
				$('#modal_overlay').show();
				$('#modal_container').hide();
				$('#modal_container').html( data.message );
				$('#modal_container').fadeIn();
			}
			else
			{
				// afisam eroare!
				use_modal( 'error', 'Eroare!', data.message )
			}
		},
		'json'
	);
}
function show_contact( scroll )
{
	$.post(
		'libs/kissfm/ajax_requests_general.php',
		{
			act: 'get_modal_contact'
		},
		function( data )
		{
			if( data.success )
			{
				// afisam modal-ul
				if( scroll == 1 )
				{
					$.scrollTo('250px', 300);
				}
				$('#modal_overlay').show();
				$('#modal_container').hide();
				$('#modal_container').html( data.message );
				$('#modal_container').fadeIn();
			}
			else
			{
				// afisam eroare!
				use_modal( 'error', 'Eroare!', data.message )
			}
		},
		'json'
	);
}
function show_inregistrare( scroll )
{
	$.post(
		'libs/kissfm/ajax_requests_general.php',
		{
			act: 'get_modal_inregistrare'
		},
		function( data )
		{
			if( data.success )
			{
				if( scroll == 1 )
				{
					$.scrollTo('250px', 300);
				}
				// afisam modal-ul
				$('#modal_overlay').show();
				$('#modal_container').hide();
				$('#modal_container').html( data.message );
				$('#modal_container').fadeIn();
			}
			else
			{
				// afisam eroare!
				use_modal( 'error', 'Eroare!', data.message )
			}
		},
		'json'
	);
}
function use_modal( clasa, titlu, mesaj )
{
	clearTimeout(modal_timeout);
	$('#modal').stop(true, false).hide();
	$('#modal p span.ico').removeClass('error info ok alert').addClass(clasa);
	$('#modal p span.ico').html(titlu);
	$('#modal p span.text').html( mesaj );
	$('#modal').css( 'top', 80 ).css( 'opacity', 0.05 ).show();
	$('#modal').stop().animate({
		opacity: 1,
		top: 40
	}, modal_fade_timer);
	modal_timeout = setTimeout( "modal_hide()", modal_timer );
}
function modal_hide()
{
	$('#modal').animate({
		opacity: 0.05,
		top: 80
	}, modal_fade_timer, function()
	{
		$('#modal').hide();
	});
}

function comments_add()
{
	$nume = $('#comment_nume').val();
	$email = $('#comment_email').val();
	$comment = $('#comment_mesaj').val();
	$page_id = $('#comment_page_id').val();
	$page_pk = $('#comment_page_pk').val();
	
	modal_hide();
	kissfm_remove_inputerror( '#comment_nume' );
	kissfm_remove_inputerror( '#comment_email' );
	kissfm_remove_inputerror( '#comment_mesaj' );
	$.post(
		'libs/kissfm/ajax_requests_general.php' , 
		{ 
			act: 'comments_add' , 
			autor: $nume,
			email: $email,
			comment: $comment,
			page_id: $page_id,
			page_pk: $page_pk
		} , 
		function( data )
		{
			if( data.success )
			{
				use_modal( 'ok', 'Ok', data.message );
				if( !data.logged )
				{
					$('#comment_nume').val('').blur();
					$('#comment_email').val('').blur();
				}
				$('#comment_mesaj').val('').blur();
			}
			else
			{
				if( data.err_autor ) kissfm_add_inputerror( '#comment_nume' );
				if( data.err_email ) kissfm_add_inputerror( '#comment_email' );
				if( data.err_comment ) kissfm_add_inputerror( '#comment_mesaj' );
				use_modal( 'error', 'Eroare!', data.message );
			}
		}, 
		'json'
	);
}

function comments_more()
{
	$page_id = comment_page_id;
	$page_pk = comment_page_pk;
	$page_nr = comment_page_nr;
	
	$.post(
		'libs/kissfm/ajax_requests_general.php' , 
		{ 
			act: 'show_more_comments', 
			page_id: $page_id,
			page_pk: $page_pk,
			page_nr: $page_nr
		} , 
		function( data )
		{
			if( data.success )
			{
				$('#comments_container').append( data.message );
				comment_page_nr = comment_page_nr + 1;
				if( !data.has_more )
				{
					$('#comments_more_button').hide();
				}
			}
		}, 
		'json'
	);
}

function check_email_valid( value )
{
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

function kissfm_add_inputerror( id )
{
	$(id).parent().parent().addClass('error');
}

function kissfm_remove_inputerror( id )
{
	$(id).parent().parent().removeClass('error');
}

function kissfm_add_error( el )
{
	el.addClass('error');
}
function kissfm_remove_error( el )
{
	el.removeClass('error');
}
function open_live( base_url )
{
	window.open( base_url + '/live/', null, 'width=1000,height=800' );
	
	/*$.post(
		base_url + '/libs/kissfm/ajax_requests_general.php',
		{
			act: 'open_live'
		},
		function(data)
		{
			window.open( base_url + '/live/', null, 'width=1000,height=800' );
		}
		, 'json'
	);*/
}
