var DZ = {						
	currIndex:0,
	designerArray:[],
	imageLoaded:false,
	isFirstDesigner:true,
	isFirstDesignerImage:true,
	prodArray:[],
	direction:'left',
	isAnimating:true
};	

DZ.loadSection = function(){
	DZ.currIndex = 0;
	DZ.designerArray = [];
	DZ.imageLoaded = false;
	DZ.isFirstDesigner = true;
	DZ.isFirstDesignerImage = true;
	DZ.prodArray = [];
	DZ.direction='left';
	DZ.isAnimating=true;
	
	$('#designers .content').empty();
	$('#designers a.nextItem').die('click', DZ.goNextDesigner);
	$('#designers a.nextItem').live('click', DZ.goNextDesigner);
	
	$('#designers a.prevItem').die('click', DZ.goPrevDesigner);
	$('#designers a.prevItem').live('click', DZ.goPrevDesigner);
	
	$('#designers a.designerProdThumb').die('click', DZ.goProduct);
	$('#designers a.designerProdThumb').live('click', DZ.goProduct);
	
	
	$.ajax({
	  type: "GET",
		 url: 'content/' + lang + '/designers.xml',
		 context:$('#designers'),
		 dataType: "xml",
		 success: function(xml) {
			 var counter = 0;
			$(xml).find('designer').each(function(){
				var i = {};
				var pArray = [];					// array of products designed
				i.nome = $(this).attr('nome');
				i.img = $(this).attr('img');
				i.description = $(this).find('description').text();
				
				$(this).find('prodotto').each(function(){
					var prodObj = {};									   
					prodObj.nome=$(this).attr('nome');
					prodObj.tipo=$(this).attr('tipo');
					prodObj.thumb=$(this).attr('thumb');
					pArray.push(prodObj);
					
				});				
				i.prodArray = pArray;
				
				// check jquery address
				if(PO.subSection.split("+").join(" ").toLowerCase() == i.nome.toLowerCase()){
					DZ.currIndex = counter;
				}
				counter++;
				DZ.designerArray.push(i);
			});
			DZ.loadDesigner();			
		 },
		 error:function(e){
			//
		}
	}); //close $.ajax
	
	$(window).bind('keydown.designer', function(e) {
		if(e.keyCode == 37){
			DZ.goPrevDesigner(e);
		}
		if(e.keyCode == 39){
			DZ.goNextDesigner(e);
		}
	});
	
};


DZ.loadDesigner = function(){
	DZ.isAnimating = true;
	if(DZ.isFirstDesigner){
		$('#designers h1').html(DZ.designerArray[DZ.currIndex].nome.toLowerCase());	
		
		$('#designers .secondaryMenuDiv').html('<a href="javascript:DZ.showProdotti();void(0);" rel="showProdottiLink">' + langObject.prodottiDisegnati + '</a><br><br>');
		$('#designers .content').html(DZ.designerArray[DZ.currIndex].description);
		$('#designers .content').append($('<br style="clear:both">'));
		$('#designers .content').append('<a href="" class="prevItem">' + langObject.previous + '</a>');
		$('#designers .content').append('<a href="" class="nextItem">' + langObject.next + '</a>');		
		$('#designers .prodDiv').empty();
		for(var i=0; i< DZ.designerArray[DZ.currIndex].prodArray.length; i++){
			$('<div style="float:left;margin-right:10px;"><a href="" class="designerProdThumb" rel="' + DZ.designerArray[DZ.currIndex].prodArray[i].nome + '"><img src="' + DZ.designerArray[DZ.currIndex].prodArray[i].thumb + '"><br>' + DZ.designerArray[DZ.currIndex].prodArray[i].nome + '</a></div>')
				.appendTo($('#designers .prodDiv'));							 
		}
		$('#designers .prodDiv').append($('<br style="clear:both">'));
		$('#designers .prodDiv').hide();
		DZ.isFirstDesigner = false;
	}else{
		var contentWrapper = $('<div class="contentWrapper"></div>');
		contentWrapper.hide()
			.append('<h1></h1>')
			.append('<div class="secondaryMenuDiv"></div>')
			.append('<div class="prodDiv"></div>')
			.append('<div class="content"></div>')
			.append('<div class="underline"></div>');
		
		$(contentWrapper).find('.prodDiv').hide();
		$(contentWrapper).find('.secondaryMenuDiv').html('<a href="javascript:DZ.showProdotti();void(0);" rel="showProdottiLink">' + langObject.prodottiDisegnati + '</a><br><br>');
		$(contentWrapper).find('h1').html(DZ.designerArray[DZ.currIndex].nome.toLowerCase());
		
		$(contentWrapper).find('.content').html(DZ.designerArray[DZ.currIndex].description);
		$(contentWrapper).find('.content').append($('<br style="clear:both">'));
		$(contentWrapper).find('.content').append('<a href="" class="prevItem">' + langObject.previous + '</a>');
		$(contentWrapper).find('.content').append('<a href="" class="nextItem">' + langObject.next + '</a>');
		//$('#designers .nextItem').wait(200).css('top',$(contentWrapper).find('.content').height() + 'px');
		for(var i=0; i< DZ.designerArray[DZ.currIndex].prodArray.length; i++){
			$('<div style="float:left; margin-right:10px;"><a href="" class="designerProdThumb" rel="' + DZ.designerArray[DZ.currIndex].prodArray[i].nome + '"><img src="' + DZ.designerArray[DZ.currIndex].prodArray[i].thumb + '"><br>' + DZ.designerArray[DZ.currIndex].prodArray[i].nome + '</a></div>')
				.appendTo($(contentWrapper).find('.prodDiv'));
		}
		$(contentWrapper).find('.prodDiv').append($('<br style="clear:both">'));
		
		$('#designers .container').append(contentWrapper);
		contentWrapper.css({'left' : $(window).width()+66});				// put it "offstage"
		
		
	}	
	Cufon.replace('h1');
		DZ.loadImg(DZ.designerArray[DZ.currIndex].img);		
}

