function imageurl(img) {
    var href=document.getElementsByTagName('BASE')[0].href;

    return href+'application/se/ui/images/'+img;

}


var naviObj = Class.extend({
    init: function(options) {
       this.maxElement=$("#"+options.target+" tbody > tr").length-1;
       this.onSelect=options.onSelect;
       this.target=options.target;
       this.position=0;
    }
})


var navi = Class.extend({
    init: function(options) {
        this.options = {
            target:null
        };


    $.extend(this.options,options || {});
    //this.maxElement=($(this.options.target).childNodes[1].childNodes.length-1)/2;
    this.navis.push(new naviObj({target:this.options.target, onSelect: this.options.onSelect}));
    $(document).bind('keydown',jQuery.proxy(this.keyPressEvent,this));

    },
    keyPressEvent: function(event) {
         if (event.keyCode==9) {
            this.focusNext();
        }
        if (!this.hasFocus()) return;

        this.positionValidate();

        this.unlight();
        if (event.keyCode==38) {
            this.moveUp();
        }
        if (event.keyCode==40) {
            this.moveDown();
        }
        if (event.keyCode==13) {
            $.mask.close();
            this.options.onSelect(this.row);
        }
        if (event.keycode==27) {
            this.cancelFocus();
        }
        event.stopPropagation();
        event.preventDefault();
        this.positionValidate();
        this.highlight();
    },
    cancelFocus: function() {
       this.focus=null;

    },
    hasFocus : function() {
        if (this.focus!=null) {
            return true;
        } else {
            return false;
        }
    },
    focusNext: function() {

               // place a mask but let selected elements show through (expose)

        this.focus=this.navis[this.focusNum++];
        this.position=this.focus.position;
        $("#"+this.focus.target).expose({
            onClose: jQuery.proxy(function(event) {
                this.cancelFocus();
            },this)});
       if (this.focusNum >= this.navis.length) {
            this.focusNum = 0;
        }




    },
    moveUp : function () {
        if (!this.hasFocus()) return;
        this.position--;
    },
    moveDown: function () {
         if (!this.hasFocus()) return;
        this.position++;
    },
    positionValidate: function() {
         if (!this.hasFocus()) return;
        if (this.position<0) {
            this.position=this.focus.maxElement;
        }
        if (this.position>this.focus.maxElement) {
            this.position=0;
        }
        this.focus.position=this.position;
    },
    highlight: function() {
        if (!this.hasFocus()) return;
        this.row=$("#"+this.options.target+" tbody > tr");
        this.row=this.row[this.position];
        $(this.row).addClass('highlight');
    },
    unlight: function() {
        if (!this.hasFocus()) return;
        this.row=$("#"+this.options.target+" tbody > tr");
        this.row=this.row[this.position];
        $(this.row).removeClass('highlight');
    }
});
navi.prototype.navis=[];
navi.prototype.focus=null;
navi.prototype.focusNum=0;

