/*
 * 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*/
        dialog:'',
		animateOut:false,
        animateIn:false,
		animateOutSpeed:'normal',
		animateInSpeed:'normal',
		method: 'POST', /* 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_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:'',/*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*/
		onSuccessFormProcess:function(op){},
		onSuccessFormProcessRefresh:'',
		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;
	}
	return this.each(function() {
	var current = jQuery.extend({},jQuery.AjaxifyDefaults, options);
	if(typeof current.target == 'function')
		current.target = current.target(this);

	if($(this).is('form')){
		if(options==undefined || !options.event)
			current.event = 'submit';
	}

	if(jQuery.metadata){
	current = jQuery.extend(current,jQuery(this).metadata());
	}
	
	if(current.event){
		if($(this).is('form')){
			$(":submit,input:image", this).bind('click',function(e) {
				var form = this.form;
				current.clk = this;
				if (this.type == 'image') {
					if (e.offsetX != undefined) {
						current.clk_x = e.offsetX;
						current.clk_y = e.offsetY;
					} else {
						if(e.pageX != undefined){
							current.clk_x = e.pageX - this.offsetLeft;
							current.clk_y = e.pageY - this.offsetTop;
						}else{
							current.clk_x = 1;
							current.clk_y = 1;
						}
					}
				}
				if (this.type == 'submit') {
					 current.button_submit = $(this).attr('name')+'='+$(this).attr('value');
				}
				$(form).triggerHandler('submit');
				return false;
			});
		}
		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;
	object = this;
	if($(this).is('tr')){
//		if($('a',this).attr('href')){
//			var link = $('a',this).attr('href').replace(/^#/, "");
//			current.link = link || current.link;
//		}
		object = $('a',this);
	}
	if(jQuery(object).is('a')){
		if(jQuery(object).attr('href')){
			var link = jQuery(object).attr('href').replace(/^#/, "");
			current.link = link || current.link;
		}else 
			current.link;
		if($(object).attr('title')){
			var title = jQuery(object).attr('title');
			current.title = title || current.title;
		}
	}
	if(jQuery(this).is('form')){
		current.forms = this;
		if(jQuery(this).attr('action')){
			var link = jQuery(this).attr('action').replace(/^#/, "");
			current.link = link || current.link;
		}else 
			current.link;
//		if($(this).attr('id'))
//			current.forms = '#'+$(this).attr('id');
		if(current.event=='click')
			current.event = 'submit';
	}

	if(typeof current.tagToload != 'object')
		if(jQuery(object).attr('target')){
			current.target = jQuery(object).attr('target');
		}else
			current.target;
	else
		current.target = '#AjaxifyTemp';
	
	if(current.dialog){
		if(!$(current.target).is('div')){
//			$('body',document).append('<div class="AjaxifyTarget" id="'+current.target.replace(/^#/, '')+'"></div>');
//			$(current.target).dialog({
			$('<div class="AjaxifyTarget" id="'+current.target.replace(/^#/, '')+'"></div>').appendTo('body').dialog({
				resizable: false,
				draggable: true,
				bgiframe: true,
				autoOpen: false,
				modal: true,
				close: function(event, ui) {
					//$(this).html('');
				}
			});
			$.each(current.dialog,function(i,val){$(current.target,document).dialog('option', i, val);});
// current.target.data('ObjToRefresh') przechowuje liste objektow ktore trzeba odswiezyc po zamknieciu okna
			$(current.target).bind("dialogclose",function(){ 
				var ObjToRefresh = $(this).data('ObjToRefresh');
				$(this).data('ObjToRefresh','');
				if(typeof ObjToRefresh == 'object')
					$.each(ObjToRefresh, function(i,val){
						$(val).find("[name='refresh']").trigger("click");
						$(val).trigger("refresh");
					})
			})
		};
// w elementach do tej pory aktywnych usuwamy klase EnableKeys, 
// jednoczesnie zapamietujemy te elementy aby przy zamknieciu okna przywrocic im to ustawienie
		if(typeof current.ObjEnableKeys != 'object' || current.ObjEnableKeys.length==0){	
			current.ObjEnableKeys = $(".EnableKeys",document);
		}
		current.ObjEnableKeys.removeClass('EnableKeys');	
		$(current.target).bind("dialogclose",function(){current.ObjEnableKeys.addClass('EnableKeys')})
	}
	
	current.paramres = "";
	if(current.forms){
		var text = jQuery(current.forms).serialize();
		current.paramres = text;
		if(current.clk_x){
			text = $(current.clk).attr('name')+'.x='+current.clk_x+'&'+$(current.clk).attr('name')+'.y='+current.clk_y;
			if(current.paramres)
			current.paramres +='&'+text;
			else
			current.paramres = text;
		}
	}
	
	if(typeof current.params == 'function')
		var params = current.params(current);
	else
		var params = current.params;

	if(typeof params == 'string'){
		if(current.paramres)
		current.paramres +='&'+params;
		else
		current.paramres = params;
	}

	if(current.button_submit)
	{
		if(current.paramres)
			current.paramres +='&'+current.button_submit;
		else
			current.paramres = current.button_submit;
	}

	if(typeof current.target == 'string'){
	  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){
//	jQuery.ajaxifyManip('#div_inprogress','<div id="AjaxifyLoading" class="SmallLoading">&nbsp;</div>');
//	var html = "<div id='AjaxifyLoading'><img src='"+options.loading_img+"' title='Loading...' >"+options.loading_txt+"</div>";
	$('.loader').removeClass('loader-hidden');
}





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) {
	if(current.link.indexOf("?") == -1)
		current.link = current.link + "?ajax=1";
	else
		current.link = current.link + "&ajax=1";

	// 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(){
					if(!current.loading_target)//already fired
					current.loading_fn(current);		
				});
			}else
				current.loading_fn(current);
			},
		success: function(data){
		jQuery(current.target).stop();
		$('.loader').addClass('loader-hidden');

		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);
		  }
		if(current.dialog)
			$(current.target).dialog( 'open' );
		else
//			$(current.target).filter(".ui-dialog-content").dialog('option', 'position', 'center');

		current.onSuccess(current,data);
		if($(current.forms).is('form') && current.event=='submit'){
			if(!$('#'+$(current.forms).attr('name')+'_error').html()){
				if(typeof current.onSuccessFormProcess == 'function')
				current.onSuccessFormProcess(current,data);
				$(current.target).triggerHandler("onSuccessFormProcess",[current, data]);
				if(typeof current.onSuccessFormProcessRefresh == 'object'){
					var ObjToRefresh = $(current.target).data('ObjToRefresh');
					if(typeof ObjToRefresh == 'object'){
						$.each(current.onSuccessFormProcessRefresh,function(i, val){
							if($.inArray(val,ObjToRefresh)===-1)
								ObjToRefresh[sizeof(ObjToRefresh)] = val
						})
//						$.merge(ObjToRefresh,current.onSuccessFormProcessRefresh);
					}else
						ObjToRefresh = current.onSuccessFormProcessRefresh
					$(current.target).data('ObjToRefresh',ObjToRefresh);
				}
//				if($(current.target).dialog( 'isOpen' ) && (data.length==0 || $('<div>'+data+'</div>').find("input:hidden[name='close']").size()))
//					$(current.target).dialog( 'close' );
			}
		}
		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);
