function desativaItemMenu(idItem){
	//document.getElementById(idItem).href='#';
	document.getElementById(idItem).className = "disabled";
}

(function($){

		var
			config,
			$stage,
			$previous,
			$next,
			currentItem,
			stageWidth,
			loadLock = false,
			toLoad = null,
			transition = "slide",
			onChange,
			onShow,
			iePremios=0,
			ok;		
			

		var
			setTypes = function() {
				$.each(config.dataSource, function(i, v) {
					var
						extension = v.url.match(/[^.]+$/)[0];

					if (extension.length == 3 || extension.length == 4) {
						switch (extension.toLowerCase()) {
							case "jpg":
							case "jpeg":
							case "gif":
							case "png":
								v.mime = extension;
								v.type = "image";
								break;
						}
					} else {
						v.mime = v.type = "html";                        
					}
				});
			},
			getIndexById = function(id) {
				var index = null;
				$.each(config.dataSource, function(i, v) {
					if (v.id == id) {
						index = i;
					}
				});
				return index;
			},
			getReferenceByIndex = function(index) {
				var reference = null;
				$.each(config.dataSource, function(i, v) {
					if (i == index) {
						reference = v;
					}
				});
				return reference;
			},
			getReferenceById = function(id) {
				var index = getIndexById(id);
				if (index !== null) {
					return config.dataSource[index];
				}
			},
			isLoaded = function(mixed) {
				if (typeof mixed !== "object") {
					mixed = getReferenceById(id);
				}
				return mixed.loaded == true;
			},
			goTo = function(id) {				
				var
					slide = getReferenceById(id),
					orientation = (arguments.length > 1 ? arguments[1] : true);

				if (slide){									
					
					//[-- adionado para permitir a visualição do itens no menu lateral: Jailson 16/06/2009		
					//PDF					
					if (slide.folio == 0){ // não entra quando for portifólio
						if (slide.pdf) {							
							href = 'caramelo.php?m=site.menu&tipo=pdf&item=' + slide.pdf;
							document.getElementById('openPDF').className = "";
							document.getElementById('openPDF').href = href;
						}else {
							desativaItemMenu('openPDF');
						}
						
						// IMAGEM
						if (slide.image3d){							
							document.getElementById('imagem').value = slide.image3d;
							document.getElementById('open3DModel').className = "";
							ok=1; // flag habilita acao do link do menu
							$("#open3DModel").click(function(){
									document.getElementById('tipo').value = 'imagem';	
									if(ok==1){ // testa a execução do link do menu
										$('#formMenu').submit();
									}
							})
						}else{						
							desativaItemMenu('open3DModel');
							ok=0; // flag desahabilita acao do link do menu 
						}
						
						
						//SYNTH
						if (slide.synth) {
							document.getElementById('synth').value = slide.synth;
							document.getElementById('open3d').className = "";
							ok=1; // flag habilita acao do link do menu
							$("#open3d").click(function(){
								document.getElementById('tipo').value = 'synth';
								if(ok==1){ // testa a execução do link do menu
									$('#formMenu').submit();
								}								
							})							
						}else {
							desativaItemMenu('open3d');
							ok=0; // flag desahabilita acao do link do menu
						}
						
						// LOCAL
						if (slide.local) {
							document.getElementById('mapa').value = slide.local;
							document.getElementById('openGMaps').className = "";
							ok=1; // flag habilita acao do link do menu
							$("#openGMaps").click(function(){
								document.getElementById('tipo').value = 'local';
								if(ok==1){ // testa a execução do link do menu
									$('#formMenu').submit();
								}							
							})
						}
						else{
							desativaItemMenu('openGMaps');
							ok=0; // flag desahabilita acao do link do menu
						}
					}
					//--]
					if (!isLoaded(slide)){						
						
						slide.dom = $('<div class="slide"/>').hide();

						if (slide.type == "image") {
							$('<img />')
								.bind("load", function() {
									slide.loaded = true;
									showSlide(id);
									progressiveLoad(id);
								})
								.attr("src", slide.url)
								//.attr("title", "clique para acessar o PDF")
								.click(function (){
                                   if(slide.pdf)
                                   {
                                       //window.open(slide.pdf,null,"height=500,width=700,status=yes,toolbar=no,menubar=no,location=no");
                                   }          
                                })
								.appendTo(slide.dom);                    
						} else {								
								slide.dom.load(slide.url.replace(/&amp;/g, "&"), {}, function() {
								slide.loaded = true;
								showSlide(id);
								progressiveLoad(id);
							});
						}
						slide.dom.appendTo($stage);
					}else{							
						showSlide(id, orientation);
					}
				}
			},			
			showSlide = function(id, orientation) {				
				var currentItemIndex = getIndexById(id), slide;
				$next.removeClass("disabled");
				$previous.removeClass("disabled");

				if (currentItemIndex === 0) {
					$previous.addClass("disabled");
				}
				if (currentItemIndex + 1 === config.dataSource.length) {
					$next.addClass("disabled");
				}

				if (transition == "slide") {					
					if (currentItem) {
						var
							bLeft = -(stageWidth), // before
							cLeft = 0,             // current
							aLeft = stageWidth;    // after

						if (orientation == false) {
							bLeft = stageWidth;
							aLeft = -(stageWidth);
						}

						getReferenceById(currentItem).dom.animate({
							"left": bLeft
						}, 500);						
						slide = getReferenceById(id);
						slide.dom.show().css("left", aLeft).animate({
							"left": cLeft
						},{
							duration: 500,
							complete: function() {							
								currentItem = id;
								onChange(slide, config.dataSource);
								onShow(slide, config.dataSource);
							}
						});
					} else {
						slide = getReferenceById(id);
						slide.dom.fadeIn("slow", function() {
							currentItem = id;
							onShow(slide, config.dataSource);
						});
					}
				} 
				else if (transition == "fade") {
					if (currentItem) {
						var
							bOpacity = 0, // before
							cOpacity = 0, // current
							aOpacity = 1, // after
							slide;

						getReferenceById(currentItem).dom.animate({
							"opacity": bOpacity
						},{
							duration: 1000,
							complete: function() {
								$(this).hide();
							}
						});

						slide = getReferenceById(id);
						slide.dom.show().css("opacity", cOpacity).animate({
							"opacity": aOpacity
						},{
							duration: 1000,
							complete: function() {
								currentItem = id;
								onChange(slide, config.dataSource);
								onShow(slide, config.dataSource);
							}
						});
					} else {
						slide = getReferenceById(id);
						slide.dom.fadeIn("slow", function() {
							currentItem = id;
							onShow(slide, config.dataSource);
						});
					}
				}
			},
			progressiveLoad = function(id) {

				if (loadLock == false) {

					var
						index = getIndexById((toLoad === null ? id : toLoad)) + 1,
						slide = getReferenceByIndex(index),
						countLoaded = 0;

					while (slide !== null && isLoaded(slide) === true) {
						index++;
						countLoaded++;
						slide = getReferenceByIndex(index);
					}

					if (slide !== null) {

						slide.dom = $('<div class="slide" />').hide();

						if (slide.type == "image") {
							loadLock = true;
							var $img = $('<img />')
								.bind("load", function() {
									slide.loaded = true;
									loadLock = false;
									progressiveLoad(slide.id);
								})
								.attr("src", slide.url)
								//.attr("title", "clique para acessar o PDF")
                                .click(function (){
                                   if(slide.pdf)
                                   {
                                      //window.open(slide.pdf,null,"height=500,width=700,status=yes,toolbar=no,menubar=no,location=no");
                                    }                                    
                                })
								.appendTo(slide.dom);                                
                               
						} else {
							loadLock = true;
							slide.dom.load(slide.url.replace(/&amp;/g, "&"), {}, function() {
								slide.loaded = true;
								loadLock = false;
								progressiveLoad(slide.id);
							});
						}

						slide.dom.appendTo($stage);

					} else {
						toLoad = null;

						if (countLoaded < config.dataSource.length - 1) {
							progressiveLoad(getReferenceByIndex(0).id);
						}
					}

				} else {
					toLoad = id;
				}

			};

			$.fn.SlideScroll = function(c) {
		
				if (typeof c === "object") {
					config = c;
					$stage = this;
					$previous = config.buttons.previous !== undefined ? $(config.buttons.previous) : $([]);
					$next = config.buttons.next !== undefined ? $(config.buttons.next) : $([]);
					stageWidth = $stage.width();

					if (config.dataSource == "stageLength") {
						var
							moveTo = 0,
							containerWidth = config.container.width();

						if (containerWidth > stageWidth) {
							$next.removeClass("disabled");
						}

						$previous
							.bind("click", function() {
								moveTo = -1;
								location.hash = "#m-none";								
								moveCustomers();
							});
							
							
						$next
							.bind("click", function() {						
								moveTo = 1;
								location.hash = "#m-none";	
								moveCustomers();
							});

						$(document)
							.bind("mouseup", function() {
								moveTo = 0;
						});

						function moveCustomers(){	
							
							if (moveTo !== 0) {
								if (moveTo === -1) {
									var left = config.container.position()["left"];
									if (config.container.position()["left"] < 0) {                           
								   
										//config.container.css("left", left + 5); 
										config.container.animate({"left": left + espaco}, 500);
										$previous.removeClass("disabled");
										if (config.container.position()["left"] * -1 < containerWidth - stageWidth) {
											$next.removeClass("disabled");
										}
										
									} else {
										$previous.addClass("disabled");
									}
								} else {
									var left = config.container.position()["left"];
									te=containerWidth - stageWidth;
									//alert('left: '+left+'soma: '+te);
									if (left * -1 < containerWidth - stageWidth) {                            
										
										//config.container.css("left", left - 5);
										
										config.container.animate({"left": left - espaco}, 500);
										
										$next.removeClass("disabled");
										$previous.removeClass("disabled");
									
									} else {
										$next.addClass("disabled");
									}
								}
							}							
						}

						return this;
                
			}
			else if(config.dataSource == "customers"){
                var
					moveTo = 0,
					containerWidth = config.container.width();

				if (containerWidth > stageWidth) {
					$next.removeClass("disabled");
				}				

				$previous
					.bind("click", function() {
						moveTo = -1;
						location.hash = "#m-none";
                        moveCustomers();
					});
					
				$next
					.bind("click", function() {
						moveTo = 1;
						location.hash = "#m-none";
                        moveCustomers();
					});

				/*$(document)
					.bind("mouseup", function() {
						moveTo = 0;
				});*/

				function buscaVariacaoEspaco(qtd,tam,acrescimo){
					var retorno=0;
					tamT=qtd*(tam+acrescimo);
					retorno=tamT/qtd;
					return retorno;
				}
				
				function moveCustomers() {															
					if (espaco!=408)
						espaco=buscaVariacaoEspaco(espaco,314,25) // 314 - tamanho da div ,25 - diferença para chegar ao tamanho total da div																										
					if (moveTo !== 0){						
						if (moveTo === -1) { //voltar
							var left = config.container.position()["left"];
							if (config.container.position()["left"] < 0) {
								//config.container.css("left", left + 320);
								config.container.animate({"left": left + espaco}, 500);
								$previous.removeClass("disabled");
								if (config.container.position()["left"] * -1 < containerWidth - stageWidth){
									$next.removeClass("disabled");
								}
							}else{
								$previous.addClass("disabled");
							}
						}else{ //avançar
							var left = config.container.position()["left"];							
							if (left * -1 < containerWidth - stageWidth){
								//config.container.css("left", left - 5);
								//espaco=339;								
								//alert("espaco:"+espaco+" left: "+left+" soma:"+t);
								config.container.animate({"left": left-espaco}, 500);								
								$next.removeClass("disabled");
								$previous.removeClass("disabled");
							}else{
								$next.addClass("disabled");
							}
						}
					}
				}
				return this;
            }

			onChange = config.onChange || function() {};
			onShow = config.onShow || function() {};

			if (c.transition) {
				transition = c.transition;
			}

			$previous
				.bind("click", function() {					
					var
						currentItemIndex = getIndexById(currentItem),
						ref = getReferenceByIndex(currentItemIndex - 1);

					if (ref !== null) {
						goTo(ref.id, false);
					}
				});

			$next
				.bind("click", function() {					
					var
						currentItemIndex = getIndexById(currentItem),
						ref = getReferenceByIndex(getIndexById(currentItem) + 1);

					if (ref !== null) {
						goTo(ref.id, true);
					}
				});

			if (config.dataSource.length > 1) {
				$next.removeClass("disabled");
			}

			setTypes();
			goTo(config.dataSource[0].id);

		}
		else{
			if (c == "goTo" && arguments.length > 1){
				goTo(arguments[1]);
			}
		}

		return this;
	};

})(jQuery);