/* VARIÁVEIS GLOBAIS */
var current_tab = 'colors';
var products = new Array();
var lenses = new Array();
var count = 0;


/* OBJETOS */
function Product() {
  lens_id = 0;
}

function Lens() {}

function Environment() {}




/* EVENTOS */
function mouseDown(e) {
  document.onmousemove = mouseMoved;
  document.onmouseup = function() { document.onmouseup = document.onmousemove = null; };
  mouseMoved(e);
}

function mouseMoved(e) {

  var overlayForeground = document.getElementById('overlay_foreground');
  var overlayBackground = document.getElementById('overlay_background');

  var overlayOffset = findPos(overlayForeground);
  var mouseOffset = findMousePos(e);
  
  var newWidth = mouseOffset[0] - overlayOffset[0];
  if (isPhotochromic && (newWidth < 80)) {
    newHeight = mouseOffset[1] - overlayOffset[1] - 3.5;
    if (newHeight < 28)
      newHeight = 28;
    else if (newHeight > 168)
      newHeight = 168;
    document.getElementById('photochromic_slider').style.top = '' + newHeight + 'px';
    var opacity = 1.0 - ((168 - newHeight) / 140.0);
    document.getElementById('overlay_photochromic').style.opacity = opacity;
    document.getElementById('overlay_photochromic').style.filter = 'alpha(opacity=' + parseInt(opacity*100) + ')';
  } else {
    if (newWidth < 0)
      newWidth = 0;
    else if (newWidth > overlayBackground.clientWidth)
      newWidth = overlayBackground.clientWidth;
    document.getElementById('overlay_photochromic').style.width = overlayForeground.style.width = '' + newWidth + 'px';
    document.getElementById('lens_slider').style.left = '' + newWidth - 6.5 + 'px';
  }
}




/* FUNÇÕES */

/* TROCA ABA DA AREA DE CONTEUDO */
function setCurrentTab(new_tab) {
	$('#'+current_tab).addClass('hide');
	$('#'+current_tab + '_header').removeClass('on');
	current_tab = new_tab;
	$('#'+current_tab).removeClass('hide');
	$('#'+current_tab + '_header').addClass('on');
}


/* VERIFICA VERSÂO DO NAVEGADOR */
if (navigator.appVersion.indexOf("Mac") > -1) window.mac = true;



/* TROCA AS INFORMAÇÕES DOS PRODUTOS */
function setCurrentProductID(productID) {

  try{ document.getElementById('loading').style.visibility = 'visible'; }catch(e){ }

  var product = products[productID];
  if (product.mainImage)
    document.getElementById('product_main_image').src = product.mainImage;
  else
    swfElement('product_main_3d').loadNewVideo(product.main3d);
  document.getElementById('product_lightbox').title = product.name;
  document.getElementById('enlarge').href = product.viewLargeURL;
  if (document.getElementById('product_lightbox'))
    document.getElementById('product_lightbox').href = product.viewLargeURL;
	
  if (product.viewLarge3d){
    document.getElementById('threedee').href = product.viewLarge3d;
	document.getElementById('threedee').style.display = 'block';
	document.getElementById('threedee').onclick = function(){openLightBox(product.viewLarge3d,'',product.name);return false;};
  }else{
	document.getElementById('threedee').href = '#';
	document.getElementById('threedee').style.display = 'none';
	document.getElementById('threedee').onclick = null;
  }

  document.getElementById('product_select').value = productID;
  currentProductID = productID;

  if (document.getElementById('size_select')) {
    var options = document.getElementById('size_select').options;
    if (options != null)
    for (var i=0;i<options.length; i++) {
        var option = options[i];
      if (option.value != "") {
        var sku = products[currentProductID].skus[option.value];
            if ( sku == null || (!sku.canSell)) {
                option.innerHTML = option.value + " (Call)";
      } else {
                option.innerHTML = option.value;
            };
      };
    };
  }

  if (product.lens_id) {
    changeLens(product.lens_id);
  }

  return false;
}


/*  */
function productSelectDidChange() { 
	setCurrentProductID(document.getElementById('product_select').value); 
}


/* ALTERA CENA DO SIMULADOR DE LENTES */
function environmentSelectDidChange() {
  currentLensEnvironment = document.getElementById('environment_select').value;
  updateLensImages();
}


/* FUNÇÃO QUE ALTERA AS IMAGENS DE SIMULAÇÃO PARA CORRESPONDER A LENTE SELECIONADA */
function updateLensImages() {

	if(currentLensEnvironment == 0)return false;

	var lens = lenses[currentLensID];

	// ATUALIZA AS IMAGENS DO SIMULADOR
	currentLensEnvironmentObject = lens.environments[currentLensEnvironment];
	document.getElementById('overlay_background').style.background = 'url(' + currentLensEnvironmentObject.image + ') no-repeat';
	document.getElementById('overlay_foreground').style.background = 'url(' + currentLensEnvironmentObject.imageWithLens + ') no-repeat';
	
	var overlayPhotochromic = document.getElementById('overlay_photochromic');
	if (isPhotochromic) {
		overlayPhotochromic.style.background = 'url(' + currentLensEnvironmentObject.photochromic + ') no-repeat';
		$('#overlay_photochromic').show();
		$('#photochromic_background').show();
		var overlayForeground = document.getElementById('overlay_foreground')
		if (overlayForeground.clientWidth < 80) {
			document.getElementById('overlay_photochromic').style.width  = overlayForeground.style.width = '375px';
			document.getElementById('lens_slider').style.left = '368px';
		}
	} else {
		$('#photochromic_background').hide();    
		$('#overlay_photochromic').hide();
	}
  
}