DZ.showProdotti = function(){
	$('#designers .prodDiv:last').slideToggle();	
}

DZ.loadImg = function(imgToLoad){
	var div = $('#designers .container:last');
	var src = imgToLoad;
	var img = new Image();
	
	// wrap our new image in jQuery, then:
	  $(img)
		.load(function () { 
						
			$(this).addClass('fullHeight');	
			$(div).append(this);
			PO.doResize();	
			
			if(DZ.isFirstDesignerImage){
				DZ.imageLoaded = true;
				$(this).hide();
				$(this).fadeIn(1000, function(){
					$(this).parent().find('img.fullHeight').not(':last').remove();	
					DZ.isAnimating = false;
				});	
				DZ.isFirstDesignerImage = false;
			}else{
				/*** animate in the content ***/
				var contentWrapper = $('#designers .contentWrapper:last'); 
				contentWrapper.show();
				if(DZ.direction == 'left'){
					$('#designers .contentWrapper:first').animate({'left':-$(window).width()}, 800, 'easeInOutExpo', function(){
							$('#designers .contentWrapper:first').remove();								
					})
					$(contentWrapper).animate({'left':66}, 800, 'easeInOutExpo');
					
					/*** animate in the image ***/
					$(this).css({'right':-$(window).width()});
					$(this).parent().find('img.fullHeight').not(':last').animate({'right':$(window).width()}, 800, 'easeInOutExpo', function(){
						$(this).parent().find('img.fullHeight').not(':last').remove();																	
					})
					$(this).animate({'right':0}, 800, 'easeInOutExpo', function(){
						DZ.isAnimating = false;															
					});
				}else{
					contentWrapper.css({'left' : -$(window).width()+66});
					$('#designers .contentWrapper:first').animate({'left':$(window).width()}, 800, 'easeInOutExpo', function(){
							$('#designers .contentWrapper:first').remove();		
					})
					$(contentWrapper).animate({'left':66}, 800, 'easeInOutExpo');
					
					/*** animate in the image ***/
					$(this).css({'right':$(window).width()});
					$(this).parent().find('img.fullHeight').not(':last').animate({'right':-$(window).width()}, 800, 'easeInOutExpo', function(){
						$(this).parent().find('img.fullHeight').not(':last').remove();																	
					})
					$(this).animate({'right':0}, 800, 'easeInOutExpo', function(){
						DZ.isAnimating = false;															
					});
				}
			}
								
			
		})		
		.error(function () {
					// notify the user that the image could not be loaded
		})
		.attr('src', src);
}

DZ.goProduct = function(e){
	e.preventDefault();
	$.address.value("/" + lang + "/collections/all/" + $(this).attr('rel'));  
}

DZ.loadSubSection = function(){
	var tempIndex = DZ.currIndex;
	var counter = 0;
	for(var i=0; i<DZ.designerArray.length; i++){
		if(DZ.designerArray[i].nome.toLowerCase() == PO.subSection.split("+").join(" ").toLowerCase()){
			DZ.currIndex = counter;			
		}	
		counter++;
	}
	if(tempIndex < DZ.currIndex){
		DZ.direction='left';
	}
	if(tempIndex > DZ.currIndex){
		DZ.direction='right';
	}
	DZ.loadDesigner();
}

DZ.goNextDesigner = function(e){
	e.preventDefault();
	if(PO.newSection != 'designers'){
		$(window).unbind('.designer');
		return;
	}	
	if(DZ.isAnimating){
		return;				  
	}
	DZ.currIndex++;
	if(DZ.currIndex == DZ.designerArray.length){
		DZ.currIndex = 0;			
	}
	DZ.direction = 'left';
	$.address.value("/" + lang + "/designers/" + DZ.designerArray[DZ.currIndex].nome); 
}

DZ.goPrevDesigner = function(e){
	e.preventDefault();
	if(PO.newSection != 'designers'){
		$(window).unbind('.designer');
		return;
	}	
	if(DZ.isAnimating){
		return;				  
	}	
	DZ.currIndex--;
	if(DZ.currIndex < 0){
		DZ.currIndex = DZ.designerArray.length-1;			
	}
	DZ.direction='right';
	$.address.value("/" + lang + "/designers/" + DZ.designerArray[DZ.currIndex].nome); 
}



