jQuery(document).ready(function(){
    $('.menuHeader').click(function(){
        if ($(this).hasClass('unactive')){
            $(this).removeClass('unactive');
        }else{
            $(this).addClass('unactive');
        }
        $('#'+$(this).attr('id')+'ul').slideToggle();
    });

    
    $('.username').click(function(){
    	if($(this).hasClass('plus')){
    		$(this).removeClass('plus');
    		$(this).addClass('minus');
    	}else {
    		$(this).removeClass('minus');
    		$(this).addClass('plus');
    	}
        $('#'+$(this).attr('id')+'ul1').slideToggle();
    });
    
    $('.modulename').click(function(){
    	if($(this).hasClass('plus')){
    		$(this).removeClass('plus');
    		$(this).addClass('minus');
    	}else {
    		$(this).removeClass('minus');
    		$(this).addClass('plus');
    	}
        $('#'+$(this).attr('id')+'ul2').slideToggle();
    });
    
    $('.actionname').click(function(){
    	if($(this).hasClass('plus')){
    		$(this).removeClass('plus');
    		$(this).addClass('minus');
    	}else {
    		$(this).removeClass('minus');
    		$(this).addClass('plus');
    	}
        $('#'+$(this).attr('id')+'ul3').slideToggle();
    });
    
    
    $('.username').hover(function() {
        $(this).addClass('user-hover');
     }, function() {
         $(this).removeClass('user-hover');
     });
    $('.modulename').hover(function() {
        $(this).addClass('module-hover');
     }, function() {
         $(this).removeClass('module-hover');
     });
    
    
    $('.menuHeader').hover(function() {
       $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $('#submitButton').click(function(){
        $('#fForm').submit();
        return false;
    });

    $('tbody > tr').hover(
        function(){
            $(this).stop();
            $(this).animate({
                backgroundColor: '#f3f3f3'
            }, 100);
        },
        function(){
            $(this).stop();
            $(this).animate({
                backgroundColor: '#ffffff'
            }, 300);
        }
    );
    $('ul.addtag-list li').hover(
        function(){
            $(this).stop();
            $(this).animate({
                backgroundColor: '#f3f3f3'
            }, 100);
        },
        function(){
            $(this).stop();
            $(this).animate({
                backgroundColor: '#ffffff'
            }, 300);
        }
    );
    
    $('a, img').focus(function(){this.blur();});
});

function advancedTinyMCE (textarea_id) {
    tinyMCE.init({
        mode : "exact",
        elements : textarea_id,
        theme : "advanced",
        skin : "o2k7",
        skin_variant : "silver",
        language: "pl",
        width: "100%",
        height: "400px",
        plugins : "table,inlinepopups,save,advhr,advimage,media,advlink,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,fullscreen,nonbreaking,xhtmlxtras,inlinepopups",
        theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,cleanup,|,search,replace,|,bullist,numlist,hr,|,undo,redo",
        theme_advanced_buttons2 : "tablecontrols,|,link,unlink,anchor,image,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,removeformat,fullscreen,code",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_align : "left",
        theme_advanced_toolbar_location : "top",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
        document_base_url : "/",
        relative_urls : false,
        extended_valid_elements : "img[src|border=0|width|height|alt|title|hspace|vspace|align|style|class]",
        theme_advanced_blockformats : "p,h2",
        theme_advanced_styles : "Font 11=font-11;Font 12=font-12;Font 13=font-13;Font 14=font-14;Font 15=font-15;Font 16=font-16",
        content_css : "/css/admin/tinymce.css?x=" + new Date().getTime(),
        theme_advanced_path : false,
        file_browser_callback : "filemanager"
    });
}

/*        theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,|,search,replace,|,bullist,numlist,hr,|,outdent,indent,blockquote,|,undo,redo",
        theme_advanced_buttons2 : "tablecontrols,|,link,unlink,anchor,image,media,cleanup,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,removeformat,fullscreen,code",*/

function simpleTinyMCE (textarea_id) {
    tinyMCE.init({
        mode : "exact",
        elements : textarea_id,
        theme : "advanced",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,forecolor,link,unlink,formatselect,cleanup,separator,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,separator,code",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "bottom",
        theme_advanced_toolbar_align : "center",
        theme_advanced_statusbar_location : "none",
        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
        skin : "o2k7",
        skin_variant : "silver",
        language: "pl",
        width: "100%",
        height: "100px",
        document_base_url : "/",
        relative_urls : false,
        content_css : "/css/admin/tinymce.css?" + new Date().getTime(),
        theme_advanced_styles : "Font 10=font-10;Font 11=font-11;Font 12=font-12;Font 14=font-14;Font 16=font-16;",
        theme_advanced_path : false
    });
}

function initToggleTinyMCE () {
    tinyMCE.init({
        mode : "none",
        theme : "advanced",
        theme_advanced_buttons1 : "",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "none",
        theme_advanced_toolbar_align : "none",
        theme_advanced_statusbar_location : "none",
        valid_elements : "strong/b",
        language: "pl",
        width: "146px",
        height: "63px",
        document_base_url : "/",
        relative_urls : false,
        theme_advanced_path : false
    });
}

function ShowToggleTinyMce(textarea_id) {
    if (!tinyMCE.getInstanceById(textarea_id)) {
       tinyMCE.execCommand('mceAddControl', false, textarea_id);
    }
}

function HideToggleTinyMce(textarea_id) {
    if (tinyMCE.getInstanceById(textarea_id)) {
       tinyMCE.execCommand('mceRemoveControl', false, textarea_id);
    }
}

function pickUpCalendar (input_id, display_id, button_id, time) {
    if(time) {
        Calendar.setup({
        inputField     :    input_id,
        displayArea    :    display_id,
        button         :    button_id,
        ifFormat       :    "%Y-%m-%d %H:%M:%S",
        daFormat       :    "%d-%m-%Y %H:%M:%S",
        showsTime      :    true,
        timeFormat     :    "24",
        align          :    "Tl",
        singleClick    :    true});
    } else {
        Calendar.setup({
        inputField     :    input_id,
        displayArea    :    display_id,
        button         :    button_id,
        ifFormat       :    "%Y-%m-%d",
        daFormat       :    "%d-%m-%Y",
        align          :    "Tl",
        singleClick    :    true});
    }
}

function setFile(field_name){
	elementId = field_name;
	elementWin = window;
	
	$.nyroModalManual({
		type: 'swf',
		height: $(window).height(),
		width: $(window).width(),
		minHeight: $(window).height(),
		minWidth: $(window).width(),
		url: '/lib/filemanager/public/swf/FileManager.swf',
		closeButton: '',
		bgColor: '#ffffff', 
		zIndexStart: 300000,
		padding: 0,
		showBackground: showBackground,		
		swf: {
		  wmode: 'window',
		  FlashVars: 'files_dir=../../user/&elementId='+field_name
		}
	});
	
    return false;
}

function showBackground(elts, settings, callback) {
    elts.bg.css({opacity:0}).fadeTo(500,1, callback);
}

function filemanager(field_name, url, type, win) {

	switch (type) {
		case "image":
			break;
		case "media":
			break;
		case "flash": 
			break;
		case "file":
			break;
		default:
			return false;
	}
	
	elementId = field_name;
	elementWin = win;
	
	$.nyroModalManual({
		type: 'swf',
		height: $(window).height(),
		width: $(window).width(),
		minHeight: $(window).height(),
		minWidth: $(window).width(),
		url: '/lib/filemanager/public/swf/FileManager.swf',
		closeButton: '',
		bgColor: '#ffffff', 
		zIndexStart: 900000,
		padding: 0,  
		showBackground: showBackground,	
		swf: {
		  wmode: 'transparent',
		  FlashVars: 'files_dir=../../user/&elementId='+field_name
		}
	});

}

var elementId = '';
var elementWin = null;

function selectFile(url){
	url = '/lib/filemanager/' + url;
	var win = elementWin;
	win.document.getElementById(elementId).value = url;
	//for image browsers
	try { win.ImageDialog.showPreviewImage(url); }
	catch (e) { void(e); }
	$.nyroModalRemove();
}

function cancelSelectFile(){
    $.nyroModalRemove();
}