/*var jsWindow = Class.create();
jsWindow.zIndex=100;
jsWindow.prototype = {
    initialize: function(options) {
        this.options={
            target: 'body',
            width: 'auto',
            height: 'auto',
            modal: false,
            fixed: false,
            noheader: false,
            center: false,
            effect_down: true
        };
        Object.extend(this.options, options || { });
        this.sHeader=this.options['header'];
        this.uuid=new UUID();
        this.window=new Element('div',{
            id:'window_'+this.uuid,
            'class':'jsWindow'
        });
        this.window.setStyle({
            'position':'absolute',
            //'width':'auto',
            'height':this.options.height,
            'width':this.options.width,
            'top':'100px',
            'left':'150px',
            'zIndex':jsWindow.zIndex++
        });
        this.header=new Element('div',{
            id:'header_'+this.uuid,
            'class':'jsWindowHeader'
        });
        this.body=new Element('div',{
            id:'body_'+this.uuid,
            'class':'jsWindowBody'
        });

        this.header.setStyle({
            height: '23px'
        });
        Event.observe(document,'mousedown',this.DragStart.bind(this));
        Event.observe(document,'mousedown',this.ResizeStart.bind(this));

        Event.observe(document,'mouseup',this.DragStop.bind(this));
        Event.observe(document,'mouseup',this.ResizeStop.bind(this));

        Event.observe(document,'mousemove',this.DragMove.bind(this));
        Event.observe(document,'mousemove',this.Resize.bind(this));

        Event.observe(document,'mouseover',this.DragOver.bind(this));
        this.img=new Element('img',{
            src: imageurl('close.png')
        });
        this.img.setStyle({
            position:'absolute',
            right:'5px',
            cursor:'pointer'
        });
        this.headerText=new Element('h1', {
            id:'headertexth1'+this.uuid

        });
        //this.headerText.setStyle({position:'absolute',left:'0px',width:'100%',padding:'0px',margin:'0px'});
        this.headerText.setStyle({
            'fontSize':'12px',
            'textAlign': 'center',
            position:'absolute',
            left:'0px',
            width:'100%',
            padding:'5px',
            margin:'0px'
        });
        this.imgdiv= new Element('div', {
            id:'imgdiv'
        });
        this.header.appendChild(this.headerText);
        this.header.appendChild(this.img);
        if (!this.options.noheader) {
            this.window.appendChild(this.header);
        }
        Event.observe(this.img,'click',this.close.bind(this));
        this.window.appendChild(this.body);
        this.window.hide();
        $('__windows').appendChild(this.window);
        this.inResize=false;
        this.dragged=false;
        return this;
    },
    initWin: function() {

    },

    ResizeStart: function(event) {
        if (!this.allowResize) {
            return;
        }
        var l=false;
        if (this.options.fixed) return;
        if (event.target==this.window) {
            this.ElementXPos = parseInt(this.window.style.left,10)+parseInt(this.window.clientWidth, 10);
            this.ElementYPos = parseInt(this.window.style.top,10)+parseInt(this.window.clientHeight, 10);
            this.ElementHeight = parseInt(this.window.clientHeight, 10);
            this.ElementWidth = parseInt(this.window.clientWidth, 10);
            l=true;
        }
        if (event.target==this.body) {
            this.ElementXPos = parseInt(this.window.style.left,10)+parseInt(this.window.clientWidth, 10);
            this.ElementYPos = parseInt(this.window.style.top,10)+parseInt(this.window.clientHeight, 10);
            this.ElementHeight = parseInt(this.window.clientHeight, 10);
            this.ElementWidth = parseInt(this.window.clientWidth, 10);
            l=true;
        }
        if (Event.isLeftClick(event) && (l)) {
            this.DragXPos=Event.pointerX(event);
            this.DragYPos=Event.pointerY(event);


            if ((this.DragXPos<=this.ElementXPos+3 && this.DragXPos >=this.ElementXPos-10) &&
                (this.DragYPos<=this.ElementYPos+3 && this.DragYPos >=this.ElementYPos-10)) {
                this.inResize=true;
                event.stop();
            }
        }

    },
    ResizeStop: function(event) {
        if (this.inResize) {
            this.inResize=false;
        }
    },
    Resize: function(event) {
        if (this.inResize) {
            this.resizeWindow(event);
        }
    },
    DragStart: function(event) {
        if (Event.isLeftClick(event) && event.target==this.headerText && !this.options.fixed) {
            this.window.setStyle({
                'zIndex':jsWindow.zIndex++
            });
            this.Dragged=true;
            this.DragXPos=Event.pointerX(event);
            this.DragYPos=Event.pointerY(event);
            this.ElementXPos = parseInt(this.window.style.left, 10);
            this.ElementYPos = parseInt(this.window.style.top, 10);
            //event.preventBubble();
            //event.stopPropagation();
            event.stop();
        }
        if (Event.isLeftClick(event) && event.target==this.window) {
            this.setFocus();
        }
        if (Event.isLeftClick(event) && event.target==this.window && event.altKey && !this.options.fixed) {
            this.window.setStyle({
                'zIndex':jsWindow.zIndex++
            });
            this.Dragged=true;
            this.DragXPos=Event.pointerX(event);
            this.DragYPos=Event.pointerY(event);
            this.ElementXPos = parseInt(this.window.style.left, 10);
            this.ElementYPos = parseInt(this.window.style.top, 10);
            //event.preventBubble();
            //event.stopPropagation();
            event.stop();
        }

    },
    setFocus: function() {
        this.window.setStyle({
            'zIndex':jsWindow.zIndex++
        });
    },
    DragStop: function(event) {
        this.Dragged=false;
    },

    DragMove: function(event) {

        if (this.Dragged) {
            this.moveWindow(event);
            event.stop();

        }
    },
    DragOver: function(event) {

        if (this.Dragged) {
            event.stop();
        }
    },
    DragOut: function(event) {

        if (this.Dragged) {
            this.moveWindow(event);
            event.stop();
        }
    },
    moveWindow: function(event) {
        var CurXPos=Event.pointerX(event);
        var CurYPos=Event.pointerY(event);
        this.window.setStyle({
            'top':CurYPos-this.DragYPos+this.ElementYPos+'px',
            'left':CurXPos-this.DragXPos+this.ElementXPos+'px'
        });
    },
    resizeWindow: function(event) {
        var CurXPos=Event.pointerX(event);
        var CurYPos=Event.pointerY(event);
        this.window.setStyle({
            'height':CurYPos-this.DragYPos+this.ElementHeight+'px',
            'width':CurXPos-this.DragXPos+this.ElementWidth+'px'
        });
    },
    onClose: function() {
        if (this.options.modal) {
            $('__windows').removeChild(this.modalDiv);
        }
        var newEl = document.createElement('div');
        newEl.innerHTML ="";
        $('__windows').replaceChild(newEl, this.window);
    //window.scroll(this.windoworiginalx,this.windoworiginaly);
    //		$('__windows').removeChild(this.window);

    },
    onShow: function() {
        this.windoworiginalx=window.scrollX;
        this.windoworiginaly=window.scrollY;
        if (this.options.modal) {
            this.modalDiv= new Element('div',{
                id:'modalDiv'+this.uuid
            });
            this.modalDiv.setStyle({
                position:'absolute',
                top:'0px',
                left:'0px',
                height:'5000px',
                width:'5000px',
                zIndex:'98',
                overflow:'hidden',
                backgroundColor:'black',
                opacity:'0.5'
            });
            Element.setOpacity(this.modalDiv,'0.5');
            $('__windows').appendChild(this.modalDiv);
        }
        return;
    },
    onHide: function() {

    },
    onOpen: function() {

    },
    show: function(header,msg) {
        this.onShow();
        if (this.options.center) {
            if (typeof window.innerWidth== "undefined") {
                this.window.show();
                return this;
            }
            var x=(window.innerWidth / 2) + (window.scrollX);
            var y=(window.innerHeight / 2) + (window.scrollY);
            this.window.style.top = y+'px';
            this.window.style.left = x+'px';
        }
        this.window.show();
        if (this.options.center) {
            if (typeof window.innerWidth== "undefined") {
                this.window.show();
                return this;
            }
            this.window.style.top = (window.scrollY+15)+'px';
            this.window.style.left = x-(this.window.offsetWidth/2)+'px';
        }
        return this;
    },
    hide: function() {
        this.window.hide();
    //Effect.Fold(this.window,{duration: 0.7,afterFinish:this.onHide.bind(this)});
    },
    close: function() {
        //Effect.Fold(this.window,{duration: 0.7,afterFinish:this.onClose.bind(this)});
        new Effect.Shrink(this.window, {
            afterFinish: this.onClose.bind(this)
            } );
    //$('__windows').removeChild(this.window);
    },
    appendChild: function(element) {
        return this.window.appendChild(element);
    },
    update : function(text) {
        this.window.update(text);
    },
    fixed : function () {
        this.allowResize=false;
        return this;
    }
};

jsMsgDialog= Class.create(jsWindow,{
    initialize : function($super,options) {
        this.options={
            width: 'auto',
            header: ' '
        };
        Object.extend(this.options, options || { });
        $super(this.options);
        this.window.appendText(this.options.message || '');
        return this;
    },
    show: function($super,header,msg) {
        this.options.header=header;
        this.options.msg=msg;
        this.headerText.update(header);
        //this.body.hide();
        var newEl = this.body.cloneNode(false);
        newEl.update(msg);
        if (this.body.parentNode) {
            this.body.parentNode.replaceChild(newEl, this.body);
        }
        //this.body.update(msg);
        //this.body.show();
        $super();
        return this;
    }
});

jsConfirmDialog= Class.create(jsMsgDialog,{
	initialize : function($super,options) {
		this.choosed=false;
		this.options={
			width: 'auto',
			header: ' ',
			yesCaption:'Igen',
			noCaption:'Nem'
		};
		Object.extend(this.options, options || { });
		$super(this.options);
		this.btnYes=new Element('input',{
    		type:'button',
    		value:this.options.yesCaption
    	});
    	this.btnNo=new Element('input', {
    		type:'button',
    		value:this.options.noCaption
    	});
    	this.window.appendChild(this.btnYes);
    	this.window.appendChild(this.btnNo);

    	Event.observe(this.btnYes,'click',this.onYes.bind(this));
    	Event.observe(this.btnNo,'click',this.onNo.bind(this));
    	return this;

	},
	onYes: function() {
		if (typeof this.options.onYes=='function') {
			this.options.onYes();
		} else {
			new jsMsgDialog({}).show('Hiba','Nincs beállítva az elfogadó esemény vagy hibás a paraméter!');
		}
		this.close();
	},
	onNo: function() {
		if (typeof this.options.onNo=='function') {
			this.options.onNo();
		} else {
			//new jsMsgDialog({}).show('Hiba','Nincs beállítva az elutasító esemény!');
		}
		this.close();
	},
	show: function($super,header,msg) {
		$super(header,msg);
		return this;
	}
});*/

