Ajax.Request.prototype.abort = function() {
	// prevent and state change callbacks from being issued
	this.transport.onreadystatechange = Prototype.emptyFunction;
	// abort the XHR
	this.transport.abort();
	// update the request counter
	Ajax.activeRequestCount--;
};

Object.extend2 = function(destination, source) {
	if (destination === null)
		return source;
	for (var property in source) {
		switch (typeof(destination[property])) {
			case 'object' : {
				destination[property] = (typeof(source[property]) == 'object' ? Object.extend2(destination[property],source[property]) : source[property]);
				break;
			}
			default: {
				destination[property] = source[property];
			}
		}
	}
	return destination;
};

Object.extend(Object, {
	propcnt: function(object) {
	
		var cnt = 0;
		for (var property in source) cnt++;
		return cnt;
	}/*,
	isFHash: function(object) {
		return object instanceof FHash;
	}*/
});

Array.prototype.insert = function (index, value) {

	if (index >= 0) {
		var a = this.slice(), b = a.splice(index);
		a[index] = value;
		return a.concat(b);
        }
};
        

/*
function $FH(object) {
  return new FHash(object);
};

var FHash = Class.create(Enumerable, (function() {

  function toQueryPair(key, value) {
    if (Object.isUndefined(value)) return key;
    return key + '=' + encodeURIComponent(String.interpret(value));
  }

  return {
    initialize: function(object) {
      this._fobject = Object.isFHash(object) ? object.toObject() : Object.clone(object);
    },

    _each: function(iterator) {
      for (var key in this._object) {
        var value = this._object[key], pair = [key, value];
        pair.key = key;
        pair.value = value;
        iterator(pair);
      }
    },

    set: function(key, value) {
      return this._object[key] = value;
    },

    get: function(key) {
      // simulating poorly supported hasOwnProperty
      if (this._object[key] !== Object.prototype[key])
        return this._object[key];
    },

    unset: function(key) {
      var value = this._object[key];
      delete this._object[key];
      return value;
    },

    toObject: function() {
      return Object.clone(this._object);
    },

    keys: function() {
      return this.pluck('key');
    },

    values: function() {
      return this.pluck('value');
    },

    index: function(value) {
      var match = this.detect(function(pair) {
        return pair.value === value;
      });
      return match && match.key;
    },

    merge: function(object) {
      return this.clone().update(object);
    },

    update: function(object) {
      return new Hash(object).inject(this, function(result, pair) {
        result.set(pair.key, pair.value);
        return result;
      });
    },

    toQueryString: function() {
      return this.inject([], function(results, pair) {
        var key = encodeURIComponent(pair.key), values = pair.value;

        if (values && typeof values == 'object') {
          if (Object.isArray(values))
            return results.concat(values.map(toQueryPair.curry(key)));
        } else results.push(toQueryPair(key, values));
        return results;
      }).join('&');
    },

    inspect: function() {
      return '#<Hash:{' + this.map(function(pair) {
        return pair.map(Object.inspect).join(': ');
      }).join(', ') + '}>';
    },

    toJSON: function() {
      return Object.toJSON(this.toObject());
    },

    clone: function() {
      return new FHash(this);
    }
  }
})());

FHash.prototype.toTemplateReplacements = FHash.prototype.toObject;
FHash.from = $FH;

*/












Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
    
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
    
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } else {
    
    }
  },
  update: function(position) {    
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});

Effect.PhaseIn = function(element) {
  element = $(element);
  new Effect.BlindDown(element, arguments[1] || {});
  new Effect.Appear(element, arguments[1] || {});
};

Effect.PhaseOut = function(element) {
  element = $(element);
  new Effect.Fade(element, arguments[1] || {});
  new Effect.BlindUp(element, arguments[1] || {});
};

Effect.Phase = function(element) {
  element = $(element);
  if(element.style.display == 'none') { new Effect.PhaseIn(element, arguments[1] || {}); }
  else { new Effect.PhaseOut(element, arguments[1] || {}); }
};

