// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
    var img = new Image
    img.src = '/images/loading.gif'

    function createSpinner() {
      new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
})

document.getElementByTrueId = function (id) {
	var vResult = this.getElementById (id);
	if (vResult) {
    	if (vResult.id != id) {
			// IE gets elements by name, not ID, FUCK !!!!
 			for (i = 0; i < this.all.length; i++) {
 				if (this.all[i].id == id)
 					return this.all[i];
 			}
 			return null;
    	}
		return vResult;
	}
	return null;
}

document.getElementByName = function (name) {
	for (i = 0; i < this.all.length; i++) {
		if (this.all[i].name == name)
			return this.all[i];
	}
 	return null;
}

function toggle_link(id, text1, text2) {
	link = $(id);
	if (link.innerHTML == text1)
		link.update(text2);
	else
		link.update(text1);
}

function toggle_help(iDisplayValue, iWrapper1ID, iWrapper2ID, iIconID, iIcon1, iIcon2)
{
		var obj1 = document.getElementByTrueId (iWrapper1ID);
		if (!obj1)
			return;
		var obj2 = document.getElementByTrueId (iWrapper2ID);
		if (!obj2)
			return;
		var icon = document.getElementByTrueId (iIconID);

		if (!obj2.style.display || obj2.style.display == 'none') {
			obj1.style.display = 'none';
			obj2.style.display = iDisplayValue;
			if (icon)
				icon.src = iIcon2;
		} else {
			obj1.style.display = iDisplayValue;
			obj2.style.display = 'none';
			if (icon)
				icon.src = iIcon1;
		}
}

function toggle_old(iDisplayValue, iWrapper1ID, iWrapper2ID, iIconID, iIcon1, iIcon2)
{
	var obj1 = document.getElementByTrueId (iWrapper1ID);
	if (!obj1)
		return;
	var obj2 = document.getElementByTrueId (iWrapper2ID);
	if (!obj2)
		return;
	var icon = document.getElementByTrueId (iIconID);
	
	if (!obj2.style.display || obj2.style.display == 'none') {
		obj1.style.display = 'none';
		obj2.style.display = iDisplayValue;
		if (icon)
			icon.src = iIcon2;
	} else {
		obj1.style.display = iDisplayValue;
		obj2.style.display = 'none';
		if (icon)
			icon.src = iIcon1;
	}
}


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function forceNumberFormat(s)
{   
  	if (s==null || s=='') return '0'
 	return s.replace(/ /gi, "").replace(/,/gi, ".")
}

function isArray(obj)
{
  return( !(obj.constructor.toString().indexOf("Array") == -1) );
}

function getExtension(fileName)
{
  var tmp = fileName.split('.');
  return(tmp[tmp.length-1]);
}

if ( !Array.indexOf ) {
  Array.prototype.indexOf = function(obj) {
    for(var i = 0; i < this.length; i++) {
      if ( this[i] == obj ) {
        return(i);
      }
    }
    return(-1);
  }
}

function clearText(field)
{
  if ( field.defaultValue == field.value ) {
    field.style.color = '';
    field.value = '';
  } else if ( field.value == '' ) {
    field.style.color = '#AAAAAA';
    field.value = field.defaultValue;
  }
}

var updateEvery = 59;
var activeRequests = [];

function stopPeriodicalRequest(containerId)
{
  activeRequests[containerId].stop();
}

function periodicalRequest(containerId, url, options)
{
  activeRequests[containerId] = new PeriodicalExecuter(function() {
    if ( $(containerId) ) {
      new Ajax.Request(url, {
        asynchronous:true,
        evalScripts:true,
        onComplete:function() {
          //eval(toEval);
          //Effect.toggle(generalWrapper, 'blind', {duration:0.5, afterFinish:function() { Element.hide("spinner_count"+id); }});
        }
      });
    } else {
      stopPeriodicalRequest(containerId);
    }
  }, updateEvery);
  
  updateEvery += 0.1;
}

function loadAndSetImage(imgObj, options) {
  var jImgObj = jQuery(imgObj);
  var realSrc = imgObj.getAttribute('realSrc');
  var percentOfIncrease = ( options && options.percentOfIncrease != undefined ) ? parseInt(options.percentOfIncrease) : undefined;
  var percentOfDecrease = ( !percentOfIncrease && options && options.percentOfDecrease != undefined ) ? parseInt(options.percentOfDecrease) : undefined;
  var setSize = function(jObj) {
    if ( percentOfIncrease ) {
      var toIncrease = 1 + (percentOfIncrease / 100);
      var newWidth = jObj.width() * toIncrease;
      var newHeight = jObj.height() * toIncrease;
      jObj.width(newWidth).height(newHeight);
    } else if ( percentOfDecrease ) {
      var toDecrease = percentOfDecrease / 100;
      var newWidth = jObj.width() - (jObj.width() * toDecrease);
      var newHeight = jObj.height() - (jObj.height() * toDecrease);
      jObj.width(newWidth).height(newHeight);
    }
  };
  
  imgObj.removeAttribute('onLoad');
  
  if ( realSrc ) {
    imgObj.removeAttribute('realSrc');
    var jNewImgObj = jQuery('<img />');
    jNewImgObj.load(function() {
      setSize(jNewImgObj);
    });
    jImgObj.replaceWith(jNewImgObj);
    jNewImgObj.attr({src:realSrc, border:jImgObj.attr('border'), alt:jImgObj.attr('alt'), title:jImgObj.attr('title'), 'class':jImgObj.attr('class'), style:jImgObj.attr('style')});
  } else {
    setSize(jImgObj);
  }
  
  return(false);
}