// JavaScript Document

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}




$(document).ready(function(){
	////////////////////////////////////////////////////////////////////
	///// RÖVID HÍREK
	////////////////////////////////////////////////////////////////////
	//plusz ninusz gombok funkcióinak hozzáadása
	$('.rightContainer').find(".add").click(function(){
		var p=$(this).parents("div:first");
		if (p.find(".add").attr('href')=="#")
			{
				p.find("li.blocked:first").removeClass();
				//createCookie('num', p.find('li:not(.blocked)').length),7);
				var num=p.find('li:not(.blocked)').length;
				createCookie('num',num, 9999);
				if(!p.find('ul>li.blocked').get(0))
					{
						$(this).css({opacity:'0.4'}).removeAttr('href');
					} 
					else 
					{
						p.find(".remove").css({opacity:'1'}).attr('href', '#');
					}
			}
		return false;
	})
	
	$('.rightContainer').find(".remove").click(function(){
	
		if ($(this).attr('href')=="#") 
			{
				var p=$(this).parents("div:first");
				//alert(p.find('ul>li:not(.blocked)').length); muködik
				p.find("li:not(.blocked):last").addClass("blocked");
				var num=p.find('li:not(.blocked)').length;
				createCookie('num',num, 9999);
				
				if(!p.find('ul>li:not(.blocked)').get(1))
					{
						$(this).css({opacity:'0.4'}).removeAttr('href');
					} 
					else 
					{
						p.find(".add").css({opacity:'1'}).attr('href', '#');
					}
				
			}
		return false;
	})
	
	//cookiek beolvasása
	
	//hirlistában szereplo hirek száma:
	var hirnum=$('.right1').find('ul>li').length;
	
	var x = readCookie('num');
	if (x<1) {x=3}
	
	//ha csak egy hir van, akkor a minusz ne legyen aktiv
	if (x==1)
		{
			$('.right1').find('.remove').css({opacity:'0.4'}).removeAttr('href');
		}
		
	//ha minden hir kint van, akkor ne legyen aktiv a plusz jel
	if (x==hirnum)
		{
			$('.right1').find('.add').css({opacity:'0.4'}).removeAttr('href');
		}
	
	//beállítja, hogy mennyi hír jelenjen meg
	if (x<3)
		{
			x=3-x;
			for (i=1; i<=x; i++)
				{
					$('.right1').find('ul>li:not(.blocked):last').addClass('blocked');
				}

		}
	if (x>3)
		{
			x=x-3;
			for (i=1; i<=x; i++)
				{
					$('.right1').find('ul>li.blocked:first').removeClass();
				}
		}
		
		
	///////////////////////////////////////////////////////////////////
	///// TV musor
	////////////////////////////////////////////////////////////////////


	//plusz ninusz gombok funkcióinak hozzáadása
	$('.tvmusor').find('.add').click(function(){
		var p=$(this).parents(".tvmusor");
		//alert(p.find("tr.blocked").length);
		if (p.find(".add").attr('href')=="#")
			{
				
				p.find("tr.blocked:first").removeClass();
				//createCookie('num', p.find('li:not(.blocked)').length),7);
				var num=p.find('tr:not(.blocked)').length;
				createCookie('tvnum',num, 9999);
				if(!p.find('tr.blocked').get(0))
					{
						$(this).css({opacity:'0.4'}).removeAttr('href');
					} 
					else 
					{
						p.find(".remove").css({opacity:'1'}).attr('href', '#');
					}
			}
		return false;
	})

	$('.tvmusor').find('.remove').click(function(){
												 
		if ($(this).attr('href')=="#") 
			{
				var p=$(this).parents(".tvmusor");
				//alert(p.find('ul>li:not(.blocked)').length); muködik
				p.find("tr:not(.blocked):last").addClass("blocked");
				var num=p.find('tr:not(.blocked)').length;
				createCookie('tvnum',num, 9999);
				if(!p.find('tr:not(.blocked)').get(1))
					{
						$(this).css({opacity:'0.4'}).removeAttr('href');
					} 
					else 
					{
						p.find(".add").css({opacity:'1'}).attr('href', '#');
					}
				
			}
		return false;
	})
	
	//cookiek beolvasása
	
	//hirlistában szereplo hirek száma:
	var hirnum=$('.tvmusor').find('tr').length;
	
	var x = readCookie('tvnum');
	if (hirnum<x) {x=hirnum}//ha kevesebb musor van, mint a beállított
	if (x<1) {x=3}
	
	//ha csak egy hir van, akkor a minusz ne legyen aktiv
	if (x==1)
		{
			$('.tvmusor').find('.remove').css({opacity:'0.4'}).removeAttr('href');
		}
		
	//ha minden hir kint van, akkor ne legyen aktiv a plusz jel
	if (x==hirnum)
		{
			$('.tvmusor').find('.add').css({opacity:'0.4'}).removeAttr('href');
		}
	
	//beállítja, hogy mennyi hír jelenjen meg
	if (x<3)
		{
			x=3-x;
			for (i=1; i<=x; i++)
				{
					$('.tvmusor').find('tr:not(.blocked):last').addClass('blocked');
				}

		}
	if (x>3)
		{
			x=x-3;
			for (i=1; i<=x; i++)
				{
					$('.tvmusor').find('tr.blocked:first').removeClass();
				}
		}




});

