// Define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie7	=	((is_ie) && (userAgent.indexOf('msie 7.')!=-1));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

var UD = 'undefined';

var pointer_cursor = (is_ie ? 'hand' : 'pointer');

/**
* make an array to store cached locations of objects called by fetch_object
*/
var _objects = new Array();

/**
* function to emulate document.getElementById
*
* @param string idname ID name of object
* @param boolean force Force object fetching
*
* @return object
*/
var $ = function(idname, force)
{
	if (force || typeof(_objects[idname]) == "undefined")
	{
		if (document.getElementById)
		{
			_objects[idname] = document.getElementById(idname);
		}
		else if (document.all)
		{
			_objects[idname] = document.all[idname];
		}
		else if (document.layers)
		{
			_objects[idname] = document.layers[idname];
		}
		else
		{
			_objects[idname] = null;
		}
	}
	return _objects[idname];
}

/**
* Function to emulate document.getElementsByTagName
*
* @param	object	Parent tag (eg: document)
* @param	string	Tag type (eg: 'td')
*
* @return	array
*/
function fetch_tags(parentobj, tag)
{
	if (typeof parentobj.getElementsByTagName != 'undefined')
	{
		return parentobj.getElementsByTagName(tag);
	}
	else if (parentobj.all && parentobj.all.tags)
	{
		return parentobj.all.tags(tag);
	}
	else
	{
		return null;
	}
}

/**
* Handles the different event models of different browsers and prevents event bubbling
*
* @param	event	Event object
*
* @return	event
*/
function do_an_e(eventobj)
{
	if (!eventobj || is_ie)
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		eventobj.stopPropagation();
		eventobj.preventDefault();
		return eventobj;
	}
}

function init_cover()
{
	if (!is_ie)
	{
		with (document)
		{
			window.span = null;
			window.span = body.appendChild(createElement("SPAN"));
			window.span.style.display = 'none';
			window.span.innerHTML = '<style type="text/css">.ha{visibility:hidden}</' + 'style>';
		}
	}
	else
	{
		document.styleSheets[0].addRule(".ha", "visibility:hidden");
	}
	
	var img = is_ie ? '/img/cover.png' : '/img/cover2.png';
	//var img = '/img/cover2.png';
	
	window.bgimg = new Image();
	window.bgimg.src = img;
	window.bgimg.onload = function(){stop_cover(false);}
}

function stop_cover(c)
{
	if (!is_ie)
	{
		if (!c)
		{
			with (document)
			{
				var span = createElement("SPAN");
				span = body.replaceChild(span, window.span);
				span.style.display = 'none';
				span.innerHTML = '<style type="text/css">.ha{visibility:visible}</' + 'style>';
			}
		}
	}
	else
	{
		if (c)
		{
			document.styleSheets[0].addRule(".ha", "visibility:visible");
			if (!is_ie7)
			{
				$('cover').style.cursor = 'pointer';
				$('cover').onclick = function()
				{
					$('cover').style.display='none';
					$('cover').style.cursor = 'default';
				}
			}
		}
	}
}

/**
* Switch image
* Function to replace image
*
* @param object imgs Object of images
* @param string tnparent ID of tag containing the tn links
* @param string imgidname ID of img tag where image show
* @param string nxtid ID of next link
* @param string prvid ID of prev link
* @param string navid ID of next prev container
*/
function chimage(imgs, tnparent, imgidname, nxtid, prvid, navid)
{
	this.actclass = 'active';
	this.min_width = 640;
	this.navonimage = (navid && $(navid)) ? $(navid) : false;
	this.tnidpref = 'tn';
	this._imgs = imgs;
	this._tnparent = $(tnparent, true);
	this._imgobj = $(imgidname);
	this._nxt = nxtid != UD ? $(nxtid) : null;
	this._prv = prvid != UD ? $(prvid) : null;
	this._active = -1;
	this._tns = {};
	this.construct();
}

