// main.js v1 - Fri, June 19 2009
// author: aaron aberg and matt pardee

$(document).ready(function()
{
   $("#btnSignUp").click(function(e){
    if($("#name").val() == '')
    {
      showPopup(200,300,'Error','Please enter a Name.');
      $('#name').focus();
      return;
    }
    
    if($("#email").val() == '')
    {
      showPopup(200,300,'Error','Please enter an Email.');
      $('#email').focus();
      return;
    }

    if(!$("#email").val().match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/))
    {
      showPopup(200,300,'Error','Please enter a valid Email.');
      $('#email').focus();
      return;
    }
    
    subscribeToList($("#name").val(), $("#email").val());
  });
  
  // pop up stuff
  $(document).keypress(function(e){
    if(e.keyCode==27 && popupStatus==1){
      hidePopup();
    }
  });
  $("#popupBackground").click(function(){
    hidePopup();
  });
  $("#popup a").click(function(){
    hidePopup();
  });
  $("#HDLink").click(function(){
	if($("#HDLink")[0].innerHTML == 'Turn HD off')
	{
      $("#video").html(getVideoHtml(false,'P908XsbiBRg', 853, 505));
      $("#HDLink")[0].innerHTML = 'Turn HD on';
	}
	else
	{
      $("#video").html(getVideoHtml(true,'P908XsbiBRg', 853, 505));
      $("#HDLink")[0].innerHTML = 'Turn HD off';
	}
  });
});

/* Ajax calls */
function subscribeToList(name, email)
{
  if(!name) name = '';
  if(!email) email = '';
  
  var error = 'An error occured when trying to submit this form.';
  $.ajax({
    url: 'ajax_subscribe.php',
    type: 'POST',
    data: 'name=' + name + '&email=' + email,
    error: function() {
	  showPopup(200,300,'Error', error);
    },
    success: function(result){
      if(result != '1')
      {
    	showPopup(200,300,'Error', error);
      }
      else
      {
        showPopup(200,300,'Success!','The following Name/Email has been submitted:<br>Name: ' + name + '<br>Email: ' + email);
        $("#name").val('Name');
        $("#email").val('Email');
      }
    }
  });
}
/* End Ajax Calls */

/* Popup methods */
var popupStatus = 0;
function showPopup(height, width, title, htmlMessage)
{
  if(popupStatus == 0)
  {
    $('tr#welcomeVideo').hide();
    centerPopup(height, width);
    $('div#popup div').empty();
    $('div#popup h1').empty();
    $('div#popup div')[0].innerHTML = htmlMessage;
    $('div#popup h1')[0].innerHTML = title;
    $("div#popupBackground").css({"opacity": "0.7"});
    $("div#popupBackground").fadeIn("slow");
    $("div#popup").fadeIn("slow");
    popupStatus = 1;
  }
}

function showBio(id, title)
{
  var innerHTML = $('div#bio_' + id)[0].innerHTML;
  showPopup(500,500,title,innerHTML);
}

function hidePopup()
{
  if(popupStatus == 1)
  {
    $("div#popupBackground").fadeOut("slow");
    $("#popup").fadeOut("slow");
    $('tr#welcomeVideo').show();
    popupStatus = 0;
  }
}

function centerPopup(height, width){
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  
  if(height > 0)
  {
    $("div#popup").height(height);
    var popupHeight = height;
  }
  else
  {
    $("div#popup").height(500);
    var popupHeight = 500;
  }
  
  if(width > 0)
  {
    $("div#popup").width(width);
    var popupWidth = width;
  }
  else
  {
    $("div#popup").width(600);
    var popupWidth = 600;
  }

  $("div#popup").css({
    "position": "absolute",
    "top": windowHeight/2-popupHeight/2,
    "left": windowWidth/2-popupWidth/2
  });

  $("div#popupBackground").css({
    "height": windowHeight
  });
}
/* End Pop up methods*/

/* Start menu methods */
//open menu with given ID
function openMenu(menuID,linkObj,offset)
{
  //if the menu code is ready
  if(um.ready)
  {
    //find co-ordinates of link object
    var coords = {
      'x' : um.getRealPosition(linkObj,'x'),
      'y' : um.getRealPosition(linkObj,'y')
    };

    //increase y-position to place it below the link
    coords.y += (linkObj.offsetHeight + 0);
    coords.x += (offset);

    //activate menu at returned co-ordinates
    um.activateMenu(menuID, coords.x + 'px', coords.y + 'px');
  }
}

//close menu with given ID
function closeMenu(menuID)
{
  //if the menu code is ready
  if(um.ready)
  {
    //deactive menu
    um.deactivateMenu(menuID);
  }
}
/* Stop menu methods */

function browser() {
  var agt=navigator.userAgent.toLowerCase();
  if (agt.indexOf("opera") != -1) return 'Opera';
  if (agt.indexOf("staroffice") != -1) return 'Star Office';
  if (agt.indexOf("webtv") != -1) return 'WebTV';
  if (agt.indexOf("beonex") != -1) return 'Beonex';
  if (agt.indexOf("chimera") != -1) return 'Chimera';
  if (agt.indexOf("netpositive") != -1) return 'NetPositive';
  if (agt.indexOf("phoenix") != -1) return 'Phoenix';
  if (agt.indexOf("firefox") != -1) return 'Firefox';
  if (agt.indexOf("safari") != -1) return 'Safari';
  if (agt.indexOf("skipstone") != -1) return 'SkipStone';
  if (agt.indexOf("msie") != -1) return 'Internet Explorer';
  if (agt.indexOf("netscape") != -1) return 'Netscape';
  if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
  if (agt.indexOf('\/') != -1) {
    if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
      return navigator.userAgent.substr(0,agt.indexOf('\/'));}
    else return 'Netscape';
  }
  else if (agt.indexOf(' ') != -1)
    return navigator.userAgent.substr(0,agt.indexOf(' '));
  else
    return navigator.userAgent;
}

function getVideoHtml(hd, code, width, height)
{
  var html = '<param name="movie" value="http://www.youtube.com/v/' + code + '&hl=en&fs=1';
  html += (hd ? '&hd=1' : '');
  html += '"></param>';
  html += '<param name="allowFullScreen" value="true"></param>';
  html += '<param name="allowscriptaccess" value="always"></param>';
  html += '<embed src="http://www.youtube.com/v/' + code + '&hl=en&fs=1';
  html += (hd ? '&hd=1' : '');
  html += '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed>';
  return html; 
}