gallery = { 
	
	init: function(){
	
		iTotalFeatures = $("#features_list li").length;
		iFeaturesRight = 4;		
		checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 4);

		$("#features_list li").click(function(){
			$("#features_list li").removeClass("feature_selected");
			$(this).addClass("feature_selected");

			var now = $(this).attr("id");
			if( now == 'feature_item_0' ){
				$('#feature_car').show(0);
			} else {
				$('#feature_car').hide(0);
			}
			$("#feature_panes li.selected").removeClass("selected").fadeOut('fast',function(){
				$("#feature_panes li#"+now+"_pane").fadeIn("fast").addClass("selected");
			});
		});
		
		$("#feature_arrows .rev a").click(function(event){ iFeaturesRight = moveFrames("#features_list_wrapper", 165, 'left', iTotalFeatures, 4, iFeaturesRight); checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 4); return false; });
		$("#feature_arrows .fwd a").click(function(event){ iFeaturesRight = moveFrames("#features_list_wrapper", 165, 'right', iTotalFeatures, 4, iFeaturesRight); checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 4); return false; });
	}
}

function checkButtons(isHorizontal, stLeftArrowID, stRightArrowID, iRightFeature, iTotalFeatures, iQuantityVisible){
	if(isHorizontal){if(iRightFeature >= iTotalFeatures){ $(stRightArrowID).removeClass('on').addClass('off'); }else{ $(stRightArrowID).removeClass('off').addClass('on'); }if((iRightFeature-iQuantityVisible) > 0){ $(stLeftArrowID).removeClass('off').addClass('on'); }else{ $(stLeftArrowID).removeClass('on').addClass('off'); }}else{if(iRightFeature >= (iTotalFeatures-1)){ $(stRightArrowID).removeClass('on').addClass('off'); }else{ $(stRightArrowID).removeClass('off').addClass('on'); }if((iRightFeature-iQuantityVisible) > 0){ $(stLeftArrowID).removeClass('off').addClass('on'); }else{ $(stLeftArrowID).removeClass('on').addClass('off'); }}
}

function moveFrames(stFrameID, iFrameWidth, stDirection, iTotalQuantity, iMoveAmount, iCurrentEdgeElement){
	if(stDirection == 'right'){ iFeaturesLeft = iTotalQuantity-(iCurrentEdgeElement); if(iFeaturesLeft <= 0){ return iCurrentEdgeElement; } if(iMoveAmount >= iFeaturesLeft){ iMoveAmount = iFeaturesLeft; } iCurrentEdgeElement += iMoveAmount; if(iCurrentEdgeElement >= iTotalQuantity){ iCurrentEdgeElement = iTotalQuantity; } iFullMove = iMoveAmount*iFrameWidth; $(stFrameID).animate({"left": "-="+iFullMove+"px"}, "slow"); }else if (stDirection == 'left'){ if(iCurrentEdgeElement == iMoveAmount){ return iCurrentEdgeElement; }else if((iCurrentEdgeElement-iMoveAmount) > iMoveAmount){ iShiftAmount = iMoveAmount;  }else{ iShiftAmount = (iCurrentEdgeElement-iMoveAmount); } iFullMove = iShiftAmount*iFrameWidth; $(stFrameID).animate({"left": "+="+iFullMove+"px"}, "slow"); iCurrentEdgeElement -= iShiftAmount; } else { iCurrentEdgeElement = 4; $(stFrameID).animate({"left": "0px"}, "slow"); } return iCurrentEdgeElement; 
}

$(document).ready(function() {

	$.ajax({

		 type: "GET",
		 url: xmlGaleria,
		 dataType: "xml",
		 success: function(xml) {		 
			i=1;	
			var path =  $(xml).find("galeria").attr("path");
			$(xml).find("item").each(function(){
				
				var descricao =  $(this).find("descricao").text();
				var imagem =  $(this).find("imagem").text();
				
				    $("<li id='feature_item_"+i+"'></li>").html("<img src='"+ path + imagem +"_thumb.jpg' />").appendTo("#features_list");
					$("<li id='feature_item_"+i+"_pane'></li>").html("<div class='nome fontCitroenLight'>" + descricao + "</div><img src='"+ path + imagem +".jpg' />").appendTo("#feature_panes");
				
				i++;
			 });

            Cufon.replace('.fontCitroenLight', { fontFamily: 'Citroen Light' });
            Cufon.replace('.fontCitroen', { fontFamily: 'Citroen' });
			
			gallery.init();

		}

	 });
	 

     $.ajax({

		 type: "GET",
		 url: xmlDados,
		 dataType: "xml",
		 success: function(xml) {		 
			//alert('sucesso');
			$(xml).find("veiculo").each(function(){
				var id =  $(this).attr("id");
				
				$('#feature_car h2 strong').html($(this).find("nome").text());
				
				$(this).find("apartir").each(function(){
					$('#preco i').html($(this).attr("preco"));
				});
				
				$(this).find("features").each(function(){
					$(this).find("feature").each(function(){
						$("<li><img src='"+ $(this).attr('imagem') +"' /></li>").appendTo("#features");
					});
				});
				
				$(this).find("modelos").each(function(){
					$(this).find("modelo").each(function(){
						$("<li></li>").html($(this).attr("nome")).appendTo("#motorizacao");
					});
				});
				
				$(this).find("selos").each(function(){
					$(this).find("selo").each(function(){
						if($(this).attr("tipo") == "xhtml"){
							$("<li></li>").html($(this).text()).appendTo("#selos");
						} else {
							$("<li></li>").html($(this).find("descricao").text()).appendTo("#selos");
						}
					});
				});
				
				$(this).find("cores").each(function(){
					var path = $(this).attr("path");
					$(this).find("cor").each(function(){
						$("<li id='cor"+$(this).attr('id')+"'><span><strong>cor</strong> "+$(this).attr('nome')+"</span><img src='/_media/cores/"+ $(this).attr('id') +".jpg' title='"+path + $(this).attr('imagem') +"' /></li>").appendTo("#cores");
						if ( $(this).attr("default") ) {
							$('#selected-color').attr('src', path + $(this).attr('imagem'));
							$('#cor'+$(this).attr("id")+' span').show();
						}
					});
				});
				
			 });

			 $('#cores li').click(function(){
				$('#cores li span').hide(0);
				$(this).children('span').show();
				var imagem = $(this).children('img').attr('title');
				$('#selected-color').attr('src', imagem);
			 });
			 
			 $('#cores li:first-child').addClass('first');
			 
			Cufon.replace('.fontCitroenLight', { fontFamily: 'Citroen Light' });
			Cufon.replace('.fontCitroen', { fontFamily: 'Citroen' });
		 }
	 });
	 
	 
	 
	$('#preco, #texto-legal').hover(
      function () { $('#texto-legal').css("display","block"); }, 
      function () { $('#texto-legal').css("display","none"); }
	 );

});