var nav = ['hear', 'learn', 'get'];
var subNav = new Array();
subNav[0] = ['funny-ads', 'videos'];
subNav[1] = ['news', 'genres', 'quiz', 'giving', 'history', 'about'];
subNav[2] = ['gear'];

var autoClear = [];

$(document).ready(function() {
  // figure out which page is selected
  var parts = window.location.href.split('/');
  var page = parts[parts.length-1].split('.')[0];
  
  // auto clear fields with default text
  $('input.autoclear').each(function() {
    autoClear[this.name] = this.value;
  }).click(function() {
    if(this.value == autoClear[this.name]) this.value = '';
  }).blur(function() {
    if(this.value == '') this.value = autoClear[this.name];
  });
  
  // go through 
  for(var i=0; i<nav.length; i++) {
    if(in_array(page, subNav[i])) {
      $('#'+nav[i]+' img').attr('src', 'images/nav-'+nav[i]+'-over.png');
    }
  }
  if(page != '' && !$('#'+page).hasClass('popup'))
    $('li#'+page+' img').attr('src', 'images/nav-'+page+'-over.png');
    
  if(page == '' || page == 'index')
    $('li#home img').attr('src', 'images/nav-home-over.png');
  
  $('form#sign-up').submit(submitSignUp);
  
  // check quiz
  if($('#quiz-results').length > 0) {
    var correct = 0;
    if(getUrlParam('q1') == 'c') correct++;
    if(getUrlParam('q2') == 'b') correct++;
    if(getUrlParam('q3') == 'b') correct++;
    if(getUrlParam('q4') == 'a') correct++;
    if(getUrlParam('q5') == 'b') correct++;
    $('#quiz-header').attr('src', 'images/header-quiz-results-'+correct+'.jpg');
  }
  
});

function submitQuiz() {
  var fields = $('form input:checked');
  if(fields.length < 5) {
    alert('Please answer all questions.');
    return false;
  }
  var width = 636; var height = 424;
  var left = (window.screen.width - width) / 2;
  var top = (window.screen.height - height) / 2;
  window.open(null, 'results', 'width='+width+',height='+height+',left='+left+',top='+top);
  return true;
}

function submitSignUp() {
  if($('input[name="first_name"]').val() == 'First') {
    alert('Please provide your first name.'); return false;
  } else if($('input[name="last_name"]').val() == 'Last') {
    alert('Please provide your last name.'); return false;
  } else if(!isEmail($('input#email').val())) {
    alert('Please provide a valid email address.'); return false;
  }
  var width = 469; var height = 279;
  var left = (window.screen.width - width) / 2;
  var top = (window.screen.height - height) / 2;
  window.open(null, 'signup', 'width='+width+',height='+height+',left='+left+',top='+top);
  return true;
}

function getUrlParam(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];
}

var mp3s = [];
function embedMp3Player(id, file) {
  document.writeln('<div id="'+id+'">JavaScript must be enabled and Flash installed to listen to the MP3</div>');
  mp3s[mp3s.length] = new SWFObject('js/player.swf','player','395','20','9','#ffffff');
  mp3s[mp3s.length-1].addParam('flashvars','file='+file);
  mp3s[mp3s.length-1].write(id);
}

function embedVideoPlayer(id, file) {
  document.writeln('<div id="'+id+'">JavaScript must be enabled and Flash installed to watch the video.</div>');
  so = new SWFObject('js/player.swf','mpl','395','320','9');
  so.addParam('allowscriptaccess','always');
  so.addParam('allowfullscreen','true');
  so.addParam('flashvars','file='+file);
  so.write(id);
}

function aboutTuner() {
  var width = 469; var height = 324;
  var left = (window.screen.width - width) / 2;
  var top = (window.screen.height - height) / 2;
  window.open('about-tuner.htm', 'abouttuner', 'width='+width+',height='+height+',left='+left+',top='+top);
}

function clearField(el) {
  if(el.value == el.title) {
    el.value = '';
  } else if(el.value == '') {
    el.value = el.title;
  }
}

function in_array(needle, haystack, strict) { 
  var found = false, key, strict = !!strict;

  for (key in haystack) {
    if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
      found = true;
      break;
    }
  }

  return found;
}

function isEmail(v) {
  return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v);
}
