﻿// Douro Azul - Javascript functions
function MoreCruises(ParagraphToShow, ParagraphToHide) {
  document.getElementById('Paragraph' + ParagraphToHide).style.display = "none";
  document.getElementById('Paragraph' + ParagraphToShow).style.display = "block";
}

function OverMainMenu(TheMenuItem) {
 document.getElementById(TheMenuItem).style.backgroundColor = "#000000";
}

function OutMainMenu(TheMenuItem) {
 document.getElementById(TheMenuItem).style.backgroundColor = "#6699CC";
}



// Ler parâmetros URL
function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if(results==null)
    return "";
  else
    return results[1];
}



// Adicionar aos favoritos
function CreateBookmarkLink() {
  title = document.title + " - Douro Azul";
  url = window.location;

  if (window.sidebar) { // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  }
  else if( window.external ) { // IE Favorite
   window.external.AddFavorite(url, title);
  }
  else if(window.opera && window.print) { // Opera Hotlist
    alert("Press CTRL + D, to add this page to your bookmarks!");
  }
}



/* PopUp Functions */
var originalDocumentWidth;

function showPopup(CruiseID,TheLanguage) {
  
  // Get source HTML structure
  var el = document.getElementById('popupMask');
  var elContent = document.getElementById('popupContent');
  
  // Store input dimensions
  originalDocumentWidth = document.documentElement.scrollWidth;
  
  // Set dimensions
  setPopupDimensions();
  
  // Set visibility
  el.style.display = '';
  elContent.style.display = '';
  
  // Detect resizes
  window.onresize = setPopupDimensions;
  window.onscroll = setPopupDimensions;

  document.getElementById('ReservasDouroAzul').src = "https://reservas.douroazul.pt/reservation1.aspx?Lang=lang" + TheLanguage + "&CruiseID=" + CruiseID;
}

function setPopupDimensions() {

  // Get source HTML structure
  var el = document.getElementById('popupMask');
  var elContent = document.getElementById('popupContent');
  
  // Get scroll position
  var scrollY = document.documentElement.scrollTop;
  var scrollX = document.documentElement.scrollLeft;
  
  // Get document dimensions
  var documentHeight = document.body.clientHeight > document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight;
  var documentWidth = originalDocumentWidth < document.documentElement.scrollWidth ? originalDocumentWidth : document.documentElement.scrollWidth;
  
  // Get browser dimensions
  if(typeof(window.innerWidth) == 'number') {
    //Non-IE
    myInnerWidth = window.innerWidth;
    myInnerHeight = window.innerHeight;
  }
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myInnerWidth = document.documentElement.clientWidth;
    myInnerHeight = document.documentElement.clientHeight;
  }
  else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    myInnerWidth = document.body.clientWidth;
    myInnerHeight = document.body.clientHeight;
  }
  
  if(documentWidth < myInnerWidth)
    documentWidth = myInnerWidth;
  
  // Set dimensions of background mask
  el.style.width = document.body.clientWidth + 'px';  //myInnerWidth + 'px';
  el.style.height = document.body.clientHeight + 'px';  //myInnerHeight + 'px';
  el.style.top = 0;
  el.style.left = 0;
  
  // Set dimensions of content
  height = myInnerHeight - 30;
  width = myInnerWidth - 30;
  elContent.style.top =  '10px';   //(scrollY + myInnerHeight / 2 - height / 2 - 20 / 2) + 'px';
  elContent.style.left =  (myInnerWidth / 2 - 710 / 2) + 'px';   //(scrollX + myInnerWidth / 2 - width / 2) + 'px';
}

function cancelPopup() {
  // Hide view
  document.getElementById('popupMask').style.display = 'none';
  document.getElementById('popupContent').style.display = 'none';
  
  // Don't handle resizes
  window.onresize = null;
  window.onscroll = null;
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name) {
    return $.getUrlVars()[name];
  }
});

$(document).ready(function () {

  var ano            = $.getUrlVar('ano'),
      id             = $.getUrlVar('ID'),
      years          = new Array(),
      dates          = new Array(),
      inArray        = false,
      yearSelect     = $('#investors-year'), //1
      dataSelect     = $('#investors-data'), //0
      documentSelect = $('#investors-document');   
    
  $('.investors-table td:nth-child(1)').each(function () {
    for(i=0;i<years.length;i++) {
      if(years[i] == $(this).html()) inArray = true;
    }
    if(!inArray) years.push($(this).html());
    inArray = false;
  });

  $('.investors-table td:nth-child(2)').each(function () {
    for(i=0;i<dates.length;i++) {
      if(dates[i] == $(this).html()) inArray = true;
    }
    if(!inArray) dates.push($(this).html());
    inArray = false;
  });

  thisYear = new Date().getFullYear();

  for(i=0;i<=years.length-1;i++) {
    selected = (ano == years[i]) ? ' selected="selected"' : '';
    yearSelect.append('<option value="'+years[i]+'"'+selected+'>'+years[i]+'</option>');
  }
  /*
  $('.investors-table-search span').click(function() {
    window.location = 'http://douroazul.com/Default.aspx?ID='+id+'&eComSearch=1&ano=' + $('.investors-table-search select:eq(1) option:selected').val();
  });
  */
  for(i=0;i<=dates.length-1;i++) {
     selected = (ano == dates[i]) ? ' selected="selected"' : '';
     dataSelect.append('<option value="'+dates[i]+'"'+selected+'>'+dates[i]+'</option>');
  }

  $('.investors-table-search table td select').click(function () {
  $('.investors-table-search table td select').not($(this)).each(function () {
      $(this).find('option:eq(0)').attr('selected','selected');
    });
  });

  $('.investors-table-search span').click(function() {

    $('.investors-table tr').css('display','table-row');
    if(dataSelect[0] && dataSelect.find('option:selected').val() != '') {
      //dates
      $('.investors-table td:nth-child(2)').each(function () {
        if(dataSelect.find('option:selected').val() != $(this).html()) {
         $(this).parent().css('display','none');
        }
      });
    } else if(yearSelect[0] && yearSelect.find('option:selected').val() != '') {
      //years
      $('.investors-table td:nth-child(1)').each(function () {
        if(yearSelect.find('option:selected').val() != $(this).html()) $(this).parent().css('display','none');
      });
    } else if(documentSelect[0] && documentSelect.find('option:selected').val() != '') {
      $('.investors-table td:nth-child(4)').each(function () {
        if($(this).find('a').attr('href').indexOf(documentSelect.find('option:selected').val()) == -1) $(this).parent().css('display','none');
      });    
    } else {    
      $('.investors-table tr').each(function() {
        $(this).css('display','table-row');
      });
    }
    
    //$('.investors-table-search form')
    
    /*
      date = $('.investors-table-search select:eq(0) option:selected').val().split('/');
      window.location = 'http://douroazul.com/Default.aspx?ID='+id+'&eComSearch=1&dia='+date[0]+'&mes='+date[1]+'&ano='+date[2];
    */
  });
  
      //ai...
    //documentSelect
    
    //console.log($('.investors-table td:nth-child(4)'));
    var optionList = '';
    $('.investors-table td:nth-child(4) a').each(function () {
      var ext    = new String($(this).attr('href')),
          length = ext.length;
      ext = ext.charAt(length-3) + ext.charAt(length-2) + ext.charAt(length-1);      
      optionList += '<option value="'+ext+'">'+ext+'</option>';
    });
    
    documentSelect.append(optionList);
  
});

