// 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 render_view_and_highlight(view, target, params) {
	render_view(view, target, params,
		
		function() {
			new Effect.Highlight($(target));
		}
		
	);
}

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

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;
}


function reZebra(tbody_id, params) 
{
	
	// get the rows in the tBody
	rows = $(tbody_id).getElementsByTagName('tr');

	flip = false;
	start_pos = 0;
	count = rows.length;
	skipRows = params.skipRows;

 	if(skipRows < 0) { 		// skipping from the bottom
		count += skipRows;
	} else if (skipRows > 0 ) {
		start_pos += skipRows;
	}
	
	for(i = 0; i < count; i++) 
	{
		console.log(rows[i].style.display);
		if(rows[i].style.display != 'none')
		{
			console.log("changing class to..." + params.even);
			rows[i].className = flip ? params.even : params.odd;
			flip = !flip;
		}
	}

} 




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 dropBasket() {
   
   new Effect.BlindDown($('basketArea'),{duration:'0.2'});
   $('basketArea').onmouseover=function(){
        this.show();
   }
}
function dragBasket() {
    $('basketArea').hide();
    $('basketArea').onmouseout=function(){
        this.hide();
   }
    
}

function waitMessage() {
	document.getElementById('button').style.display = "none";
	document.getElementById('waiting').style.display = "block";
}
