// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var ProductImage = {
  
  open: function(id, width, height) {
    specs = 'location=no, menubar=no, resizable=no, width=' + (width + 15) + ', height=' + (height + 15);
    window.open('/image/raw/' + id, 'product_image', specs)
  }
  
};

var tickerItems;
var currentItem;

function add_event_related_document(doc_index) {
  var doc_el = $('#docs')[0];
  if (doc_index > -1) {
    var doc_option = doc_el.options[doc_index];
    var doc_id = doc_option.value;
    var doc_name = doc_option.innerHTML;
    var new_doc = "<li id='doc" + doc_id + "'>" + doc_name + "<input id='doc_name" + doc_id + "' type='hidden' value='" + doc_name + "'/><input type='hidden' name='event[doc_ids][]' value='" + doc_id + "'/> (<a href='#' onclick='remove_related_document(" + doc_id + "); return false;'>Remove</a>)</li>";
    $('#related_docs')[0].innerHTML += new_doc;
    new_options = [];
    for (i = 0; i < doc_el.length; i++) {
      if (doc_el.options[i].value != doc_id) {
        new_options.push(doc_el.options[i]);
      }
    }
    doc_el.length = 0;
    if (new_options.length == 0) {
      $('#add_document_button')[0].disabled = true;
      doc_el.innerHTML = "<option value='-1'>No more document</option>";
    } else {
      for (i = 0; i < new_options.length; i++) {
        try {
          doc_el.add(new_options[i], null);
        }
        catch (e) {
          doc_el.add(new_options[i]);
        }
      }
    }
  }
}

function add_course_related_document(doc_index) {
  var doc_el = $('#docs')[0];
  if (doc_index > -1) {
    var doc_option = doc_el.options[doc_index];
    var doc_id = doc_option.value;
    var doc_name = doc_option.innerHTML;
    var new_doc = "<li id='doc" + doc_id + "'>" + doc_name + "<input id='doc_name" + doc_id + "' type='hidden' value='" + doc_name + "'/><input type='hidden' name='course[doc_ids][]' value='" + doc_id + "'/> (<a href='#' onclick='remove_related_document(" + doc_id + "); return false;'>Remove</a>)</li>";
    $('#related_docs')[0].innerHTML += new_doc;
    new_options = [];
    for (i = 0; i < doc_el.length; i++) {
      if (doc_el.options[i].value != doc_id) {
        new_options.push(doc_el.options[i]);
      }
    }
    doc_el.length = 0;
    if (new_options.length == 0) {
      $('#add_document_button')[0].disabled = true;
      doc_el.innerHTML = "<option value='-1'>No more document</option>";
    } else {
      for (i = 0; i < new_options.length; i++) {
        try {
          doc_el.add(new_options[i], null);
        }
        catch (e) {
          doc_el.add(new_options[i]);
        }
      }
    }
  }
}

function add_related_document(doc_index) {
  var doc_el = $('#docs')[0];
  if (doc_index > -1) {
    var doc_option = doc_el.options[doc_index];
    var doc_id = doc_option.value;
    var doc_name = doc_option.innerHTML;
    var new_doc = "<li id='doc" + doc_id + "'>" + doc_name + "<input id='doc_name" + doc_id + "' type='hidden' value='" +doc_name + "'/><input type='hidden' name='page[doc_ids][]' value='" + doc_id + "'/> (<a href='#' onclick='remove_related_document(" + doc_id + "); return false;'>Remove</a>)</li>";
    $('#related_docs')[0].innerHTML += new_doc;
    var new_options = [];
    for (i = 0; i < doc_el.length; i++) {
      if (doc_el.options[i].value != doc_id) {
        new_options.push(doc_el.options[i]);
      }
    }
    doc_el.length = 0;
    if (new_options.length == 0) {
      $('#add_document_button')[0].disabled = true;
      doc_el.innerHTML = "<option value='-1'>No more documents</option>";
    } else {
      for (i = 0; i < new_options.length; i++) {
        try {
          doc_el.add(new_options[i], null);
        }
        catch (e) {
          doc_el.add(new_options[i]);
        }
      }
    }
  }
}

function clearImage() {
  $('#pageimagespan')[0].innerHTML = '';
  $('#page_imagefile')[0].value = '';
  $('#page_imagename')[0].value = '';
  $('#clearBtn')[0].disabled = true;
}

function clearEventImage() {
  $('#eventimagespan')[0].innerHTML = '';
  $('#event_imagefile')[0].value = '';
  $('#event_imagename')[0].value = '';
  $('#clearBtn')[0].disabled = true;
}

function fadeTicker(item) {
  new Effect.Fade(item, {duration: .2, delay: 3});
}

function next_product_thumbnail() {
  var old_visible = $('#product_thumbnail img:visible');
  old_visible.hide();
  var new_visible = old_visible.next();
  new_visible.show();
  if (new_visible.next().is('p')) {
    $('#nextLink').html('Next &gt;')
  }
  $('#previousLink').html("<a href='#' onclick='previous_product_thumbnail();'>&lt; Previous</a>");
}

function openImageManager() {
  height = 600;
  width = 800;
  window.open('/javascripts/imagemanager/popup.htm', '_blank', 'width = ' + width + ', height = ' + height + ', scrollbars=yes');
}