/* FUNÇÃO TRIGADA NO MOMENTO QUE SE SELECIONA UMA NOVA LENTE */
function changeLens(newLensID) {

  $('#lens_selector').hide();
  
  var lens = lenses[newLensID];
  document.getElementById('lens_color').innerHTML = document.getElementById('show_lenses_link_title').innerHTML = lens.color;
  document.getElementById('show_lenses_link_image').src = lens.imageName;
  document.getElementById('lens_application').innerHTML = lens.application;
  document.getElementById('lens_light_transmission').innerHTML = lens.lightTransmission;
  document.getElementById('lens_protection_index').innerHTML = lens.protectionIndex;
  document.getElementById('lens_usage').innerHTML = lens.usage;
  document.getElementById('lens_neutral').innerHTML = lens.neutral;
  
  isPhotochromic = lens.isPhotochromic;
  currentLensID = newLensID;
  
	// ATUALIZA COMBO BOX DE SELEÇÃO DE CENA
	var select = document.getElementById('environment_select');
	select.innerHTML = '';
  
	for (var i in lens.environments){
		select.innerHTML = select.innerHTML + '<option value="'+lens.environments[i].id+'">'+lens.environments[i].name+'</option>';
	}
	
  
  if(lens.environments[currentLensEnvironment]){
	select.value = currentLensEnvironment;
  }else{
	currentLensEnvironment = select.value;
  }
  
  updateLensImages();
  
  return false;
  
}


/* FUNÇÃO DE ACIONAMENTO DO LIGHTBOX */
function openLightBox(url,title,description) {
	$.prettyPhoto.open(url,title,description);
	return false;
}
function openLightBox2(object,title,description,width,height) {
	$.prettyPhoto.openObject(object,title,description,width,height);
	return false;
}


/* AREA DE SUPERIORIDADE OTICA > HDO */
function hdoSelectChanged(select) {

	var div = select.parentNode;
	var h3 = div.getElementsByTagName('h3')[0];
	var img = div.getElementsByTagName('img')[0];
	var option = select.options[select.selectedIndex];

	h3.innerHTML = option.innerHTML;
	img.src = option.value;
	
}

/* AREA DE SUPERIORIDADE OTICA > IMPACT PROTECTION */
function impactSelectChanged(select) {

	var div = select.parentNode;
	var h3 = div.getElementsByTagName('h3')[0];
	var div = div.getElementsByTagName('div')[0];
	var option = select.options[select.selectedIndex];

	h3.innerHTML = option.innerHTML;

	var movieId = div.id + '_swf';
	if (window.ie)
		window[movieId].changeVid(option.value);
	else
		document[movieId].changeVid(option.value);
	
}
function stopOakleyVideo() {}


/* FUNCIONAMENTO DO RODAPÉ */
function swapLabel(label){
	document.getElementById("int_label").innerHTML = label.title;
};
function revertLabel() { document.getElementById("int_label").innerHTML = document.getElementById("int_label").title; };



function mainImageLoadComplete(){
	//document.getElementById('loading').style.visibility = 'none';
	document.getElementById('loading').style.visibility = 'hidden';
}


function switchActive(element, bgposition){
	links=document.getElementsByName("mframe") ;
	for (var i = 0 ; i < links.length ; i ++)
		links.item(i).style.backgroundPosition = '0 35px' ;
	element.style.backgroundPosition=bgposition ;
} 


/* FUNÇÕES DE APOIO */

/* ACHA A POSIÇÃO ABSOLUTA DE UM OBJETO DENTRO DA PAGINA */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

/* ACHA A POSIÇÃO ABSOLUTA DO PONTEIRO DO MOUSE NO MOMENTO DE UM EVENTO */
function findMousePos(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return [posx,posy];
}

/* RETIRA AS OCORRENCIAS DE UMA SUBSTRING DENTRO DUMA STRING MAIOR */
function remove(str, sub) {
	  i = str.indexOf(sub);
	  r = "";
	  if (i == -1) return str;
	  r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	  return r;
}




/* FUNÇÃO DE OPERAÇÃO DO BOX DE TROCA DE PAISES */

$(document).ready(function() {
	jQuery('.hoverable').bind('mouseenter', function(e) {
		jQuery(this).find('.popup-for-hoverable').css('display', 'block');
	}).bind('mouseleave', function(e) {
		jQuery(this).find('.popup-for-hoverable').css('display', 'none');
	});
});


/* FUNÇÕES DE OPERAÇÂO DA TELA DE CARREGANDO */
var busy = null;

function showBusy(message){
	busy = getBusyOverlay("viewport", {color:'black',opacity:0.8,text:message},{color:'#FFFFFF',size:40});
}

function hideBusy(){
	busy.remove();
	busy = null;
}
