var popup_window;
function showPopup(url) {
    popup_settings = 'width=1000,height=550,resizable=yes,scrollbars=yes,toolbar=yes';
    if (!popup_window || popup_window.closed) {
        popup_window = window.open(url, 'popup', popup_settings);
    } else {
        popup_window.close();
        popup_window = window.open(url, 'popup', popup_settings);
    }
}

var printview_window;
function showPrintView(url) {
    printview_settings = 'width=1000,height=550,resizable=yes,scrollbars=yes,toolbar=yes';
    if (!printview_window || printview_window.closed) {
        printview_window = window.open(url, 'printview', printview_settings);
    } else {
        printview_window.close();
        printview_window = window.open(url, 'printview', printview_settings);
    }
}

var calobj, calurl, ctype1, ctype2, cmode1, cmode2, cmode3;
function calClickable(obj, user) {
	obj.addEvents({
        click: function() {document.location = '/tasks/?start=' + obj.id.substr(1) + '&u=' + (user || 0);},
		mouseover: function() {this.addClass('calday-hover')},
        mouseout: function() {this.removeClass('calday-hover')}
	});
}

function plainTextFromHTML(text) {
    text = text.replace(/<([^>]*)>/g, "");
    text = text.replace(/&lt;/g, "<");
    text = text.replace(/&gt;/g, ">");
    text = text.replace(/&nbsp;/g, " ");
    text = text.replace(/&quot;/g, '"');
    text = text.replace(/&copy;/g, "(c)");
    text = text.replace(/&amp;/g, "&");
    text = text.replace(/\r\n|\r/g, "\n");
    text = text.replace(/[\t ]+/g, " ");
    text = text.replace(/([ ])?\n([ ])?/g, "\n");
    text = text.replace(/\n{3,}/g, "\n\n");
    return text.trim();
}

function daySwitch(obj) {
	$('changeDay').innerHTML = 'Loading...';
	$('action_edit').hide();
    $('action_complete').hide();
	if (calobj) {
		calobj.removeClass('selectedDate');
	}
	calobj = obj;
	obj.addClass('selectedDate');
	var myRequest = new Request({method: 'post', 
        url: calurl, 
        evalScripts: true,
        timeout: 20000,
        onFailure: function() {
            $('changeDay').innerHTML = '';
            alert('There was an error processing your request. Please try again.');
        }
    }).send('f=' + obj.id);
}

