/*//////////////////////////////////////////////////////////////////////////////////////////
 * Ajaxify - jQuery Plugin
 * version: 2.00 (11/12/2008)
 * Created by: MaX
 * Examples and documentation at: http://max.jsrhost.com/ajaxify/
 * licensed under and GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 */

(function($){
jQuery.AjaxifyDefaults = {  
		event:'click', /*specify the event*/
		link:false, /* specify the link, priority is for the href attr.*/
		target:'#container', /*the data loaded via ajax will be placed here*/
		animateOut:false,
		animateIn:false,
		animateOutSpeed:'normal',
		animateInSpeed:'normal',
		method: 'GET', /* the request method GET or POST*/
		tagToload:false, /* inserts just the tag from the data loaded, it can be specified as t a second argument in the 'target' attr(#box,#result)*/
		loading_txt:'',
		loading_img:"",
		loading_target: false,
		loading_fn:function(options){
			jQuery.ajaxifyLoading(options);
		},
		loadHash:false,	/* for use this to resolve bookmarking issues, see example for more details*/
		title:false, /* change page title along with the request. */
		forms:false, /* send form data along with th request (forms, input , radio ... etc jquery selector) */
		params:'ajax=true',/*extend parameters for the webpage. it can be set to function since v2*/
		timeout:false, /*in ms.  there is a problem in this option on linux servers*/
		contentType:"application/x-www-form-urlencoded",
		dataType:'html',
		cache:false, /* force the browser not to cache*/
		username:false, /*username HTTP access authentication*/
		password:false, /*password HTTP access authentication*/
		onStart:function(op){}, /* a callback function before start requesting.*/
		onError:function(op){
			jQuery.ajaxifyManip(op,"<font style='color: #CC0000'>Error: </font> Couldn't open the page.");		
		}, /* a callback function if error happened while requesting*/
		onSuccess:function(op){},/* a callback function if the request finished successfuly*/
		onComplete:function(op){}//*a callback function when the request finished weather it was a successful one or not.*/
	};
jQuery.AjaxifyFirstLoad = true;
jQuery.AjaxifyhistorySet = new Object();
jQuery.AjaxifyPageTitle = document.title;
jQuery.AjaxifyDebug = false;
jQuery.fn.ajaxify = function(options) {  
	if(!jQuery(this).size()){
		jQuery.ajaxifylog('Error: No matched element/s for your ajaxify selector " '+jQuery(this).selector+' ".');
		return false;
	}
	var ver = jQuery.fn.jquery.split('.');
	if(ver[0] < 1 || ver[1] < 2 || ver[2] < 6){
		jQuery.ajaxifylog('Error: Your jQuery version is old. Version 1.2.6 or newer is required.');
		return false;
	}
	return this.each(function() {
	var current = jQuery.extend({},jQuery.AjaxifyDefaults, options);
	if(jQuery.metadata){
	current = jQuery.extend(current,jQuery(this).metadata());
	}
	
	
	if(current.event){
		jQuery(this).bind(current.event,function(){		
			jQuery(this).ajaxifyAnalyse(current);
			if(!current.hash)
				jQuery.ajaxifyLoad(current);
			else{
				jQuery.ajaxifyHash(current);
			}
			 //stop browser
			if(jQuery(this).is('a') || jQuery(this).is('form')) return false;
		});
	}else{
		jQuery(this).ajaxifyAnalyse(current);
		jQuery.ajaxifyLoad(current);		
	}	
		//for bookmarking	
		if(current.loadHash  && jQuery.AjaxifyFirstLoad){
			jQuery(this).ajaxifyAnalyse(current);
			if(document.location.hash.replace(/^#/, '') == current.hash	&& current.hash){
				jQuery.ajaxifyHash(current);
				jQuery.AjaxifyFirstLoad = false;
			}
		}
		
  }); // end each fn 
}; // end ajaxify fn
jQuery.fn.ajaxifyAnalyse = function(current){
	current.object = this;
	if(jQuery(this).is('a')){
		if(jQuery(this).attr('href')){
			//if(jQuery.browser.msie)
				//var link = jQuery(this).attr('href').replace(/^#/, "");
			//else
				var link = jQuery(this).attr('href').replace(/^#/, "");
				//alert(link);
			current.link = link || current.link;
		}else 
			current.link;
			
		if(typeof current.tagToload != 'object')
			if(jQuery(this).attr('target'))
				current.target = jQuery(this).attr('target');
			else
				current.target;
		else
			current.target = current.loading_target || '#AjaxifyTemp';
	}
	   
	if(!current.loading_target)
	   current.loading_target = current.target;
	if(current.forms){
		var text = jQuery(current.forms).serialize();
		current.paramres = text;
	}
	if(typeof current.params == 'function')
		var params = current.params(current);
	else
		var params = current.params;

	if(typeof params == 'string'){
		if(text)
		current.paramres +='&'+params;
		else
		current.paramres = params;
	}
	
	var len = current.target.length-1;
	if(typeof current.tagToload !='object')
		if(current.target.charAt(len) == '+' || current.target.charAt(len)=='-'){
			current.manip = current.target.charAt(len);
			current.target = current.target.substr(0,len);
		}

   	if(current.loadHash){
		if(!jQuery.historyInit){
			jQuery.ajaxifylog('Error: loadHash is enabled but history plugin couldn\'t be found.');
		return false;
		}
		
		if(current.loadHash === true){
			jQuery.ajaxifylog('Info: It seemes you are upgrading from v1.0. Please see the new documentation about loadHash. "attr:href" will be used instead of "true".');
			current.loadHash = "attr:href";
		}
		if(current.loadHash.toLowerCase() == 'attr:href' || 
			current.loadHash.toLowerCase() == 'attr:rel' ||
			current.loadHash.toLowerCase() == 'attr:title'){
			
			current.loadHash = current.loadHash.toLowerCase();
			current.hash = jQuery(this).attr(current.loadHash.replace('attr:',''));
			if(jQuery.browser.opera){
				current.hash = current.hash.replace('?','%3F');
				current.hash = current.hash.replace('&','%26');
				current.hash = current.hash.replace('=','%3D');
			}
		}else
			current.hash = current.loadHash;
		
		if(!current.hash)
			jQuery.ajaxifylog('Warning: You have specified loadHash, but its empty or attribute couldn\'t be found.');
	}
	
	if(!jQuery(current.target).size() && typeof current.tagToload !='object')
		jQuery.ajaxifylog('Warning: Target " '+current.target+' " couldn\'t be found.');
};
jQuery.ajaxifyLoading = function(options){
	var html = "<div id='AjaxifyLoading'><img src='"+options.loading_img+"' alt='Loading...' title='Loading...' >"+options.loading_txt+"</div>";
	if(options.loading_target)
		jQuery.ajaxifyManip(options.loading_target,html);
	else
		jQuery.ajaxifyManip(options,html);
}
jQuery.ajaxifyHash = function(current){
	var ob = new Object();
	jQuery.each(current, function(key, value) {
		ob[key] = value;
	});
	jQuery.AjaxifyhistorySet[ob.hash] = ob;
	location.hash = ob.hash;
	//if(jQuery.AjaxifyFirstLoad.history){
	//alert(ob.hash);
		jQuery.historyInit(jQuery.ajaxifyHistory);
		jQuery.AjaxifyFirstLoad.history = false;
	//}
};
jQuery.ajaxifyLoad = function(current) {
	// turn off globals 
	//alert('ajaxifyLoad'+print_r(current,true));
	jQuery.ajaxSetup({global:false});	
	//start calling  jQuery.ajax function. thank you jquery for making this easy
	jQuery.ajax({
		type: current.method,
		url: current.link,
		dataType: current.dataType,
		data: current.paramres,
		contentType:current.contentType,
		processData:true,
		timeout:current.timeout,
		cache:current.cache,
		username:current.username,
		password:current.password,
		complete: function(){
			current.onComplete(current)
		},
		beforeSend: function(){
			current.onStart(current);
			
			if(current.animateOut){
				if(current.loading_target != current.target);//diff target? fire before start anim
					current.loading_fn(current);
				jQuery(current.target).animate(current.animateOut,current.animateOutSpeed,function(){
					//alert('hr');
					if(!current.loading_target)//already fired
					current.loading_fn(current);		
				});
			}else
				current.loading_fn(current);
			},
		success: function(data){
		jQuery(current.target).stop();
		jQuery('#AjaxifyLoading').remove();
		
		if(current.title)
			document.title = current.title;
		else if(document.title != jQuery.AjaxifyPageTitle)
			document.title = jQuery.AjaxifyPageTitle;
		
		if(current.tagToload){
		data = '<div>'+data+'</div>'; //wrap data so we can find tags within it.
			if(typeof current.tagToload == 'string'){
					jQuery.ajaxifyManip(current,jQuery(data).find(current.tagToload)); 					
			}else if(typeof current.tagToload == 'object') {
					jQuery.each(current.tagToload, function(tag, target) {
						if(jQuery(data).find(tag).size())
							jQuery.ajaxifyManip(target,jQuery(data).find(tag)); 
						else
							jQuery.ajaxifylog('Warning: Tag "'+tag+'" couldn\'t be found.');
						
					});
			}
		
		}else{
		 // 
		 jQuery.ajaxifyManip(current,data);
		  }
		current.onSuccess(current,data);
		if(current.animateIn)
			jQuery(current.target).animate(current.animateIn,current.animateInSpeed);
		  
		  },
		  error:function(msg){
			  jQuery(current.target).stop();
			  current.onError(current,msg);
			  if(current.animateIn)
		  jQuery(current.target).animate(current.animateIn,current.animateInSpeed);
		  }
		});
};
jQuery.ajaxifylog = function(message) {
	if(jQuery.AjaxifyDebug)
		if(window.console) {
			 console.debug(message);
		} else {
			 alert(message);
		}
};
jQuery.ajaxifyHistory = function(hash){
	if(hash){
		if(jQuery.browser.safari){
			var options = jQuery.AjaxifyhistorySet[location.hash.replace(/^#/,'')]; //fix bug in history.js
		}else
			var options = jQuery.AjaxifyhistorySet[hash];
		
		if(options)
			jQuery.ajaxifyLoad(options);
		else
			jQuery.ajaxifylog('History Fired. But I couldn\'t find hash. Most propabley, the hash is empty. If so, its normal.');
	}
};
jQuery.ajaxifyManip = function(current,data){
if(typeof current != 'object'){
	var target = current;
	var current = new Object;
	var len = target.length-1;
	if(target.charAt(len) == '+' || target.charAt(len)=='-'){
		current.manip = target.charAt(len);
		current.target = target.substr(0,len);
	}
	else{
		current.manip = '';
		current.target = target;
	}
	if(!jQuery(current.target).size())
		jQuery.ajaxifylog('Warning: Target "'+current.target+'" couldn\'t be found.');
}
	if(current.manip == '+')
		jQuery(current.target).append(data);
	else if(current.manip == '-')
		jQuery(current.target).prepend(data);
	else
		jQuery(current.target).html(data);
};


})(jQuery);

//////////////////////////////////////////////////////////////////////////////////////////////////////////

/*	jQuery Combobox with autocomplete plugin 1.0.1
 *	www.frebsite.nl
 *	Copyright (c) 2009 Fred Heusschen
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 */

(function($) {

	$.fn.combobox = function(settings) {
   		var opts = $.extend({}, $.fn.combobox.defaults, settings);
   		
   		if (!$('#combo_overlay').length) {
			$('body').append('<div id="combobox_overlay"></div>');
			$('#combobox_overlay').unbind('click').click($.fn.combobox.sluitSelect);
		}
		
		return this.each(function() {
			var el = $(this);

			var id = el.attr('id');
			var ic = (id.length > 0)				? ' id="combobox_container_'+id+'"' 	: '';
				id = (id.length > 0)				? ' id="'+id+'"' 						: '';
			var na = (el.attr('name').length) 		? ' name="'+el.attr('name')+'"' 		: '';
			var ti = (el.attr('tabindex').length)	? ' tabindex="'+el.attr('tabindex')+'"'	: '';

			var cb  = '<input class="combobox_input" type="text" value="'+$(':selected', el).text()+'" '+ti+' autocomplete="off" />'; 
			//KZ: fixed from ' '+ti+'" to '" '+ti+' (so not to include the tab index in the input text)
				cb += '<input class="combobox_hidden" type="hidden"'+na+''+id+' value="'+el.val()+'" />';
			
			if (opts.buttonText.length > 0) {	
				cb += '<a href="#" class="combobox_button"><span>'+opts.buttonText+'</span></a>';
			}
				cb += '<ul class="combobox_summary">';
				
			$('option', el).each(function() {
				var t = $(this).html();
				var c = ($(this).attr('class').length) ? ' class="'+$(this).attr('class')+'"' : '';
				var v = $(this).attr('value');
				if (!v.length) v = t;
				if (v == "Other") cb += '<li'+c+'><a href="'+v+'" class="other">'+t+'</a></li>';
					else cb += '<li'+c+'><a href="'+v+'">'+t+'</a></li>';
			});
			
				cb += '</ul>';
				
				el.wrap('<div class="combobox_container"'+ic+'></div>');
			var di = el.parent();	
				di.append(cb);
				el.remove();
				


			//	selectie openen via button
			if (opts.showOnMouseOver) {
				$(di).mouseover(function() {
					$.fn.combobox.openSelectFromButton($(this), opts);
				}).click(function() {
					return false;
				});
			} else if (opts.buttonText.length > 0) {
				$('a.combobox_button', di).click(function() {
					if (opts.buttonToggle &&
						$('#combobox_overlay:visible').length
					) {
							$.fn.combobox.sluitSelect();
					} else	$.fn.combobox.openSelectFromButton($(this), opts);
					return false;
				});
			}
			
			//	selectie sluiten na mouse-out
			if (opts.hideOnMouseOut) {
				$(di).mouseout(function() {
					$.fn.combobox.sluitSelect();
				});
			}
			
			//	selectie openen via autocomplete
			$('input.combobox_input', di).keyup(function() {
				$('input.combobox_hidden', di).val($(this).val());
			});
			if (opts.autoComplete) {
				$('input.combobox_input', di).keyup(function() {
					var ul = $(this).parent().find('ul');
					var va = $(this).val().toLowerCase();
					var ar = new Array();
					if (va.length > 0) {
						$('li', ul).each(function() {
							var a = $(this).find('a')
							if (a.html().toLowerCase().indexOf(va) != -1 ||
								a.attr('href').toLowerCase().indexOf(va) != -1) {
									$(this).show();
									ar.push(a.attr('href'));
							} else 	$(this).hide();
						});
					}
					$.fn.combobox.sluitSelect();
					if (ar.length > 0) $.fn.combobox.openSelect(ul, ar.length, opts);
					
					opts.callbackShowOptions(ar);
					return false;
				});
			}
			
			//	gekozen selectie toepassen
			$('ul.combobox_summary li a', di).click(function() {
				var v = $(this).html(); //changed from gref in IE6 takes a full path
				//alert(v);		
				if (v == "Other") {
					$('input.combobox_input', di).val('');
					$('input.combobox_input', di).focus();
					$('input.combobox_input', di).css("background-color", "#F7DD8B");
				} else {
					$('input.combobox_input', di).val($(this).html());
					$('input.combobox_hidden', di).attr('value', v);
				}
				$.fn.combobox.sluitSelect();
				opts.callbackSelectOption(v);
				return false;
			});
			$.fn.combobox.sluitSelect();
		});
	};
	$.fn.combobox.openSelectFromButton = function(btn, o) {
		var ul = btn.parent().find('ul');		
		$('li', ul).show();
		$.fn.combobox.sluitSelect();
		$.fn.combobox.openSelect(ul, ul.children('li').size(), o);
		var ar = new Array();
		$('li a', ul).each(function() {
			ar.push($(this).attr('href'));
		});
		o.callbackShowOptions(ar);
	}
	$.fn.combobox.openSelect = function(el, aa, o) {
		el.show();
		var nh = (aa > o.maxVisibleOptions) ? (o.maxVisibleOptions * $('li:visible', el).height())+'px' : '';
		el.css('height', nh);
		el.scrollTop(0);
		$(el).parent().css('z-index', '200');
		$('#combobox_overlay').show();
	}
	$.fn.combobox.sluitSelect = function() {
		$('ul.combobox_summary').hide();
		$('#combobox_overlay').hide();
		$('ul.combobox_summary').parent().css('z-index', '0');
	}
	$.fn.combobox.defaults = {
		showOnMouseOver:		false,
		hideOnMouseOut:			false,
		autoComplete:			true,
		maxVisibleOptions:		1000000,
		buttonText:				'&gt;',
		buttonToggle:			true,
		callbackShowOptions:	function(arr) {},
		callbackSelectOption:	function(arr) {}
	};
	
})(jQuery);
