// checkbox kijelölés
$(document).ready(function() {
	$('#checkall').click(
	 function() {
		 $("INPUT[type='checkbox']").attr('checked', $('#checkall').is(':checked'));
	 });
 });

// mp3 letöltés számláló
$('.dl_mp3').live('click', function(){
	mp3id=$(this).attr("rel");
	$.get("/functions.php", { mode: "dl_mp3", id: mp3id } );
});

// mp3 értékelés
$('.rate_mp3').live('click', function(){
	mp3id=$(this).attr("id");
	mp3rt=$(this).attr("rel");
	mp3rates = parseInt(mp3rt)+1;
	$.get("/functions.php", { mode: "rate_mp3", id: mp3id } );
	$("#rate"+mp3id).html(mp3rates);
	return false;
});

// tetszik (új rendszer)
$('.like').live('click', function(){
	lid=$(this).attr("id");
	ln=$(this).attr("rel");
	ltype=$(this).attr("rev");
	likes = parseInt(ln)+1;
	$.get("/functions.php", { mode: "like", id: lid, type: ltype } );
	$("#like"+lid).html(likes);
	return false;
});

// tetszik (adatlap)
$('.btlike').live('click', function(){
	lid=$(this).attr("id");
	ln=$(this).attr("rel");
	ltype=$(this).attr("rev");
	likes = parseInt(ln)+1;
	$.get("/functions.php", { mode: "like", id: lid, type: ltype } );
	$("#like"+lid).html(likes);
	alert("Köszönjük!");
	return false;
});

// új jelzés eltuntetése
$('#update').live('click', function(){
	$(".new").fadeOut('slow');
	$("#update").fadeOut('slow');
	pg=$(this).attr("rel");
	$.get("/functions.php", { mode: "hide_new", page: pg } );
	return false;
});


// mp3 jelentés
$('.report_mp3').click(function(){
	mp3id=$(this).attr("rel");
	var answer = confirm ("Biztos jelenteni akarok a zeneszámot, mint nem elérheto feltöltés?")
	if(answer) {
		$.get("/functions.php", { mode: "report_mp3", id: mp3id } );
		alert("Köszönjük a bejelentést, átnézzük a linket!");
		}
	else
	alert ("Rendben, nem történt bejelentés.")
	return false;
});

// bejelentkezés ellenenorzése
function vlogin() {
	if (document.getElementById('username').value == '' || document.getElementById('password').value == '') {
		alert("Nem töltöttél ki minden mezot!");
		return false;
	}
	return true;
}

// feltöltés váltása XE?
$(document).ready(function() {
  $(".udl").hide();
});

$(document).ready(function() {
  $("[name=utoggler]").each(function(i) {
    $(this).change(function(){
      divId = $(this).val();
      $(".udl").hide(); {
        $("#"+divId).show();
	  }
    });
  });
});


// komment törlés
$('img.delc').click(function(){
	cid=$(this).attr("alt");
	$.get("/functions.php", { mode: "del_com", id: cid } );
		$('#msg'+cid).hide('slow',1);
});

// kedvencekhez adás
$('.favorite').live('click', function(){
	mp3id=$(this).attr("rel");
	if($(this).attr("src")=="/images/bt_passive_star.gif") {
		// hozzáadás		
		$('#s'+mp3id).attr({ src: "/images/bt_active_star.gif"});
		$.get("/functions.php", { mode: "add_favorite", id: mp3id } );
	}
	else {
		// törlés
		$('#s'+mp3id).attr({ src: "/images/bt_passive_star.gif"});	
		$.get("/functions.php", { mode: "del_favorite", id: mp3id } );
	}
	return false;
});

// kedvencekhez adás, ha nincs bejeltkezve
$('.no-favorite').click(function(){
	alert("A szolgáltatás használatához kell jelentkezned!");	
	return false;
});

// kedvencekbol törlés
$('img.delfav').click(function(){
	cid=$(this).attr("alt");
	$(mit+'_kep').attr({ src: "themes/"+theme+"/news_expand.gif"});	
	$.get("/functions.php", { mode: "delete_favorite", id: cid } );
});

function iAmACallbackFunction() {
	$("#top10").show('slow');
	$("#top10").fadeTo('slow',1);
}

mode = "rates";
time = "month";
list = "10";
	
