/*
 * Copyright (C) 2009 Jonathan Azoff <jon@azoffdesign.com>
 *
 * This script is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
 
 // toAJAX v1.0.1 - A script that can be used to turn any page into an AJAX page
 // Usage: 	call $("selector").toAJAX() on any link to turn it into an AJAX link. The links you 
 //         use toAJAX on must have a rel attribute defined so that the AJAX handler knows what 
 //         to look for on the AJAX call.
 // Arguments: The toAJAX function currently only takes 1 argument, whch is a callback function
 // Returns: A jQuery object that represents the selected toAJAX links
 // Notes:	toAJAX works best with sites that already work without AJAX and only have content that 
 //         changes in a distinct area or areas. 

;(function($) {
	
	// Preloader Image
	var preloader = $("<img/>").attr("alt", "loading...").attr("src", "http://www.cia-france.com/images/ajax-preloader.gif");
	
	// XHR Object
	var xhr = null;
	
	// The toAJAX extension
	$.fn.toAJAX = function(callback)
	{
		// The actual worker routine, it does all of the AJAX stuff
		var handler = function()
		{
			var obj = $(this);
			var url = obj.attr("href") || obj.attr("src");
			var id = "#" + obj.attr("rel");
			var target = $(id);
			var tmpBackground;
			var tmpBackgroundImage;
			var tmpBackgroundPositionX;
			var tmpBackgroundPositionY;
			
			// if we have an id to load into and a link where we want the information from...
			if(url && id != "#" && xhr == null)
			{
				xhr = $.ajax({
					type: "POST",
					url: url,
					cache: false,
					complete: function(){
						if(callback) callback();
						xhr = null;
					},
					beforeSend: function(){ 
						target.empty();//.append(preloader);
						tmpBackground = target.css('background');  // Needed for FF only
						// Getting the position like this is for IE only
						tmpBackgroundImage = target.css('background-image');
						tmpBackgroundPositionX = target.css('background-position-x');
						tmpBackgroundPositionY = target.css('background-position-y');
						
						target.css('background', "#FEEF83 url('" + preloader.attr('src') + "') no-repeat center 70px");
						
						target.text('Je geselecteerde horecabedrijven wordt bijgewerkt. Even geduld, er kan er maar 1 tegelijk!');
						target.css('width', "278px");
						target.css('padding', "10px");

						target.css('height', "116px");

					},
					error: function(){ 
						throw("toAJAX could not complete, an AJAX error ocurred."); 
					},
					dataFilter: function(data){
						// jQuery does not parse HTML well
						// return $(data).filter(id).html();
						data = data.substring(data.indexOf('id="' + id.substring(1) + '"')-5);
						data = data.substring(data.indexOf('>')+1);
						var divPosition = 0;
						var divEndPosition = 0;
						var stringPointer = 0;
						var divCounter = 1;
						// Loop alle divs af totdat aantalDivs 0 is
						do
						{
//							alert(stringPointer + "\n" + divCounter + "\n" + data.substring(stringPointer));
							divPosition = data.indexOf('<div', stringPointer+1);
							divEndPosition = data.indexOf('</div', stringPointer+1);
							if (divPosition > -1 && divPosition < divEndPosition)
							{
								divCounter++;
								stringPointer = divPosition;
							}
							else if (divPosition > divEndPosition && divEndPosition > -1)
							{
								divCounter--;
								stringPointer = divEndPosition;
							}
							else
							{
								break;
							}
						}
						while (divCounter > 0)
						data = data.substring(0, data.indexOf('</div', stringPointer));
						return data;
					},
					success: function(data){
						// Setting the background
						target.css('background-image', tmpBackgroundImage);
						target.css('background-position-x', tmpBackgroundPositionX);
						target.css('background-position-y', tmpBackgroundPositionY);
						target.css('background', tmpBackground); // Needed for FF only
						// Setting the output
						target.html(data);
						
						if ($('#ColumnResultsContainer').length == 1)
						{
							$('.InformationAdd').siblings('.AddedLabel').text('Geselecteerd');
						}
						
					}
				});
			}

			// make sure the link does not load
			return false;
		}
		
		return this.click(handler).keypress(handler);
	}
	
})(jQuery);
