
NS.Package('NS.Site.Menu');
NS.Package('NS.Site.addToSend');
NS.Package('NS.Site.global.loader');

/**
 * @vesrion 1.2.8
 * menu is based on the te genereared menu lists
 * and the menu css frame 
 * 
 * @option[ajax] 				(Object)		de ajax jquery object integrateed in the menu	
 											set false to turn of
 * @option[ajax][type] 			(String)		type of "method" 														default "POST"
 * @option[ajax][dataType]		(String) 		type data 																default "json"
 * @option[ajax][text]			(String) 		text message loader 													default "De pagina wordt geladen"
 * @option[ajax][url] 			(String)		the content request handler												default "request/handler.php"
 * @option[ajax][data] 			(Object)		optional data set for request											default false
 * @option[ajax][target] 		(String)		the target 	of the content 												default "#content"
 * @option[ajax][imgLoader] 	(String)		the image to view by the loading prosses								default "images/ajax-loader2.gif"
 * @option[ajax][appendLoader] 	(Boolean)		append the loader to the contnet (whitout replacing the contnet)		default false
 * @option[ajax][positionTop] 	(Integer)		the postion of the loader if "appendLoader" isset						default 50
 * @option[ajax][success] 		(Function)		method callback method execute when 
 												finished request returns param(data[object], url[object])
 * @option[breadcrumb]			(String)		true or false to update the breadcrunb	 		default "true"
 * @option[breadcrumb][target]	(String)		the target selector of the breadcrunb holder	default ".breadcrumb"
 * @option[breadcrumb][prefix]	(String)		prefix string for te breadgrumb menu
 
 
 ------------------------------------------------------------------------------------------------------------------------------------------------------------
 global properties about the request kunnen benadert worden via the namespace
 NS.Site.contentRequest
 ------------------------------------------------------------------------------------------------------------------------------------------------------------ 
 
 
 Site.addToSend function parameter return data set request information 
 *
 * @param[breadcrumb]			the breadcrumb html string
 * @param[construction]			setting construction 1 or 0
 * @param[content]				number id content / article
 * @param[group]				group id number
 * @param[group_name]			group name
 * @param[name]					name content / article
 * @param[previewmode]			previewmode 1 or 0 (http get var)
 * @param[status]				status content
 * @param[text]					the content html 
 *
 */


