/*	###	GI_Button: Effekte mit Javascript und Prototype
	###	Momentan unterstützt:
		GI_CheckBox:	Grafische Checkbox
		GI_Shadow:		Schlagschatten für Text und Links
*/

var GI_Button =
{

	ImagePath     : '/bilder/GraphInput',

	CheckboxImg   :	{ On: 'haken.gif',
					  Off: 'keinhaken.gif' },

	SelectOptions 	: new Array(),
	SelectTree 	  	: new Array(),
	SelectStates  	: new Array(),
	SelectFathers 	: new Array(),
	SelectOriginal	: new Array(),
	SelectFadeOut 	: new Array(),
	SelectDirection	: new Array(),

	IE6 : false,

	UniqueCounter : 0,

	CheckboxValues : { },

	Init : function(){

		GI_Button.PRE = new Array();
		GI_Button.IE6 = (navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.match(/MSIE 6\.0/)) ? true : false;

		if(typeof(Prototype) == "undefined"){
			return false;
		}else{
			GI_Button.CheckToDo();
		}

	},

	CheckToDo : function(){

		GI_Button.ToDo = { 	Checkbox: $$('input.GI_Checkbox'),
							Selects: $$('select.GI_Select'),
							ShadowText: $$('.GI_Shadow'),
							HideObjects: $$('.GI_Hide'),
							BuildCheckboxes: false,
							BuildSelects: false,
							BuildShadows: false,
							BuildHides: false};

		GI_Button.ToDo.BuildCheckboxes = (GI_Button.ToDo.Checkbox.length > 0) ? true : false;
		GI_Button.ToDo.BuildSelects = (GI_Button.ToDo.Selects.length > 0) ? true : false;
		GI_Button.ToDo.BuildShadows = (GI_Button.ToDo.ShadowText.length > 0) ? true : false;
		GI_Button.ToDo.BuildHides = (GI_Button.ToDo.HideObjects.length > 0) ? true : false;

		GI_Button.InitButtons();

		GI_Button.InitEffects();

	},

	InitButtons : function(){

		if(!GI_Button.ToDo.BuildSelects && ! GI_Button.ToDo.BuildCheckboxes){
			return false;
		}

		arguments = new Array();

		if(GI_Button.ToDo.BuildCheckboxes){
			GI_Button.PRE['CheckboxOn'] = new Image();
			GI_Button.PRE['CheckboxOff'] = new Image();
			GI_Button.PRE['CheckboxOn'].src = GI_Button.ImagePath+'/'+GI_Button.CheckboxImg.On;
			GI_Button.PRE['CheckboxOff'].src = GI_Button.ImagePath+'/'+GI_Button.CheckboxImg.Off;
			arguments[arguments.length] = "GI_Button.PRE['CheckboxOn'].complete";
			arguments[arguments.length] = "GI_Button.PRE['CheckboxOff'].complete";

			GI_Button.WaitFor(	{argument:arguments.join(' && '),
							 nextStep:GI_Button.Builder,
							 stopper:10
							});
		}else{

			GI_Button.Builder();

		}

	},

	Builder : function(){

		if(GI_Button.ToDo.BuildCheckboxes){
			GI_Button.ToDo.Checkbox.each( function(thing){

				GI_Button.AddHelper(thing);
				GI_Button.ReplaceWithCheckbox(thing);

			});
		}

		if(GI_Button.ToDo.BuildSelects){
			GI_Button.ToDo.Selects.each( function(thing){

				GI_Button.replaceSelect(thing);

			});
		}

	},

	AddHelper : function(thing){

		var FromElement = thing.ancestors().each( function(line){ if(typeof(line.getInputs) != "undefined"){return(line);} } );

		if(thing.checked){
			var val = thing.value;
		}else{
			var val = "";
		}

		new Insertion.Before(thing.id,'<input type="hidden" name="'+thing.name+'" id="H'+thing.id+'" value="'+val+'" />');

		GI_Button.CheckboxValues[thing.id] = thing.value;

	},

	ReplaceWithCheckbox : function(thing){

		var width = thing.getWidth();
		var height = thing.getHeight();


		if(thing.checked){
			var stateI = GI_Button.CheckboxImg.On;
			var Img = GI_Button.PRE['CheckboxOn'];
		}else{
			var stateI = GI_Button.CheckboxImg.Off;
			var Img = GI_Button.PRE['CheckboxOff'];
		}

		var margin_width = 0;
		var margin_height = 0;
		var div = 2;

		if(Prototype.Browser.Gecko){
			div = 1;
		}

		if(Img.width < width){
			margin_width = Math.round((width - Img.width) / div);
		}
		if(Img.height < height){
			margin_height = Math.round((height - Img.height) / div);
		}

		var labelforthis = document.getElementsByTagName('label');

		if(labelforthis.length > 0){
			for(var i=0; i < labelforthis.length; i++){
				line = labelforthis[i];
				if(line.htmlFor == thing.id){
					line.onclick = function (){
						GI_Button.ClickCheckbox(thing.id);
					};
					line.style.cursor = 'pointer';
				}
			}

		}

		if(thing.siblings().length>0){
			new Insertion.Before(thing.id,'<img src="'+GI_Button.ImagePath+'/'+stateI+'" id="GI_Button_'+thing.id+'" onclick="GI_Button.ClickCheckbox(\''+thing.id+'\');" style="margin:'+margin_height+'px '+margin_height+'px;cursor:pointer;" />');
			thing.remove();
		}else{
			thing.ancestors()[0].update('<img src="'+GI_Button.ImagePath+'/'+stateI+'" id="GI_Button_'+thing.id+'" onclick="GI_Button.ClickCheckbox(\''+thing.id+'\');" style="margin:'+margin_height+'px '+margin_height+'px;cursor:pointer;" />');
		}

	},

	ClickCheckbox : function(thingid){

		var state = $("H"+thingid).value;

		if(state != ""){
			var newState = "";
			var newImg = GI_Button.ImagePath+'/'+GI_Button.CheckboxImg.Off;
		}else{
			var newState = GI_Button.CheckboxValues[thingid];
			var newImg = GI_Button.ImagePath+'/'+GI_Button.CheckboxImg.On;
		}

		$("H"+thingid).value = newState;

		$("GI_Button_"+thingid).src = newImg;

	},

	getBorderDimensions: function(obj){

		return {
			width : (parseInt(obj.getStyle('border-left-width').replace(/px/,'')) + parseInt(obj.getStyle('border-right-width').replace(/px/,''))),
			height : (parseInt(obj.getStyle('border-top-width').replace(/px/,'')) + parseInt(obj.getStyle('border-bottom-width').replace(/px/,'')))
		};

	},

	getPaddingDimensions: function(obj){

		return {
			width : (parseInt(obj.getStyle('padding-left').replace(/px/,'')) + parseInt(obj.getStyle('padding-right').replace(/px/,''))),
			height : (parseInt(obj.getStyle('padding-top').replace(/px/,'')) + parseInt(obj.getStyle('padding-bottom').replace(/px/,'')))
		};

	},

	replaceSelect : function(thing){

		thing.makePositioned();

		var off = thing.positionedOffset();
		var dims = thing.getDimensions();
		var pars = thing.ancestors();

		var borderDims = GI_Button.getBorderDimensions(thing);
		var paddingDims = GI_Button.getPaddingDimensions(thing);

		var newWidth = dims.width;
		var newHeight = dims.height;

		if(!Prototype.Browser.IE){
			newWidth = newWidth - paddingDims.width - borderDims.width;
			newHeight = newHeight - paddingDims.height - borderDims.height;
		}

		var newID = thing.id+'_GI_SELECT_0';

		GI_Button.SelectOriginal[newID] = thing.id;
		GI_Button.SelectTree[newID] = new Array();
		GI_Button.SelectStates[newID] = false;

		var thingVal = thing.options[thing.selectedIndex].value;
		var thingText = thing.options[thing.selectedIndex].text;

		thing.insert({before:'<a href="#" id="'+newID+'" class="GI_SELECT GI_SELECT_BOX GI_OPTION_'+thingVal+'">'+thingText+'</a>'});
		thing.setStyle({visibility:'hidden'});
		$(newID).setStyle({display:'block',position:'absolute',top:off.top+'px',left:off.left+'px',width:newWidth+'px',height:newHeight+'px'});

		$(newID).onclick = GI_Button.GI_Select_0;

		thing.insert({before:'<a href="#" id="GI_SELECT_DROPDOWN_BTN_'+newID+'" class="GI_SELECT_DROPDOWN_BTN"><img src="/bilder/blank.gif" id="GI_SELECT_DROPDOWN_DUMMY_'+newID+'"></a>'});

		GI_Button.SelectOptions[newID] = {value: thingVal, text: thingText};
		GI_Button.SelectFathers[newID] = newID;

		var newDims = $(newID).getDimensions();
		var newPaddings = GI_Button.getPaddingDimensions($(newID));

		var dropdowndims = $('GI_SELECT_DROPDOWN_BTN_'+newID).getDimensions();
		var dropdownpos = {
			position:'absolute',
			top:	parseInt((off.top+newDims.height/2)-(dropdowndims.height/2))+'px',
			left:	(off.left+newDims.width-dropdowndims.width-$(newID).getStyle('borderRightWidth').replace(/px/,''))+'px',
			zIndex: $(newID).getStyle('zIndex')+1
		};

		$('GI_SELECT_DROPDOWN_BTN_'+newID).setStyle(dropdownpos);

		$('GI_SELECT_DROPDOWN_BTN_'+newID).onclick=GI_Button.GI_Select_0_BTN;

		var opts = thing.childElements();

		var optCount = opts.length;

		var setPositionAbove = (off.top+newHeight+(dims.height*optCount) > document.viewport.getHeight());

		GI_Button.SelectDirection[newID] = (setPositionAbove) ? 'TOP' : 'BOTTOM';

		for(var i=0; i < optCount; i++){

			var optID = thing.id+'_GI_SELECT_'+(i+1);

			var lastClass = "";

			if(i == optCount-1){
				var lastClass = " GI_SELECT_LAST_"+GI_Button.SelectDirection[newID];
			}

			$(newID).insert({after:'<a href="#" id="'+optID+'" class="GI_SELECT GI_OPTION GI_OPTION_'+opts[i].value+lastClass+'">'+opts[i].text+'</a>'});

			if(setPositionAbove){
				var newPos = off.top - (dims.height * (i+1));
			}else{
				var newPos = off.top + (dims.height * (i+1));
			}

			$(optID).setStyle({position:'absolute',top:newPos+'px',left:off.left+'px',width:newWidth+'px',height:newHeight+'px',display:'none'});
			$(optID).onclick = GI_Button.GI_Select;

			GI_Button.SelectTree[newID].push(optID);
			GI_Button.SelectOptions[optID] = {value: opts[i].value, text: opts[i].text};

			GI_Button.SelectFathers[optID] = newID;

		}
	},

	GI_Select_0_BTN : function(){

		GI_Button.GI_Select_0(this.id.replace(/GI_SELECT_DROPDOWN_BTN_/,''));

	},

	GI_Select_0 : function(opt){

		if((Prototype.Browser.IE && typeof(opt) == 'undefined') || opt.type){
			newID = this.id;
		}else{
			newID = opt;
		}


		if(!GI_Button.SelectStates[newID]){

			$(newID).addClassName('GI_SELECT_ACTIVE_'+GI_Button.SelectDirection[newID]);

			GI_Button.SelectTree[newID].each( function (pe){

				$(pe).setStyle({display:'block'});
				$(pe).onmouseover = GI_Button.GI_SelectFadeOutStop;
				$(pe).onmouseout = GI_Button.GI_SelectFadeOut;

			});

			$(newID).onmouseover = GI_Button.GI_SelectFadeOutStop;
			$(newID).onmouseout = GI_Button.GI_SelectFadeOut;

			GI_Button.SelectStates[newID] = true;

		}else{

			$(newID).removeClassName('GI_SELECT_ACTIVE_'+GI_Button.SelectDirection[newID]);

			GI_Button.SelectTree[newID].each( function (pe){

				$(pe).setStyle({display:'none'});
				$(pe).onmouseover = null;
				$(pe).onmouseout = null;

			});

			$(newID).onmouseover = null;
			$(newID).onmouseout = null;

			GI_Button.SelectStates[newID] = false;
		}

		if(GI_Button.SelectFadeOut[newID]){
			window.clearTimeout(GI_Button.SelectFadeOut[newID]);
		}

		return false;

	},

	GI_Select : function(){

		var newVal = GI_Button.SelectOptions[this.id].value;
		var newText = GI_Button.SelectOptions[this.id].text;
		var father = GI_Button.SelectFathers[this.id];
		var original = GI_Button.SelectOriginal[father];
		var fatherVal = GI_Button.SelectOptions[father].value;

		$(original).value = newVal;

		$(father).removeClassName('GI_OPTION_'+fatherVal);
		$(father).addClassName('GI_OPTION_'+newVal);
		$(father).innerHTML = newText;

		GI_Button.SelectOptions[father].value = newVal;
		GI_Button.SelectOptions[father].text= newText;

		if(GI_Button.SelectFadeOut[father]){
			window.clearTimeout(GI_Button.SelectFadeOut[father]);
		}

		if($(original).onclick){
			$(original).onclick();
		}

		if($(original).onchange){
			$(original).onchange();
		}

		GI_Button.GI_Select_0(father);

		return false;

	},

	GI_SelectFadeOut : function(){

		newID = this.id;

		var father = GI_Button.SelectFathers[newID];

		if(!GI_Button.SelectFadeOut[father]){
			GI_Button.SelectFadeOut[father] = window.setTimeout('GI_Button.GI_Select_0("'+father+'")',500);
		}

	},

	GI_SelectFadeOutStop : function(){

		newID = this.id;

		var father = GI_Button.SelectFathers[newID];

		if(GI_Button.SelectFadeOut[father]){
			window.clearTimeout(GI_Button.SelectFadeOut[father]);
			GI_Button.SelectFadeOut[father] = false;
		}

	},


	InitEffects : function(){

		if(GI_Button.ToDo.BuildShadows){

			GI_Button.ToDo.ShadowText.each(function(pe){

				if(pe.getStyle("position") == "static"){
					pe.style.position = "relative";
				}

				var check = pe.firstDescendant();

				if(check){
					var content = check.innerHTML;
				}else{
					var content = pe.innerHTML;
				}

				pe.update("<div style=\"position:absolute;top:-1px;left:-1px;color:"+pe.getStyle("color")+";background-color:transparent;\">"+pe.innerHTML+"</div>"+content);
				pe.style.color="#000";

			});

		}

		if(GI_Button.ToDo.BuildHides){

			GI_Button.ToDo.HideObjects.each(function(pe){

				pe.hide();

			});

		}

	},

	WaitFor : function(arguments){
		GI_Button.WF_Counter = 0;
		GI_Button.WF_Stopper = (arguments.stopper) ? arguments.stopper : 50;
		GI_Button.WF_argument = arguments.argument;
		GI_Button.WF_nextStep = arguments.nextStep;
		GI_Button.WaitForLonger();
	},

	WaitForLonger : function(){

		if(eval(GI_Button.WF_argument) || GI_Button.WF_Counter > GI_Button.WF_Stopper){
			GI_Button.WF_nextStep();
		}else{
			GI_Button.WF_Counter = GI_Button.WF_Counter+1;
			window.setTimeout('GI_Button.WaitForLonger()',100);
		}
	},

	ApplyToOnload : function(){
		if( document.addEventListener ){
			addEventListener('load',GI_Button.Init,false);
		}else if( window.attachEvent ){
			window.attachEvent('onload',GI_Button.Init);
		}
	}

}
