﻿/**

requires:

provides: 

*/


(function(){ //alles kapseln wegen window scope

//+++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++

/**
@preserve G8_Worx_ax: Version 0.0.4
*/

/**
--- --- --- --- --- --- ---

Class Static: 	G8_Worx_ax

--- --- --- --- --- --- ---
provides:

	window public namespace:
		G8.worx
	
	window public function:
		$Self
	
	public methods:	
		worx.prxy(scope,func)
		worx.makeDiv(params, strContent1,strContent2...)
		worx.makeDom([tagName, params, strContent1,strContent2...])
		worx.dbg()
		worx.addGlobalListener(eventName,callback)
		worx.extendObject(destination, source)
		worx.extendMethods(map)
		
	String object methods:	
		string.hasStr
		
	jQuery object methods:
		jqObj.tgl(bool)
		jqObj.tglFadeIn(speed, callback)
		jqObj.tglFadeOut(speed, callback)
		jqObj.opacity(val)
		jqObj.stopp()
		jqObj.isOrIsChildOf(parentObj)
		jqObj.readInputVal()
		jqObj.outerHtml()
		jqObj.getIdFromClass(prefix)
		jqObj.bindProxy(eventType,listenerFunc,scope)
		jqObj.bindOnce(eventType,listenerFunc)
		jqObj.bindOnceProxy(eventType,listenerFunc,scope)

	private methods:
		_ctm_globalListenerCallbackOnAnyEvent(e)
		_arrayCutFirst(obj)
		_getTempDiv()
		
--- --- --- --- --- --- ---
changelog:

0.0.4
	removed		setClassSettings() und getClassSettings() und initAutoInit() komplett weggefallen. 

0.0.3
	refactor	makeDom() jetzt hier in G8_Worx_ax drin (mit neuer arguments-struktur). makeDiv() benutzt diese.

0.0.2
	neu				bindOnceProxy()

0.0.1
	neu				tglFadeIn() benutzt jetzt andere methode (von IST zu 100%) mit speedanpassung	
		
*/

var G8_Worx_ax = (function G8_Worx_ax(){	
	
	var _glo_classConfig;
	var _glo_tempDiv;

	//--- create window public namespace "G8.worx"
	window.G8 = window.G8 || {};
	window.G8.worx = {};

	//--- namespace extension helper
	G8.worx.extendMethods = function extendMethods(map){
		$.each(map, function(key, val){
			G8.worx[key] = val;
		});
	}
	
	
	//--- window public functions
	window.$Self = function $Self(selector,scope){ /*
		wie $, findet aber selector angewendet auf -scope childs und -scope selbst */
		
		return scope.find(selector).add(scope.filter(selector));
	}
	
	
	//--- public functions via "G8.worx" namespace
	G8.worx.extendMethods({
		
		prxy:function(scope,func){
			//shortcut to $.proxy() with swapped arguments
			
			return $.proxy(func,scope);
		},
		
		dbg:function(){ 		
			if(typeof console!="undefined") console.info(arguments);
			else {
				var strOut = "";
				$.each(arguments,function(key,val){
					if($.isArray(val) || typeof val=="object"){
						$.each(arguments,function(key2,val2){
							strOut += "  "+key2+":"+val2+"\n";
						});
					} else {
						strOut += val + "\n\n";
					}
				});
				//alert(strOut);
			}
		},
	
		makeDom:function(arr){ 
		/*
			call:
		
			makeDom([
				"div"
				{id:xx,cl:classNames,hid:true,pos:"absolute"},
				content1,content2...]);
		*/
			
			var domType = arr[0];
			var options = arr[1];
			var contents = arr[2]!=undefined? _arrayCutFirst(_arrayCutFirst(arr)).join("") : ""; // !=undefined, damit 0 auch als content gilt (wichtig bei z.b. countern)
	
			var classStr = options.cl? ' class="' + options.cl + '"' : ''; //buginfo: naming nicht "class" wg. IE bug
			var idStr = options.id? ' id="'+options.id+'"' : '';
			var posStr = options.pos? 'position:'+options.pos+';' : '';
			var hiddenStr = options.hid? 'display:none;' : '';
			var styleStr = posStr!="" || hiddenStr!=""? ' style="'+posStr+hiddenStr+'"' : '';
			
			return '<'+domType+idStr+classStr+styleStr+'>'+contents+'</'+domType+'>';
		},
		
		makeDiv:function(){ 
		/*
			shortcut for makeDom(["div",arguments...])
			
			call:
		
			makeDiv(
				{id:xx,cl:classNames,hid:true,pos:"absolute"},
				content1,content2...); 
		*/
			
			var argumentsArr = [];
			argumentsArr.push("div");
			
			$.each(arguments,function(key,val){
				argumentsArr.push(val);
			});

			return G8.worx.makeDom(argumentsArr);
		},
		
		addGlobalListener:function(eventName,callback){
		/*
			wenn irgendein object das event triggert, wird callback(scope) gecallt (nicht callback(e))
		*/
		
			$(document).bind(eventName, {callback:callback}, _ctm_globalListenerCallbackOnAnyEvent);
		},
	
		extendObject:function(destination, source){ 
			//Native Object Extension for e.g. prototyping
			
		  for (var property in source) destination[property] = source[property];
		  return destination;
		}
		
	});
	
	
	//--- private funcs
	function _ctm_globalListenerCallbackOnAnyEvent(e){
		var triggeringObj = $(e.target);
		var callback = e.data.callback;
		callback(triggeringObj);
	}
	
	function _arrayCutFirst(obj){
	  var arr = $.makeArray(obj);
	  arr.shift();
	  return arr;
	}	

	function _getTempDiv(){
		if(!_glo_tempDiv) _glo_tempDiv = $("<div></div>"); //only once created
		_glo_tempDiv.empty(); //reset
		
		return _glo_tempDiv;
	}
	

	//--- String object methods
	G8.worx.extendObject(String.prototype, {	
	  
	  hasStr: function(){
	  	/*
		  parameter: entweder liste von strings ("a","abcd"...) oder array (["a","abc"])
		  checkt, ob heuhaufen.indexOf(mindEinerDerNeedles)!=-1. also, ob wenigstens einer der needles im heuhaufen vorkommt
		  ist NICHT case sensitive 
		  */
		  
	  	var strs = $.isArray(arguments[0])? arguments[0] : arguments;
			for(var i=0; i<strs.length; i++) if(this.toLowerCase().indexOf(strs[i].toLowerCase())!=-1) return true;
			return false;
		}
		
	});	

		
	//--- jQuery object methods
	jQuery.fn.extend({	

		tgl: function(bool){
			//wie toggle(switch) von jQuery eigentlich sein sollte: bool==true -> show(); bool==false -> hide()
			//(original fehlerhaft umgesetzt: -man kann nicht chainen und -manchmal funzt der toggle nicht)
			//buginfo: manchmal muss man erst show machen, dann toggle, sonst funzt es nicht. warum?
			//buginfo: nach toggle kann man nicht chain mit .toggle().end().find(nächster)... warum?
	
			return bool? this.show() : this.hide();
		},
		
	  tglFadeIn: function(speed, callback){
		  //wie fadeIn, aber geeignet für fade Toggling (z.b. item faded-out und während fading soll er wieder einfaden)
	
	  	//methode 1: faded von 0-100
	  	//return this.stopp().hide().opacity(1).fadeIn(speed,callback); //opacity = ziel-opacity!
	
	  	//methode 2: faded von IST-100 (aber mit angepasstem speed je nach bereits opacity)
	  	var newSpeed = this.opacity()*speed;
	  	return this.stopp().show().fadeTo(newSpeed,1,callback);
	  },
	  
	  tglFadeOut: function(speed, callback){

	  	//methode 1: faded von 100-0
	  	if(this.is(":visible"))	return this.stopp().show().opacity(1).fadeOut(speed,callback); //opacity = ziel-opacity!
	  	//wenn nicht sichtbar: kein fade erzwingen!
	  	else return this.hide();
	
	  	//methode 2: faded von IST-0
	  	//return this.stopp().show().fadeTo(speed,0,callback);
	  },

	  opacity: function(val){
	  	if(val || val===0){return this.css({"opacity":val});}
	    else return this.css("opacity");
	  },

	  stopp: function(){
		//wie stop, aber danach wird NICHTS ausgeführt
	
	    //return(this.queue([]).stop());
	    return this.stop(true);
	  },
	
	  isOrIsChildOf: function(scope){ 
	
	  	var boolIsOrIsChild = false;
	  	var thisDomObj = this[0];
	  	
	  	scope.each(function(){
	  		var currScopeDomObj = this; //achtung: here this != jqueryMathcedObj
	  		if($.contains(currScopeDomObj,thisDomObj) || currScopeDomObj==thisDomObj) {
	  			boolIsOrIsChild = true;
	  			return false;
	  		}
	  	});
	  	
	  	return boolIsOrIsChild;
	  	
	  	/*
	  	//alte version:
			//prüft nur für erstes objekt aus scope!  	
	  	if($.contains(scope[0],this[0]) || scope[0]==this[0]) return true;
	  	else return false;
	  	*/
	  },
	
		readInputVal: function(){
			return $.trim(this.val());
		},
	
	  outerHtml: function(){
	    return _getTempDiv().html(this.clone()).html();
	  },
	
		getIdFromClass: function(prefix){
			//lies teilstring aus class, die mit prefix beginnt(prefix enthält)
			//beispiel: findet MEINEID per getIdFromClass($(),"needle") aus <div class="text gross needleMEINEID">
	
	  	var ret = "";
	   	if(this.attr("class")){ //error handling
	    	$.each(this.attr("class").split(/\s+/), function(){
	       	if(this.hasStr(prefix)) ret = this.replace(prefix, "");
	     	});
	   	}
	   	return ret;
		},
	
		bindProxy: function(){
			//arguments can be:
			//	eventType, listenerFunc, scope
			//	eventType, {additional:data}, listenerFunc, scope
			
			var eventType = arguments[0];
			var additionalData = !$.isFunction(arguments[1])? arguments[1] : false;
			var listenerFunc = additionalData? arguments[2] : arguments[1];
			var scope = additionalData? arguments[3] : arguments[2];
			
			if(additionalData) this.bind(eventType, additionalData, $.proxy(listenerFunc,scope));
			else this.bind(eventType, $.proxy(listenerFunc,scope));
	  	
	  	return this;
		},
		
		bindOnce: function(eventType, listenerFunc){
			var eventType = arguments[0];
			var additionalData = !$.isFunction(arguments[1])? arguments[1] : false;
			var listenerFunc = additionalData? arguments[2] : arguments[1];
			
			this.unbind(eventType, listenerFunc);
			if(additionalData) this.bind(eventType, additionalData, listenerFunc);
			else this.bind(eventType, listenerFunc);
	
			return this;
		},
		
		bindOnceProxy: function(){
			//arguments can be:
			//	eventType, listenerFunc, scope
			//	eventType, {additional:data}, listenerFunc, scope
			
			var eventType = arguments[0];
			var additionalData = !$.isFunction(arguments[1])? arguments[1] : false;
			var listenerFunc = additionalData? arguments[2] : arguments[1];
			var scope = additionalData? arguments[3] : arguments[2];
			
			if(additionalData) this.bindOnce(eventType, additionalData, $.proxy(listenerFunc,scope));
			else this.bindOnce(eventType, $.proxy(listenerFunc,scope));
	  	
	  	return this;
		}

	});
	
	
	//this class wont return public methods directly
	//instead makes methods accessible via "G8.worx" namespace
	
	//--- return public methods & properties --- --- --- --- --- --- ---
	return {
	};

})();

/**
@preserve G8_Worx_Src_ax: Version 0.0.1
*/

/**
--- --- --- --- --- --- ---

Class Static: 	G8_Worx_Src_ax

--- --- --- --- --- --- ---
provides:

	jQuery object methods:
		jqObj.imgSetHi()
		jqObj.imgSetLo()
		jqObj.imgSetAct()
		jqObj.imgFilterNonAct()
		jqObj.imgSetExp()
		jqObj.imgSetCol()
		jqObj.switchSrc(val,newVal)
				
	private methods:

--- --- --- --- --- --- ---
changelog:
		
*/

var G8_Worx_Src_ax = (function G8_Worx_Src_ax(){	

	//--- jQuery object methods:
	jQuery.fn.extend({	
		
		imgSetHi: function(){
			return this.switchSrc("_lo.","_hi.").switchSrc("_act.","_hi.");
		},
	
		imgSetLo: function(){
			return this.switchSrc("_hi.","_lo.").switchSrc("_act.","_lo.");
		},
		
		imgSetAct: function(){
			return this.switchSrc("_hi.","_act.").switchSrc("_lo.","_act.");
		},
	
		imgFilterNonAct: function(){
			return this.filter(function(){return $(this).attr("src").indexOf("_act.")==-1;});
		},
	
		imgSetExp: function(){
			return this.switchSrc("_col","_exp");
		},
	
		imgSetCol: function(){
			return this.switchSrc("_exp","_col");
		},
	
	  switchSrc: function(val,newVal){
	  //ersetzt einen teil aus src="..." mit neuem string (für img-rollover)
	  //val kann string oder regExp sein.
	
	  	//TODO: wieso function(){return...} ?
	    return this.attr("src", function(){return this.src.replace(val, newVal);} );
		}
		
	});


	//--- return public methods & properties --- --- --- --- --- --- ---
	return {
	};

})();



//+++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++ +++

//--- set globals/shortcuts
window.dbg = G8.worx.dbg;
window.mD = G8.worx.makeDiv;
window.prxy = G8.worx.prxy;

//+++ onload ++++++++++++++++++++++++++++++++++++++++
$(function(){
	
});

})();
