/*
    ##    td_rollover_engine.js             ##
    ##    10/03/2009                        ##
    ##    Jaydson Gomes                     ##
    ##    Dependências : jquery-1.3.1.js    ##
    ##                   jquery.ifixpng.js  ##
                      
    
*/

// GLOBAIS
var x = 0; // Controla a posição TOP
var x_volta = 0;
var counter = 1; // Controla o contador
var counter_volta = 1; 
var array_reference = new Array();
var array_options = new Array();
var $td = jQuery.noConflict();
var GLOBAL_ANIMATE_FIRST = false;
   
$td(document).ready(function(){ 
  //LOAD JSON
  //$td("#loading").ajaxSuccess(function(evt, request, settings){
   //$td(this).empty();
   //$td(this).append("<img src='../img/consulte_compre.png' />");
  //});
if(counter == 1){
    $td("#down").css("visibility","hidden");
  }else{
    $td("#down").css("visibility","visible");
  }
  if(counter_volta == 1){
    $td("#down_volta").css("visibility","hidden");
  }else{
    $td("#down_volta").css("visibility","visible");
  }
  loadJSONCidades();
  document.getElementById('rollover_volta').appendChild(array_reference[0]);
  //FIX PNG
  $td.ifixpng('media/pixel.gif'); 
  $td('div#up').ifixpng();
  $td('div#down').ifixpng();
  $td('div#up_volta').ifixpng();
  $td('div#down_volta').ifixpng();
  $td('div#down_click').ifixpng();
  $td('div#up_click').ifixpng();
  $td('div#down_click_volta').ifixpng();
  $td('div#up_click_volta').ifixpng();
  $td('div#loading').ifixpng();
   
   // CLICK UP DESTINO
   $td("#up_volta").click(function(){
    for(var i=0;i<document.getElementById('rollover_volta').getElementsByTagName('DIV').length;i++){
      if(i+1 == counter_volta){
       }
   }
    $td("#up_volta").css("display","none");
    $td("#up_click_volta").css("display","block");
    if(parseInt($td("#rollover_volta").css("top")) <= -220){
      $td("#up_volta").css("display","block");
      $td("#up_click_volta").css("display","none");
      return false;
    }
    counter_volta++;
    $td("#reference_volta").text(counter_volta + " de 5");
    x_volta-= 55;
    $td("#rollover_volta").animate({ 
      top : x_volta + 'px'
    },800,function(){
            $td("#up_volta").css("display","block");
            $td("#up_click_volta").css("display","none");
            if(counter_volta > 1){
              $td("#down_volta").css("visibility","visible");
            }
            if(counter_volta == 5){
              $td("#up_volta").css("visibility","hidden");
            }
          });
    document.getElementById("select_cidades_origem").onchange(document.getElementById('select_cidades_origem'));   
    document.getElementById("select_cidades_destino").onchange(document.getElementById('select_cidades_destino'));   
    document.getElementById("select_cidades_destino").selectedIndex = counter_volta - 1;
    document.getElementById("preco").innerHTML = document.getElementById("select_cidades_destino").value + "<span style='font-size:40px'>*</span>";
    document.getElementById("price_cidades").value = document.getElementById("select_cidades_destino").value;
   });
   
    // CLICK DOWN DESTINO
   $td("#down_volta").click(function(){
      for(var i=0;i<document.getElementById('rollover_volta').getElementsByTagName('DIV').length;i++){
        if(i+1 == counter_volta){
          //alert(document.getElementById('rollover').getElementsByTagName('DIV')[i].id);
         }
      }
      $td("#down_volta").css("display","none");
      $td("#down_click_volta").css("display","block");
      if(parseInt($td("#rollover_volta").css("top")) >= 0){
        $td("#down_volta").css("display","block");
        $td("#down_click_volta").css("display","none");
        return false;
      }
      counter_volta--;
      $td("#reference_volta").text(counter_volta + " de 5");
      x_volta+= 55;
      if(GLOBAL_ANIMATE_FIRST){
        $td("#rollover_volta").animate({ 
          top : 0 + 'px'
        }, 800,function(){
                $td("#down_volta").css("display","block");
                $td("#down_click_volta").css("display","none");
                GLOBAL_ANIMATE_FIRST = false;
                if(counter_volta > 1){
                  $td("#up_volta").css("visibility","visible");
                  }
                  if(counter_volta == 1){
                    $td("#down_volta").css("visibility","hidden");
                  }
              });
      }else{
        $td("#rollover_volta").animate({ 
          top : x_volta + 'px'
        }, 800,function(){
                $td("#down_volta").css("display","block");
                $td("#down_click_volta").css("display","none");
                if(counter_volta > 1){
                  $td("#up_volta").css("visibility","visible");
                  }
                  if(counter_volta == 1){
                    $td("#down_volta").css("visibility","hidden");
                  }
              });
      }
      document.getElementById("select_cidades_origem").onchange(document.getElementById('select_cidades_origem'));   
      //document.getElementById("select_cidades_destino").onchange(document.getElementById('select_cidades_destino'));   
      document.getElementById("select_cidades_destino").selectedIndex = counter_volta - 1;
      document.getElementById("preco").innerHTML = document.getElementById("select_cidades_destino").value + "<span style='font-size:40px'>*</span>";
      document.getElementById("price_cidades").value = document.getElementById("select_cidades_destino").value;
    });
   
   
   // CLICK UP ORIGEM
   $td("#up").click(function(){  
    $td("#up").css("display","none");
    $td("#up_click").css("display","block");
    if(parseInt($td("#rollover").css("top")) <= -116){
      $td("#up").css("display","block");
      $td("#up_click").css("display","none");
      return false;
    }
    
    document.getElementById("select_cidades_origem").selectedIndex = counter;    
    counter++;
    
    $td("#reference").text(counter + " de 3");
    x-= 58;
    $td("#rollover").animate({ 
      top : x + 'px'
    },800,function(){
            $td("#up").css("display","block");
            $td("#up_click").css("display","none");
            if(counter > 1){
              $td("#down").css("visibility","visible");
            }
            if(counter == 3){
              $td("#up").css("visibility","hidden");
            }
			fixGodDammitPngsForIE6();
          });
   document.getElementById("select_cidades_origem").onchange(document.getElementById('select_cidades_origem'));
   document.getElementById("select_cidades_destino").onchange(document.getElementById('select_cidades_destino'));
   //$td("#up_volta").click();
   //$td("#down_volta").click();
    rollToFirst();
  });
  
  // CLICK DOWN ORIGEM
  $td("#down").click(function(){
  fixGodDammitPngsForIE6();
  if(counter == 1){
      return false;
    }
   for(var i=0;i<document.getElementById('rollover').getElementsByTagName('DIV').length;i++){
      if(i+1 == counter){
        for(var y=0;y<array_reference.length;y++){
          if(array_reference[y].getAttribute('parent') == document.getElementById('rollover').getElementsByTagName('DIV')[i].id){
             $td('#rollover_volta').empty();
              try{
                $td("#preco").css("display","none");
                $td("#preco").text(array_reference[y+1].childNodes[0].getAttribute('price'));
                $td("#preco").fadeIn("fast");
              }catch(e){}
            document.getElementById('rollover_volta').appendChild(array_reference[y-1]);
          }
        }
       }
   }
    $td("#down").css("display","none");
    $td("#down_click").css("display","block");
    if(parseInt($td("#rollover").css("top")) >= 0){
      $td("#down").css("display","block");
      $td("#down_click").css("display","none");
      return false;
    }
    counter--;
    document.getElementById("select_cidades_origem").selectedIndex = counter - 1;    
    $td("#reference").text(counter + " de 3");
    x+= 58;
    $td("#rollover").animate({ 
      top : x + 'px'
    }, 800,function(){
            $td("#down").css("display","block");
            $td("#down_click").css("display","none");
            if(counter  > 1){
              $td("#up").css("visibility","visible");
            }
            if(counter == 1){
              $td("#down").css("visibility","hidden");
            }
          });
   document.getElementById("select_cidades_origem").onchange(document.getElementById('select_cidades_origem'));   
   //$td("#up_volta").click();
   //$td("#down_volta").click();
   rollToFirst();
  });
  //document.getElementById('li_origem').appendChild(document.getElementById('InCdOrigem'));
  //document.getElementById('li_destino').appendChild(document.getElementById('InCdDestino'));
  //document.getElementById('li_data').appendChild(document.getElementById('dtViagem'));
  //document.getElementById('box_busca_cidade').appendChild(document.getElementById('div_busca'));
  fixGodDammitPngsForIE6();
})