function showError(header,message,options) {
    var center=false;
    var modal=undefined;
    var effect_down = true;
    if (options==undefined) {
        modal=false;
    } else {
        if (typeof options == "object") {
            height= options.height;
            width= options.width;
            modal=options.modal;
            center=options.center;
            effect_down=options.effect_down;
        } else {
            modal=true;
        }
    }

    uuid=new UUID();
    win=$('<div></div>');
    win.close=function() {
        this.dialog('close');
        $j(this).detach();
    };
    win.html(message);
    win.dialog({
        beforeclose: function(event,ui) {
        },
        close: function(event,ui) {


        },
        destroy: function() {

        },
        open: function() {
            this.closeThis=win;

        },

        title:header,
        modal:modal,
        width:width,
        height:height
    });

    return win;

}

function showDialog(header,message,options) {
	var center=false;
	var modal=undefined;
	var effect_down = true;
	if (options==undefined) {
		modal=false;
	} else {
		if (typeof options == "object") {
			height= options.height;
			width= options.width;
			modal=options.modal;
			center=options.center;
			effect_down=options.effect_down;
		} else {
			modal=true;
		}
	}

        uuid=new UUID();
        win=$('<div test="'+uuid+'"></div>');
        win.close=function() {
            this.dialog('close');
            $(this).detach();
        };
	win.html(message);
	win.dialog({
            beforeclose: function(event,ui) {
            },
            close: function(event,ui) {

               $('textarea.mceEditor').each(function(index) {
                    tinyMCE.get($j(this).attr('id')).remove();
               });
    this.closeThis.dialog('destroy').detach();

        },
        destroy: function() {

            },
            open: function() {
                this.closeThis=win;
            $('textarea.mceEditor').each(function(index) {
                $(this).tinymce({
				// General options
				script_url : $('base').attr('href')+'ui/html/javascript/tinymce/tiny_mce.js',

                    theme : "advanced",
                    editor_selector: "mceEditor",

				width: '368px',
                                height: '368px',
                                language:'hu',
				convert_newlines_to_brs : true,
				force_br_newlines : true,
                                theme_advanced_toolbar_location : "top",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,xhtmlxtras",

				// Theme options
				theme_advanced_buttons1 : "pasteword,save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,insertlayer,moveforward,movebackward,absolute,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,sub,sup,",
				theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,spellchecker",
				theme_advanced_buttons3 : "",
				theme_advanced_buttons4 : "",

				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : false,
                                spellchecker_languages : "+English=en,Hungary=hu",


				// Office example CSS

				content_css: $("base").attr("href")+'/application/se/ui/css/'+"custom.css"


				// Replace values for the template plugin

			});
//                     /*   $j('.mceEditor').each(function(index) {
//                            tinyMCE.execCommand('mceAddControl', false, $j(this).attr('id'));
//                        });*/
            });
        },

        title:header,
        modal:modal,
        width:width,
        height:height
    });

    return win;

}

