
function gourl(x)
{
	location.href = x;
}

function preLoadImg() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=preLoadImg.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

document.lastTabIdx = 0;
function swtab(obj)
{
	var i = obj.getAttribute('id').toString().split('_')[1];
	cssReplace('tabtrig_' + document.lastTabIdx, 'btnwii-behavior-hover', '');
	cssReplace('tabcont_' + document.lastTabIdx, 'dis1', 'dis0');
	cssReplace('tabtrig_' + i, '', 'btnwii-behavior-hover');
	cssReplace('tabcont_' + i, 'dis0', 'dis1');
	
	document.lastTabIdx = i;
}

document.lastSubTabIdx = 10;
function swsubtab(obj)
{
	var i = obj.getAttribute('id').toString().split('_')[1];
	cssReplace('subtabtrig_' + document.lastSubTabIdx, 'hovered', '');
	cssReplace('subtabcont_' + document.lastSubTabIdx, 'dis1', 'dis0');
	cssReplace('subtabtrig_' + i, '', 'hovered');
	cssReplace('subtabcont_' + i, 'dis0', 'dis1');
	
	document.lastSubTabIdx = i;
}

////////////////////////////////////////////////////////////////////////////////
//
// prototype
//

var ASCII_BS = String.fromCharCode(8);
var ASCII_HT = String.fromCharCode(9);

if (!String.prototype.splitDate)
{
  String.prototype.splitDate = function()
  {
  	var res = this.toString().match(/(\d+)-(\d+)-(\d+), (\d+):(\d+):(\d+)/i);
  	var arr = Array();
  	for (var i=1; i < res.length; i++) arr[arr.length] = res[i];
  	arr[arr.length] = new Date(arr[0], arr[1]-1, arr[2]).getDay();
	  return arr;
  }
}

if (!Array.prototype.push)
{
  Array.prototype.push = function()
  {
		var startLength = this.length;
		for (var i = 0; i < arguments.length; i++)
      this[startLength + i] = arguments[i];
	  return this.length;
  }
}

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

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

if (!String.prototype.trim)
{
  String.prototype.trim = function()
  {
		var str = this.toString();
		var q = str.search(/[s]*([Ss]*)[s]*/);
		return q == -1 ? str : RegExp.$1;
	}
}

if (!String.prototype.nl2br)
{
  String.prototype.nl2br = function()
  {
		var str = this.toString();
		return str.replace(/\\r\\n|\\r|\\n|\r\n|\r|\n/g, '<br>');
	}
}

if (!String.prototype.parse_nl) {
	String.prototype.parse_nl = function()
	{
  	var str = this.toString();
  	str = str.replace(/\\n|\\r|\\r\\n/gi, '\n');
  	return str;
	}
}

if (!String.prototype.str_repeat)
{
  String.prototype.str_repeat = function(count)
  {
  	var str = this.toString();
  	var x = '';
  	for (var i=0; i < count; i++)
			x += str;
		return x;
	}
}

if (!Function.prototype.apply) {
  // Based on code from http://www.youngpup.net/
  Function.prototype.apply = function(object, parameters)
  {
    var parameterStrings = new Array();
    if (!object)     object = window;
    if (!parameters) parameters = new Array();

    for (var i = 0; i < parameters.length; i++)
      parameterStrings[i] = 'parameters[' + i + ']';

    object.__apply__ = this;
    var result = eval('object.__apply__(' +
      parameterStrings.join(', ') + ')');
    object.__apply__ = null;

    return result;
  }
}

if (!Object.prototype.is_array) {
	Object.prototype.is_array = function()
	{
		return this.constructor.toString().indexOf('Array') != -1;
	}
}

if (!Object.prototype.is_number) {
	Object.prototype.is_number = function()
	{
		return this.constructor.toString().indexOf('Number') != -1;
	}
}

function num2str(num, digit)
{
	var str = num + '';
	return ('0'.str_repeat(digit - str.length) + str);
}


////////////////////////////////////////////////////////////////////////////////
//
// event
//