// top10 tartalom váltás
$('.changemp3').click(function(){
	
	wtf = $(this).attr("rel");

	// idoszak vagy típus
	if((wtf=="downloads") || (wtf=="rates") || (wtf=="favorites"))
		mode = wtf;
	else if((wtf=="10") || (wtf=="50") || (wtf=="100"))
		list = wtf;
	else
		time = wtf;
	
	// letöltés/értékelés
	$('.miner').removeClass('miner');
	$("#m"+mode).addClass('miner');
	
	// idoszak
	$('.minor').removeClass('minor');
	$("#t"+time).addClass('minor');
	
	// idoszak
	$('.minar').removeClass('minar');
	$("#l"+list).addClass('minar');

	// betöltés
	$("#top10").html("<center><img style=\"vertical-align: -4px;\" src=\"/images/ajax-loader.gif\"> Betöltés folyamatban!</center>");
	$("#top10").load("/top10enginer.php?mode="+mode+"&time="+time+"&list="+list, {}, iAmACallbackFunction);
	return false;
});

// top video váltás
$('.changevideo').click(function(){
	if($.browser.msie){
		alert("Ez a funkció Internet Explorer böngészobol nem elérheto!");
		return false;
	}
	$('#load_gif').fadeIn('slow');
	videoid = $(this).attr("rel");
	$('#video_title').load("/functions.php?mode=get_video&id="+videoid+"&info=title");
	$('#video_month').load("/functions.php?mode=get_video&id="+videoid+"&info=month");
	
	// kijelölés
	$('.vminer').removeClass('vminer');
	$(this).addClass('vminer');

      $.ajax({
            type: "GET",
            url: "/functions.php?mode=get_video&id="+videoid+"&info=url",
            dataType: "html",
            error: function(){
              alert('A klipet nem sikerült betölteni, próbáld újra!');
			  $('#load_gif').fadeOut('slow');
          },
          success: function(data){
                s1.addParam('flashvars','file='+data);
				s1.write('mediaspace');
				$('#load_gif').fadeOut('slow');
      }
      }); 
	  
	//return false;
});

// barátokhoz adás
$('.dvfriend').click(function(){
	friendid = $('.friend').attr("id");
	if($('.friend').attr("rel") == '0') {
		$('.friend').fadeIn('slow');
		$('.friend').attr({ rel: "1"});	
		$.get("/functions.php", { mode: "add_friend", id: friendid } );
}
	else {
		$('.friend').fadeOut('slow');
		$('.friend').attr({ rel: "0"});
		$.get("/functions.php", { mode: "del_friend", id: friendid } );
	}
	return false;
});

// div klónozás
$('#clonedb').blur(function(){
  max1=$(this).value;

  for(var i= 1; i<= max1; i++)
  {
    $('#ideide').prepend($('#ezaz').clone());
  }
 });

// zenehallgatás
$(".py").click(function() {
	if($("#openCloseIdentifier").is(":hidden")) {
		$(mit+'_div').fadeTo('slow',0);
		$(mit+'_div').slideUp('slow');
	} else {
		alert("Új betöltése");
	}
	return false;
});


mid = 0;

// lejátszó
$('.play_mp3').live('click', function(){
	mit = "#slider";
	$(this).attr({ src: "/images/icon_play_off.gif"});	
	if($(this).attr("rel") == mid){
		if($.browser.msie){
			slider.style.display = 'none';
		}
		else {
			$(mit).fadeTo('slow',0);
			$(mit).slideUp('slow');	
		}
		$('#p'+mid).attr({ src: "/images/icon_play.gif"});
		mid = 0;
	}
	else {
		$('#p'+mid).attr({ src: "/images/icon_play.gif"});
		mid = $(this).attr("rel");
		if($(mit).css("display")!='block'){
			if($.browser.msie){
				slider.style.display = 'block';
			}
			else {
				$(mit).slideDown('slow');
				$(mit).fadeTo('slow',1);
			}
		}
		$('#p'+mid).attr({ src: "/images/icon_load_playbar.gif"})
		$("#player").load("/functions.php?mode=player&id="+mid, {}, $('#p'+mid).attr({ src: "/images/icon_play_off.gif"}));
	}
	return false;
});

// lejátszó bezár
$('.closebig').live('click', function(){
	mit = "#slider";
	if($(mit).css("display")=='block'){
		if($.browser.msie){
			slider.style.display = 'none';
		}
		else {
			$(mit).fadeTo('slow',0);
			$(mit).slideUp('slow');	
		}
	}
	$('#p'+mid).attr({ src: "/images/icon_play.gif"});
	return false;
});

// feltöltés tipusának kiválasztása
$(":upi").click(function(){
	if($("#up_m").is(":checked"))
	{
		$("#pbp").hide();
		$("#pbm").show();	
				  
	}
	if($("#up_p").is(":checked"))
	{
		$("#pbm").hide();
		$("#pbp").show();	  
	}
});