Effect.BlindOutLeft = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false,
      scaleY: false,
      restoreAfterFinish: true,
      afterFinishInternal: function(effect) {
        effect.element.hide();
        effect.element.undoClipping();
      }
    }, arguments[1] || {})
  );
};

Effect.BlindInLeft = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100,
    Object.extend({ scaleContent: false,
      scaleY: false,
      scaleFrom: 0,
      scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
      restoreAfterFinish: true,
      afterSetup: function(effect) {
        effect.element.makeClipping();
        effect.element.setStyle({width: '0px'});
        effect.element.show();
      },
      afterFinishInternal: function(effect) {
        effect.element.undoClipping();
      }
    }, arguments[1] || {})
  );
};




var MVDElementExts = {
	moveUp: function(element, cnt) {  

		element = $(element);
		if (element === null) return null;
		if (typeof(cnt) == 'undefined') cnt = 1;
		var prev = element;
		for (var i = 0; i < cnt; i++) {
			var prev1 = prev.previous();
			if (prev1 === null) break;
			prev = prev1;
		}
		if (prev == element) return element;
		element.parentNode.removeChild(element);
		prev.insert ({before: element});
		return element;
	},
	moveDown: function(element, cnt) {  

		element = $(element);
		if (element === null) return null;
		if (typeof(cnt) == 'undefined') cnt = 1;
		var next = element;
		for (var i = 0; i < cnt; i++) {
			var next1 = next.next();
			if (next1 === null) break;
			next = next1;
		}
		if (next == element) return element;
		element.parentNode.removeChild(element);
		next.insert ({after: element});
		return element;
	},
	
	getInnerDimensions: function (element) {
		element = $(element);
		if (element === null) return null;
		var el_size = element.getDimensions();
		['border-top-width','border-bottom-width','padding-top','padding-bottom'].each(function(n) {
			var t = parseInt(element.getStyle(n));
			if (!isNaN(t)) el_size.height -= t;
		});
		['border-left-width','border-right-width','padding-left','padding-right'].each(function(n) {
			var t = parseInt(element.getStyle(n));
			if (!isNaN(t)) el_size.width -= t;
		});
		return el_size;
	},
	invoke: function (element, method) {
		var args = $A(arguments).slice(2);
		element[method].apply(element, args);
	}
};
Element.addMethods(MVDElementExts);