(function($){
NS.Site.contentRequest = {};	
NS.Site.global.loader  = {};
NS.Site.Menu = function(options){
	
	var _option = {};
	var _method = {
		
		// ---
		init : function(opt){

			return {
				ajax : (opt.ajax == undefined)? false : {
					type 			: opt.ajax.type 			|| 'POST',
					dataType 		: opt.ajax.dataType 		|| 'json',
					url  			: opt.ajax.url  			|| 'request/handler.php',
					data  			: opt.ajax.data  			|| false,
					text  			: opt.ajax.text  			|| 'De pagina wordt geladen',
					target	 		: opt.ajax.target       	|| '#content',
					imgLoader   	: opt.ajax.imgLoader    	|| 'images/loader.gif',
					appendLoader   	: (opt.ajax.appendLoader == undefined)? false : opt.ajax.appendLoader,
					positionTop   	: opt.ajax.positionTop    	|| 50,
					success 		: opt.ajax.success      	|| false
				},
				breadcrumb	: (opt.breadcrumb == undefined)? false : {
					prefix : opt.breadcrumb.prefix || '<span>U bevindt zich hier: </span>',
					target : (!opt.breadcrumb || !opt.breadcrumb.target)? '.breadcrumb' : opt.breadcrumb.target
				},
				contentRequest : {} 			
			}
		},
		

		// --- request handler --- 
		request : function(button){
			if(button){
				var _url 	=  NS.UT.Tools.getUrlVars(window.location.search +'&'+button.attr('href').substr(1));
				var _parent = button.parent();
				if(button.parent().length > 0){
					_url['name'] 		= _parent.attr('name');
					_url['group_name']  = _parent.attr('group_name');
				}
				// store... 
				NS.Site.contentRequest = _url;
			}
			var _timer = function(){};
			$.ajax({
			  type			: _option.ajax.type,
			  url			: _option.ajax.url,
			  data			: _option.ajax.data || _url,
			  dataType		: _option.ajax.dataType,
			  beforeSend 	: function(){
			  	if(!_option.ajax.appendLoader){
			  		$(_option.ajax.target).html('');
			  	}
			  	// set loader
			  	
			  	NS.UT.Tools.loader({
		  			target   : _option.ajax.target,
					cssClass : 'pageLoader',
					// boolean geld voor alle properties !
					center	 : _option.ajax.appendLoader,
					overlay	 : _option.ajax.appendLoader,
					modal	 : _option.ajax.appendLoader,
					top		 : _option.ajax.positionTop,
					text	 : '<span>'+_option.ajax.text+'</span>',
					src      : _option.ajax.imgLoader
			  	});
			  	var _textLoader = $('.pageLoader').find('span');
			  	var _c=0;
				_timer = NS.UT.Tools.timer(function (){
					_c++;
					var _point = '';
					if(_c == 4){
						_c=0;
					}
					for(var i=0; i< _c; i++){
						_point +='.';
					}
					_textLoader.html(_option.ajax.text+_point+'');
				},800);					  	
			  },
			  success		: function(data){

			  		// set content
			  		_timer.stop();
			   		$(_option.ajax.target).html( data.text );
			   		// collback
			    	if( _option.ajax.success){
			    	 	_option.ajax.success(data, _url);
			    	 	
			    	}
			    	// execute addToSend functions...
			    	NS.Site.addToSend().functions( $.extend(data, NS.Site.contentRequest) );
			    	
			    	
			    	if(_option.breadcrumb){	    		
			    		$(_option.breadcrumb.target).html( _option.breadcrumb.prefix + data.breadcrumb);
			    	}
			  }
			});			
		}
	};
	// --------- initialization ---------
	_option = _method.init(options);

	if(Site['global']){
		Site.global['loader'] = {
			'appendLoader' 	: _option.ajax.appendLoader,
			'positionTop' 	: _option.ajax.positionTop,
			'imgLoader' 	: _option.ajax.imgLoader,
			'target'		: _option.ajax.target,
			'text'			: _option.ajax.text,
			'url'			: _option.ajax.url
		};
	};	
	return {
		request : _method.request
	};
};
})(jQuery);



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

	de addToSend function sorcht er voor dat javascript makkelijk en dinamisch 
	uitgevoerd kan worden als een content gedeelte "article" wordt geladen via ajax
	
	------------------------------------------------------------------------------------------------
	Site.addToSend(string[param], function)
	 
	param 1: (string OR Object)is optional geef hier aan op welke group or article 
			 de functies moeten getriggert worden 
		
	param 2: function de functionality dat moet worden uitgevoerd
	
	marametrs to use:
	------------------------------------------------------------------------------------------------	
	group 		(Int)		the id of the group to match	
	content 	(Int)		the id of the content / article to match	
	name 		(String)	the name of the article or content to match	
	group_name	(String)	the name of the group to match
	
	...............................................................................................
	[let op!] name values kunnen zoms bij andere menu types dan het hoofd menu niet toegankelijk 
	zijn het werkt altijd onder een menu created met de php site utilitie "listMenu"
	daarnaast kunnen name values aangepast worden dus het gebruik van ids is altijd beter
	...............................................................................................

	
	voorbeeld:
	------------------------------------------------------------------------------------------------
	dit zal alleen uitgevoerd worden als content param is 10 and group is 3...
	
	Site.addToSend("content=10&group=3", function(){
		$$('return joe hoe');
	});
	alternatief request on name values
	"name=Contact&group_name=Informatie"
	
	de object notation of the addToSend request...
	Site.addToSend({content : 10 , group : 3}, function(){
		$$('return joe hoe');
	});
	
	dit wordt overal an altijd bij iedere request uitgevoerd..
	
	Site.addToSend(function(){
		$$('return joe baaa');
	});

	
	------------------------------------------------------------------------------------------------**/
/**
 * @vesrion 1.2
 * @name "addToSend"
 * 
 */
NS.Site.addToSend = function(){
	
		var method = {
			colector : [],
			// data collect to collector
			dataCollect : function(arguments){
				if(arguments.length > 0){
					method.colector.push(arguments);
				}
			},
			
			// check for current page
			checkForContent : function(data){
		
				var _storage = NS.Site.contentRequest; // request parameters...
				
				if(typeof(data) === 'string'){			
					var _request = NS.UT.Tools.getUrlVars( data ); // addToSend request...
				
				}else{
					_request = data;
				}
				for(i in _storage){
					if(_request[i] !== undefined){
						if(_request.content && _request.group){
							// check on article and group ids...	
							if(_request.content == _storage.content && _request.group == _storage.group){
								return true;
							}
						// check on name values for group and articles...	
						}else if(_request.name && _request.group_name){
							if(_request.name == _storage.name && _request.group_name == _storage.group_name){
								return true;
							}
						}else{
							// ceck for the single value match...
							if(_request[i] == _storage[i]){
								return true;
							}
						}
					}
				}

				return false;
			},
			
			// execute functions
			functions : function(para){
				var a = 0;
				var data = method.colector;
				while(a < data.length){
					// excute functions
					
					if(typeof(data[a][0]) === 'function'){
						try{
							data[a][0](para);
						}catch(err){
							$$('addToSend exception on');
							$$(err);
						}
					}
					if(typeof(data[a][1]) === 'function'){
						
						if(typeof(data[a][0]) === 'string'){
							if(method.checkForContent(data[a][0])){
									try{
										data[a][1](para);
									}catch(err){
										$$('addToSend exception on');
										$$(err);									
									}
							}				 
						}else if(typeof(data[a][0]) === 'object'){
							
							if(method.checkForContent(data[a][0])){
								try{
									data[a][1](para);
								}catch(err){
									$$('addToSend exception on');
									$$(err);									
								}
							}																
						}
					}
				 a++;	
				}
			}
		}
		return function(param){
			method.dataCollect(arguments);
			return {
				functions : method.functions 
			}
		}
}();

