/*------------JSON JS---------------- */
Object.extend = function(destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
  return destination;
}
var Hash = function(object) {
  if (object instanceof Hash) this.merge(object);
  else Object.extend(this, object || {});
};

Object.extend(Object,{
  toJSON: function(object) {
    var type = typeof object;
    //alert(type+" => "+object);
    switch(type) {
      case 'undefined':
      case 'function':
      case 'unknown': return;
      case 'boolean': return object.toString();
    }
    if (object === null) return 'null';
    if (object.toJSON) return object.toJSON();
    if (object.ownerDocument === document) return;
    var results = [];
    for (var property in object) {
      var value = Object.toJSON(object[property]);
      if (value !== undefined)
        results.push(property.toJSON() + ':' + value);
    }
    return '{' + results.join(',') + '}';
  }
});
Object.extend(Number.prototype,{
  toJSON: function() {
    return isFinite(this) ? this.toString() : 'null';
  }
});
Object.extend(Date.prototype,{
  toJSON: function() {
    return '"' + this.getFullYear() + '-' +
    (this.getMonth() + 1).toPaddedString(2) + '-' +
    this.getDate().toPaddedString(2) + 'T' +
    this.getHours().toPaddedString(2) + ':' +
    this.getMinutes().toPaddedString(2) + ':' +
    this.getSeconds().toPaddedString(2) + '"';
  }
});
Object.extend(String.prototype,{
  toJSON: function(useDoubleQuotes) {
    var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) {
      var character = String.specialChar[match[0]];
      return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16);
    });
    return '"' + escapedString.replace(/"/g, '\\"') + '"';
  }
});
Object.extend(Array.prototype,{
  each: function(iterator) {
    var index = 0;
    try {
      this._each(function(value) {
        iterator(value, index++);
      });
    } catch (e) {
      if (e != $break) throw e;
    }
    return this;
  },
  _each: function(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
      iterator(this[i]);
  },
  toJSON: function() {
    var results = [];
    this.each(function(object) {
      var value = Object.toJSON(object);
      if (value !== undefined) results.push(value);
    });
    return '[' + results.join(',') + ']';
  }
});
Object.extend(Hash, {
  toJSON: function(object) {
    var results = [];
    this.prototype._each.call(object, function(pair) {
      var value = Object.toJSON(pair.value);
      if (value !== undefined) results.push(pair.key.toJSON() + ':' + value);
    });
    return '{' + results.join(',') + '}';
  }
});
Object.extend(Hash.prototype, {
  _each: function(iterator) {
    for (var key in this) {
      var value = this[key];
      if (value && value == Hash.prototype[key]) continue;

      var pair = [key, value];
      pair.key = key;
      pair.value = value;
      iterator(pair);
    }
  },
  toJSON: function() {
    return Hash.toJSON(this);
  }
});
Object.extend(String.prototype,{
  parseJSON: function(sanitize) {
	if (this.length == 0) return {};
    try {
      if (!sanitize || (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)))
        return eval('(' + this + ')');
    } catch (e) {}
    throw new Exception('Badly formated JSON string: ' + this);
  },
  gsub: function(pattern, replacement) {
    var result = '', source = this, match;
    replacement = arguments.callee.prepareReplacement(replacement);

    while (source.length > 0) {
      if (match = source.match(pattern)) {
        result += source.slice(0, match.index);
        result += String.interpret(replacement(match));
        source  = source.slice(match.index + match[0].length);
      } else {
        result += source, source = '';
      }
    }
    return result;
  }
});
Object.extend(String, {
  interpret: function(value) {
    return value == null ? '' : String(value);
  },
  specialChar: {
    '\b': '\\b',
    '\t': '\\t',
    '\n': '\\n',
    '\f': '\\f',
    '\r': '\\r',
    '\\': '\\\\'
  }
});
Object.extend(Object, {
  keys: function(object) {
    var keys = [];
    for (var property in object)
      keys.push(property);
    return keys;
  },

  values: function(object) {
    var values = [];
    for (var property in object)
      values.push(object[property]);
    return values;
  },

  clone: function(object) {
    return Object.extend({}, object);
  }
});

String.prototype.gsub.prepareReplacement = function(replacement) {
  if (typeof replacement == 'function') return replacement;
  var template = new Template(replacement);
  return function(match) { return template.evaluate(match) };
}
/*------------END JSON JS---------------- */


/** logo link **/
var invalidImage;
var islabelexists;
$('#header div.header_logo').click(function() {
	if (!$(this).attr('x-cms-id')) {
		window.location.href='index.php';
	}
});

/* language selectors */
// hover
$('div.language_selector li').hover(function() {$(this).addClass("over");},function() {if (!$(this).hasClass("x-stay")) $(this).removeClass("over");});
// click
$('div.language_selector li').click(function () { if ($('a',$(this)).attr("href")) { window.location.href = $('a',$(this)).attr("href");}});

/** category menu **/
// hover
$('div#category_navigation li').hover(function() {$(this).addClass("over");},function() {if (!$(this).hasClass("x-stay")) $(this).removeClass("over");});
// click
$('div#category_navigation li').click(function () { if ($('a',$(this)).attr("href")) { window.location.href = $('a',$(this)).attr("href");}});

/** page menu **/
// hover
$('div#page_navigation li').mouseover(function(e) {e.stopPropagation();$(this).addClass("over");});
$('div#page_navigation li').mouseout(function(e) {e.stopPropagation();if (!$(this).hasClass("x-stay")) $(this).removeClass("over");});
// click
$('div#page_navigation li').click(function (e) { e.stopPropagation(); if ($('a',$(this)).attr("href")) { window.location.href = $('a',$(this)).attr("href");}});

/** print icon **/
$('a.printlink').click(function() {
	window.print();
});

/** plugin filter searching - filters ... once more
$('select.x-cms-selfilter , input.x-cms-txtfilter').each(function() {
	$(this).change(function() {
		selectionFilter($(this).attr('name'),$(this).val());
	});
});
$('input.x-cms-txtfilter').keyup(function() {
	selectionFilter($(this).attr('name'),$(this).val());
});**/

function trimString(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

$(document).ready(function() {
	$('form.validatable').each(function() {
    	$(this).submit(function() {
        	var allow = true;
	    	$('.required',$(this)).each(function() {
            	if ($(this).val().length == 0) {
                	allow = false;
                    $(this).addClass("error");
                } else {
                	$(this).removeClass("error");
                }
            });
			return allow;
        });
    });
});
