/**
 * @author nurun
 */

var updateProductAvailability = {
	init:function(){
		//updateProductAvailability.temporize();
		var lg = $('#formAvailability input:hidden[name=lg]').attr('value');
		var market = $('#formAvailability input:hidden[name=market]').attr('value');
		var country = $('#formAvailability input:hidden[name=country]').attr('value');
		var productId = $('#formAvailability select[name=product]').attr('value');
		var countryId = $('#formAvailability select[name=country]').attr('value');
		if (productId == 0 && countryId == 0)
		{
			updateProductAvailability.handleResponse('');
			return ;
		}
		 $.ajax({
			   type: "GET",
			   //url: this.href+'&ajax=true',
			   url: '/index.php?option=com_product&view=availability&format=raw&lg='+lg+'&market='+market+'&country='+country+'&productId='+productId+'&countryId='+countryId,
			   success: function(retour){
				updateProductAvailability.handleResponse(retour);				
			   }, 
			   dataType: "html"
		 });	
		 /*
		 ajaxStart(
			function(){
				// installer le loader
			}
		 )	*/					
		return false;
	},
	
	handleResponse:function(data){
		var tempInnerHTML = document.getElementById('answerContentWrapper');
		tempInnerHTML.innerHTML = data;
		addScrollToContainer('#answerContentWrapper');
		console.log('before isLoaded');
	}
}

var updateProductSheet = {

	clickValid : true,

	init:function(){
		$('#next').click(
			function(){
				 $.ajax({
					   type: "GET",
					   url: this.href+'&ajax=true',
					   //url: '/tmp/fakeAjaxProduct.html',
					   success: function(retour){
						if(updateProductSheet.clickValid){
							updateProductSheet.handleResponseNext(retour);
							var temp = new Array();
							temp = retour.split('<span id="Ga">');
							temp = temp[1].split('</span>');
							_url = _url = '/' + jQuery.trim(temp[0]);
							GATracker.init('UA-7824708-1', _url);
							var propertyID = $('#tracker').text();
							GATracker.init(propertyID, _url);
							parentKey = $('#Xiti').text();
							 createXitiAjax.init(_url, parentKey);
							$('.externalLink').click(function() { 
								window.open(this,'external');
								return false;
							} );						
						}						
					   }, 
					   dataType: "html"
				 });
				return false;
			}
		);
		
		$('#prev').click(
			function(){			
				 $.ajax({
					   type: "GET",
					   url: this.href+'&ajax=true',
					   //url: '/tmp/fakeAjaxProduct.html',
					   success: function(retour){
						if(updateProductSheet.clickValid){
							updateProductSheet.handleResponsePrev(retour);
							var temp = new Array();
							temp = retour.split('<span id="Ga">');
							temp = temp[1].split('</span>');
							_url = _url = '/' + jQuery.trim(temp[0]);
							GATracker.init('UA-7824708-1', _url);
							var propertyID = $('#tracker').text();
							GATracker.init(propertyID, _url);
							parentKey = $('#Xiti').text();
							 createXitiAjax.init(_url, parentKey);
							$('.externalLink').click(function() { 
								window.open(this,'external');
								return false;
							} );						
						}
					   }, 
					   dataType: "html"
				 });			 
				return false;
			}
		);
	},
	
	handleResponseNext:function(data){
		var time = 1200;
		updateProductSheet.clickValid = false;
		$("<div id='productsSheetNext'>"+ data +"</div>").insertAfter("#productsSheet");
		$("#productsSheet").animate({left: "-900px"}, time,"swing",function(){
			$("#productsSheet").remove();
		});
		$("#productsSheetNext").animate({left: "0"}, time,"swing");
        
		if (typeof document.body.style.maxHeight == 'undefined') {
			for( var i= 0, els= $('#productsSheetNext .png'), elsLen= els.size(); i< elsLen; i++ ) {
				var el= els.get(i);
				el.style.background= 'none';
				el.style.filter= 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + el.src + '", sizingMethod="image")';
				el.src= '/images/px.gif';
			}
		}
		setTimeout('$("#productsSheetNext").attr("id","productsSheet");',time+50);
		setTimeout("updateProductSheet.clickValid = true;",time+100);
		setTimeout("editTitreProduct.init()",time+150);
	},
	
	handleResponsePrev:function(data){
		var time = 1200;
		updateProductSheet.clickValid = false;
		$("<div id='productsSheetPrev'>"+ data +"</div>").insertBefore("#productsSheet");
		$("#productsSheet").animate({left: "900px"}, time,"swing",function(){
			$("#productsSheet").remove();
		});
		$("#productsSheetPrev").animate({left: "0"}, time,"swing");
        
		if (typeof document.body.style.maxHeight == 'undefined') {
			for( var i= 0, els= $('#productsSheetPrev .png'), elsLen= els.size(); i< elsLen; i++ ) {
				var el= els.get(i);
				el.style.background= 'none';
				el.style.filter= 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + el.src + '", sizingMethod="image")';
				el.src= '/images/px.gif';
			}
		}
		setTimeout('$("#productsSheetPrev").attr("id","productsSheet");',time+50);
		setTimeout("updateProductSheet.clickValid = true;",time+100);
		setTimeout("editTitreProduct.init()",time+150);
	}
}

var editTitreProduct = {
	init : function(){
		var Titre = $('h1 img').attr('alt');
		$('#headContent #path ul li:eq(1)').html('> '+Titre);
	}
}

