$(document).ready(function() {
	var melyikoldal = $('#header_left').attr('class');

	if (melyikoldal == 'itv') {
		var currentTime = new Date()
  		var hours = currentTime.getHours()
		if (hours >= 18 && hours <24 && currentTime.getDay() != 6) {
			$('#halozat_tv').load('proba.php', function(){
				$('#halozat_tv').slideDown();
			});			
		}
	}

	$("#halozat_tv a#o21").live("click", function(){
		$("#csatornak").load("itv_csatornak.php?mitkeres=21:00");
		return false;
    });

	$("#halozat_tv a#o23").live("click", function(){
		$("#csatornak").load("itv_csatornak.php?mitkeres=23:00");
		return false;
    });
	fix_height_uzenofal();

	if($.browser.msie && $.browser.version=="6.0") $('#ie6').show();

	var refreshId = setInterval(function() {
				$("#uzenofal_ajax").load('uzenofal.php', function(){
						fix_height_uzenofal();
				});
	}, 60000);
	
		//$("#uzenofal").insertBefore("#csatornak_doboz");

		
    $(document).ajaxSend(function() {
        $('#loading').show();
    });
    $(document).ajaxStop(function() {
        $('#loading').hide();
    });

	$("a.csatorna").live("click", function(){
		var url = $(this).attr("href");
		$("#tvplayer").load(url);
		return false;
    });
	// Keres?	
	autoFill($("#kereso_input"), "Keresés...");
	
    $("#kereso_input").keyup(function(e){
        ajax_search();
    });

	var timeout = null;

	function ajax_search() {
		var melyikoldal = $('#header_left').attr('class');
		if (timeout) clearTimeout(timeout);
		timeout = setTimeout(function(){
			var kulcsszo = $("#kereso_input").val();
			$("#csatornak").load(melyikoldal+"_csatornak.php?mitkeres="+kulcsszo);
    	}, 400);
	}
    $("form#kereso_form").submit(function() {
		return false;
    });
	// Keres?
	
	$("ul.kategoriak a").click(function () {
		$("ul.kategoriak a").removeClass("active");
		$(this).addClass("active");
		var url = $(this).attr('href');
		$('#csatornak').html("");
		$('#csatornak').addClass("loading");
		$("#csatornak").load(url, function(){
			$('#csatornak').removeClass("loading");
		});

		return false;
    });
    
	$("ul.kategoriak li.simalink a").click(function () {
		var url = $(this).attr('href');
		window.open(url)
		return false;
    });	

	$("a.adatmodositas").click(function () {
		$(this).addClass("active");
		$('#adatmodositas').slideDown();
		return false;
    });
	$("a.bezar").click(function () {
		$("a.adatmodositas").removeClass("active");
		$('#adatmodositas').slideUp();
		return false;
    });
	$("a.torles").live("click", function(){
		$(this).addClass("active");
		$(this).html('Biztos törlöd? <span class="igen">Igen</span> <span class="nem">Nem</span>');
		return false;
    }); 
	$("a.torles span.igen").live("click", function(){
		var ez = $(this);
		var url = $(this).parent().attr("href");
		$.ajax({
   			type: "POST",
   			url: url,
   			success: function(msg){
     			ez.parent().parent().remove();
   			}
		});		
		return false;
    });
	$("a.torles span.nem").live("click", function(){
		$(this).parent().removeClass("active");
		$(this).parent().html("");
		return false;
    });
	$("a.fioktorles").click(function () {
		var answer=confirm("Biztosan törölni szeretnéd a fiókodat?")
		if(answer)
		window.location="fiok_torles.php?vegleges_torles=GR12xClt91W" 
		return false;
    });     
	$("a.csatorna").live("click", function(){
		var url = $(this).attr("href");
		$("#tvplayer").load(url);
		return false;
    });
	$("a#player_menu_kedvenchez").live("click", function(){
		var ez = $(this);
		var url = $(this).attr("href");
		$.ajax({
   			type: "POST",
   			url: url,
   			success: function(msg){
				$("li.kedvenceim a").trigger("click");
   			}
		});		
		return false;
    });
    
	$("a#player_menu_informacio").live("click", function(){
		return false;
    });
    
	$('input[name=csatorna_link]').live("click", function(){
		$(this).select();
    });
    
	$('input[name=csatorna_kod]').live("click", function(){
		$(this).select();
    });

	$('a#email_linkcsere').attr('href','mailto:support@i-tv.hu?subject=Linkcsere');
	
	
	autoFill($("#uzenofal_input input"), "Írd be az üzeneted és nyomj egy Entert!");
	
	$('#uzenofal_fel_lee').toggle(
      function () {
		$("#uzenofal").insertAfter("#csatornak_doboz");
		$("#uzenofal").removeClass('fent');
		$(this).html('Fel');
		return false;
      },
      function () {
		$("#uzenofal").insertBefore("#csatornak_doboz");
		$("#uzenofal").addClass('fent');
		$(this).html('Le');
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
      }
    );

	$('#uzenofal_frissit').live("click", function(){
		$("#uzenofal_ajax").load('uzenofal.php', function(){
						fix_height_uzenofal();
		});
		return false;
    });

	$('#uzenofal_szabalyok').live("click", function(){
		$("#uzenofal_ajax").load('uzenofal.php?oldal=szabalyok', function(){
						fix_height_uzenofal();
		});
		return false;
    });

	$("#uzenofal_input input").keypress(function (e) {  
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        	var adat = $(this).val();
        	/*
			$.ajax({
   				type: "GET",
   				url: 'uzenofal.php?ujuzenet='+adat,
   				success: function(msg){
   					 $("#uzenofal_ajax").load("uzenofal.php", function(){
						fix_height_uzenofal();
					});
   				}
			});	
			*/
			$("#uzenofal_ajax").load("uzenofal.php", {ujuzenet:adat} , function(){
				fix_height_uzenofal();
			});
			$(this).val('');
            return false;  
        } else {  
            return true;  
        }  
    }); 
    
	$("a.uzenet_torles").live("click", function(){
		var url = $(this).attr('href');
		$.ajax({
   			type: "POST",
   			url: url,
   			success: function(msg){
				$("#uzenofal_ajax").load('uzenofal.php', function(){
						fix_height_uzenofal();
				});
   			}
		});	
		return false;
    });    

	function fix_height_uzenofal() {
		var height = $("#uzenofal_ajax ul").height();
		if (height > 300) {
			$("#uzenofal_ajax").css({ "overflow":"auto", "height":"300px" });
    	}
    	else {
    		$("#uzenofal_ajax").css({ "overflow":"auto", "height":"auto" });
    	}
	}

	function limitChars(textid, limit, infodiv)
	{
		var text = $('#'+textid).val();	
		var textlength = text.length;
		if(textlength > limit)
		{
			$('#'+textid).val(text.substr(0,limit));
			$("#betu_szamlalo div").css({ "width":"240px", "background":"#ff7575" });
			return false;
		}
		else
		{
			$("#betu_szamlalo div").css({ "width":textlength+'px', "background":"#cbffbf" });
			return true;
		}
	}
	
 	$('#uzenofal_input input').keyup(function(){
 		limitChars('uzenofal_input input', 240, 'charlimitinfo');
 	});

});

function autoFill(id, v){
	$(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#333" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#b2adad" }).val(v);
		}
	});

}