$(document).ready(function(){	
	$('#screens a').lightBox();
	$('.jsskryt').hide();
	
	checkNewInChat();
	var nich = setInterval("checkNewInChat()", 20000); 
	
	$('#pomocnidetekb').click(function(event){
		if($('#pomocnidetekb').html()=='Zobraziť pomocných detektívov') {
			$('#pomocnidetek').fadeIn("fast");
			$('#pomocnidetekb').html('Skryť pomocných detektívov');
		} else {
			$('#pomocnidetek').fadeOut("fast");
			$('#pomocnidetekb').html('Zobraziť pomocných detektívov');
		}
		event.preventDefault();
	});

	$('#formular_img')
	.click(function(event) { $('#feedback').show(); $('#formular_img').hide(); event.preventDefault(); })
	.mouseover(function(){ $(this).css('backgroundImage', 'url(images/gangbar/units/gbunits_h.gif)'); })
	.mouseout(function(){ $(this).css('backgroundImage', 'url(images/gangbar/units/gbunits.gif)'); });
	$('#hidefeed')
	.click(function(event) { $('#feedback').hide(); $('#formular_img').show(); event.preventDefault(); })
	.mouseover(function(){ $(this).css('backgroundPosition', '-256px -39px'); })
	.mouseout(function(){ $(this).css('backgroundPosition', '-256px 0'); });
	
	$('.likeit_h, .dislikeit_h, .tlikeit_h, .tdislikeit_h').live('click', function(event){ event.preventDefault(); });
	
	$('.likeit')
	.live('click', function(event) {
		var tento = this;
		var prispevokId = $(this).attr('id').substr(2);
		$.get("ajaxfiles/like.php", { id: prispevokId, kind: 'post', dislike: 'no' }, function(data) {
			if(data=='OK') {
				var pocetl = $('#likespan'+prispevokId).html().substr(2)*1+1;
				$('#likespan'+prispevokId).html('+ '+pocetl);
				$(tento).removeClass('likeit').addClass('likeit_h');
			}
		});
		event.preventDefault(); 
	})
	.live('mouseover', function(){ $(this).css('backgroundPosition', '0 -15px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '0 0'); });
	$('.dislikeit')
	.live('click', function(event) {
		var tento = this;
		var prispevokId = $(this).attr('id').substr(2);
		$.get("ajaxfiles/like.php", { id: prispevokId, kind: 'post', dislike: 'yes' }, function(data) {
			if(data=='OK') {
				var pocetl = $('#dislikespan'+prispevokId).html().substr(2)*1+1;
				$('#dislikespan'+prispevokId).html('- '+pocetl);
				$(tento).removeClass('dislikeit').addClass('dislikeit_h');
			}
		});
		event.preventDefault(); 
	})
	.live('mouseover', function(){ $(this).css('backgroundPosition', '-15px -15px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '-15px 0'); });
	
	$('.tlikeit')
	.live('click', function(event) {  
		var tento = this;
		var prispevokId = $(this).attr('id').substr(2);
		$.get("ajaxfiles/like.php", { id: prispevokId, kind: 'topic', dislike: 'no' }, function(data) {
			if(data=='OK') {
				var pocetl = $('#tlikespan'+prispevokId).html().substr(2)*1+1;
				$('#tlikespan'+prispevokId).html('+ '+pocetl);
				$(tento).removeClass('tlikeit').addClass('tlikeit_h');
			}
		});
		event.preventDefault(); 
	})
	.live('mouseover', function(){ $(this).css('backgroundPosition', '0 -15px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '0 0'); });
	$('.tdislikeit')
	.live('click', function(event) {  
		var tento = this;
		var prispevokId = $(this).attr('id').substr(2);
		$.get("ajaxfiles/like.php", { id: prispevokId, kind: 'topic', dislike: 'yes' }, function(data) {
			if(data=='OK') {
				var pocetl = $('#tdislikespan'+prispevokId).html().substr(2)*1+1;
				$('#tdislikespan'+prispevokId).html('- '+pocetl);
				$(tento).removeClass('tdislikeit').addClass('tdislikeit_h');
			}
		});
		event.preventDefault(); 
	})
	.live('mouseover', function(){ $(this).css('backgroundPosition', '-15px -15px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '-15px 0'); });
	
	$('#reg_muz').click(function(event) { $('input[name=pohlavie]').val('1'); $(this).css('backgroundPosition', '0 -56px'); $('#reg_zena').css('backgroundPosition', '0 0'); $(this).removeClass('pohlmuz'); event.preventDefault(); });
	$('#reg_zena').click(function(event) { $('input[name=pohlavie]').val('2'); $(this).css('backgroundPosition', '0 -63px'); $('#reg_muz').css('backgroundPosition', '0 0'); $(this).removeClass('pohlzena'); event.preventDefault(); });
	$('.pohlmuz')
	.live('mouseover', function(){ $(this).css('backgroundPosition', '0 -56px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '0 0'); });
	$('.pohlzena')
	.live('mouseover', function(){ $(this).css('backgroundPosition', '0 -63px'); })
	.live('mouseout', function(){ $(this).css('backgroundPosition', '0 0'); });
	$('input[name=submit]')
	.mouseover(function(){ $(this).attr('src', '/images/registration/registrovat_h.gif'); })
	.mouseout(function(){ $(this).attr('src', '/images/registration/registrovat.gif'); });
	
	$('#skflag')
	.mouseover(function(){ this.src='images/zlatky/sk_h.png'; })
	.mouseout(function(){ this.src='images/zlatky/sk.png'; });
	$('#czflag')
	.mouseover(function(){ this.src='images/zlatky/cz_h.png'; })
	.mouseout(function(){ this.src='images/zlatky/cz.png'; });
	
	$('#fotkystiahnut')
	.mouseover(function(){ this.src='images/download_h.gif'; })
	.mouseout(function(){ this.src='images/download.gif'; });
	
	$('#hidebar')
	.click(function(event) { $('#gangbar, #feedback, #formular').hide(); $('#showbar').css('display', 'block'); $.cookie('gangbar', '2'); event.preventDefault(); })
	.mouseover(function(){ $(this).css('backgroundPosition', '-256px -39px'); })
	.mouseout(function(){ $(this).css('backgroundPosition', '-256px 0'); });
	$('#showbar')
	.click(function(event) { $('#gangbar, #formular').show(); $('#showbar').hide(); $.cookie('gangbar', '1'); event.preventDefault(); })
	.mouseover(function(){ $(this).css('backgroundPosition', '-210px -39px'); })
	.mouseout(function(){ $(this).css('backgroundPosition', '-210px 0'); });
	
	$(".otaznik,#formular_img, .zobrazitpomocnika").click(function(event){
		$("#napoved").show();
		$("#napoved").animate({
			height: '215px'
		}, 300, function() {
		    $(document).scrollTo(0, 500);
		});
    	event.preventDefault();
  	});
	$("#zatvorit").click(function(event){
    	$("#napoved").animate({
			height: '0'
		}, 300, function() {
			$("#napoved").hide();
		});
    	event.preventDefault();
  	});
  	
  	//moneybookers gateway
  	$('#suma').keyup(function() {
  		if($('#suma').val()>50) { 
  			var tehliciek = Math.round(($('#suma').val()-0.5)*50); 
  			var zatehlicky = Math.round(($('#suma').val()*100-50))/100; 
  			var poplatok = 0.5;
  		}
  		else { 
  			var tehliciek = Math.round($('#suma').val()/1.01*50); 
  			var zatehlicky = Math.round($('#suma').val()*100/1.01)/100; 
  			var poplatok = Math.round(($('#suma').val()-zatehlicky)*100)/100;
  		}
  		$('#suma').val($('#suma').val().replace(",", "."));
  		$('#pocettehliciek').val(tehliciek);
  		$('input[name=pocettehliciek]').val(tehliciek);
  		$('#tehliciek').html(tehliciek);
  		$('#other').html(tehliciek);
  		$(".sumy").removeAttr("checked");
  		$("#otherch").attr("checked", "checked");
  		$("#otherch").val($('#suma').val());
  		$("#amount2").val(zatehlicky);
  		$("#amount3").val(poplatok);
  	});
  	$(".sumy").change(function() { 
  		var tehliciek = Math.round($(this).val()/1.01*50);
  		var zatehlicky = Math.round($(this).val()*100/1.01)/100;
  		var poplatok = Math.round(($(this).val()-zatehlicky)*100)/100;
  		$("#suma").val("");
  		$('#pocettehliciek').val(tehliciek);
  		$('input[name=pocettehliciek]').val(tehliciek);
  		$("#amount2").val(zatehlicky);
  		$("#amount3").val(poplatok);
  	});
  	
  	$('#mbsubmit').submit(function(event){
  		$.ajax({ type: "POST", url: "/ajaxfiles/mb.php", data: $("form").serialize(), async: false });
  	});
  	
  	//garaz
  	$("#allcars").click(function(){
  		if($("#allcars").is(":checked")) { $("input[type=checkbox]").attr("checked", "checked"); }
  	 	else { $("input[type=checkbox]").removeAttr("checked"); }
  	});
  	
  	//poulicne preteky
  	$("#p18").click(function(){
  		if($("#p18").is(":checked")) { $(".p18").attr("checked", "checked"); }
  	 	else { $(".p18").removeAttr("checked"); }
  	});
  	$("#p22").click(function(){
  		if($("#p22").is(":checked")) { $(".p22").attr("checked", "checked"); }
  	 	else { $(".p22").removeAttr("checked"); }
  	});
  	
  	// sukromne spravy
  	$('#vyhladavanie').click(function(){
  		if($(this).html()=='Vyhľadávanie') {
  			$("#vyhladavanie2").show();
  			$("#vyhladavanie2").animate({
  				height: '50px'
  			}, 100, function(){
  				$('#vyhladavanie').html('Vyhľadávanie (skryť)');
  			});
  			
  		} else {
  			$("#vyhladavanie2").animate({
  				height: '0'
  			}, 100, function() {
  				$("#vyhladavanie2").hide();
  				$('#vyhladavanie').html('Vyhľadávanie');
  			});
			event.preventDefault();
  		}
  		event.preventDefault();
  	});
  	
  	// BB a smajliky
  	$('.bbb')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/bold_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/bold.gif'); })
  	.click(function(event) {
  		insertBB('[b]', '[/b]', $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	$('.bbi')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/italic_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/italic.gif'); })
  	.click(function(event) {
  		insertBB('[i]', '[/i]', $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	$('.bbu')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/underline_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/underline.gif'); })
  	.click(function(event) {
  		insertBB('[u]', '[/u]', $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	$('.bbimg')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/image_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/image.gif'); })
  	.click(function(event) {
  		var href = prompt("Vlož adresu obrázka:", "");
  		if(href==null) href = '';
  		insertBB('[img]'+href, '[/img]', $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	$('.bbcolour')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/color_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/color.gif'); })
  	.click(function(event) {
  		var farba = prompt("Napíš farbu textu (anglickým slovom alebo hexadecimálne):", "red");
  		if(farba!=null) {
  			insertBB('[color=' + farba + ']', '[/color]', $('.bbtarget').get(0));
  		}
  		event.preventDefault();
  	});
  	$('.bbcenter')
  	.mouseover(function(){ $(this).children().attr('src', '/images/bbicons/center_h.gif'); })
  	.mouseout(function(){ $(this).children().attr('src', '/images/bbicons/center.gif'); })
  	.click(function(event) {
  		insertBB('[center]', '[/center]', $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	
  	$('.smile').click(function(event) {
  		insertBB('', $(this).children().attr('alt'), $('.bbtarget').get(0));
  		event.preventDefault();
  	});
  	
  	$('#showmoresmilies').click(function(){
  		if($(this).html()=='Ďalšie...') {
  			$("#moresmilies").show();
  			$("#moresmilies").animate({
  				height: '60px'
  			}, 200, function(){
  				$('#showmoresmilies').html('Menej...');
  			});
  			
  		} else {
  			$("#moresmilies").animate({
  				height: '0'
  			}, 200, function() {
  				$("#moresmilies").hide();
  				$('#showmoresmilies').html('Ďalšie...');
  			});
  			event.preventDefault();
  		}
  		event.preventDefault();
  	});
  	
});

//funkcia vracajuca nove spravy v sukromnom chate
function novyChat() {
	$.get("ajaxfiles/chat.php", { kind: 'sukromnynove' }, function(data) {
		return data;
	});
}

//funkcia aktualizuje ukazovatel nad Domov
function checkNewInChat() {
	$.get("ajaxfiles/chat.php", { kind: 'sukromnynove' }, function(data) {
		if(data=='NO') $('#novaspravavchate').attr('src', '/images/chat/newinchat.png');
		else $('#novaspravavchate').attr('src', '/images/chat/newinchat_h.png');
	});
}

//funckia na vlozenie zaciatocneho a koncoveho prvku
function insertBB(start, end, element) {
  if (document.selection) {
     element.focus();
     sel = document.selection.createRange();
     sel.text = start + sel.text + end;
  } else if (element.selectionStart || element.selectionStart == '0') {
     element.focus();
     var startPos = element.selectionStart;
     var endPos = element.selectionEnd;
     element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + end + element.value.substring(endPos, element.value.length);
  } else {
    element.value += start + end;
  }
}