// FUNÇÕES
function populateRollover(objJson){ 
  for(var i=0;i<objJson.length;i++){
    var a = document.createElement("A");
    //a.setAttribute("class","tooltip");
    //a.setAttribute("className","tooltip");
    //a.setAttribute("title","Clique para ver esta rota");
    a.onmouseover = function(){
      Tip('Clique para ver esta rota.',FONTCOLOR,'#000000', FONTSIZE,'12px', TITLEFONTFACE, 'Arial',BGCOLOR, '#FFFFCC',BORDERCOLOR,'#000000')
    }
    a.onmouseout = function(){
      UnTip();
    }
    var div= document.createElement("DIV");
    div.id = objJson[i].class_name + i;
    div.setAttribute("class",objJson[i].class_name);
    div.setAttribute("className",objJson[i].class_name);
    a.appendChild(div);
    document.getElementById('rollover').appendChild(a);
	$td(div).ifixpng();
    
    //Options SELECT ORIGEM
    var option = document.createElement('OPTION');
    option.value = objJson[i].class_name;
    option.setAttribute("code",objJson[i].code);
    option.setAttribute("name",objJson[i].name);
    option.innerHTML = objJson[i].name;
    document.getElementById('select_cidades_origem').appendChild(option);
    
    //Div com as cidades que devem ser exibidas
    var div_temp = document.createElement("DIV");
    div_temp.setAttribute("parent",objJson[i].class_name);
      try{
          for(var j=0;j<objJson[i].references[j].length;j++){
            var a = document.createElement("A");
            //a.setAttribute("class","tooltip");
            //a.setAttribute("className","tooltip");
            //a.setAttribute("title","Clique para ver esta rota");
            //a.setAttribute("price",objJson[i].references[j].split(',')[2]);
             a.onmouseover = function(){
                Tip('Clique para ver esta rota.',FONTCOLOR,'#000000', FONTSIZE,'12px', TITLEFONTFACE, 'Arial',BGCOLOR, '#FFFFCC',BORDERCOLOR,'#000000')
              }
              a.onmouseout = function(){
                UnTip();
              }
            var div= document.createElement("DIV");
            div.id = objJson[i].references[j].split(",")[1] + j;
            div.setAttribute("class",objJson[i].references[j].split(",")[1]);
            div.setAttribute("className",objJson[i].references[j].split(",")[1]);
            a.appendChild(div);
            div_temp.appendChild(a);
			//$td(div).ifixpng();
            //document.getElementById('testa').appendChild(div_temp);
            array_reference[i] = div_temp;
            
              //Options SELECT DESTINO
              var option = document.createElement('OPTION');
              option.value = objJson[i].references[j].split(",")[2];
              option.setAttribute("reference",objJson[i].class_name);   
              option.setAttribute("code",objJson[i].references[j].split(",")[0]); 
              option.setAttribute("name",objJson[i].references[j].split(",")[3]);              
              option.innerHTML = objJson[i].references[j].split(",")[1];
              array_options.push(option);
            }
        }catch(e){}
  }
 //Todas as cidades
  document.getElementById("select_cidades_destino").appendChild(array_options[0]);
  document.getElementById("select_cidades_destino").appendChild(array_options[1]);
  document.getElementById("select_cidades_destino").appendChild(array_options[2]);
  document.getElementById("select_cidades_destino").appendChild(array_options[3]);
  document.getElementById("select_cidades_destino").appendChild(array_options[4]);
  
  $td("#price_cidades").attr("value",$td("#select_cidades_destino").attr("value"));
  $td("#preco").css("display","none")
  //$td("#preco").text($td("#price_cidades").attr("value"));
  document.getElementById("preco").innerHTML = document.getElementById("price_cidades").value + "<span style='font-size:40px'>*</span>";
  
  $td("#preco").fadeIn("fast");
}

