ajax_url = "http://www.historia-hamburg.de/pub/ajax.scboost.php";
COOKIE_NAME = "scboost";

$(document).ready ( function () {
	initAll();
	addCategoryRow();
	addDefaultSelects();
	addDefaultSelects();
	addDefaultSelects();

 });


function test() {

	alert("test");

}

function initAll() {

	initSelectBoxOptions();
//	initExtendeSearchOptions();
	initButtons();
	initSelectCategory();
}


function initSelectBoxOptions() {

	$(".selectpredicate").unbind("change");


	$(".selectpredicate").change( function() {

		// get the item number
		var no=$(this).attr("custom_no");

		// get the id
		var id=$(this).attr("id");
		$("#"+id+" option:selected").each(function () {
		       predicate=$(this).text();
		});
		predicate =  encode_utf8(predicate);

		// get Category
		$("#values_0"+" option:selected").each(function () {
		       category=$("#values_0").attr("value");
		});
		category =  encode_utf8(category);

		// get the new calculated options from php
		$.ajax({
			type: "GET",
			url: ajax_url,
			data: "action=" + encode64("getvalues") + "&predicate=" + encode64(predicate) + "&category=" + encode64(category)  ,
			success: function(msg){
				$("#values_"+no).html($(msg));

	
				initAll();
			}
		});
	} );
}
function initExtendeSearchOptions() {

	$("#extendedsearch").unbind("click");
	$("#extendedsearch").click( function() {
		var onoff= $("#extendedsearch").attr("custom_on");
		if (onoff=="false"){
			$("#turbomode").attr("value", "true");		
			$("#extendedsearch").attr("custom_on", "true");		
			$(".extendedsearchoptions").css("visibility", "visible");		
		}
		else {
			$("#turbomode").attr("value", "false");		
			$("#extendedsearch").attr("custom_on", "false");		
			$(".extendedsearchoptions").css("visibility", "hidden");		
		}

		initAll();
	})
}

function initButtons() {

	$(".del_icon").unbind("click");
	$(".add_icon").unbind("click");



		// delete a line
		$(".del_icon").click( function() {
			$(this).parent().parent().parent().remove();
		} );


		// add a line
		$(".add_icon").click( function() {


			// get Category
			$("#values_0"+" option:selected").each(function () {
			       value=$("#values_0").attr("value");
			});
			value =  encode_utf8(value);

			var on=$("#extendedsearch").attr("custom_on");
			var custom_no=$("#div_scboostsearch").attr("custom_counter");
			var no=parseInt(custom_no)+1;
			$("#div_scboostsearch").attr("custom_counter",no);

				$.ajax({
					type: "GET",
					async: false,
					url: ajax_url,
					data: "action=" + encode64("addRow") + "&custom_no=" + no + "&turbomode=" + on +"&category="+  encode64(value) ,
				
					success: function(msg){
				
						$("#div_scboostsearch").append(msg);
						initAll();
					}
				});
		} );
}


function initSelectCategory() {

	$(".Kategorie").unbind("change");


	$(".Kategorie").change( function() {

		// get the id
		var id=$(this).attr("id");
		$("#"+id+" option:selected").each(function () {
		       value=$(this).attr("value");
		});
		value =  encode_utf8(value);

		var no=$(this).attr("custom_no");

		// get the new calculated options from php
		$.ajax({
			type: "GET",
			url: ajax_url,
			data: "action=" + encode64("setCategory") + "&category=" + encode64(value) ,
			success: function(msg){
				$(".selectpredicate").html($(msg));

	
				initAll();
			}
		});
	} );
}


function addDefaultSelects() {
		var on=$("#extendedsearch").attr("custom_on");
		var custom_no=$("#div_scboostsearch").attr("custom_counter");
		var no=parseInt(custom_no)+1;
		$("#div_scboostsearch").attr("custom_counter",no);

		$.ajax({
			type: "GET",
			async: false,
			url: ajax_url,
			data: "action="+encode64("addRow")+ "&custom_no=" + no + "&turbomode=" + on ,
			
			success: function(msg){
			
				$("#div_scboostsearch").append(msg);
				initAll();
			}
		});
}

function addPreparedSelects(predicate) {
		var on=$("#extendedsearch").attr("custom_on");
		var custom_no=$("#div_scboostsearch").attr("custom_counter");
		var no=parseInt(custom_no)+1;
		predicate = encode_utf8(predicate);

		$("#div_scboostsearch").attr("custom_counter",no);

			$.ajax({
				type: "GET",
				async: false,
				url: ajax_url,
				data: "action=" + encode64("addRow")+"&custom_no=" + no+"&turbomode=" + on + "&predicate=" + encode64(predicate),
				
				success: function(msg){
				
					$("#div_scboostsearch").append(msg);
					initAll();
				}
			});
}


function addCategoryRow(predicate,edit) {
			$.ajax({
				type: "GET",
				async: false,
				url: ajax_url,
				data: "action=" + encode64("addCategoryRow"),
				
				success: function(msg){
				
					$("#div_scboostsearch_category").append(msg);
					initAll();
				}
			});
}



function initSubmit() {
	$("#scboost_submit").unbind("click");
}


function encode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function encode_utf8(rohtext) {
	// dient der Normalisierung des Zeilenumbruchs
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++)
	{
		// ermitteln des Unicodes des  aktuellen Zeichens
		var c=rohtext.charCodeAt(n);
		// alle Zeichen von 0-127 => 1byte
		if (c<128)
			utftext += String.fromCharCode(c);

		// alle Zeichen von 127 bis 2047 => 2byte
		else if((c>127) && (c<2048)) {
			utftext += String.fromCharCode((c>>6)|192);
			utftext += String.fromCharCode((c&63)|128);
		}
		// alle Zeichen von 2048 bis 66536 => 3byte
		else {
			utftext += String.fromCharCode((c>>12)|224);
			utftext += String.fromCharCode(((c>>6)&63)|128);
			utftext += String.fromCharCode((c&63)|128);
		}
	}
		return utftext;
}




