var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

Array.prototype.deleteValue = function(el){
	
	neu = [];

	for(i=0; i<this.length; i++){
		
		if(el != this[i]){
			neu.push(this[i]);
		}

	}
	
	return neu;

}

var data;
var hours = ["8","9","10","11","12","13","14","15","16","17","18","19"];
var minutes = ["00","30"];
var bereich_count;
var bereich_el;
var date_message=false;
var id;
var ausgewaehlt = [];

$(document).ready(function(){

	$('#anwendung_anreise').datepicker({
		dateFormat: "d.m.yy",
		showOn: "both", 
		buttonImage: "assets/templates/standart/images/calendar.gif", 
		buttonImageOnly: true 
	});

	$('#anwendung_abreise').datepicker({
		dateFormat: "d.m.yy",
		showOn: "both", 
		buttonImage: "assets/templates/standart/images/calendar.gif", 
		buttonImageOnly: true 
	});


	$.getJSON(anwendungen_data,function(response){
	  data = response;
	$("#loader").hide();
	
		$(data).each(function(bereich_count, bereich_el){
			
			ausgewaehlt[bereich_count] = [];
					
			bereich = '<li class="subhead" ><div class="anwendung_aktivieren">&nbsp;</div><div class="anwendung_name bereich_head"><label>'+bereich_el.bereich+'</label> <span id="summe_'+bereich_count+'" class="anwendung_summe"></span></div><div class="clear"></div><ul id="bereich_'+bereich_count+'" class="bereich"></ul></li>';

			$(bereich).appendTo("#anwendungen");
			
			data = bereich_el.data;
			
			$(data).each(function(anwendung_count, anwendung_el){

				id = bereich_count+"_"+anwendung_count;

				hours_id = 'anwendung_'+id+'_hours';
				minutes_id = 'anwendung_'+id+'_minutes';
				datum = 'anwendung_'+id+'_datum';
				anzahl = 'anwendung_'+id+'_anzahl';
				input_id = '['+bereich_count+']['+anwendung_count+']';

				if(anwendung_el.dauer == "") anwendung_el.dauer = "-";
				
				html = '<li id="anwendung_'+bereich_count+'_'+anwendung_count+'"><div class="anwendung_aktivieren"><input type="checkbox" class="anwendung_aktivieren_box" id="anwendung_'+id+'_aktivieren_box" ref="'+id+'" value="true" name="anwendung'+input_id+'[active]" /></div><div class="anwendung_name"><label for="anwendung_'+id+'_aktivieren_box">'+anwendung_el.name+'</label></div><div class="anwendung_dauer">'+anwendung_el.dauer+'</div><div class="anwendung_preis">'+anwendung_el.preis+'</div><div id="anwendung_'+id+'_zeit" class="anwendung_zeit"><input type="text" size="8" style="margin: 0 !important;" id="'+datum+'" name="anwendung'+input_id+'[datum]" /><select id="'+hours_id+'" name="anwendung'+input_id+'[hour]"></select><select id="'+minutes_id+'" style="margin: 0 !important;" name="anwendung'+input_id+'[minute]"></select> Uhr</div><div class="clear"></div></li>';

				$(html).appendTo("#bereich_"+bereich_count);

				$(hours).each(function(hour_count, hour){
					$('<option value="'+hour+'">'+hour+'</option>').appendTo('#'+hours_id);
				});

				$(minutes).each(function(minutes_count, minute){
					$('<option value="'+minute+'">'+minute+'</option>').appendTo('#'+minutes_id);
				});

				$('#anwendung_'+id+'_datum').datepicker({
					dateFormat: "d.m.yy",
					showOn: "both", 
					buttonImage: "assets/templates/standart/images/calendar.gif", 
					buttonImageOnly: true 
				});
				
				$('#anwendung_'+id+'_aktivieren_box').click(function(){

					$("#anwendung_"+bereich_count+"_"+anwendung_count).toggleClass("anwendung_active");
					
					//anwendung_url = Url.encode(anwendung_el.name);
					
					wunschtermin = (wunschtermin)? wunschtermin : "Bitte Wunschtermin wählen";

					if(!date_message){
						$('<div id="date_message"><div class="date_message_arrow" style="left: 0;"></div><div class="date_message_arrow" style="right: 5px;"></div><div class="date_message_arrow" style="right: 40px;"></div>'+wunschtermin+'</div>').appendTo("#anwendung_"+bereich_count+"_"+anwendung_count+"_zeit");
						date_message = true;
					}else{
						if($("#date_message").length) $("#date_message").hide();
					}

					id = $(this).attr("ref");
					splitId = id.split("_")
					bereich_id = splitId[0];
					anwendung_id= splitId[1];

					if(this.checked == true){
						ausgewaehlt[bereich_id].push(anwendung_id);

						anwendung_url = anwendung_el.name;
						anwendung_url = anwendung_url.replace(/ö/,"oe");
						anwendung_url = anwendung_url.replace(/Ö/,"Oe");
						anwendung_url = anwendung_url.replace(/ä/,"ae");
						anwendung_url = anwendung_url.replace(/Ä/,"ae");
						anwendung_url = anwendung_url.replace(/Ü/,"Ue");
						anwendung_url = anwendung_url.replace(/ü/,"ue");
						anwendung_url = anwendung_url.replace(/ß/,"ss");
						anwendung_url = anwendung_url.replace(" ","_");

						google_url = "/de/spa-and-medical-wellness/spa-planer/"+anwendung_url;
						urchinTracker(google_url);
					}else{
						ausgewaehlt[bereich_id] = ausgewaehlt[bereich_id].deleteValue(anwendung_id);
					}
					

					anwendung_sel = (anwendung_sel)? anwendung_sel : "Anwendung ausgewählt";
					anwendungen_sel = (anwendungen_sel)? anwendungen_sel : "Anwendungen ausgewählt";

					anzahl = ausgewaehlt[bereich_id].length;
					if(anzahl <= 0){
						$("#summe_"+bereich_count).html("");
					}else if(anzahl == 1){
						$("#summe_"+bereich_count).html("("+anzahl+" "+anwendung_sel+")");
					}else{
						$("#summe_"+bereich_count).html("("+anzahl+" "+anwendungen_sel+")");
					}

				});

			});

		});

		$(".subhead .bereich_head").click(function(){
			$(this).parent().toggleClass("bereich_active");
		});

	});

});