
// Common scripts based on Prototype, written by janlay


var ie = document.all ? true : false;
var $C = function(tagName) {
	return Element.extend(document.createElement(tagName));
};

function changeByItem(key) {
	if(!key || key.blank())
		return;

	var element = Element.extend(event.srcElement);
	var value = $F(element);
	if(value.blank())
		return;

	var hash = $H(location.search.toQueryParams());
	if(value == '0') {
		hash.remove(key);
	} else {
		var pair = key + '=' + value;
		hash = hash.merge(pair.toQueryParams());
	}

	location.href = '?' + hash.toQueryString();
}

function selectAll(obj) {
	var o = $(obj || document.forms[0]);
	if(!o)
		return;

	var value = (event.srcElement.type == 'checkbox' ? event.srcElement.checked : true);
	var elements = $A(o.getElementsByTagName('input')).select(function(element) {
			return element.type == 'checkbox';
		});

	elements.map(Element.extend);
	elements.each(function(element) {
		element.checked = value;
		selectRow(element);
	});
}

function selectRow(o) {
	var element = $(o);
	var parent = element.up('tr');
	
	if(parent) {
		if(element.checked)
			parent.addClassName('sel');
		else
			parent.removeClassName('sel');
	}
	
	return true;
}

// Utility functions
String.prototype.format = function() {
	var s = this;
	var temp = s;
	for (var i = 0; i < arguments.length; i++) {
		temp = s.replace('{' + i + '}', arguments[i]);
		while(temp != s) {
			s = temp;
			temp = s.replace('{' + i + '}', arguments[i]);
		}
	}
	return s;
}

var Request = {
	queryString: function(item) {
		var hash = $H(location.search.toQueryParams());
		var retValue;
		hash.find(function(pair) {
			return pair.key == item
		});
		return retValue;
	}
};

var Cookie = {
	set : function() {
		var name = arguments[0], value = escape(arguments[1]), days = 365, path = "/";
		if(arguments.length > 2) days = arguments[2];
		if(arguments.length > 2) path = arguments[3];
		with(new Date()) {
			setDate(getDate() + days);
			days = toUTCString();
		}
		document.cookie = "{0}={1};expires={2};path={3}".format(name, value, days, path);
	},
	get : function(){
		var returnValue=document.cookie.match(new RegExp("[\b\^;]?" + arguments[0] + "=([^;]*)(?=;|\b|$)","i"));
		return returnValue?unescape(returnValue[1]):returnValue;
	},
	del : function() {
		var name = arguments[0];
		document.cookie = name + "=1 ; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
	}
}

var Flash = Class.create();
Flash.prototype = {
	initialize: function(objectId, src, width, height, bgColor) {
		if(!Flash.prototype.detect()) {
			window.status = 'Flash is requried.';
		}

		this.attributes = {id: objectId, src: src, width: width || 400, height: height || 300};
		this.vars = $H({});

		// scripting is disabled by default for security reason.
		this.params = $H({AllowScriptAccess: 'never'});

		if(bgColor)
			this.params['bgcolor'] = bgColor;
		this.reload = false;
	},
	render: function(containerId) {
		var element = $(containerId);
		if(!element) {
			alert('Container for the Flash not found.');
			return;
		}

		var source = this.attributes.src;
		if(this.reload) {
			var rnd = Math.random();
			source += '?_rnd=' + rnd;
			this.vars['_rnd'] = rnd;
		}

		if(this.vars.size() > 0)
			this.params['flashvars'] = this.vars.toQueryString();

		if(Prototype.Browser.IE) {
			this.params['src'] = source;
			this.params['movie'] = source;
		}
		//alert(this.params.toJSON());

		var arr = [];
		this.params.each(function(pair) {
			arr.push({key: pair.key, value: pair.value});
		});

		var html = '', t;
		if(Prototype.Browser.IE) {
			t = new Template('<object id="#{id}" name="#{id}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" src="#{src}" width="#{width}" height="#{height}">');
			html = t.evaluate(this.attributes);

			t.template = '<param name="#{key}" value="#{value}" />';
			arr.each(function(item) {
				html += t.evaluate(item);
			});

			html += '</object>';			
		} else {
			t = new Template('<embed id="#{id}" name="#{id}" type="application/x-shockwave-flash" src="#{src}" width="#{width}" height="#{height}"');
			html = t.evaluate(this.attributes);

			t.template = ' #{key}="#{value}';
			arr.each(function(item) {
				html += t.evaluate(item);
			});
			html += '/>';
		}

		element.update(html);
		return $(this._id);
	}
};
Flash.prototype.detect = function() {
	try{
		var f = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.9');
		return true;
	}
	catch(e){
		return false;
	}
};

