var pricefix = Class.create({
	requestUrl: "index.php?eID=booking",
	initialize: function(){
		this.findTags();
	},
	findTags: function(){
		var spans = document.getElementsByTagName("span");
		var id = "";
		 for (var i = 0; i < spans.length; ++i) {
			id = spans[i].id;
			if(this.stristr(id, "###")){
				id = this.str_replace("###", "", id);
				id = this.str_replace("<span>", "", id);
				id = this.str_replace("</span>", "", id);
				spans[i].id = spans[i].id+"-"+i;
				//spans[i].innerHTML = "<span id='"+id+"'>€ / $</span>";
				
				this.getPrice(id,i);
			}
		 }
	},
	getPrice: function(id,i){
		var url = this.requestUrl+"&action=getPriceText&id="+id+"&i="+i;
		new Ajax.Request(url,{
			method: "post",
			parameters: {"action": "getPriceText", "id" : id, "i": i},
			onSuccess: function(response){
				var values = response.responseText.evalJSON(true);
				var span = document.getElementById('###'+values.id+'###-'+values.i);
				span.innerHTML = values.price+ " € / AU$"+values.price_aud;
				return false;
			}.bind(this),
			onComplete: function(response){
				var values = response.responseText.evalJSON(true);
				var span = document.getElementById('###'+values.id+'###-'+values.i);
				span.innerHTML = values.price+ " € / AU$"+values.price_aud;
				return false;
			},
			onFailure: function(response, transport){
				return false;
			}
		});
	},
	stristr: function (haystack,needle,bool){
		 // Finds first occurrence of a string within another, case insensitive  
	    // 
	    // version: 1004.2314
	    // discuss at: http://phpjs.org/functions/stristr    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // +   bugfxied by: Onno Marsman
	    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
	    // *     returns 1: 'van Zonneveld'
	    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);    // *     returns 2: 'Kevin '
	    var pos = 0;
	 
	    haystack += '';
	    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );    if (pos == -1){
	        return false;
	    } else{
	        if (bool) {
	            return haystack.substr( 0, pos );        } else{
	            return haystack.slice( pos );
	        }
	    }
	},
	str_replace: function(search,replace,subject){
		return subject.split(search).join(replace);
	}
	
});


Event.observe(window, "load", function(){
	var my_pricefix = new pricefix();
	}
);
