// From: http://cass-hacks.com/articles/code/js_url_encode_decode/
// Explanation: http://cass-hacks.com/articles/discussion/js_url_encode_decode/
function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if ((match !== null) && (match.length > 1) && (match[1] !== '')) {
      output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] === ' ') {
        output += '+';
      }else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while (((match = myregexp.exec(output)) !== null)
             && (match.length > 1)
             && (match[1] !== '')) {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

/******************************************************************************/
$.ajaxSetup({
  cache: false
});

var ajax_loading = "<img src='images/loading.gif' alt='loading...' />";

function readyForms() {
  $("#contact_form").ajaxForm({
    target: '#contact_wrapper',
    success: function() { readyForms(); }
  });

  $("#list_form").ajaxForm({
    target: '#list_wrapper',
    success: function() { readyForms(); }
  });

}

$(document).ready(function() {
  $("#contact").click(function(e) {
    list = $("#list");
    if (list.hasClass("selected")) { list.click(); }

    contact = $("#contact");
    if (contact.hasClass("selected")) {
      $("#contact_box").slideToggle('fast', function() { contact.toggleClass("selected box_out"); });
    } else {
      contact.toggleClass("selected box_out");
      $("#contact_box").slideToggle();
    }
    return false;
  });

  $("#list").click(function(e) {
    contact = $("#contact");
    if (contact.hasClass("selected")) { contact.click(); }

    list = $("#list");
    if (list.hasClass("selected")) {
      $("#list_box").slideToggle('fast', function() { list.toggleClass("selected box_out"); });
    } else {
      list.toggleClass("selected box_out");
      $("#list_box").slideToggle();
    }
    return false;
  });

  $("#thumbnail_year_links a").click(function(e) {
    var target = $(e.target);
    $("#thumbnail_year_links a").removeClass('active');
    target.addClass('active');
    var year = target.text();
    // $("#videos").html(ajax_loading).load("includes/videos.php?year=" + year);

    $.get("includes/videos.php?r=1&year=" + year, function (data) {
      $("#videos").html($(innerShiv(data, false)));
    });

    return false;
  });

  $("a.fancybox").fancybox({
          'transitionIn' : 'elastic',
          'transitionOut' : 'elastic',
          'speedIn' :	'fast',
          'speedOut' :	'fast',
          'overlayOpacity' : 0.7
  });

  readyForms();
});

function initThumbnails() {
  $("#thumbnails").scrollable({
    easing: "swing",
    speed: "slow"
  }).navigator({ navi: '#thumbnail_page_links .numbers' });

  $("#thumbnails a").click(function(e) {
    // just swap the page url, since the year and the anchor are ignored anyway
    var link = $(this).closest("a").attr('href').replace('films.php', 'includes/video.php');

    $("#theater").load(link);
    $.scrollTo('#theater', 'slow', { offset: -10 });
    return false;
  });
}


