var whereAmI = null;
var J = jQuery.noConflict();

function loading(container, template_dir, withoutDiez) {
	
	if(!withoutDiez)
		container = '#'+container;
	
	if(template_dir !== undefined)
		J(container).html('<img src="'+template_dir+'defined_pictures/ajax_load.gif" border=0 />');
}

function load_page(container, file, template_dir) {
	
	loading(container, template_dir);
	
	file = file.replace('aj','php');
	file = 'files/'+file;

	J.ajax({
		type:'GET',
		cache: false,
		url:file,
		success: function (r) {
			J('#'+container).html(r);
			
			if(J('.menuCart').size() > 0) {
				J.ajax({
					type:'GET',
					cache: false,
					url:'menuCart.aj.html',
					success: function (r2) {
						J('.menuCart').parent().html(r2);
					}
				});
			}
			
			bindPage('#'+container);
			
		},
		error:function (xhr, ajaxOptions, thrownError){
            J('#'+container).load('error_'+xhr.status+'.aj.html');
        }
	});

}

function send_page(infosForm, container, file, template_dir)  {

	loading(container, template_dir);
	
	file = file.replace('aj','php');
	file = 'files/'+file;

	J.ajax({
		type:'POST',
		data:J(infosForm).serialize(),
		cache: false,
		url:file,
		success: function (r) {
			J('#'+container).html(r);
			bindPage('#'+container);
		},
		error:function (xhr, ajaxOptions, thrownError){
            J('#'+container).load('error_'+xhr.status+'.aj.html');
        }
	});
}

// Put the vertical slider into a pause mode while the mouse is over an element

function sliderInitCallback(carousel) {

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
	
}

// History

function pageload(hash) {
	// hash doesn't contain the first # character.
	// restore ajax loaded state
	if(hash != '') {
		loadThis(hash);
		whereAmI = hash;
	}
}

function loadThis(page) {

	J.ajax({
		type:'GET',
		cache: false,
		url:page,
		success: function (r) {
			J("#IncludeDiv").html(r);
			J("#ajaxLoader").hide();
			J("#IncludeDiv").show();
			J(document).attr('title',J('#IncludeDiv').find('h1').html()+' - '+J('#shop_name > a').html()+' - '+J('#catchword').html());
			bindPage('#IncludeDiv');
		},
		error: function (xhr, ajaxOptions, thrownError) {
            J("#IncludeDiv").load('/error_'+xhr.status+'.aj.html', function () {
            	
				J("#ajaxLoader").hide();
				J("#IncludeDiv").show();
				J(document).attr('title','Error '+xhr.status+' - '+J('#shop_name > a').html()+' - '+J('#catchword').html());
				
            });
        }
	});

}

// Ajax sur tous les liens

function loadP(id, destination) {
	
	J("#IncludeDiv").hide();
	J("#ajaxLoader").show();
	
	if(typeof id == 'string')
		var lien = id.split('.html');
	else
		var lien = J(id).attr("href").split(".html");
	
	// Google Analytics
	if(typeof pageTracker != "undefined")
		pageTracker._trackPageview("/" + lien[0] + ".html");
	
	// Putting the right link after the "#" in address bar
	
	if(typeof id == 'string')
		var hash = id;
	else
		var hash = J(id).attr("href");
		
	var reg = new RegExp("^.*/(.*)\.html", "");
	
	if( ! hash.match(reg) ) {
		
		// If there isn't http://.../
		
		reg = new RegExp("^(.*)\.html", "");
	}
	
	// Replacing .html by .aj.html in the new link
	
	hash = hash.replace(reg, "$1.aj.html");
	
	// Pageload is called at once. 
	if(hash == whereAmI) {
		J("#ajaxLoader").hide();
		J("#IncludeDiv").show();
	}
	else
		J.history.load(hash);
	
	hauteur = J('#menuTop').offset().top;
	J('html,body').animate({scrollTop:hauteur},1);

	return false;

}
// Dedicated to what to do after a load
function bindPage(context) {

	if(typeof context == 'undefined')
		context = '*';
		
	// Carousel
	if(J(".horizontalCarousel > li").size() > 2) {
		J(".horizontalCarousel").parent().jCarouselLite({
			btnNext: ".button-next-viewer",
			btnPrev: ".button-prev-viewer",
			vertical:false,
			scroll:2,
			visible:2,
			circular:false,
			mouseWheel:true
		});
	}
	else {
		J('.button-prev-viewer, .button-next-viewer').hide();
		J(".horizontalCarousel").css('margin-left',J('.button-prev-viewer').width());
	}

	// jQuery-ui
	J('.wannaAccordion', context).accordion({ icons: false });
	J('.wannaTab', context).tabs({
		cookie: {
			expires: 1
		}
	});
	J('.wannaChoice', context).buttonset();
	J('.wannaButton', context).each(function () {
		img = J(this).attr('title');
		noText = J(this).hasClass('noText');
		
		J(this).button();
		if(img != '')
			J(this).button('option', 'icons', { primary: img });
		if(noText)
			J(this).button('option', 'text', false);
		
	});
	
	J('form', context).submit(function () {
										
		if(J(this).attr('name') != 'payments') {
	
			var datas = J(this).serialize();
			var link = J(this).attr('action');
			var container = J(this).find('input[name=target]').val();
			var template_dir = J(this).find('input[name=template_dir]').val();
		
			loading(container, template_dir, true);
		
			var reg = new RegExp("^.*/(.*)\.html", "");
		
			if(!link.match(reg)) {
			
				// If there isn't http://.../
				reg = new RegExp("^(.*)\.html", "");
				
			}
		
			// Replacing .html by .aj.html in the new link
			link = link.replace(reg, "$1.aj.html");
	
			J.ajax({
				type: 'POST',
				data: datas,
				cache: false,
				url: link,
				success: function (r) {
					J(container).html(r);
					bindPage(container);
				},
				error:function (xhr, ajaxOptions, thrownError){
					J(container).load('error_'+xhr.status+'.aj.html');
				}
			});
			
			return false;
		
		}
		else
			return;
		
	});

}