chimage.prototype = {
	construct: function()
	{
		if ((typeof(this._imgs) != 'object' || !this._imgs.length) || !this._tnparent || !this._imgobj) return;
		
		var elms = fetch_tags(this._tnparent, 'a');
		var anyelm = false;
		var th = this;
		var num = 0;
		for (var i = 0; i < elms.length; i++)
		{
			elm = elms[i];
			if (!elm.id || elm.id.indexOf(this.tnidpref) == -1) continue;
			anyelm = true;
			
			if (this._active == -1 && elm.className == this.actclass) this._active = i;
			elm.__id = num;
			elm.onclick = function(e)
			{
				e = typeof(e) == UD ? event : e;
				do_an_e(e);
				th.ch(this.__id);
				this.blur();
			}
			
			this._tns[num] = elm;
			num++;
		}
		
		if (!anyelm && !this._nxt && this._prv) return;
		
		if (this._nxt)
		{
			this._nxt.onclick = function(e)
			{
				e = typeof(e) == UD ? event : e;
				do_an_e(e);
				th.ch('next');
				this.blur();
			}
		}
		
		if (this._prv)
		{
			this._prv.onclick = function(e)
			{
				e = typeof(e) == UD ? event : e;
				do_an_e(e);
				th.ch('prev');
				this.blur();
			}
		}
	},
	
	ch: function(i)
	{
		var last = this._imgs.length - 1;
		switch (i)
		{
			case 'next':
				i = this._active == last  ? 0 : (this._active + 1);
				break;
			case 'prev':
				i = this._active < 1 ? last : this._active - 1;
				break;
		}
		
		if (i < 0 || typeof(this._imgs[i]) == UD) return false;
		if (i === this._active) return false;
		
		if (this._active != -1 && typeof(this._tns[this._active]) != UD)
		{
			this._tns[this._active].className = '';
		}
		this._active = i;
		if (typeof(this._tns[this._active]) != UD)
		{
			this._tns[this._active].className = this.actclass;
		}
		
		// change active image
		this._imgobj.style.visibility = 'hidden';
		
		// only for current project
		this._imgobj.parentNode.style.margin = 0;
		this._imgobj.parentNode.style.width = this.min_width + 'px';
		
		if (this._imgs[i].width < this.min_width)
		{
			this._imgobj.parentNode.style.width = this._imgs[i].width + 'px';
			//this._imgobj.parentNode.style.marginLeft = Math.floor((this.min_width - this._imgs[i].width) / 2) + 'px';
		}

		var img = document.createElement(this._imgobj.tagName);
		if (this._imgobj.className)
		{
			img.className = this._imgobj.className
		}
		img.style.width = this._imgs[i].width + 'px';
		img.style.height = this._imgs[i].height + 'px';
		
		if (img.tagName.toLowerCase() == 'img')
		{
			img.src = this._imgs[i].src;
		}
		else
		{
			img.style.background = '(url:' + this._imgs[i].src + ') no-repeat';
		}
		
		var p = this._imgobj.parentNode;
		
		p.replaceChild(img, this._imgobj);
		p.replaceChild(img, img);
		this._imgobj = img;
		
		if (this._nxt && this.navonimage)
		{
			this.navonimage.style.width = (this._imgs[i].width > this.min_width ? this.min_width : this._imgs[i].width) + 'px';
			this.navonimage.style.height = this._imgs[i].height + 'px';
		}
		return;
	}
};

function print_r(obj, depth)
{
	var res_out = '';
	depth = parseInt(depth);
	if (!depth || depth < 1)
	{
		depth = 1;
	}
	
	if (typeof obj == 'object')
	{
		res_out += "object {\n";
		var counter = 0;
		var obj_len = obj.length;
		for (var i in obj)
		{
			counter++;
			obj_d = null;
			var p_symbol = true;
			if (obj[i] == null)
			{
				obj_d = 'null';
			}
			else if (typeof obj[i] == 'object')
			{
				obj_d = print_r(obj[i], depth + 1);
				p_symbol = false;
			}
			else
			{
				obj_d = obj[i];
			}
			
			var symbol = ",\n";
			if (counter == obj_len)
			{
				symbol = "\n";
			}
			else if(!p_symbol)
			{
				symbol = '';
			}
			
			res_out += str_repeat("\t", depth) + "[" + i + "] => " + obj_d + symbol;
		}
		res_out += str_repeat("\t", depth - 1) + "}" + ((depth > 1) ? ',' : '') + "\n";
	}
	else
	{
		res_out = obj;
	}
	return res_out;
}

// #############################################################################
function str_repeat(str, count)
{
	str = '' + str;
	
	if (!count)
	{
		count = 0;
	}
	
	count = parseInt(count);
	
	if (count <= 0) return '';
	arry = new Array(count + 1);
	
	return arry.join(str);
}