//<![CDATA[
$(document).ready(function(){
    jQuery.fn.limitMaxlength = function(options){

	var settings = jQuery.extend({
		attribute: "maxlength",
		onLimit: function(){},
		onEdit: function(){}
	}, options);

	// Event handler to limit the textarea
	var onEdit = function(){
		var textarea = jQuery(this);
		var maxlength = parseInt(textarea.attr(settings.attribute));

		if(textarea.val().length > maxlength){
			textarea.val(textarea.val().substr(0, maxlength));

			// Call the onlimit handler within the scope of the textarea
			jQuery.proxy(settings.onLimit, this)();
		}

		// Call the onEdit handler within the scope of the textarea
		jQuery.proxy(settings.onEdit, this)(maxlength - textarea.val().length);
	}

	this.each(onEdit);

	return this.keyup(onEdit)
				.keydown(onEdit)
				.focus(onEdit)
				.live('input paste', onEdit);
}

    $("li a").hover(function() {
        $(this).next("em").animate({opacity: "show", left: "5"}, "fast");
    }, function() {
        $(this).next("em").animate({opacity: "hide", left: "5"}, "fast");
    });
    $("li.submenu a").hover(function() {
        $(this).next("em").animate({opacity: "show", left: "5"}, "fast");
    }, function() {
        $(this).next("em").animate({opacity: "hide", left: "5"}, "fast");
    });
    $("#loading").hide();
    $("#loading").ajaxStart(function(){
        $(this).show();
    });
    $("#loading").ajaxStop(function(){
        $(this).hide();
    });
    $("div.modal_widow_background").click(function(){
        close_modal(null);
    });
    $("#areac").click(function(){
        $(".napoveda").fadeIn(500);
        var input = $("input[name='nahlad-obrazok']");
        var position = input.position();
        $("#nahlad-obrazok").css({top: position.top+"px"});
    });
    $(".zobraz_obrazok").click(function(){
        $("img.ukazka").fadeIn(2500);
    });
    $("option[value='2'], option[value='3']").click(function(){
        $("li.info_obrazok").fadeIn(500);
    });
    $("option[value='1'], option[value='4']").click(function(){
        $("li.info_obrazok").fadeOut(500);
    });
    $('a[rel="_blank"]').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
    $('a[rel="facebox"]').facebox({
        loadingImage : 'facebox/loading.gif',
        closeImage   : 'facebox/closelabel.png'
    });
    var onEditCallback = function(remaining){
        $('.counter').text("Zostáva znakov: " + remaining);

        if(remaining > 0){
            $("textarea[name='kratky_popis']").css('border', '1 px solid white');
        }
    }

    var onLimitCallback = function(){
        $("textarea[name='kratky_popis']").css('border', '1px solid red');
    }

    $("textarea[name='kratky_popis']").limitMaxlength({
        onEdit: onEditCallback,
        onLimit: onLimitCallback
    });
    
    $("input[name='show-article']").click(function(){
        $.facebox(function() {
        
        var page = null;
        if(!$("option[name='pripojitObrazok']").length){
            page = "other";
        }else{
            page = "novinky";
        }
        $.ajax({
            data: {'text' : $('#areac').val(), 'nadpis' : $("input[name='nazov_clanku']").val(), 'obrazok' : $("select[name='pripojitObrazok']").val()},
            error: function() {
                $.facebox('Nastala neznáma chyba, skúste to neskor');
            },
            success: function(data) {
                $.facebox(data);
            },
            type: 'post',
            url: 'show-articles.php?page='+page
        });
       
    });
    });
    $("input[name='url-banner']").blur(function(){
        if(($(this).val().length) > 4)
        {
            var id = $("input[name='id']").val();
            var url = $("input[name='url-banner']").val();
            $.ajax({
                data: {'id' : id, 'url' : url},
                beforeSend: function()
                {
                    $("#sponzoriVysledok").show();
                    $("#sponzoriVysledok").html("&nbsp;");
                },
                success: function(response)
                {
                    $("#sponzoriVysledok").html(response).fadeOut(2000);
                },
                type: 'post',
                url: 'ajax.php'
            });            
        }
    });
    
//    $("input").focus(function(){
//       $(this).addClass("idleField");
//    });
//    $("input").blur(function(){
//       $(this).removeClass("idleField");
//    });

//------------------- Modal widow ---------------------
    //get the height and width of the page

    var window_width = $(window).width();
    var window_height = $(window).height();

    //vertical and horizontal centering of modal window(s)
    /*we will use each function so if we have more then 1
    modal window we center them all*/
    $('.modal_window').each(function(){

        //get the height and width of the modal
        var modal_height = $(this).outerHeight();
        var modal_width = $(this).outerWidth();

        //calculate top and left offset needed for centering
        var top = (window_height-modal_height)/2;
        var left = (window_width-modal_width)/2;

        //apply new top and left css values
        $(this).css({'top' : top , 'left' : left});
    });
        $('.activate_modal').click(function(){
              //get the id of the modal window stored in the name of the activating element
              var modal_id = $(this).attr('name');
              //use the function to show it
              show_modal(modal_id);
        });

        $('.close_modal').click(function(){
            //use the function to close it
            close_modal(null);
        });
        $(".add_textarea_b").click(function(){
            $("#areac").insertAtCaret("*\*tučné*\*");
        });
        $(".add_textarea_i").click(function(){
            $("#areac").insertAtCaret("*kurzíva*");
        });
        $(".add_textarea_h1").click(function(){
            $("#areac").insertAtCaret("Nadpis úrovne 1.\n###########\n");
        });
        $(".add_textarea_h2").click(function(){
            $("#areac").insertAtCaret("Nadpis úrovne 2.\n***********\n");
        });
        $(".add_textarea_h3").click(function(){
            $("#areac").insertAtCaret("Nadpis úrovne 3.\n===========\n");
        });
});

        function addObrazokTextarea(){
            var obrazok = $("#nahladObrazku").val();
            var zarovnanie = "style=\"float:" + $("#zarovnajObrazok").val() + "\"";
            var velkost = $("#nahlad-check").is(':checked');
            var parse = obrazok.split("/");
            var addObrazok = "<a href=\"admin/" + parse[0] + "/" + (velkost == true ? "medium/" : "original/") + parse[2] + "\" rel=\"facebox\" title=\"Kliknutim zobrazite obrázok\" "+zarovnanie+">\n\[* " + obrazok + " .(" + parse[2] + ") *]\n\</a>";
            $("#areac").insertAtCaret(addObrazok);
            //<a href="admin/novinkyObrazky/original/cyklista_na_ceste.jpg" rel="facebox" title="Kliknutim na obrazok ho zvecsite">
            //[* novinkyObrazky/small/cyklista_na_ceste.jpg .(cyklista_na_ceste.jpg) <]
            //</a>
            close_modal(null);
        }

    //THE FUNCTIONS

    function close_modal(cas){
        if(cas == null){
            //hide modal window(s)
            $('.modal_window').fadeOut(500);
            $(".modal_widow_background").fadeOut(500);
        }else{
            setTimeout
                (function(){
                     $('.modal_window').fadeOut(500);
                     },
                 cas
                );
        }
    }
    function show_modal(modal_id){
        //show the modal window
        $('#'+modal_id).fadeIn(500);
        $(".modal_widow_background").show();
        $(".modal_widow_background").css({opacity: 0.3});
        $(".modal_widow_background").css({height: $("html").height()});
    }
    $.fn.extend({
  insertAtCaret: function(myValue){
  var obj;
  if( typeof this[0].name !='undefined' ) obj = this[0];
  else obj = this;

  if ($.browser.msie) {
    obj.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    obj.focus();
    }
  else if ($.browser.mozilla || $.browser.webkit) {
    var startPos = obj.selectionStart;
    var endPos = obj.selectionEnd;
    var scrollTop = obj.scrollTop;
    obj.value = obj.value.substring(0, startPos)+myValue+obj.value.substring(endPos,obj.value.length);
    obj.focus();
    obj.selectionStart = startPos + myValue.length;
    obj.selectionEnd = startPos + myValue.length;
    obj.scrollTop = scrollTop;
  } else {
    obj.value += myValue;
    obj.focus();
   }
 }
});
//]]>
