var player;
var curpos;
var top101selected = 0;

function nav_n(w)
{
	var current = $('#nav_c').text();
	var limit = $('#nav_l').text();
	var next = parseInt(current) + 1;
	
	curpos = current;
	
	if(curpos != limit)
	{
		$('#nav_c').text(next);
		
		switch(w)
		{
			case 'clips':
			case 'clips_hu':
				$('#i_latestclips').load('jx_selector.php?wp='+w+'&sp='+next);
				break;
			case 'news':
			case 'riport':
			case 'movieon':
				$('#i_news_container').load('jx_selector.php?wp='+w+'&sp='+next);
				break;
			default:
				$('#i_uni_selector').load('jx_selector.php?wp='+w+'&sp='+next);
		}
	}
}

function nav_p(w)
{
	var current = $('#nav_c').text();
	var limit = $('#nav_l').text();
	var prev = parseInt(current) - 1;
	
	curpos = current;
	
	if(curpos != 1)
	{
		$('#nav_c').text(prev);
		
		switch(w)
		{
			case 'clips':
			case 'clips_hu':
				$('#i_latestclips').load('jx_selector.php?wp='+w+'&sp='+prev);
				break;
			case 'news':
			case 'riport':
			case 'movieon':
				$('#i_news_container').load('jx_selector.php?wp='+w+'&sp='+prev);
				break;
			default:
				$('#i_uni_selector').load('jx_selector.php?wp='+w+'&sp='+prev);
		}
	}
}

function LoadClip () {
	if ($('#onair span.mc').length && $('#nexton span.mc').length) {
		$('#clip1').load('clip_wrapper.php?id=1');
		$('#clip2').load('clip_wrapper.php?id=2');
	}
	setTimeout(LoadClip, 10000);
}

function ShowEmbed(id) {
	switch (id)
	{
		case 0:
			$('div#embedcode-wrapper').slideUp('fast');
			$('div#embedbutton a').attr('href', 'javascript:ShowEmbed(1)');
			$('div#embedbutton a').html('Beágyazás &#xBB;');
			break;
		case 1:
			$('div#embedcode-wrapper').slideDown('fast');
			$('div#embedbutton a').attr('href', 'javascript:ShowEmbed(0)');
			$('div#embedbutton a').html('Beágyazás &#xAB;');
			break
	}
}

function checks () {
	$('div.checkbox').each(function () {
		var replacement = $(this).find('span');
		var input = $(this).find('input');
		if (replacement.length == 1 && input.length == 1) {
			input.css('display', 'none');
			replacement.css('display', 'inline');
			if (input.is(':checked')) replacement.addClass('checked');
			else {
				input.attr('checked', false);
				replacement.removeClass('checked');
			}
			if (!input.hasClass('event')) {
				input.change(function () {
					checks();
				}).addClass('event');
			}
			if (!replacement.hasClass('event')) {
				replacement.click(function () {
					input.click();
					checks();
				}).addClass('event');
			}
		}
	});
}

function top101 () {
	/* TOP 101 BEGIN */
	$(document).delegate('div.top101', 'click', function(e) {
		e.preventDefault();
		var clicked = $(this);
		var input = clicked.find('input');
		
		if(clicked.hasClass('top100selected'))
		{
			if($('.lulz').length == 0)
			{
				input.attr('checked', false);
				clicked.removeClass('top100selected');
				clicked.find('b').css('color', '#ffffff');
				clicked.find('p').css('color', '');
				
				$('#valasztott').empty();
				
				top101selected--;
			} else {
				/* miket valasztott?! */
				$('#valasztott').empty();
				$('div.top100selected p').each(function () {
					var current = $(this);
					
					$('<p class="top101top5">'+current.html()+'</p>').appendTo($('#valasztott'));
					
				});
				$('#jwsubmit').click();
			}
		} else {
			if(top101selected < 5 && $('.lulz').length == 0)
			{
				input.attr('checked', 'checked');
				clicked.addClass('top100selected');
				clicked.find('b').css('color', '#68fa07');
				clicked.find('p').css('color', '#68fa07');
				
				$('#valasztott').empty();
				
				top101selected++;
				
				if(top101selected == 5)
				{
					/* miket valasztott?! */
					$('#valasztott').empty();
					$('div.top100selected p').each(function () {
						var current = $(this);
						
						$('<p class="top101top5">'+current.html()+'</p>').appendTo($('#valasztott'));
						
					});
					$('#jwsubmit').click();
				}
			} else {
				/* miket valasztott?! */
				$('#valasztott').empty();
				$('div.top100selected p').each(function () {
					var current = $(this);
					
					$('<p class="top101top5">'+current.html()+'</p>').appendTo($('#valasztott'));
				});
				$('#jwsubmit').click();
			}
		}
	});
	
	$(document).delegate('div.top101', 'mouseenter', function () {
		var el = $(this);

		var to = window.setTimeout(function () {
			el.find('.top101-play').fadeIn('fast');
		}, 500);
		el.bind('mouseleave.top101', function () {
			el.find('.top101-play').fadeOut('fast');
			window.clearTimeout(to);
		});
	});
	
	$(document).delegate('form#top101', 'submit', function (e) {
		e.preventDefault();
		var form = $(this);
		var error = [];
		
		if(form.find('input[checked]').length != 5)
		{
			error.push('error');
		}
		
		if (!error.length) {
			$.ajax({
				url: form.attr('action'),
				type: form.attr('method'),
				data: form.serialize(),
				dataType: 'json',
				success: function (response) {
					form.find(':input').attr('disabled', true);
					$('#szavazogomb').addClass('lulz');
					$('#szavazogomb').text('Köszönjük a szavazatot!');
					$('#szavazogomb').css('cursor', 'default');
				}
			});
		}
	});
	
	$('#szavazogomb').click(function () {
		$('#top101submit').click();
	});
	/* TOP 101 END */
}

$(document).bind('DOMNodeInserted', function(event) {
	//event.target.nodeName // new node
	//event.relatedNode.nodeName // parent
	
	if ($(event.target).hasClass('twtr-tweet')) {
		$(event.target).removeClass('twtr-tweet');
		$(event.target).addClass('mc-tweet');
	}
	
	var button = $(event.target).find('button.dsq-post-edit');
	
	if (button.length) {
		$(button).removeAttr('onclick');
		$(button).click(function (e) {
			e.preventDefault();
		});
	}
});

$(document).ready(function() {
	checks();
	LoadClip();
	top101();
	
	$('.s_head_left').corner('bevel tr 40px shl');
	$('.s_head_right').corner('bevel tr 40px shr');
	
	$('ul.menu li').each(function() {
		// hover on
		$(this).mouseenter(function() {
			if($(this).find('ul.submenu').css('display') == 'none') {
				$(this).find('ul.submenu').slideDown('fast');
			}
		});
		// hover out
		$(this).mouseleave(function() {
			if($(this).find('ul.submenu').css('display') == 'block') {
				$(this).find('ul.submenu').slideUp('fast');
			}
		});
	});
	
	if ($('img.newsimg').length) {
		$('img.newsimg').lightBox();
	};
	
	if ($('img.boffposter').length) {
		$('img.boffposter').lightBox();
	};
	
	$('td').each(function () {
		if ($(this).find('a').length > 0) {
			$(this).addClass('hoverclass');
		}
	});
	
	$('td.hoverclass').click(function() {
		window.location = $(this).find('a').attr('href');
	});
	
	$('#clip1').load('clip_wrapper.php?id=1');
	$('#clip2').load('clip_wrapper.php?id=2');
});