function addEvent(obj, evType, fn)
{
  if (obj.addEventListener)
  {
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent) {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
}

function getEventObject(e)
{
	if (window.event && window.event.srcElement)
	{
		obj = window.event.srcElement;
		eType = window.event.type;
	} else if (e && e.target) {
		obj = e.target;
		eType = e.type;
	}
	return true;
}

function selectstart()
{
	if (window.event.cancelBubble)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	return false;
}

function el(obj)
{
	if (typeof obj == 'string')
	{
		obj = document.getElementById(obj);
	}
	return (typeof obj == 'object' ? obj : null);
}

function redirect(actionUrl)
{
	newValue = arguments.length > 1 ? arguments[1] : '';
	formTarget = arguments.length > 2 ? arguments[2] : '';
	if (actionUrl == '')
	{
		var dl = document.location;
		var re = new RegExp(dl.hash+'|\\\\'+dl.search, "gi");
		actionUrl = dl.href.replace(re, '');
	}
	if (!(f = document.forms['redirecter']))
	{
		f = document.createElement('form');
		f.setAttribute('name', 'redirecter');
		f.setAttribute('method', 'post');
		f.setAttribute('action', actionUrl);
		f.setAttribute('target', formTarget != '' ? formTarget : '');
		document.body.appendChild(f); // document.appendChild is not allowed in firefox but IE
	} else {
		f.setAttribute('action', actionUrl);
		f.setAttribute('target', formTarget != '' ? formTarget : '');
	}

	if (newValue != '')
	{
		var xx = newValue.split("&");
		for (i=0; i < xx.length; i++)
		{
			x = xx[i].split("=");
			if (x[0].toLowerCase() == 'action')
			{
				alert('Bad parameter name "'+x[0]+'", try another one.');
				return false;
			}
			var _el = document.createElement('input');
			_el.setAttribute('type', 'hidden');
			_el.setAttribute('name', x[0]);
			_el.setAttribute('value', x[1]);
			f.appendChild(_el);
		}
	}

	f.submit();
}

// IE 5 for Windows doesn't understand element.getElementsByTagName('*') to retrieve all descendants of an element in the DOM
function getAllTags()
{
	return element.all ? element.all : element.getElementsByTagName('*');
}

document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = new Array();

	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}

	return elements;
}

// mouse wheel

function getWheel(event)
{
	var delta = 0;
	if (!event) // For IE
		event = window.event;
	if (event.wheelDelta)
	{
		// IE/Opera
		delta = event.wheelDelta/120;
		// In Opera 9, delta differs in sign as compared to IE
		if (window.opera)
			delta = -delta;
	} else if (event.detail) {
		// Mozilla case
		// In Mozilla, sign of delta is different than in IE
		// Also, delta is multiple of 3
		delta = -event.detail/3;
	}
	/* If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta)
		handleWheel(delta);
}

function handleWheel(delta)
{
	if (delta > 0)
	{
	} else {
	}
}

// addEvent(window, 'DOMMouseScroll', getWheel);

////////////////////////////////////////////////////////////////////////////////
//
// image
//

// correctly handle PNG "IMG Element" transparency in Win IE 5.5 or higher.

function correctPNG()
{
	if (document.all)
	{
		for (i=0; i<document.images.length; i++)
		{
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className + "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				var imgStyle = "display:inline-block;" + img.style.cssText;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
					+ " style=\"width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src='" + img.src + "', sizingMethod='image');\"></span>";
				img.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
}

// correctly handle PNG "BACKGROUNDCOLOUR" transparency in Win IE 5.5 or higher.
function alphaBackgrounds()
{
	if (!document.all) return;
	var rslt = navigator.appVersion.match(/MSIE (\d+.\d+)/);
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (i=0; i<document.all.length; i++) {
		var bg = document.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg) {
			if (bg.match(/.png/i) != null) {
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='crop')"; // 'crop' but 'scale' need no width/height attribute 
				document.all[i].style.backgroundImage = "url('images/blank.gif')";
				document.all[i].style.zIndex = '0';
			}
		}
	}
}

// Replace or insert className
// cssReplace(object obj, old_class, new_class [, ...])

function cssReplace()
{
	if (arguments.length % 3 != 0 || arguments.length == 0) return;

	for (var i=0; i < arguments.length / 3; i++)
	{
		if (typeof arguments[i*3] == 'string')
		{
			if (!(obj = el(arguments[i*3]))) continue;
		} else {
			obj = arguments[i*3];
		}

		// check object
		if (typeof obj.className == 'undefined') continue;

		var str_old = arguments[i*3+1].split(' ');
		var str_new = arguments[i*3+2].split(' ');
		c_old = obj.className.split(' ');
		c_new = new Array();

		for (var j=0; j < c_old.length; j++)
		{
			if (str_old.contains(c_old[j]) == -1)
				c_new[c_new.length] = c_old[j];
		}
		for (var k=0; k < str_new.length; k++)
		{
			if (c_new.contains(str_new[k]) == -1)
				c_new[c_new.length] = str_new[k];
		}
		// reconstruct class name
		obj.className = c_new.join(' ');
	}
}

// initiate

addEvent(window, 'load', function(){
	if (document.all)
	{
		correctPNG();
		alphaBackgrounds();
	}
});