function loadJSONCidades(){
   var JSON_response = $td.ajax({
    url: "../conteudo/load_cidades.asp",
    async: false
   }).responseText;
   var objJson = json_parse(JSON_response);
  populateRollover(objJson);
}

function getCidadesDestino(obj){
  //alert(document.getElementById('select_cidades_destino').options[0].text); 
  $td("#select_cidades_destino").empty();
  for(var i=0;i<array_options.length;i++){
    if(array_options[i].getAttribute("reference") == obj.value){
      document.getElementById("select_cidades_destino").appendChild(array_options[i]);
      $td("#price_cidades").attr("value",$td("#select_cidades_destino").attr("value"));
      $td("#preco").css("display","none");
      $td("#preco").text($td("#select_cidades_destino").attr("value"));
      $td("#preco").fadeIn("fast");
    }
  }
  
  for(var y=0;y<array_reference.length;y++){
    if(obj.value == array_reference[y].getAttribute('parent')){
      $td('#rollover_volta').empty();
       try{
        $td("#preco").css("display","none");
        $td("#preco").text(array_reference[y].childNodes[0].getAttribute('price'));
        $td("#preco").fadeIn("fast");
        }catch(e){}
        try{
          document.getElementById('rollover_volta').appendChild(array_reference[y]);		  
        }catch(e){}
    }
  }
  //document.getElementById('select_cidades_destino').selectedIndex = 0;
}

