function popAdmin(url)
{
	win=window.open(url, 'winname', 'scrollbars=1,status=0, location=0, resizable=1, menubar=0, directories=0, toolbar=0, width=700,height=550, top=10, left=10');
}
var win=null;
window.name="site";

		function opengal(laius,korgus,id,pid,kus,table,lang){
			var laius = '500';
			var korgus = '400';
			var kyssa=window.open('/gal.php?lang='+lang+'&pid='+pid+'&id='+id+'&kus='+kus+'&table='+table+'&did=<?=$did?>&path=<?=$path?>&width='+laius+'&height='+korgus,'','scrollbars=yes,width='+laius+',height='+korgus+',resizable=yes,top=10,left=10');	
		}		

		function nothing(){
			return;
		}	

		function checknum(val) /*forms*/
		{
			temp_val=val.split("");
			_exp = new RegExp(/\d/);
			_exp2 = new RegExp(/,/);
			_exp3 = new RegExp(/\./);
			ret_str='';
			for(i=0;i<temp_val.length;i++)
			{
				temp=_exp.test(temp_val[i]);
				temp2=_exp2.test(temp_val[i]);
				temp3=_exp3.test(temp_val[i]);
				if(temp || temp2 || temp3 || temp_val[i]==' ') 
				{
					ret_str+=temp_val[i];
				}

			}
			return ret_str;
		}


function popWin(url,he,wi){
		if(he<=0)he = '300';
		if(wi<=0)wi = '300';
		
		var kyssa=window.open(url,'','scrollbars=yes,width='+wi+',height='+he+',resizable=yes,top=10,left=10');	
}

	function myHide(useDiv){
		 if (document.getElementById) {
				eval("document.getElementById(useDiv).style.visibility = \"hidden\"");
			  } else {
				if (document.layers) {
					document.layers[useDiv].visibility = 'hidden';
				} else {
				  if (document.all) {
					eval("document.all."+useDiv+".style.visibility = \"hidden\"");
				  }
				}
			  }
		}

		function myShow(useDiv)
		{
			 if (document.getElementById) {
				eval("document.getElementById(useDiv).style.visibility = \"visible\"");
			  } else {
				if (document.layers) {
					document.layers[useDiv].visibility = 'show';
				} else {
				  if (document.all) {
					eval("document.all."+useDiv+".style.visibility = \"visible\"");
				  }
				}
			  }

		}

		function confirmation2(url,txt)
		{
			input_box=confirm(txt);
			if (input_box==true){ document.location=url}
		}

	function sendMail(name, domain){
		//window.location = "m&#97;ilto&#58;" + name + "&#64;" + domain;

			win = window.open("mailto:" + name + "@" + domain,'emailWindow'); 
				if (win && win.open &&!win.closed) win.close(); 
		 

			
		
	}


  var modalContentEdited = false;
    var refreshTimeout = null;
    $(function () {
        $('#addNewQuery').click(function(){
              popModal('addNewQuery', []);
        });

        $('#modalWindow').click(function() {
            if(modalContentEdited) {
                 if(!confirm($('#modalWindowConfirmation').text())) {
                     return;
                 }
            }
            closeModalWindows();
        });

        $('#queriesTable tr').hover(function () {
            $(this).addClass('rowHover');
          },
          function () {
            $(this).removeClass("rowHover");
          });

        $('#queriesTable tr').click(function(){
            document.location = document.location.pathname + '?act=modify&queryId='+$(this).attr('rel');
        });

        $('#queriesTable tr:first').unbind('click');

        $('body').keyup(function(){
           if(typeof refreshReload == 'function') {
                refreshReload();
            }
        });

        $('body').click(function(){
            if(typeof refreshReload == 'function') {
                refreshReload();
            }
        });

    });




    function popModal(modalName, args){
        setLoadingPage();

        $('body').append('<div id="modalPopup"></div>');
        $('#modalPopup').html('<div id="modalClose" onClick="$(\'#modalPopup\').remove();closeModalWindows();"></div>');

        if(args.html!= null) {
            $('#modalPopup').html( args.html );
            elementPosition('#modalPopup', 'horizontal', 'center');
            elementPosition('#modalPopup', 'vertical', 'center');

            $('#loading').remove();
            $('#modalPopup').show();

        } else{

        $.post('/index.php', {
            requestAjax: 1,
            act: 'popup',
            modalName: modalName,
            args: args

        }, function( data ) {
            modalContentEdited = false;

            $('#modalPopup').append( data );
            elementPosition('#modalPopup', 'horizontal', 'center');
            elementPosition('#modalPopup', 'vertical', 'center');

            $('#modalPopup').find('input, select, textarea').change(function() {
                modalContentEdited = true;
            });
            $('#modalPopup').find('input, select, textarea').keyup(function() {
                modalContentEdited = true;
            });

            $('#modalPopup').find('#addNewQueryForm').submit(function(){

                var postArguments = $("#addNewQueryForm").serialize();
                postArguments+= '&requestAjax=1';
                postArguments+= '&act=saveQuery';
                postArguments+= '&modalName='+modalName;


                 $.post('/index.php', postArguments , function(data) {
                    if(data == 'OK') {
                        $('#messages').html('Päring salvestatud!');
                        setTimeout('closeModalWindows()', 2500);
                    } else {
                        $('#messages').html('Päringu salvestamine ebaõnnestus!');

                    }
                     $('#messages').show();
                 });
                return false;
            });

            $('#loading').remove();
            $('#modalPopup').show();
            $('#modalPopup').find('input[type=text]:first').focus();



        });
        }

    }

    function setLoadingPage(){
        $('body').append('<div id="loading"><img src="/gfx/ajax-loader.gif" alt="" /></div>');
        elementPosition('#loading', 'horizontal', 'center');
        elementPosition('#loading', 'vertical', 'center', 'floating');
        $('#loading, #modalWindow').show();
    }

    function closeModalWindows() {

	$('#calPopup, #modalWindowContent, #allFilters, #modalPopup').remove();
	$('#mailPreview, #pdfPreview, #sessionNotice, #modalWindow, #loading, #saveFilter, #nxDatePicker').hide();
    modalContentEdited = false;

}

    function elementPosition( element_id, position_type, position, floating ) {

	var elem = $(this), window_width = elem.width(), window_height = elem.height(), element_width = $(element_id).width() + parseInt( $(element_id).css('padding-left') ) + parseInt( $(element_id).css('padding-right') ), element_height = $(element_id).height() + parseInt( $(element_id).css('padding-top') ) + parseInt( $(element_id).css('padding-bottom') );

	if( position_type == 'horizontal' ) {

		var css_variable = 'left';

		if( position == 'center' ) {
			var css_value = ( window_width / 2 - element_width / 2 );
		}

	} else if( position_type == 'vertical' ) {

		var css_variable = 'top', elementOffset = $(element_id).offset().top;

		if( position == 'center' ) {
			var css_value = ( window_height / 2 - element_height / 2 );
		}

		if( floating == 'floating' ) {

			$(window).scroll(function () {

				var offset = $(document).scrollTop() + ( css_value - elementOffset );
				$(element_id).animate({top: offset}, {duration: 0, queue: false});

			});

		}

		css_value += elementOffset;

	}

	$(element_id).css(css_variable, css_value);

}