var OpenWindow = function(options) {

	var sstyle = (options.scheme && options.scheme.length > 0 ? '_'+options.scheme : '');

	var window_header = new Element('div',{className: 'window_header'+sstyle});
	var window_title = new Element('div',{className: 'window_title'+sstyle});
	if (options.title)
		window_title.update(options.title);
	var window_close = new Element('div',{className: 'window_close'+sstyle});
	var window_contents = new Element('div',{className: 'window_contents'+sstyle});
	if (options.id && options.id.length > 0)
		window_contents.setAttribute('id', options.id);
	if (options.width)
		window_contents.setStyle({width: (options.width-2*6)+'px'});
	if (options.height)
		window_contents.setStyle({height: (options.height-25-2*5)+'px'});
	if (options.content) {
		window_contents.update (options.content);
		delete options.content;
	}
	var window_resize = new Element('div',{className: 'window_resize'+sstyle});

	var opts = {
		className: 'window'+sstyle,
		closeOnClick: window_close,
		overlayOpacity: 0.75,
		fade: false,
		draggable: window_header,
		insertRemoteContentAt: window_contents,
			onSuccess: function (transport) {
				this.updateContent(transport.responseText);
		}
	};
	if (typeof(options.autoDestroy) === 'undefined' || options.autoDestroy === true) {
		opts.afterClose = function() {
			this.destroy();
		};
	}
	if (options.container) {
		var c = $(options.container);
		if (c !== null) {
			c.parentNode.removeChild(c);
			window_contents.insert (c);
		}
	}
	if (options.resizable) {
		opts.resizable = window_resize;
		opts.onResize = function () {
			var w_contents = this.container.select('div.window_contents'+sstyle)[0];
			var wd = this.container.getInnerDimensions();
			if (this.options.width)
				w_contents.setStyle({width: (wd.width-10)+'px'});
			if (this.options.height)
				w_contents.setStyle({height: (wd.height-25-2*5)+'px'});
		};
		delete options.resizable;
	}

	var url;
	url = (options.url ? options.url : false);

	var objBody = document.getElementsByTagName("body").item(0);
	var arrayPageSize = get_page_size();
	var objContent;
	var div1_w = 325;
	objContent = $('am_overlay_content');
	if (objContent === null) {
		objContent = $(document.createElement("div"));
		objContent.setAttribute('id','am_overlay_content');
		objBody.appendChild(objContent);
	}
	objContent.setStyle ({
		position: 'absolute',
		display: 'none',
		left: Math.round((arrayPageSize[0] - div1_w) / 2)+'px',
		top: Math.round(arrayPageSize[1] / 2 - 60)+'px',
		backgroundColor: '#fff',
		padding: '3px',
		zIndex: 10100,
		width: div1_w +'px'
	});
	objContent.innerHTML = 
		"<div style=\"border:1px solid #C0C0C0;padding:3px;\">"+
		"<div style=\"background: url("+img_root+"dialog_bg.jpg) top left repeat-y;padding-left:83px;height:100%;color:#000;\">" +
			"<div style=\"color:#7F7F7F;font-size:14px;font-weight:900;\"><img width=\"30\" height=\"30\" alt=\"\" src=\""+img_root+"load/load.gif\" style=\"margin-right:10px;vertical-align:middle;\"/>"+js_texts['Wait']+"...</div>"+
			"<br />"+
			js_texts['Load data...']+
			"<br />"+
			"<div align=\"right\" class=\"buttonBar\">" +
			"<input type=\"button\" class=\"button\" onclick=\"AbortLastWindow();\" value=\""+js_texts['Cancel']+"\"/>" +
			"</div>" +
		"</div>" +
		"</div>";
	opts.indicator = 'am_overlay_content';

	var w;
	if (options.modal === true)
		w = new Control.Modal(url,Object.extend(opts, options || {}));
		else w = new Control.Window(url,Object.extend(opts, options || {}));
	w.container.insert(window_header);
	window_header.insert(window_title);
	window_header.insert(window_close);
	w.container.insert(window_contents);
	if (opts.resizable)
		w.container.insert(window_resize);

	if (options.open === true)
		w.open();

	return w;  
};  

var OpenModal = function(options) {
	return OpenWindow (Object.extend({
		modal: true
	}, options || {}));
}

var CloseLastWindow = function() {
	if (Object.isArray(Control.Window.windows)) {
		var w = Control.Window.windows.last();
		if (w !== null && typeof(w) != 'undefined') {
			w.close();
		}
	}
	return false;
}

var AbortLastWindow = function() {
	if (Object.isArray(Control.Window.windows)) {
		var w = Control.Window.windows.last();
		if (w !== null && w !== undefined)
			w.abort();
	}
	return false;
}