function showConfirmDialog(header,message,options) {
    var center=false;
    var modal=undefined;
    var effect_down = true;

    if (options==undefined) {
        modal=false;
    } else {
        if (typeof options == "object") {
            height= options.height;
            width= options.width;
            modal=options.modal;
            center=options.center;
            effect_down=options.effect_down;
        /*onYes = options.onYes;*/
        } else {
            modal=true;
        }
    }

    uuid=new UUID();
    win=$('<div test="'+uuid+'"></div>');
    win.close=function() {
        this.dialog('close');
        $(this).detach();
    };
    win.html(message);
    win.dialog({
        beforeclose: function(event,ui) {
        },
        close: function(event,ui) {

            this.closeThis.dialog('destroy').detach();

        },
        destroy: function() {

        },
        open: function() {
            this.closeThis=win;

        },
        buttons: {
            "Igen": function() {
                win.dialog( "close" );
                options.onYes();

            },
            "Nem": function() {
                win.dialog( "close" );
            }

        }
        ,
        title:header,
        modal:modal,
        width:width,
        height:height
    });

    return win;

}

$(document).ready(function() {

Loader=$('<div style="display: table;"><div style="display: table-row;"><div style="display: table-cell;text-align: center;vertical-align: middle;width: 300px;height: 113px;"><img src="application/se/ui/images/ajax-loader_1.gif" /></div></div></div>');
Loader.dialog({autoOpen:false, draggable: false, resizable: false  ,dialogClass:'loader',modal: true});


    Loader.hide=function() {
        Loader.dialog('close');

}
});


function showLoader() {
    Loader.dialog('open');
}
function hideLoader() {
    Loader.dialog('close');
}

function activateLastWindow() {
    DefaultWM.stack.windows[1].focus();
}
var Loader='';

