var curVideo = '0YKlyAv3nB8';
var pageHash = '';
var t;

$(document).ready(function(){

    $('#print').html('<a href="/imprimible">Versión imprimible</a>');

    $('#video-list a').click(function(event){
	loadVideo(event.target.id);
	event.preventDefault();
    });

    $('#lightbox a').lightBox({
	overlayBgColor: '#000',
	overlayOpacity: 0.8,
	imageLoading: 'img/loading.gif',
	imageBtnClose: 'img/lightbox-close.gif',
	imageBtnPrev: 'img/lightbox-prev.gif',
	imageBtnNext: 'img/lightbox-next.gif',
	containerResizeSpeed: 350,
	txtImage: 'Imagen',
	txtOf: 'de'
    });

    $('#contact').submit(function() {
	return validateContactForm();
    });

    $("#close").click(function() {
	hideCotizacion();
      });

    $('#solicitar').click(function() {
	processCotizacion();
	hideCotizacion();
    });

    t = setTimeout('loadHashThread()', 500);

});

function loadVideo (v) {
  $('#' + curVideo).removeClass('cur-video');
  $('#' + v).addClass('cur-video');
  document.getElementById('ytplayer').loadVideoById(v);
  curVideo = v;
  window.scrollTo(0, 330);
}

function loadHashThread() {
  var pathname = location.pathname.substring(1);
  var hash = (location.hash.length > 0 ? location.hash.substring(1) : '');
  
  if (pageHash != location.hash) {
    pageHash = location.hash;
    if (location.pathname == '/contacto') {
      if (location.hash == '#cotizacion') {
	showCotizacion();
      } else if (location.hash == '#') {
	hideCotizacion();
      }
    }
  }
  t = setTimeout('loadHashThread()', 500);
}

function hideCotizacion() {
  $('#overlay,#floating-box').fadeOut();
  location.replace('#');
};

function showCotizacion() {
  $('#overlay,#floating-box').show();
}

function processCotizacion() {
  var contact = document.getElementById('contact');
  var cotizacion = document.getElementById('cotizacion');
  
  var message = 'SOLICITUD DE COTIZACIÓN\n';
  message += '\nNOMBRE: ' + cotizacion.name.value;
  message += '\nE-MAIL: ' + cotizacion.mail.value;
  message += '\nTEL.: ' + 
    (cotizacion.phone.value ? cotizacion.phone.value : '---');

  message += '\n\nTIPO DE EVENTO: ' + 
    cotizacion.type[cotizacion.type.selectedIndex].text;
  message += '\nZONA: ' + 
    cotizacion.zone[cotizacion.zone.selectedIndex].text;
  message += '\nFECHA PLANEADA: ' + 
      (cotizacion.date.value ? cotizacion.date.value : '---');
  message += '\nHORARIO: ' +
    cotizacion.time[cotizacion.time.selectedIndex].text;
  message += '\nINVITADOS: ' +
    cotizacion.guests[cotizacion.guests.selectedIndex].text;
  message += '\nNOMBRE DEL SALÓN: ' + 
    (cotizacion.place.value ? cotizacion.place.value : '---');

  message += '\n\nRECOMENDADOS POR: ' + 
    (cotizacion.recommender.value ? cotizacion.recommender.value : '---');
  message += '\nCOMENTARIOS: ' + 
    (cotizacion.comment.value ? cotizacion.comment.value : '---');

  contact.name.value = cotizacion.name.value;
  contact.mail.value = cotizacion.mail.value;
  contact.message.value = message + '\n\n' ;

  if (validateContactForm()) {
    contact.submit();
  }
}

function validateContactForm() {
  var contact = document.getElementById('contact');

  if (contact.name.value.length > 0) {
    $('#client').css('display', 'none');
  } else {
    $('#client').css('display', 'inline');
    return false;
  }

  var email  =
    /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if ($("input[name='mail']").val().match(email)) {
    $('#email').css('display', 'none');
  } else {
    $('#email').css('display', 'inline');
    return false;
  }

  if (contact.message.value.length > 0) {
    $('#message').css('display', 'none');
  } else {
    $('#message').css('display', 'inline');
    return false;
  }

  return true;
}
