var wrapFrameNew;
var size;
var slideTime;
var firstURL;
var uniqueIdentifier = new Date().getTime() + "" + Math.floor(Math.random() * 10000);
var defaultURL = "home.html?uid="+uniqueIdentifier;

$(function(){

	sites = [
		{id:"paisagens", url:"http://saveiro2009.tempsite.ws/blog"},
		{id:"cinema_na_trilha", url:"http://videolog.uol.com.br/video?521706"},
		//{id:"cinema_na_trilha", url:"http://www.youtube.com/watch?v=GKQoaUeaE3w"},
		//{id:"va_pra_bem_longe", url:"http://viajeaqui.abril.com.br/indices/boasvindas/mt_boasvindas_cidades_230296.shtml"},
		{id:"va_pra_bem_longe", url:"http://viajeaqui.abril.com.br/g4r/destinos/index.php?destino=13300"},
		{id:"gostou_da_botina?", url:"http://www.casadepedra.com.br/loja/products/showcase/01766/bota_salomon_3d_fast_packer_mid_gtx_masc..html"},
		{id:"mapa_do_carro", url:"http://www.volkswagen.com/br/pt/modelos/Saveiro/technical_specifications.html"},
		//{id:"mapa_do_carro", url:"http://www.volkswagen.com/br/pt/modelos/saveiro/technical_data.html"},
		{id:"itens_de_serie", url:"http://www.volkswagen.com/br/pt/modelos/Saveiro/conheca-e-compare.supercategory-TabGroup_0001.category-TabGroup_0001_Tab_0001.variation-selmodelvariants-sel2.html"},
		//{id:"itens_de_serie", url:"http://www.volkswagen.com/br/pt/modelos/saveiro/conheca-e-compare.supercategory-TabGroup_0001.category-TabGroup_0001_Tab_0002.variation-selmodelvariants-sel0.html"},
		{id:"opcionais", url:"http://www.volkswagen.com/br/pt/modelos/Saveiro/conheca-e-compare.supercategory-TabGroup_0001.category-TabGroup_0001_Tab_0003.variation-selmodelvariants-sel2.html"},
		//{id:"opcionais", url:"http://www.volkswagen.com/br/pt/modelos/saveiro/conheca-e-compare.supercategory-TabGroup_0001.category-TabGroup_0001_Tab_0003.variation-selmodelvariants-sel0.html"},
		{id:"monte_o_seu", url:"%MONTE%"},
		//{id:"somzera_da_trilha", url:"http://albumvirtual.trama.uol.com.br/flash_download.jsp?id=1128131101"},
		{id:"somzera_da_trilha", url:"http://albumvirtual.trama.uol.com.br/flash_download.jsp?id=1128131101", center:false},
		{id:"por_dentro_do_filme", url:"http://videolog.uol.com.br/volkswagendobrasil/videos/475219"}
	];
	
	init();
});

function init() {
	
	firstURL = defaultURL;
	var deep = SWFAddress.getValue().split( "/" ).join( "" );
	
	for( var i=0; i<sites.length; i++ ){
		if( sites[i]["id"] == deep || sites[i]["id"].split("?").join("") == deep.split("?").join("") ){
			firstURL = sites[i]["url"];
		}
	}
	
	var size = getSize();
	$(".wrapFrame").css({top:"0"}).find("iframe").attr({width:size.width+ "px",height:size.height + "px"});
	
	if( firstURL == defaultURL ){
		$(".wrapFrame").find("iframe").attr({src:firstURL});
	}
	
	$(window).resize(function(){
		resizeFrame();
	});
}

function loadDeeplinkURL()
{
	if( firstURL.indexOf("%") == -1 ){
		$(".wrapFrame").find("iframe").attr({src:firstURL});
	}
}

function loadPageByIndex(index, firstJump){

	var url = sites[index]["url"];
	if( url == "%MONTE%" ){
		$("#flashContainer").get(0).pageLoadComplete();
		return;
	}

	currentSite = index;
	if($("#contentMask .wrapFrame").size()==2){
		$("#contentMask .wrapFrame:eq(1)").remove();
	}
	
	//$('<img src="img/ajax-loader-player.gif" />').insertBefore("#links ul");
	
	wrapFrameNew = $("<div/>");
	var iframeAppnd = ($.browser.msie)?'<iframe border="0" frameborder="0" scrolling="yes" ></iframe>':'<iframe border="0" frameborder="0" ></iframe>';
	
	$(wrapFrameNew).addClass("wrapFrame").html(iframeAppnd);
	$("#contentMask").append(wrapFrameNew);
	
	var size = getSize();
	//$(wrapFrameNew).find("iframe").attr({src:sites[index].url,width: (sites[index].center)?size.widthCenter:size.width+ "px",height:size.height + "px"});
	$(wrapFrameNew).find("iframe").attr({src:sites[index].url,width:size.width+ "px",height:size.height + "px"});
	
	$(wrapFrameNew).css({top:size.height+"px"});

	if( firstJump ){
	
		$("#flashContainer").css("height", 340);
		slideTime = 1700;
		startSlide();
		
	} else {
	
		slideTime = 700;
	
		$(wrapFrameNew).find("iframe").load(function(){
			//$("#links img").remove();
			$("#flashContainer").get(0).pageLoadComplete();
		});
	}
	
}

function startSlide(){
	var size = getSize();
	//$("#flashContainer").get(0).pageSlideStart();
	if($("#contentMask .wrapFrame").size()!=1) $("#contentMask .wrapFrame:eq(0)").animate({top:-size.height},slideTime);
	$(wrapFrameNew).animate({top:0},slideTime,function(){
		$("#flashContainer").get(0).onFinishSlide();
		if($("#contentMask .wrapFrame").size()!=1) $("#contentMask .wrapFrame:eq(0)").remove();
		resizeFrame({height:1});
	});
}

function resizeFrame(addSize){
	var size = getSize();
	var height = (typeof addSize != 'undefined')? size.height + addSize.height : size.height;
	$("iframe").attr({width:(typeof currentSite != 'undefined' && sites[currentSite].center)?size.widthCenter +"px":size.width +"px",height:height +"px"});
}
function getSize(){
	var size = {width: 0, height: 0};
	size.height = $(document).height();
	//size.widthCenter = ($(document).width() < 1018)?1018:(($(document).width() - 1018)/2)+1018;
	//size.width = ($(document).width() < 1018)?1018:$(document).width();
	size.widthCenter = (($(document).width() - 1018)/2)+1018;
	size.width = $(document).width();
	
	return size;
}

function dispatchPhotoClick( flv ){
	$("#flashContainer").get(0).commentPicture( flv );
}

function changeMenuAreaSize( height ){
	$("#flashContainer").css("height", height);
}