function setPrice(obj){
  document.getElementById('price_cidades').value = obj.value;
  $td("#preco").css("display","none");
  $td("#preco").text(obj.value);
  $td("#preco").fadeIn("fast");
}

//Efeito Piscar
var doBlink = function(obj,start,finish){
  jQuery(obj).fadeOut(200).fadeIn(200);
  if(start!=finish){    
    start=start+1; 
    doBlink(obj,start,finish); 
  }
  document.getElementById("txData").focus();
} 

function setOrigemDestino(){
	document.getElementById('cidade_origem2').style.backgroundImage = '';
	document.getElementById('cidade_destino2').style.backgroundImage = '';
	obj = document.getElementById('select_cidades_origem');
	objD = document.getElementById('select_cidades_destino');
  for(var i=0;i<obj.length;i++){
    if(obj.options[i].selected){
       document.getElementById('cidade_origem2').value = obj.options[i].getAttribute('name');
       GLOBAL_id_cidade_origem = obj.options[i].getAttribute('code');
    }
  }
  
  for(var i=0;i<objD.length;i++){
    if(objD.options[i].selected){
       document.getElementById('cidade_destino2').value = objD.options[i].getAttribute('name');
       GLOBAL_id_cidade = objD.options[i].getAttribute('code');
    }
  }
  //document.getElementById('txData').style.border = "solid 2px #FF9933";
  //doBlink("#txData",1,3);
  consultaViagemEspecial();
}

function rollToFirst(){
  GLOBAL_ANIMATE_FIRST = true;
  while(counter_volta !=1){
    $td("#down_volta").click();
  }
  $td("#up_volta").css("visibility","visible");
}

function fixGodDammitPngsForIE6(){
	
	if($td.browser.msie){
		divs = document.getElementsByTagName("DIV");
		for(var i=0;i<divs.length;i++){
			$td(divs[i]).ifixpng();
		}
	}
}
