/* global */
Element.implement({
    toggleform: function() {
	$S('#'+this.id+' input', '#'+this.id+' select','#'+this.id+' textarea').each(function(el, i) {
	    if(el.getProperty('disabled') == "true") el.setProperty('disabled', 'false').setStyle('background-color', '#C97702');
	    else el.setProperty('disabled','true').setStyle('background-color', 'grey');
	});
    },
    visible: function() {
	if(this.getStyle('display') == 'block') this.setStyle('display', 'none');
	else this.setStyle('display', 'block');
    },
    hide: function() {
	this.setStyle('display', 'none');
    },
    show: function() {
	this.setStyle('display', 'block');
    },
    offscreen: function() {
	this.setStyles({'position':'absolute', 'left':'-9999px'});
    },
    onscreen: function() {
	this.setStyles({'position':'static', 'left':'0px'});
    },
    serialize: function(key, tag) {
	var ret = [], key = key || this.id;
	    $A(this.getElementsByTagName(tag || 'li')).each(function(el, i){
	    ret.push(key+'['+i+']=' + el.id);
	});
	return ret.join('&');
    },
    flash: function(fromcolour) {
	colour = fromcolour || '#C97702';
	new Fx.Color(this, 'background-color', {duration:700}).fromColor(colour);
    },
    getValue: function(){
	var value = false;
	switch(this.getTag()){
	case 'select':
	    if (this.type == 'select-one') {
		if (this.selectedIndex != -1) {
		    return this.options[this.selectedIndex].value;
		}
		break;
	    }
	    var values = [];
	    for(i=0;i<this.options.length;i++) {
		opt = this.options[i];
		if (opt.selected) values.push(opt.value ? opt.value : opt.text);
	    }
	    return values; break;
	case 'input': if ((this.checked && ['checkbox', 'radio'].test(this.type)) || ['hidden', 'text', 'password'].test(this.type))
	    return this.value; break;
	case 'textarea': return this.value;
	}
	return value;
    }
});

/*
Function: pointer
    Adds cursor pointer to all elements with specified property and value
Arguments:
    els - element type to use.
    prop - property to check
    val - value to check property against
*/
function pointer(els, prop, val) {
    $$(els).each(function(el, i) {
	if(el.getProperty(prop) == val) el.setStyle('cursor', 'pointer');
    });
}

function sort(url) {
    new Sortables($S('#list li'), {
	handles: $ES('.grab','#list'),
	contain:false,
	onFailure: function() {
	},
	onComplete: function() {
	    new Ajax(url, {postBody:$('list').serialize(), method:'post',
	    onStateChange: function() {
		$('update_spinner').show();
	    },
	    onComplete:function(r){
		var row = 1;
		$ES('tr', '#list ul').each(function(el, i) {
		    if(el.className == "row1" || el.className == "row2") {
			if(row++%2 == 0) { el.className = 'row1'; } else { el.className = 'row2'; }
		    }
		});
		$('update_spinner').hide();
		$('list').flash('#C97702');
	    }}).request();
	}
    });
}

function flash() {
    new Fx.Color('update', 'background-color', {duration:700}).fromColor('#C97702');
}

function addToList(artist_id, user_id, user_type) {
    new Ajax('_artist_join_ajax.php', {
	update:'artist_list_update',
	method:'post',
	postBody:'artist_id='+artist_id+'&user_id='+user_id+'&user_type='+user_type,
	onStateChange:function() {
	    $('artist_list').show();
	},
	onComplete:function(r) {
	    $('artist_list').hide();
	    $('artist_list_update').show();
	    $('artist_list_update').flash();
	    var hide = (function() {
		$('artist_list_update').hide();
		$('list_addtolist').hide();
		$('attached_to_artist').show();
		$('attached_to_artist').flash();
	    }).delay(3000);
	}
    }).request();
    return false;
}
