// yes, i know these should be consts, but safari didn'tlike them
var CORE_STAT_WAITING = 1;
var CORE_STAT_SUCCESS = 2;
var CORE_STAT_ERROR = 4;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function render_view_and_highlight(view, target, params, highlight) 
{	
	// console.log("Rendering " + view);
	
	_postBody = 'view=' + view;
	if(params != null) {
		_postBody += '&' + params;
	}
	
	new Ajax.Updater(target, '/render_view.php', { postBody: _postBody, 
												   onSuccess: function() { if(highlight) { new Effect.Highlight(target); } } 
												 });
	
}
 

function render_view(view, target, params, _onSuccess) 
{	
	// console.log("Rendering " + view);
	
	_postBody = 'view=' + view;
	if(params != null) {
		_postBody += '&' + params;
	}
	
	new Ajax.Updater(target, '/render_view.php', { postBody: _postBody, 
												   onSuccess: _onSuccess });
	
}

function remote_function(namespace_function, data, _onSuccess, _onFail) {
	
	params = namespace_function.split('.');
	req_uri = '/ajax/' + params[0] + '/' + params[1] + '/';
	
	new Ajax.Request(req_uri, { postBody : data,
								 onSuccess : _onSuccess,
								 onFailure : _onFail });
}

function setStatus(panel, type, msg) 
{
	panel = $(panel);
	panel.innerHTML = msg;
	panel.className = 'status_' + type;
	
	if(panel.style.display == 'none') {
		Effect.BlindDown(panel, { duration: 0.1 });
	}

	if(type != 'waiting') {
		setTimeout(function() {
			Effect.BlindUp(panel, { duration: 0.1 });
		}, 5000);

	}
}


function $I(iframe_id) {
	
	_iframe = $(iframe_id);
	r = null;
	
	if(_iframe.contentDocument) {
		r = _iframe.contentDocument;
	} else if(_iframe.contentWindow.document) {
		r = _iframe.contentWindow.document;
	}
	
	return r;
}





String.prototype.pad = function(length, padchar, type) {
	pad = '';
	
	if(length > this.length) {
		(length - this.length).times( function(index) {
			pad += padchar;
		});		
	}
	
	if(type) {
		return this + pad;
	}  else {
		return pad + this
	}
}

function deleteMedia(path, classname, id, variable) {
    var postData = "path=" + path + "&class=" + classname + "&id=" + id + "&variable=" + variable;
    var view = variable + "_deletemedia"; // pdf_deletemedia
    var wrap = variable + "_wrap"; // pdf_wrap
    
	remote_function('media.delete_generic_media', postData,
	    function(request) {
		    render_view(view, wrap, postData);	        
	    },
	    function(request) {
		    alert(request.responseText);	        
	    }
	);
}