var rootUrl = getRootUrl();

$(document).ready(
	function()
	{
		embedFlash();
		onResize();
		$("#flash").scroll(onScroll);
	}
);

function getRootUrl()
{
	var url = window.location.href;
	url = url.replace("#", "");
	var arrUrl = url.split("/");
	var finalUrl = url;
	if (arrUrl[arrUrl.length-1] == "index.html")
	{
		arrUrl[arrUrl.length-1] = null;
		finalUrl = arrUrl.join("/");
	}
	
	return finalUrl;
}

function embedFlash()
{
	var flashvars = {};
	var params = {menu:"false", allowScriptAccess:"always", scale:"noscale", wmode:"opaque"};
	var attributes = {};
	swfobject.embedSWF(rootUrl + "swf/index.swf", "main-swf", "100%", "100%", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);
}


//Resize Window
var minHeight = 720;
var minWidth = 780;

$(window).resize(onResize);

function onResize() 
{
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	
	//Height
	if (winHeight < minHeight)
	{
		$("#main-swf").css("height", minHeight+"px");
	}
	else
	{
		$("#main-swf").css("height", (winHeight-18)+"px");
	}
	
	//Width
	if (winWidth < minWidth)
	{
		$("#main-swf").css("width", minWidth+"px");
	}
	else
	{
		$("#main-swf").css("width", (winWidth-15)+"px");
	}
	
	//Alinha seções ao centro
	onScroll();
}

function onScroll() 
{
	var posTop = $('#flash').scrollTop()*-1;
	$('.section').css("top", posTop + "px");
	
	var posLeft = ($("#flash").scrollLeft()*-1) + (Math.floor($("#main-swf").width()/2));
	$(".section").css("left", posLeft + "px");
}


//Show Sections
var sectionName = "";
function showContent(id)
{
	onScroll();
	
	sectionName = "";
	
	$('#downloads').css('display', 'none');
	$('#where').css('display', 'none');
	$('#news').css('display', 'none');
	$('#models').css('display', 'none');
	
	switch(id)
	{
		case "downloads" :
			sectionName = "downloads";
			$("#downloads").css("display", "block");
			loadPage(getRootUrl() + "home/downloads");
			break;
		
		case "where" :
			sectionName = "where";
			$("#where").css("display", "block");
			loadPage(getRootUrl() + "home/stores");
			break;
		
		case "news" :
			sectionName = "news";
			$("#news").css("display", "block");
			loadPage(getRootUrl() + "home/news");
			break;
		
		case "models" :
			sectionName = "models";
			$("#models").css("display", "block");
			addMenuListeners();
			
			var url = $("#models ul#main-menu li:eq(0) a").attr('href');
			loadModel(url);
			break;
	}
}

function addMenuListeners()
{
	removeMenuListeners();
	
	$("#models ul#main-menu li a").click(function()
	{
		var url = $(this).attr('href');
		loadModel(url);
		return false;
	});
}

function removeMenuListeners()
{
	$("#models ul#main-menu li a").unbind("click");
}


//Carrosel dos modelos
var modelscarousel;

function createModelsCarousel() 
{
	modelscarousel = $("#models-carousel").msCarousel( {boxClass:'li.box', scrollSpeed:400, height:250, width:310} ).data("msCarousel");
	
	//Lista de labels
	$(".sub-menu li:not('.next-prev')").click(function() 
	{
		if(modelscarousel != undefined) 
		{
			var no = $("#models ul.sub-menu li.box").index(this);
			modelscarousel.goto(parseInt(no)-1);
			
			showModelFeatures(no-1);
		}
	});
	
	//Botão anterior
	$(".prev-button").click(function() 
	{
		$("#models ul.features").fadeOut("fast");
		modelscarousel.previous();
		showModelFeatures(modelscarousel.getCurrentID());
	});
	
	//Botão próximo
	$(".next-button").click(function() 
	{
		$("#models ul.features").fadeOut("fast");
		modelscarousel.next();
		showModelFeatures(modelscarousel.getCurrentID());
	});
	
	//Ampliar imagens
	$("ul#models-carousel li.box img").click( function()
	{
		var src = $(this).attr("src");		var url = $(this).attr("rel");
		if (src.indexOf("sem_imagem_p.jpg") == -1)
		{
			src = url;
			popupWin = window.open(src, "popupWin", "location=0, status=0, scrollbars=0, width=470, height=340");
		}
	});
}

function showModelFeatures(id)
{
	var content = $("#models-carousel li.box").eq(id).find(".models-features ul").html();
	$("#models ul.features").html(content).fadeIn("fast");
}

function loadModel(url)
{
	$.ajax({url:url, success:function(html)
		{
			$(".prev-button").unbind("click");
			$(".next-button").unbind("click");
			
			$("ul#models-carousel li img").unbind("click");
			
			$("#models #models-content").empty();
			$("#models #models-content").append(html);
			
			createModelsCarousel();
			showModelFeatures(0);
		}
	});
	return false;
}

function loadContent(name)
{
	$.ajax({url:"pages/models/" + name.toLowerCase() + ".html", success:function(html)
		{
			$(".prev-button").unbind("click");
			$(".next-button").unbind("click");
			
			$("ul#models-carousel li img").unbind("click");
			
			$("#models #models-content").empty();
			$("#models #models-content").append(html);
			
			createModelsCarousel();
			showModelFeatures(0);
		}
	});
}


//Paginador
function loadPage( urlPage )
{
	$.ajax({url:urlPage, success:function(html)
		{
			destroyPaginator();
			
			$("#"+sectionName).empty();
			$("#"+sectionName).append(html);
			
			createPaginator();
		}
	});
}

function createPaginator()
{
	var nextPrevLink = "#"+sectionName+" div.paginator a";
	$(nextPrevLink).click( function() 
	{
		var urlPage = $(this).attr("href");
		loadPage(urlPage);
		
		return false;
	});
}

function destroyPaginator()
{
	$("#"+sectionName+" div.paginator div.content a").unbind("click");
}
