if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
        fromIndex = 0;
    } else if (fromIndex < 0) {
        fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex, j = this.length; i < j; i++) {
        if (this[i] === obj)
            return i;
    }
    return -1;
  };
}

var imageFolder = buttonUrl.indexOf("/game/")>=0 ? "../" : "";

$('document').ready(function(){
	$('.button').css('cursor','pointer');
	
	var imagesUsed = new Array();
	
	$('.button').each(function(){
		
		// Color
		switch($(this).attr('color')){
			case 'green':
				var image = 'green';
				var color = '#111';
				break;
			default:
				var image = 'white';
				var color = '#333';
		}
		
		// Size
		switch($(this).attr('size')){
			case 'big':
				var h = 52;
				var w = 20;
				var f = 18;
				var p = 14;
				image += '.big';
				break;
			case 'small':
				image += '.small';
				var h = 0;
				var w = 0;
				var f = 0;
				var p = 0;
				break;
			default:
				var h = 0;
				var w = 0;
				var f = 0;
				var p = 0;
		}
		
		if(imagesUsed.indexOf(image)<0) imagesUsed.push(image);
		
		// Construct HTML
		$(this).html('<div style="background:url('+imageFolder+'images/buttons/'+image+'.left.png) top left no-repeat; width:'+w+'px; height:'+h+'px;" class="inlineBlock"></div><div style="background:url('+imageFolder+'images/buttons/'+image+'.png) top center repeat-x; height:'+h+'px; font-family:Tahoma, Verdana, sans-serif; font-size:'+f+'px;'+($(this).attr('bold')=="yes"?' font-weight:bold;':'')+' color:'+color+';" class="inlineBlock noHighlight"><div style="padding-top:'+p+'px;" class="noHighlight">'+$(this).attr('label')+'</div></div><div style="background:url('+imageFolder+'images/buttons/'+image+'.right.png) top right no-repeat; width:'+w+'px; height:'+h+'px;" class="inlineBlock"></div>');
		
		// Save settings for interactive events
		$(this).attr('image',image);
		
		// Disable button?
		if($(this).attr('enabled')=='no'){
			disable_buttons($(this).attr('id'));
		}
	});
	
	// Preload images
	for(var i=0;i<imagesUsed.length;i++){
		preload_buttons([
			imageFolder+'images/buttons/'+imagesUsed[i]+'.over.left.png',
			imageFolder+'images/buttons/'+imagesUsed[i]+'.over.png',
			imageFolder+'images/buttons/'+imagesUsed[i]+'.over.right.png',
			imageFolder+'images/buttons/'+imagesUsed[i]+'.down.left.png',
			imageFolder+'images/buttons/'+imagesUsed[i]+'.down.png',
			imageFolder+'images/buttons/'+imagesUsed[i]+'.down.right.png'
		]);
	}
	
	$('.button').each(function(){
		$(this).mouseover(function(){
			if($(this).attr('enabled')!="no"){
				var image = $(this).attr('image');
				$(this).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.left.png)');
				$(this).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.png)');
				$(this).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.right.png)');
			}
		});
	});
	$('.button').each(function(){
		$(this).mouseout(function(){
			if($(this).attr('enabled')!="no"){
				var image = $(this).attr('image');
				$(this).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.left.png)');
				$(this).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.png)');
				$(this).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.right.png)');
				if($(this).attr('isDown')=='yes'){
					$(this).children(':eq(1)').children(':eq(0)').css('padding-top',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-top'))-1+'px');
					$(this).children(':eq(1)').children(':eq(0)').css('padding-left',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-left'))-1+'px');
					$(this).attr('isDown','no');
				}
			}
		});
	});
	$('.button').each(function(){
		$(this).mousedown(function(){
			if($(this).attr('enabled')!="no"){
				var image = $(this).attr('image');
				$(this).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.left.png)');
				$(this).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.png)');
				$(this).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.right.png)');
				$(this).children(':eq(1)').children(':eq(0)').css('padding-top',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-top'))+1+'px');
				$(this).children(':eq(1)').children(':eq(0)').css('padding-left',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-left'))+1+'px');
				$(this).attr('isDown','yes');
			}
		});
	});
	$('.button').each(function(){
		$(this).mouseup(function(){
			if($(this).attr('enabled')!="no"){
				var image = $(this).attr('image');
				$(this).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.left.png)');
				$(this).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.png)');
				$(this).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.over.right.png)');
				$(this).children(':eq(1)').children(':eq(0)').css('padding-top',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-top'))-1+'px');
				$(this).children(':eq(1)').children(':eq(0)').css('padding-left',parseInt($(this).children(':eq(1)').children(':eq(0)').css('padding-left'))-1+'px');
				$(this).attr('isDown','no');
			}
		});
	});
	$('.button').each(function(){
		$(this).click(function(){
			if($(this).attr('enabled')!="no"){
				eval($(this).attr('action'));
			}
		});
	});
});

function disable_buttons(ids){
	if(typeof(ids)!='object'){
		ids = new Array(ids);
	}
	for(var i=0;i<ids.length;i++){
		id = ids[i];
		var image = $('#'+id).attr('image');
		var firstDot = image.indexOf('.');
		if(firstDot>=0){
			image = 'white' + image.substr(firstDot);
		} else {
			image = 'white';
		}
		$('#'+id).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.left.png)');
		$('#'+id).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.png)');
		$('#'+id).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.down.right.png)');
		$('#'+id).children(':eq(1)').children(':eq(0)').css('color','#CCC');
		$('#'+id).css('cursor','default');
		$('#'+id).attr('enabled','no');
	}
}

function enable_buttons(ids){
	if(typeof(ids)!='object'){
		ids = new Array(ids);
	}
	for(var i=0;i<ids.length;i++){
		// Color
		switch($('#'+id).attr('color')){
			case 'green':
				var color = '#111';
				break;
			default:
				var color = '#333';
		}
		var image = $('#'+id).attr('image');
		$('#'+id).children(':eq(0)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.left.png)');
		$('#'+id).children(':eq(1)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.png)');
		$('#'+id).children(':eq(2)').css('background-image','url('+imageFolder+'images/buttons/'+image+'.right.png)');
		$('#'+id).children(':eq(1)').children(':eq(0)').css('color',color);
		$('#'+id).css('cursor','pointer');
		$('#'+id).attr('enabled','yes');
	}
}

function preload_buttons(arrayOfImages) {
	$(arrayOfImages).each(function(){
		$('<img/>')[0].src = this;
	});
}