function previous_product_thumbnail() {
  var old_visible = $('#product_thumbnail img:visible');
  old_visible.hide();
  var new_visible = old_visible.prev();
  new_visible.show();
  if (new_visible.prev().length == 0) {
    $('#previousLink').html('&lt; Previous');
  }
  $('#nextLink').html("<a href='#' onclick='next_product_thumbnail();'>Next &gt;</a>");
}

function remove_product_image(id) {
  $('#product_images_thumbnail' + id).remove();
}

function remove_related_document(doc_id) {
  var docs_el = $('#docs')[0];
  doc_name = $('#doc_name' + doc_id)[0].value;
  doc_option = document.createElement('option');
  doc_option.value = doc_id;
  doc_option.text = doc_name
  $('#add_document_button')[0].disabled = false;
  if (docs_el.length == 1 && docs_el.options[0].value == -1) {
    docs_el.remove(0);
  }
  try {
    docs_el.add(doc_option, null);
  }
  catch (e) {
    docs_el.add(doc_option);
  }
  $('#doc' + doc_id).remove();
}

function showTicker(item) {
  new Effect.Appear(item, {duration: .2});
  //new Effect.toggle(item, 'slide');
}

function startTicker() {
  tickerItems = document.getElementsByClassName('newsTickerItem');
  currentItem = 0;
  if (tickerItems.length > 0) {
    showTicker(tickerItems[currentItem]);
    fadeTicker(tickerItems[currentItem]);
    currentItem++;
    new PeriodicalExecuter(function () {
        if (currentItem >= tickerItems.length)
          currentItem = 1;
        showTicker(tickerItems[currentItem]);
        fadeTicker(tickerItems[currentItem]);
        currentItem++;
	
      },
    3.5);
  }
}

function switch_tab(tab_name) {
  $(current_tab + '_tab').className = 'inactive';
  $(current_tab + '_tab_content').className = 'inactive';
  $(tab_name + '_tab').className = 'active';
  $(tab_name + '_tab_content').className = 'active';
  current_tab = tab_name;
}

function toggle_expire_on(val) {
  var expiryField = $('#expiryField');
  if (val) {
    expiryField.show();
  } else {
    expiryField.hide();
  }
}

function updateCommentForm(val) {
  if (val == 'login') {
    $('commentGuestForm').style.display = 'none';
    $('commentLoginForm').style.display = 'block';
  } else if (val == 'guest') {
    $('commentGuestForm').style.display = 'block';
    $('commentLoginForm').style.display = 'none';
  }
}

function updateAll() {
  var category = $F('category');
  window.location = '/calendar/all/' + category;
}

function updateDay() {
  var year = document.getElementById('year');
  var month = $('month');
  var day = $('day');
  var category = $('category');
  window.location = "/calendar/day/" + year.value + '/' + month.value + '/' + day.value + '/' + category.value;
}
function updateMonth() {
  var year = document.getElementById('year');
  var month = document.getElementById('month');
  var year = $('year');
  var category = document.getElementById('category');
  window.location = "/calendar/month/" + year.value + '/' + month.value + '/' + category.value;
}

function updatePrice(el) {
  optvalprice = (Number($('price[' + el.value + ']').value));
  switch (el.type) {
    case 'checkbox':
    case 'radio':
      if (!el.checked) {
        optvalprice = -optvalprice;
      }
    break;
  }
  $('total_price').innerHTML = (Number($('total_price').innerHTML) + optvalprice).toFixed(2);
}

function updateWeek() {
  var year = $('year');
  var week = $('week');
  var category = $('category');
  window.location = "/calendar/week/" + year.value + '/' + week.value + '/' + category.value;
}

function updateYear() {
  var year = $('year');
  var category = $('category');
  window.location = "/calendar/year/" + year.value + '/' + category.value;
}

var current_tab = 'news';

var QuickShop = {

  clearForm: function () {
    $('product_code').value = '';
    $('description_field').innerHTML = '';
    $('quantity').value = '1';
    $('add_to_basket_button').disabled = true;
  }

}

$.fn.replaceWith = function( value ) {
        return this.each( function () {
            var e = $(this);
            var p = e.parent();
            var s = e.prev();
            e.remove();

            if (s[0])
              s.after(value);
            else
              p.prepend(value);
        });

}

function show_temp_form(value, temp_job_types){
	
	if(temp_job_types.indexOf(value) != -1){
		$('#temp_form').show();
	}else{
		$('#temp_form').hide();
	}
}

function send_inbox_message_reply_form(reply_action) {
	$("form#reply_form_tag").attr('action', reply_action);
    $("form#reply_form_tag").submit();
}

function retrieve_and_update_link(from_element, update_element) {
	value = from_element.attr('value');
	img = retrieve_link_image(value);
	img.hide();
	// Add image to update element
	update_element.html(img);
	img.fadeIn();
}

function retrieve_link_image(url) {
	alert("http://images.websnapr.com/?size=T&key=inMZXGz92m9p&url=" + value);
	return $(new Image()).attr('src', ("http://images.websnapr.com/?size=S&key=PX3OK7ERcWNe&url=" + value));
}