$.fn.loadScript = function(scripts){
	if(!scripts) return false;
	$.each(scripts, function(i, n){
		alert(n);
	});
}

$.fn.ajaxRedirect = function(url){
	if(!url) return false;
	window.location = url;
	return true;
}

// Rewrite all links and forms actions
$.fn.enableWeb20 = function(){ 
	return this.each(function(){
		
		// Ajax links in container
		$(this).find('a.ajx').each(function(){
			$(this).click(function(){
				$.history.load($(this).attr('href'));
				return false;
			});
		});

		// New window links
		$(this).find('a.new').each(function(){
			$(this).click(function(){
				window.open($(this).attr('href'));
				return false;
			});
		});

		// Submit forms content via AJAX (disable until any answer is givven)
		$(this).find('form.ajx').each(function(){
			var options = {
				beforeSubmit: (function(){
					$.modal('<img src="../themes/first/img/ajax-loader.gif" alt="ajaxLoader" />',
						{
							close: false,
							overlay: 20,
							onOpen: (function (dialog){
								dialog.overlay.fadeIn('slow', function(){
									dialog.container.show('fast', function(){
										dialog.data.fadeIn('slow');
									});
								});
							})
						}
					)
				}),
				success: (function(){ $.modal.close(); })
			}
			$(this).ajaxForm(options);
		});

		startThickbox();
		
		// IE bug fixes
		if($.browser.msie){
			$(this).find('td[rowspan]').each(function(){
				if($(this).attr('rowspan') != 1){
					$(this).attr('rowSpan', $(this).attr('rowspan'));
				};
			});
		};

	});
};

// Change a form's state until a response is received
$.fn.formState = function(type){
	return this.each(function(){
		$(this).find(':input').each(function(){
			if(type && type == 'enable'){
				$(this).removeAttr('disabled');
			}
			else{
				$(this).attr('disabled', 'disabled');
			};
		});
	});
};

// Three replaceContent types
$.fn.ajaxFade = function(newContentElements){
    return this.each(function(){
     	$(this).fadeOut('fast', function(){
     		$(this).empty().append(newContentElements).fadeIn('normal', function(){
     			$(this).enableWeb20();
     		});
     	});
    }); 
};
$.fn.ajaxSlide = function(newContentElements){
    return this.each(function(){ 
     	$(this).slideUp('fast', function(){
     		$(this).empty().append(newContentElements).slideDown('normal', function(){
     			$(this).enableWeb20();
     		});
     	});
    }); 
};
$.fn.ajaxNormal = function(newContentElements){
    return this.each(function(){ 
		$(this).empty().append(newContentElements).enableWeb20();
    }); 
};

function ajaxRequest(href)
{
	if(!href) var href = 'totonne/home';
	
	if(firstLoad)
	{
		href = href.replace(/\/$/, '');
		if(href.search(/\//) == -1) href = href + '/home'
		href = href + '/firstLoad/true';
		firstLoad = false;
	}
	
	$.modal('<img src="../themes/first/img/ajax-loader.gif" alt="ajaxLoader" />',
		{
			close: false,
			overlay: 20,
			onOpen: (function (dialog){
				dialog.overlay.fadeIn('slow', function(){
					dialog.container.show('fast', function(){
						dialog.data.fadeIn('slow');
					});
				});
			})
		});
	
	$.ajax({
 		url: href,
 		dataType: "xml",
 		success: function(){
			urchinTracker(href);
			$.modal.close();
 		}
	});
};
$.fn.gaRequest = function(storeString){
	if(!storeString) return false;
	urchinTracker(storeString);
}

var firstLoad = true;
$(function(){	
	// Redirect to root always
	
	if(window.location.href.search('#') == -1)
	{
		var baseHref = $('base').attr('href');
		var pattern = new RegExp(baseHref, 'i');
		var currentHref = window.location.href;
		
		// If no trailing slash is present, add one
		if(currentHref.search(/\/$/) == -1) currentHref = currentHref + '/';
		// If base href cannot be found in the url, redirect to root
		if(currentHref.search(pattern) == -1) currentHref = baseHref;
		
		// Add the #
		var newHref = currentHref.replace(pattern, baseHref + '#');
		
		// Redirect
		window.location = newHref;
	}
	
	if(window.location.pathname.search(/\/$/) === -1){
		var hrefWithTS = window.location.href.replace(window.location.host + window.location.pathname, window.location.host + window.location.pathname + '/');
		window.location = hrefWithTS;
	}

	$('ul.lang li a').click(function(){
		var baseHref = $(this).attr('href');
		var newLocation = baseHref + '#' + window.location.hash.replace(/\#/, '');
		window.location = newLocation;
		return false;
	});
	
	// Rewrite links, and send an ajax request
	$('body').enableWeb20();
	
	$.history.init(ajaxRequest);
});