/*

 * jQuery Karakter Számláló */

(function($){  
	$.fn.textareaCount = function(options, fn) {   
		var defaults = {  
			maxCharacterSize: -1,  
			originalStyle: 'originalTextareaInfo',
			warningStyle: 'warningTextareaInfo',  
			warningNumber: 20,
			displayFormat: '#input characters | #words words'
		};  
		var options = $.extend(defaults, options);
		
		var container = $(this);
		
		$("<div class='charleft'>&nbsp;</div>").insertAfter(container);
		
		//create charleft css
		var charLeftCss = {
			'width' : container.width()
		};
		
		var charLeftInfo = getNextCharLeftInformation(container);
		charLeftInfo.addClass(options.originalStyle);
		charLeftInfo.css(charLeftCss);
		
		var numInput = 0;
		var maxCharacters = options.maxCharacterSize;
		var numLeft = 0;
		var numWords = 0;
				
		container.bind('keyup', function(event){limitTextAreaByCharacterCount();})
				 .bind('mouseover', function(event){setTimeout(function(){limitTextAreaByCharacterCount();}, 10);})
				 .bind('paste', function(event){setTimeout(function(){limitTextAreaByCharacterCount();}, 10);});
		
		
		function limitTextAreaByCharacterCount(){
			charLeftInfo.html(countByCharacters());
			//function call back
			if(typeof fn != 'undefined'){
				fn.call(this, getInfo());
			}
			return true;
		}
		
		function countByCharacters(){
			var content = container.val();
			var contentLength = content.length;
			
			//Start Cut
			if(options.maxCharacterSize > 0){
				//If copied content is already more than maxCharacterSize, chop it to maxCharacterSize.
				if(contentLength >= options.maxCharacterSize) {
					content = content.substring(0, options.maxCharacterSize); 				
				}
				
				var newlineCount = getNewlineCount(content);
				
				// newlineCount new line character. For windows, it occupies 2 characters
				var systemmaxCharacterSize = options.maxCharacterSize - newlineCount;
				if (!isWin()){
					 systemmaxCharacterSize = options.maxCharacterSize
				}
				if(contentLength > systemmaxCharacterSize){
					//avoid scroll bar moving
					var originalScrollTopPosition = this.scrollTop;
					container.val(content.substring(0, systemmaxCharacterSize));
					this.scrollTop = originalScrollTopPosition;
				}
				charLeftInfo.removeClass(options.warningStyle);
				if(systemmaxCharacterSize - contentLength <= options.warningNumber){
					charLeftInfo.addClass(options.warningStyle);
				}
				
				numInput = container.val().length + newlineCount;
				if(!isWin()){
					numInput = container.val().length;
				}
			
				numWords = countWord(getCleanedWordString(container.val()));
				
				numLeft = maxCharacters - numInput;
			} else {
				//normal count, no cut
				var newlineCount = getNewlineCount(content);
				numInput = container.val().length + newlineCount;
				if(!isWin()){
					numInput = container.val().length;
				}
				numWords = countWord(getCleanedWordString(container.val()));
			}
			
			return formatDisplayInfo();
		}
		
		function formatDisplayInfo(){
			var format = options.displayFormat;
			format = format.replace('#input', numInput);
			format = format.replace('#words', numWords);
			//When maxCharacters <= 0, #max, #left cannot be substituted.
			if(maxCharacters > 0){
				format = format.replace('#max', maxCharacters);
				format = format.replace('#left', numLeft);
			}
			return format;
		}
		
		function getInfo(){
			var info = {
				input: numInput,
				max: maxCharacters,
				left: numLeft,
				words: numWords
			};
			return info;
		}
		
		function getNextCharLeftInformation(container){
				return container.next('.charleft');
		}
		
		function isWin(){
			var strOS = navigator.appVersion;
			if (strOS.toLowerCase().indexOf('win') != -1){
				return true;
			}
			return false;
		}
		
		function getNewlineCount(content){
			var newlineCount = 0;
			for(var i=0; i<content.length;i++){
				if(content.charAt(i) == '\n'){
					newlineCount++;
				}
			}
			return newlineCount;
		}
		
		function getCleanedWordString(content){
			var fullStr = content + " ";
			var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
			var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
			var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
			var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
			var splitString = cleanedStr.split(" ");
			return splitString;
		}
		
		function countWord(cleanedWordString){
			var word_count = cleanedWordString.length-1;
			return word_count;
		}
	};  
})(jQuery); 
