/* CONFIGS */
var cookie_exp = new Date();
cookie_exp.setTime(cookie_exp.getTime() + 63072000000);
var spin = '<img src="../images/elements/progress-spinner.gif" width="16" height="16" alt="Loading&#8230;" id="spinner" />';
var popdefs = 'directories=no,resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=yes';

$(document).ready(function(){
  sitetray();
  modal();
  windefs = 'width=' + $(document).width() + ',height=' + $(document).height() + ',' + popdefs;
  makepopups(windefs);
  workedfor();
});

/* SITETRAY */
function sitetray(){
  if (test_cookie()) {
    $('#site-tray > ul').prepend('<li id="text-size">Text Size: <a href="" id="small-text" title="Smallest text size" onclick="textsize(0); return false;">A</a>&nbsp;<a href="" id="medium-text" title="Medium text size" onclick="textsize(1); return false;">A</a>&nbsp;<a href="" id="large-text" title="Largest text size" onclick="textsize(2); return false;">A</a>&nbsp</li>');
    var s = get_cookie('rt_textsize');
    (s == null) ? textsize(1) : textsize(s);
	$('#site-tray').append('<a href="" id="site-tray-controller" title="Show/hide site tray"></a><span class="note">Close this if it bothers you</span>');
    if (get_cookie('rt-sitetray') == '0') {
      $('#site-tray').addClass('hidden').find('#site-tray-controller').text('show');
    } else {
      $('#site-tray').removeClass('hidden').find('#site-tray-controller').text('hide');
    }
    $('#site-tray-controller').click(function() {
      if(get_cookie('rt_sitetray') == '0') {
        $('#site-tray').animate({ width: '100%', minWidth: '980px' }, 1000).removeClass('hidden').find('.note').fadeIn('slow');
        $(this).text('hide');
        set_cookie('rt_sitetray', '1', '', '/', '', '');
      } else {
        $('#site-tray').animate({ width: '30px', minWidth: '30px' }, 1000).addClass('hidden').find('.note').fadeOut();
        $(this).text('show');
        set_cookie('rt_sitetray', '0', '', '/', '', '');
      }
      return false;
    });
  }
}

/* MODAL */
function modal() {
  if (($('a.modal-link').length > 0) && ($('#modal').length < 1)) {
    $('body').append('<div id="modal" class="reveal-modal"><a href="" id="modal-close-box" class="close-reveal-modal" title="Close this Window" accesskey="x">&#215;</a><div id="modal-content"></div></div>');
  }
  $('#modal-content').ajaxStart(function(){
    if($(this).html() == '') {
      $(this).html(spin);
    } else {
      if($('#spinner').length < 1 ) { $(this).prepend(spin); }
    }
  });
  $('a.modal-link').click(function(e) {
    var url = $(this).attr('href');
	var id = $(this).data('id');
	var mc = $('#modal-content');
	$.get(url,function(response, status, xhr) {
      if($(mc).html() == '') {
        $(mc).html(spin);
      } else {
        if($('#spinner').length < 1 ) { $(mc).prepend(spin); }
      }
      var con = $(response).find('#' + id).contents();
      if(con) {
        $(mc).html(con);
        if ($(mc).children('form').length > 0 ) {
          $('form :input:visible:enabled:first').focus();
          $(mc).children('form').unbind('submit').submit(function() {
            var $s = $(this).serialize();
            var $submit = $('button[type="submit"]').attr('name');
            $s += '&' + $submit + '=' + $submit + '&hijax=true';
            $.ajax({
              cache: false,
              data: $s,
              dataType: 'json',
              success: function(data) {
                if(data.flash) {
                  $('#spinner').remove();
                  if(data.flash.status != 'error') { $('#modal-content').html(''); }
                  if($('div.flash').length < 1 ) { $('#modal-content').prepend('<div class="flash"></div>'); }
                  $('div.flash').addClass(data.flash.status).html(data.flash.message);
				  if(data.errors) {
				    $('span.error').remove();
		            $.each(data.errors, function(k,v) {
				      $('#' + k).after('<span class="error">' + v + '</span>');
                    });
                  }
                }
              },
              type: 'POST',
              url: $(this).attr('action')
            });
            return false;
          });
        }
      } else {
        $(mc).html(response);
      }
    });
	$('#modal').reveal();
    return false;
  });
  $('a.close-reveal-modal').click(function(){ $('#modal-content').html(''); return false; });
}

function makepopups(windefs) {
  $('a.popup-link').live('click', function() {
    var u = $(this).attr('href');
    if(u.indexOf('#') > -1 ) {
      u = u.split('#');
      var a = '#' + u[1];
      u = u[0];
    }
    var r = $(this).attr('rel');
    if(!r){
      popup(u,'popup',windefs);
      return false;
    } else {
      r = $(this).attr('rel').split(' ');
      n = r[0];
      f = 'width=' + r[1] + ', height=' + r[2] + ',' + popdefs;
      if(a) { u = u+a; }
      popup(u,n,f);
      return false;
    }
  });
}
function popup(u,n,f) { var newwin = window.open(u,n,f); newwin.focus(); }

function workedfor(){
  $('aside.worked-for').attr({ id : 'worked-for' }).each(function(){
    var c = $(this).find('li').length;
	var h = $(this).find('li').height();
    $(this).find('li:gt(0)').remove();
    var o = h;
    window.setInterval(function(){
      $('aside.worked-for').find('li').animate({opacity: 0}, 1000, function(i) {
	    $(this).css('background-position', '0 -' + o + 'px');
        o = (o + h) % (c * h);
      }).animate({opacity: 1}, 1000)
    }, 3000);
  });
}



function textsize(s) {
  var opts = [['small-text','0.90em'],['medium-text','1.0em'],['large-text','1.15em']];
  $('#main, #contextual').css({ fontSize: opts[s][1] });
  for(i=0;i<3;i++) {
    (s == i) ? $('#' + opts[i][0]).css({ textDecoration: 'none' }) : $('#' + opts[i][0]).css({ textDecoration: 'underline' });
  }
  set_cookie('rt_textsize', s, '', '/', '', '');
}

function test_cookie(){
  set_cookie('rt_test', 'yes', '', '/', '', '');
  if(get_cookie('rt_test')) {
    delete_cookie('rt_test', '/', '');
    return true;
  } else {
    return false;
  }
}
function set_cookie (name, value, expires, path, domain, secure) {
  document.cookie= name + "=" + escape(value) + "; expires=" +
    ((expires) ? expires.toUTCString() : cookie_exp.toUTCString()) +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function get_cookie (name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}

function delete_cookie(name, path, domain) {
  if (! get_cookie(name)) return;
  document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

