function wait() { document.body.style.cursor = 'wait'; }
function unwait() { document.body.style.cursor = ''; }
function ajax_error() { unwait(); alert('Processing error'); }
Ajax.Responders.register({onCreate : wait, onFailure: ajax_error, onComplete : unwait});

function CountdownClock(days, hours, mins, secs) {
	
	function set_pos(s) {
		s[0].style.backgroundPosition = '0 -' + (s[1]+offset) + 'px';
	}

  function scroll() {
		scroll_info.each(set_pos);
    offset -= 2;
    if (offset < 0) {
      clearInterval(scroll_interval_id);
      scroll_interval_id = null;
		}
  }
 
  function set_scroll(n, val, length, turnover) {
    for (var i=length-1; i>=0; --i) {
      var digit = val % 10;
       scroll_info.push([n.down(i), digit*23]);
      if (digit == 9 || turnover) val = (val/10).floor(); else break;
    }
  }
  
  function tick() {
    if (secs || mins || hours || days) {
      if (scroll_interval_id) { offset = 0; scroll(); }
      scroll_info = [];
      if (!secs) {
        secs = 60;
        if (!mins) {
          mins = 60;
          if (!hours) {
            hours = 24;
            set_scroll(days_n, --days, 3);
          }
          set_scroll(hours_n, --hours, 2, hours == 23);
        }
        set_scroll(mins_n, --mins, 2);
      }
      set_scroll(secs_n, --secs, 2);
			
			if (ie8plus) {
				// Disable digit scrolling in IE8 because it's very slow due do some inefficient re-rendering algorithm.
				scroll_info.each(set_pos);
			} else {
        offset = 22;
        scroll_interval_id = setInterval(scroll, 26);
      }
      
    } else {
      if (tick_interval_id) clearInterval(tick_interval_id);
      new Ajax.Request('/competitions/update_status', {parameters: 'path=' + location.pathname});
    }
  }
  
  var days_n = $('days');
  var hours_n = $('hours');
  var mins_n = $('minutes');
  var secs_n = $('seconds');
  var scroll_info, scroll_interval_id, offset = 0;
  // var ie8plus = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) > 7;
  var ie8plus = false; // IE7 emulation mode being forced in HTML head
  var tick_interval_id;
  if (secs || mins || hours || days)
    tick_interval_id = setInterval(tick, 1000);
  else
    tick();
}

var project_idx = null;

function set_project_title(project, i) {
  project.down('h3 span').update('Project ' + (i+1));
}

function add_project(){
  var project_list = $('project-list');
  var projects = project_list.childElements();
  projects[0].down('h3').show();
  var new_project = $(projects[0].cloneNode(true));
  
  project_list.appendChild(new_project);
  set_project_title(new_project, projects.length)
  new_project.scrollTo();
  new_project.down('input').focus();
  
  project_idx = project_idx ? project_idx+1 : projects.length;
  new_project.id = 'new-project';
  $$('#new-project input, #new-project textarea').each( function(el) {
      el.value = '';
      el.id = null;
  });
  new_project.id = null;
}

function delete_project(link) {
  var project = link.up('li');
  project.remove();
  var projects = $('project-list').childElements();
  projects.each(set_project_title);
  if (projects.length == 1)  projects[0].down('h3').hide();
}

Event.observe(document, 'lightview:loaded', function() {
  $('content').observe('click', function (event) {
    var element = event.element();
    if (element.hasClassName('show-detail')) {
      event.stop();
      var link = element.tagName == 'IMG' ? element.up() : element;
      Lightview.show({
        href: link.href,
        caption: element.up('.item').down('.title').innerHTML,
        options: { autosize: true, topclose: true }
      });
    } else if (element.up('#pagination') || element.up('#filter-tags')) {
      event.stop();
      var link = element.tagName == 'A' ? element : element.up('a');
      document.location.hash = link.search.substr(1);
      new Ajax.Request('/projects/idea_gallery_page' + link.search);
    }
  });
});

function set_project_submit_visibility() {
  if ($('accept_terms').checked) {
      $('step-2').show();
      $('submit').show();
  } else {
      $('step-2').hide();
      $('submit').hide();
  }
}

Event.observe(window, 'load', function() {
  if ($('gallery') && location.hash) {
    var params = location.search ? location.search + '&' : '?';
    new Ajax.Request('/projects/idea_gallery_page' + params + location.hash.substr(1));
  }
});

function tag_match(link, tag) {
  return link.href.endsWith('=' + tag);
}

function make_tag_active(tag) {
  var tags_list = $('filter-tags').down('ul');
  var active_link = tags_list.select('.active').first();
  if (!tag_match(active_link, tag)) {
    active_link.removeClassName('active');
    var activate_li = tags_list.childElements().find(function(li) { return tag_match(li.down(), tag); });
    if (activate_li) activate_li.down().addClassName('active');
  }
}

function forgot_password(link) {
  var email = $F('email');
  if (email.search(/\S/) == -1)
    alert('Please enter your e-mail.');
  else
    new Ajax.Request('/members/forgot_password', {parameters: 'email=' + email});
}

function formlock() {
  wait();
  $('submit').disabled = true;
}

