﻿function $(e)
{
	if (typeof e == 'string')
		return document.getElementById(e);
	return e;
}

function $select(e)
{
	if (typeof e == 'string')
		e = $(e);
	try
	{
		e.focus();
		e.select();
	}
	catch (ex) { }
}

function $attach(el, evname, func)
{
	el = $(el);
	if (el.attachEvent)
		el.attachEvent('on' + evname, func);
	else if (el.addEventListener)
		el.addEventListener(evname, func, true);
	else
		el['on' + evname] = func;
}

/**
 * Runs the callback.
 * @param data Array of data in following JSON format: [{name:value},{name2:value2},...]
 */
function $callback(oncallback,context,data,url)
{	
	var x = null;
	var M = [
		'Msxml2.XMLHTTP.5.0',
		'Msxml2.XMLHTTP.4.0',
		'Msxml2.XMLHTTP.3.0',
		'Msxml2.XMLHTTP',
		'Microsoft.XMLHTTP'
	];
	
	for (var i = 0; i < M.length; i++) {
		try { x = new ActiveXObject(M[i]); }
		catch (e) { x = null; }
		if (x != null) break;
	}

	if (!x && typeof XMLHttpRequest != 'undefined') x = new XMLHttpRequest();
	if (!x) return false;
	
	var _escape = function(s)
	{
		if (typeof(encodeURIComponent) == 'function')
			return encodeURIComponent(s);
		else
			return escape(s);
	};
	
	if (data == null) data = '';	
	var s = '';
	if (typeof(data) == 'string') s += data;
	else if (typeof(data) == 'object')
	{
		for(var item in data)
		{
			var value = data[item];
			if(typeof(value) != 'object')
			{
				if(s!='')s+='&';
				s += item+"="+_escape(value);
			}
			else
			{
				for(var prop in value)
				{
					var prop_value = value[prop];
					if(s!='')s+='&';
					s += prop+"="+_escape(prop_value);
				}
			}
		}
	}
	data = s;

	x.onreadystatechange = function()
	{
		if (x.readyState != 4)
			return;
		var txt = x.responseText.replace(/^\s+|\s+$/g, '');
		x.onreadystatechange = function() { };
		oncallback(txt, context);
		try { delete x; }
		catch (e) { }
	};
	
	if (url==null) url = ''+location;
	   
	x.open('POST', url.replace(/&amp;/g, '&'), true);
	x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	x.send(data);	
	
	return true;
}

/* SLIDESHOW */
function BcSlideshow(id, img, timeout, w, h)
{	
	this.id = BcSlideshow.Instances.length;
	BcSlideshow.Instances[this.id] = this;
	this.images = [];
	this.index = 0;
	this.container = document.getElementById(id);
	for(var i=0; i<img.length; i++)
	{
		this.images[i] = new Image();
		this.images[i].src = img[i];
	}
	
	this.start(timeout);	
}

BcSlideshow.Instances = [];

BcSlideshow.prototype.start = function(timeout)
{
	if (timeout == null) timeout = 7234;
	else if (timeout < 2500) timeout = 2500;
	this.timeout = timeout;
	setTimeout('BcSlideshow.Instances['+this.id+'].next();', timeout);
}

BcSlideshow.prototype.next = function()
{
	this.index++;
	if (this.index >= this.images.length) this.index = 0;
	this.container.style.background = 'url('+this.images[this.index].src+') no-repeat 0px 0px';
	this.runOpacity(this.container.childNodes[0], 100, 0, 1800);
}

BcSlideshow.prototype.swap = function()
{
	this.container.childNodes[0].src = this.images[this.index].src;
	this.container.childNodes[0].width = 890;
	this.container.childNodes[0].height = 225;
	this.container.childNodes[0].style.width = '890px';
	this.container.childNodes[0].style.height = '225px';
	this.setOpacity(100);
	setTimeout('BcSlideshow.Instances['+this.id+'].next();', this.timeout);
}

BcSlideshow.prototype.runOpacity = function(e, so, eo, ms)
{
	var speed = Math.round(ms / 100);
	var timer = 0;

    if(so > eo)
    {
		for(var i = so; i >= eo; i--)
		{
			setTimeout('BcSlideshow.Instances['+this.id+'].setOpacity('+i+');', (timer * speed));
			timer++;
		}
    }
	else if(so < eo)
	{
		for(var i = so; i <= eo; i++)
		{
			setTimeout('BcSlideshow.Instances['+this.id+'].setOpacity('+i+');', (timer * speed));
			timer++;
		}
    }
    
    setTimeout('BcSlideshow.Instances['+this.id+'].swap();', (timer * speed));
}

BcSlideshow.prototype.setOpacity = function(opacity, e)
{
	if (e==null) e = this.container.childNodes[0];
	if (typeof e == 'string') e = document.getElementById(e);
    var style = e.style;
    try
    {
		style.opacity = (opacity / 100);
		style.MozOpacity = (opacity / 100);
		style.KhtmlOpacity = (opacity / 100);
		style.filter = "alpha(opacity=" + opacity + ")";
    }
    catch (ex) { }
}
