var preload_images = new Array();
var is_bidi = null;

function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported)
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}

function setupEditor() {
    ltr_config = "/media/js/fck_law_ltr.js?dt=" + new Date().valueOf();;
    rtl_config = "/media/js/fck_law.js?dt=" + new Date().valueOf();
    $('textarea.htmlarea').each(function(item) {
        var fckedit = new FCKeditor($(this).attr('id'));
        fckedit.BasePath = '/media/js/fckeditor/';
        if ($(this).hasClass('ltr')) {
            fckedit.Config["CustomConfigurationsPath"] = ltr_config;
        }
        else {
            fckedit.Config["CustomConfigurationsPath"] = rtl_config;
        }

        if ($(this).hasClass('simple')) {
            fckedit.ToolbarSet = 'SimpleToolBar';
            fckedit.Height = '120';
        }
        else {
            fckedit.ToolbarSet = 'LawToolBar';
            fckedit.Height = '300';
        }
        fckedit.Width = '100%';
        fckedit.ReplaceTextarea();
    });
}

/**
 * sets the category image when editing it
 */
function setCategoryImage(image_name, prefix) {
    if (!prefix) prefix = 'image';

    // update the input field
    $('#id_' + prefix).val(image_name);

    // update the preview image
    $('#'+prefix + '_preview').attr('src', '/media' + image_name);
}

function toggleList(div_name) {
    var the_div = $(div_name ? div_name : '#elements_list');

    the_div.css('display', (the_div.css('display')=='none' ? 'block' : 'none'));
}

function activateTab(folder) {
    $$('a.tab_active').setProperty('class', 'tab');
    $('tab_' + folder).setProperty('class', 'tab_active');
    $$('div.folder_items').setStyle('display', 'none');
    $('folder_'+folder).setStyle('display', 'block');
}

/**
 * Set the news title to the selected folder item url
 */
function selectFolderItem(url) {
    the_title = prompt('כותרת');

    if (the_title) {
        the_text = '<a href="/' + url + '">' + the_title +'</a>';
        $('id_title').setProperty('value', the_text);
        toggleList();
    }
}

/**
 * Change the active stylesheet
    */
function setActiveStyleSheet(title) {
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
            if(a.getAttribute("rel").indexOf("style") != -1
                            && a.getAttribute("title")) {
                    a.disabled = true;
                    if(a.getAttribute("title") == title) a.disabled = false;
            }
    }
    window.scroll(0,0);
}

/**
 * Change form elements direction according to selected lang
 */
function setElementsDirection(lang) {
    var the_dir = (lang == 'h' ? 'rtl' : 'ltr');
    var the_align = (lang == 'h' ? 'right' : 'left');

    //get all iframes (which should be the html editors)
    $('iframe').each(function(idx, item) {
        if ($(item).attr('id')) {
            // now get the inner editin iframe
            var inner_f = item.contentDocument.getElementsByTagName('iframe')[0];
            inner_f.contentWindow.document.dir = the_dir;
            inner_f.contentWindow.document.body.style.textAlign = the_align;
            inner_f.contentWindow.document.body.style.direction = the_dir;
        }
    } );

    // align other fields
    $('#id_title, #id_subtitle, #id_name').css({direction:the_dir, 'text-align':the_align});
}

function setupMultilang() {
    // find the multi lang container, and in it the items
    els = $('td.multilingual');
    if (els.length == 0) return;

    el = $(els[0]);

    lang_divs = el.children();

    tabs_el = $('<div class="multilingual_tabs"></div>');
    el.prepend(tabs_el);

    lang_divs.each(function(idx) {
        var item = $(this);
        lang_el = $('<a href="#"></a>').text(item.attr("class").toUpperCase()).css('float', is_bidi ? 'right' : 'left');
        tabs_el.prepend(lang_el);
        lang_el.click(function(ev) {
            if (ev) ev.preventDefault();
            $('a', tabs_el).removeClass('active');
            lang_divs.each(function(tab_idx) {
                $(this).css('display', tab_idx == idx ? 'block' : 'none');
            });
            $(this).addClass('active');
        });
        item.addClass('multilingual_tabs_page');
        if (!idx) {
            lang_el.addClass('active');
        }
        else {
            item.css('display', 'none');
        }
    });

    tabs_el.append($('<span>&nbsp;</span>'));
    tabs_el.append($('<div class="clearer">&nbsp;</div>'));
}