var editing, editobj, editorg, rurl, classorg;
function loadEditables() {
    window.addEvent('domready', function() {
        $$('div.editable').each(function(obj) {
            makeEditable(obj);
        });
    });
}
function makeEditable(obj) {
    obj.addEvents({
        click: function() {editReq(obj)},
        mouseover: function() {this.addClass('edit-hover')},
        mouseout: function() {this.removeClass('edit-hover')}
    });
}
function editReq(obj) {
	$$('.a_element').hide();
    if (!editing) {
        editing = true;
        if (editobj) {
            $(editobj).setStyle('background-color', '');
            $(editobj.id + '_value').empty();
            $(editobj.id + '_value').innerHTML = editorg;
			$(editobj).removeClass('ajaxBox');
			$(editobj).addClass(classorg);
            makeEditable(editobj);
        }
        editobj = obj;
        editorg = $(obj.id + '_value').innerHTML;
		classorg = $(obj).hasClass('field') ? 'field' : 'box';
        obj.addClass('edit-wait');

		var myRequest = new Request({method: 'post', 
		url: rurl, 
		evalScripts: true,
		timeout: 20000,
		onFailure: function() {
            obj.removeClass('edit-wait');
            alert('There was an error processing your request. Please try again.');
            editing = false;
        }
		}).send('part=' + obj.id);
    }
}
function updatePart(part, html) {
    $(part + '_value').empty();
    $(part + '_value').innerHTML = html;
    $(part).removeClass('edit-wait');
	$(part).removeClass('ajaxBox');
	$(part).addClass(classorg);
    $(part).effect('background-color', {'duration': 1500, 'onComplete': function(i) {
        i.addClass('edit-done');
        i.setStyle('background-color', '');
        makeEditable(i);
    }}).start('#fd0', '#ffc');
    editing = false;
    editobj = null;
}
function cancelPart(part) {
    $(part).removeClass('edit-wait');
    $(part).setStyle('background-color', '');
	$(part).removeClass('ajaxBox');
	$(part).addClass(classorg);
	
    $(part + '_value').empty();
    $(part + '_value').innerHTML = editorg;
    editing = false;
    makeEditable(editobj);
    editobj = null;
}
function editPart(action, part, html, error, onsubmit_method) {
    $(part + '_value').empty();
    $(part + '_value').innerHTML = '<form id="ajax-form" action="' + action + '"><input type="hidden" name="updated" value="1" /><input type="hidden" name="part" value="' + part + '" />'
    + html + '<div class="topspace"><input id="ajax-save" class="btnAction" style="margin-bottom: 0;" type="submit" value="Save" /> or <input id="ajax-cancel" class="btnCancel" style="margin-bottom: 0;" type="button" value="Cancel" /></div></form>';

    $('ajax-form').addEvent('submit', function(e) {
        new Event(e).stop();
        $('ajax-save').focus();
		if (onsubmit_method) {
			var result = onsubmit_method.call(this.part);
			if (!result) {
				return false;
			}
		}
        editing = true;
        $(part).addClass('edit-wait');

		this.set('send', {url: action, method: 'post',
        evalScripts: true,
        timeout: 20000
        });
        this.send(); //Sends the form.

        $(part + '_value').empty();
        $(part + '_value').innerHTML = '<strong>Saving...</strong>';
    });
    $('ajax-cancel').addEvent('click', function(e) {
        new Event(e).stop();
        $(part).setStyle('background-color', '');
        $(part + '_value').empty();
        $(part + '_value').innerHTML = editorg;

		$(part).removeClass('ajaxBox');
		$(part).addClass(classorg);
		
        editing = false;
        makeEditable(editobj);
        editobj = null;
    });
    $(part + '_field').select();
    $(part).removeClass('edit-wait');
    $(part).removeClass('edit-hover');
    $(part).removeEvents();

	$(part).removeClass(classorg);
	$(part).addClass('ajaxBox');

    if (error) {
		$(part).setStyle('background-color', '#ffdd00');
    } else {
		$(part).setStyle('background-color', '#ffe3d5');
        editing = false;
    }
}
function closeEditable() {
	if (editobj) {
       	$(editobj).setStyle('background-color', '');
        $(editobj.id + '_value').empty();
        $(editobj.id + '_value').innerHTML = editorg;
		$(editobj).removeClass('ajaxBox');
		$(editobj).addClass(classorg);
        makeEditable(editobj);
    }
}

// Passed in the image call. It's not actually used by the called system, but is instead just a unique value
// sto make sure the browser actually makes another server call each time the toggleContent function is called.
var icall = 0;
function toggleContent(evnt) {
	$('content_i').src = ($$('.content_p')[0].getStyle('display') == 'none') ? '/i/icons32/close.gif' : '/i/icons32/open.gif';
	($$('.content_p')[0].getStyle('display') != 'none') ? $$('.content_p').setStyle('display', 'none') : $$('.content_p').setStyle('display', '');
	
	var left = true;
    if ((evnt) && evnt.which) {
        if (evnt.which != 1) {
            left = false;
        }
    }
    else if (event && event.button) {
        if (event.button != 1) {
            left = false;
        }
    }
    if (left) {
		var img = new Image();
		img.src = "/user/toggle_content_collapse?i=" + icall;
		icall += 1;
    }
}

function setColor(value) {
    $('color').value = value;
    $('currentcolor').style.background = value;
}