function OpenColorPicker (field, retFunc) {

	var id= field+'_cp';
	var value = $(field).value;
	var modal = OpenModal ({
		height: 340,
		width: 450,
		id: 'color_container',
		iframe: false,
		afterOpen: function () {
			this.colorPicker = new Refresh.Web.ColorPicker(id, {
				startHex: value,
				startMode: 'h',
				clientFilesPath: img_root+'colorpicker/',
				container: 'color_container'
			});
		},
		beforeClose: function () {
			this.colorPicker.destroy();
			return true;
		},
		title: 'Select color',
		open: true,
		content: '<table style="margin:0 auto;">'+
			'<tr>'+
				'<td valign="middle"><div id="'+id+'_ColorMap" style="width:256px;"></div></td>'+
				'<td valign="middle"><div id="'+id+'_ColorBar"></div></td>'+
				'<td valign="top"><table>'+
					'<tr><td colspan="3"><div id="'+id+'_Preview" style="background-color:#fff;width:60px;height:60px;padding:0;margin:0 auto;border:solid 1px #000;"><br /></div></td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_HueRadio" value="0" /></td><td><label for="'+id+'_HueRadio">H:</label></td><td><input type="text" id="'+id+'_Hue" value="0" class="inputT50" /> &deg;</td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_SaturationRadio" value="1" /></td><td><label for="'+id+'_SaturationRadio">S:</label></td><td><input type="text" id="'+id+'_Saturation" value="100" class="inputT50" /> %</td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_BrightnessRadio" value="2" /></td><td><label for="'+id+'_BrightnessRadio">B:</label></td><td><input type="text" id="'+id+'_Brightness" value="100" class="inputT50" /> %</td></tr>'+
					'<tr><td colspan="3" height="5"> </td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_RedRadio" value="r" /></td><td><label for="'+id+'_RedRadio">R:</label></td><td><input type="text" id="'+id+'_Red" value="255" class="inputT50" /></td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_GreenRadio" value="g" /></td><td><label for="'+id+'_GreenRadio">G:</label></td><td><input type="text" id="'+id+'_Green" value="0" class="inputT50" /></td></tr>'+
					'<tr><td><input type="radio" id="'+id+'_BlueRadio" value="b" /></td><td><label for="'+id+'_BlueRadio">B:</label></td><td><input type="text" id="'+id+'_Blue" value="0" class="inputT50" /></td></tr>'+
					'<tr><td>#:</td><td colspan="2"><input type="text" id="'+id+'_Hex" value="" class="inputT60" /></td></tr>'+
					'<tr><td colspan="3" style="text-align:center;"><input type="button" id="'+id+'_Return" value="Submit" class="inputB60" onclick="ColorPicker_return(\''+field+'\''+(typeof(retFunc) !== 'undefined' ? ', '+retFunc : '')+');return false;" /></td></tr>'+
				'</table></td>'+
			'</tr>'+
		'</table>'
	});
}

function ColorPicker_return (field, retFunc) {
	var value = $(field+'_cp_Hex').value;
	$(field).value = value;
	var preview = $(field+'_preview');
	if (preview !== null)
		preview.setStyle({backgroundColor: (value != '' ? '#'+value : 'transparent')});
	CloseLastWindow();
	if (typeof(retFunc) == 'function')
		retFunc (field, value);
}

MLStrings = Class.create();
Object.extend(MLStrings, {
	strings: {},

	initialize: function() {
		MLStrings.strings = new Hash();
	},
	
	set: function (id, text) {
		var idx = parseInt (id);
		if (idx > 0) {
			MLStrings.strings.set (idx, text);
		}
	},
	
	get: function (id) {
		var idx = parseInt (id);
		if (idx > 0) {
			return MLStrings.strings.get (idx);
		}
		return null;
	},

	unset: function (id) {
		var idx = parseInt (id);
		if (idx > 0) {
			MLStrings.strings.unset(idx);
		}
	},
	
	load: function (ids, ret_callback) {

		var res = new Hash();
		var req = [];
		(Object.isArray(ids) ? ids : [ids]).each(function(id) {
			var idx = parseInt(id);
			if (idx > 0) {
				var cached_trans = MLStrings.strings.get(idx);
				if (typeof(cached_trans) !== 'undefined' && cached_trans !== null) {
					res.set (idx, cached_trans);
				} else req.push (idx);
			}
		});
		if (req.length > 0 && translate_url != '') {
			ac_Send_Req (translate_url, {str_id: req}, function (reply) {
				if (typeof(reply.strs) != 'undefined') {
					$H(reply.strs).each (function (spair) {
						MLStrings.set(spair.key, spair.value);
						res.set(spair.key, spair.value);
					});
				}
				if (typeof(ret_callback) == 'function') {
					if (Object.isArray(ids))
						ret_callback (res);
						else ret_callback(res.get(parseInt(ids)));
				}
			}, null);
		} else {
				if (typeof(ret_callback) == 'function') {
					if (Object.isArray(ids))
						ret_callback (res);
						else ret_callback(res.get(parseInt(ids)));
				}
		}
	}
});

MLStrings.initialize();

