/*

  REF. JS <-> AS3
  
  http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript
  http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication

*/

var App = {
  
  ini : function ()
  {
	App.flash.embed();
		
  },
  
  setup : function()
  {
	App.assoc();
	App.adjust(0);
	App.pages.create_scroll();
  },

  JQA :
  {
	jqa : '/',
	opath : path.lastIndexOf('/') == path.length - 1 ? path : path + '/',
	
	change : function(value)
	{
	  //alert('address: "'+event.value+'"');
		if(value!=App.JQA.jqa){  	
		  App.JQA.jqa = value;
		  App.pages.get();
		}else if(value.length<2){
		  $('#menu').find('a:first').click();
		}
		
		if(App.flash.flash_ok)
		  App.flash.toTag(App.JQA.jqa);
	},
	
	clean_path : function (str){
	  return str.split(App.JQA.opath).join('');
	}
  },

  pages :
  {
	fixIE6 : '<!-- fix ie6 bug -->',
	
	get : function()
	{	
	  $('#rama').addClass('loading');
	  $('#conteudo-holder').fadeTo(0,.3);
	  $.ajax({
		url: App.JQA.opath+App.JQA.jqa,
		cache: true,
		data: {'ajax':true},
		error:function(XMLHttpRequest, status, error)
		{  		  
		  $("#conteudo").html(XMLHttpRequest.responseText+App.pages.fixIE6);
		  App.pages.recebido();  			
		},
		
		success: function(html)
		{  		  
		  $("#conteudo").html(html+App.pages.fixIE6);
		  App.pages.recebido();
		}
		
	  });
  
	},
	
	recebido : function()
	{
	  $('#conteudo-holder').fadeTo(500,1);
	  $('#rama').removeClass('loading');
	  App.pages.remove_scroll();
	  App.pages.create_scroll();
	  App.pages.make_links();
	  App.pages.make_tabs();
	},
	
	remove_scroll : function()
	{
	  $('#conteudo-holder').jScrollPaneRemove();
	},
	
	create_scroll : function()
	{
	  $('#conteudo-holder')
		.jScrollPane({
		  //reinitialiseOnImageLoad: true,
		  animateTo: true,
		  animateInterval:50,
		  animateStep:2
		});
	},
	
	make_links :	function()
	{	
	  /*
	  $('#conteudo img').load(function(){
		//App.pages.remove_scroll();
		App.pages.create_scroll();
	  });
	  */
	  
		$("#conteudo img").one("load",function(){
			App.pages.create_scroll();
		})
		.each(function(i){
			if(this.complete) $(this).trigger("load");
		});
	  
	  $('#conteudo')
		.find('a[href$=.jpg],a[href$=.png],a[href$=.gif],a[href$=.mov],')
		.not('[rel^=shadowbox]')
		.not('[href*=file.php]')
		.attr('rel','shadowbox')
		.click(function(e){e.preventDefault();});
		
	  $('#conteudo')
		.find('a[href^='+App.JQA.opath+']')
		.not('[rel^=shadowbox]')
		.not('[href*=file.php]')
		.click(function(e)
		{
		  e.preventDefault();
		  $.address.value(App.JQA.clean_path($(this).attr('href')));
		  App.pages.showBox();
		});
	  
	  $('#conteudo')
		.find('li > a')
		  .bind('mouseover',function(e)
		  {
			$(this).parent(0).addClass('hover');
		  })
		  .bind('mouseout',function(e)
		  {
			$(this).parent(0).removeClass('hover');
		  });
		  
	  
		
			Shadowbox.setup();
	},
	
	make_tabs : function()
	{
	  $('a.tabButton').click(function(e){
		e.preventDefault();
		var $myTab = $($(this).attr('href'));
		//$('.tab').not($myTab).hide(500);
		$myTab.toggle(500, function(){
		  App.pages.create_scroll();
		  if($(this).is(':visible')){
			$('#conteudo-holder')[0].scrollTo($(this).offset().top - $('#conteudo').offset().top - 100);
		  }
		});
	  });
	},
	
	hideBox : function ()
	{
	  $('#rama').css('top','-3000px');
	},
	
	showBox : function ()
	{
	  $('#rama').css('top','0');
	}
	
  },
  
  flash : 
  {
	
	flash_ok : false,
	objName : 'VR_lens',
	
	source: '/figurasnapaisagem/flash/VisoramaLens.swf',
	expressInstall: '/figurasnapaisagem/flash/expressInstall.swf',
	
	flashvars : {
	  lang: lang,
	  file_xml: '/figurasnapaisagem/flash/visorama.xml',
	  file_panorama: '/figurasnapaisagem/flash/panorama.jpg'
	},
	
	params : {
	  bgcolor : '#000000',
		allowScriptAccess: 'always',
		wmode: 'opaque'	
	},
	
	attributes : {
	  id: 'VR_lens',
	  name: 'VR_lens'
	},

	embed : function()
	{ 
		swfobject.embedSWF(
		  App.flash.source, "flashdiv", "240", "240", "10.0.0",
		  App.flash.expressInstall,
		  App.flash.flashvars,
		  App.flash.params,
		  App.flash.attributes
	  );   
	},
	
	ini : function()
	{
	  App.setup();
	  App.flash.flash_ok = true;
	  //App.flash.toTag(App.JQA.jqa);
	  setTimeout("App.JQA.change(App.JQA.jqa)",500);
	  setTimeout("$('#menu').css('display','block').fadeTo(0,0).fadeTo(1000,1)",500);
	  $('#site').removeClass('loadCenter');
	},
	  
	getFlashMovie : function (movieName)
	{
	  if (window.document[movieName]) 
	  {
		return window.document[movieName];
	  }
	  if (navigator.AppName.indexOf("Microsoft Internet")==-1)
	  {
		if (document.embeds && document.embeds[movieName])
		  return document.embeds[movieName]; 
	  }
	  else // if (navigator.AppName.indexOf("Microsoft Internet")!=-1)
	  {
		return document.getElementById(movieName);
	  }
	},
	  
	open : function (str) 
	{
	   App.flash.getFlashMovie(App.flash.objName).toAS3_open();
	   App.pages.showBox();
	},
	
	toTag: function (str) 
	{
	  clearTimeout(App.flash.openTimeout);
	  str = str.substr(1,str.length-2);
	  App.pages.hideBox();
	  //moved to assoc! just on menu button click
	  App.flash.getFlashMovie(App.flash.objName).toAS3_tag(str);
	  App.flash.openTimeout = setTimeout("App.flash.open()",3000);
	  
	},
	
	openTimeout : null
  },
  
  assoc : function()
  {
	$(window).resize(function() {
	  $('#site').stop();
	  App.adjust(500);
	});
	
	$('#flashover').click(function(e){
		if(App.flash.flash_ok)
		  App.flash.open(); 
	});
	
	$('#menu').find('a').filter('[href^='+App.JQA.opath+']').click(function(e) {
	  e.preventDefault();
	  var newvalue = '/' + App.JQA.clean_path($(this).attr('href'));
	  //alert("'"+newvalue+"' : "+"'"+$.address.value() +"'")
	  if($.address.value() != newvalue){
		$.address.value(newvalue);
		App.pages.hideBox();
	  }
	});
	
  },

  adjust : function (time)
  {
	$('#site').animate({
	  top: Math.round($(window).height()/2) - 200,
	  left: Math.round($(window).width()/2) - 360
	},time);
  }
  
};

Shadowbox.init({
  skipSetup: false,
  //handleOversize: "drag",
  overlayColor:'#000',
  overlayOpacity:0.9
});

$.address.init( function (event)
	{
	  var base = $.address.baseURL()+'/';	
	 //alert(base+' && ' + App.JQA.opath)	
	  if (base != App.JQA.opath)
		document.location = App.JQA.opath + '/#/'+App.JQA.clean_path(base);
	} );
	
  $.address.change( function(event)
  {
	App.JQA.change(event.value);
  } );
  
jQuery(document).ready(function($){
  
	App.ini();

});


