var
	template,
	min_height = 740,
	bg_img_src,
	m,
	h2_vars = {},
	h3_vars = {},
	ajax_ready = true,
	ajax_timer = 0,
	timeouts = [],
	intervals = [],
	opacities = [],
	ie_interval = null,
	_sliding = false,
	this_time,
	_scroll_anchor = false;


//Create a boolean variable to check for a valid Internet Explorer instance.
var xmlhttp = false;
//Check if we are using IE.
try
{
	//If the Javascript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
	//If not, then use the older active x object.
	try
	{
		//If we are using Internet Explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (E)
	{
		//Else we must be using a non-IE browser.
		xmlhttp = false;
	}
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
	xmlhttp = new XMLHttpRequest();
}

function make_request(server_page, obj_id, append)
{
	if (!append) append = false;
	if (!ajax_ready)
	{
		if (!ajax_timer) ajax_timer = time();
		else if (time() - ajax_timer > 5000)
		{
			ajax_ready = true;
			ajax_timer = 0;
		}
		setTimeout('make_request("'+server_page+'", "'+obj_id+'", "'+append+'")', 100);
	}
	else
	{
		ajax_ready = false;
		xmlhttp.open("GET", server_page);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
			{
				var content = xmlhttp.responseText;
				if (null != $(obj_id))
				{
					if (append) $(obj_id).innerHTML += content;
					else $(obj_id).innerHTML = content;
				}
				xmlhttp.onreadystatechange = function(){};
				ajax_ready = true;
				ajax_finished(obj_id);
			}
		}
		xmlhttp.send(null);
	}
}

function $(obj_id){ return document.getElementById(obj_id); }

function log()
{
    if(typeof(console) != 'undefined')
    {
        for(i = 0; i < arguments.length; i++)
        {
            console.log(arguments[i]);
        }
    }
}

function init(_template)
{
	template = _template;

	on_resize();

	if($('bg_img')) bg_img_src = $('bg_img').src;

	if(_template == 'mc_gallery' && m)
	{
		m.init();
		_media_gallery_inner = $('media_gallery_inner');
		_media_gallery_inner.style.height = m.get_tallest_column()+'px';
		_media_gallery_inner.style.opacity = 1;
		_media_gallery_inner.style.filter = "alpha(opacity=100)";
		init_mc_gallery_scroll();
		make_request(PATH+'media_pack.php', 'strip_col_b_inner');
	}
	else if (_template == 'mc_documents' || _template == 'mc_videos')
	{
		_media_text_inner = $('media_text_inner');
		_media_text_inner.style.opacity = 1;
		_media_text_inner.style.filter = "alpha(opacity=100)";
		init_mc_documents_scroll();
		make_request(PATH+'media_pack.php', 'strip_col_b_inner');
	}
	else if(_template == 'text')
	{
		init_text_scroll();
	}
}

function ondomload()
{
	position_footer(get_new_dimentions());
	init_flash_text();
	init_flash_nav();
}

function get_viewport_dimensions()
{
	return {
		width: self.innerWidth || (document.body.offsetWidth || document.documentElement.offsetWidth),
		height: self.innerHeight || (document.body.offsetHeight || document.documentElement.offsetHeight)
	};
}

function get_new_dimentions()
{
	var viewport_dimentions = get_viewport_dimensions();
	var _wrap = $('wrap');
	var min_width = _wrap.offsetWidth;
	//get new width and height
	var new_width = viewport_dimentions.width > min_width ? viewport_dimentions.width : min_width;
	var new_height = viewport_dimentions.height > min_height ? viewport_dimentions.height : min_height;
	_wrap.style.height = new_height + 'px';
	return {
		new_width: new_width,
		new_height: new_height
	};
}

function resize_to_screen(obj_id,new_dimentions)
{
	//set some vars
	var obj = $(obj_id);
	//get new width and height
	var new_width = new_dimentions.new_width;
	var new_height = new_dimentions.new_height;
	//resize the obj to fit screen
	obj.style.width = new_width + 'px';
	obj.style.height = new_height + 'px';
}

function scale_to_fit(obj_id)
{
	var obj = $(obj_id);
	var new_width = obj.parentNode.offsetWidth;
	var new_height = obj.parentNode.offsetHeight;
	//get the ratio of the image
	obj.style.width = 'auto';
	obj.style.height = 'auto';
	obj.style.display = 'block';
	var ratio = obj.offsetWidth / obj.offsetHeight;
	obj.style.display = 'none';
	//check resize by width
	var img_height = new_width / ratio;
	/*if(img_height >= new_height)
	{
		obj.style.width = '100%';
	}
	else
	{
		obj.style.height = '100%';
	}*/
	obj.style.display = 'block';
}

function show_overlay(new_dimentions)
{
	var _overlay = $('overlay');
	resize_to_screen('overlay',new_dimentions);
	switch(template)
	{
		case 'login':
		case 'gallery':
		case 'home':
			_overlay.style.height = $('footer').offsetTop + 'px';
			break;
	}
	_overlay.style.display = 'block';
	overlay_showing = true;
}

function position_footer(new_dimentions)
{
	var _footer = $('footer');
	if(_footer) _footer.style.top = new_dimentions.new_height - _footer.offsetHeight + 'px';
}

function on_resize()
{
	var new_dimentions = get_new_dimentions();
	position_footer(new_dimentions);
	resize_to_screen('bg_wrap',new_dimentions);
	resize_to_screen('mesh',new_dimentions);
	scale_to_fit('bg_img');
	show_overlay(new_dimentions);
}

function gallery_enlarge(link)
{
	var vp = get_viewport_dimensions();
	var src = link.firstChild.src.replace('/medium/','/full/');
	src = src.replace('/medium_bw/','/full/');
	src = PATH+'showimage.php?i='+src+'&f=1&w='+vp.width+'&h='+vp.height;
	$('gallery_full_img').src = src;
	$('gallery_full_img').onload = function()
	{
		fade_in('gallery_full');
		show('gallery_nav_close',true);
	}
	/*$('bg_img').src = '';
	$('bg_img').src = src;
	scale_to_fit('bg_img');
	hide('mesh',true);
	hide('overlay',true);
	hide('content',true);
	hide('footer',true);*/

	return false;
}

function gallery_unlarge()
{
	hide('gallery_nav_close',true);
	fade_out('gallery_full');
	/*$('bg_img').src = bg_img_src;
	scale_to_fit('bg_img');
	show('mesh',true);
	show('overlay',true);
	show('content',true);
	show('footer',true);*/
	return false;
}

function hide(obj_id, now)
{
	if(now)
	{
		$(obj_id).style.display = 'none';
		return;
	}
}

function show(obj_id, now)
{
	if(now)
	{
		$(obj_id).style.display = 'block';
		return;
	}
}

function show_media_detail(type, link)
{
	return false;
}

function Matrix(cols, margin_bottom)
{
	var t = this;
	t.ids = [];
	var col_width = 0;
	var m = [];
	t.max_height = 0;

	t.init = function()
	{
		for(var i in t.ids)
		{
			var matrix_item = $(t.ids[i]);

			//set some vars
			if(col_width == 0) col_width = matrix_item.offsetWidth;

			//set position absolute
			matrix_item.className += ' matrix_item';

			var col = get_smallest_column();

			add(matrix_item, col);
		}
	};

	function get_smallest_column()
	{
		var min_h = 10000;
		var min_col = 0;
		for(var x = 0; x < cols; x++)
		{
			var h = 0;
			if(typeof(m[x]) == 'undefined') return x;
			var y_length = m[x].length;
			if(y_length)
			{
				for(var y = 0; y < y_length; y++)
				{
					h = h + m[x][y].offsetHeight + 15;
				}
			}
			if(h < min_h)
			{
				min_h = h;
				min_col = x;
			}
		}
		return min_col;
	}

	t.get_tallest_column = function()
	{
		for(var x = 0; x < cols; x++)
		{
			var h = 0;
			if(typeof(m[x]) == 'undefined') return x;
			var y_length = m[x].length;
			if(y_length)
			{
				for(var y = 0; y < y_length; y++)
				{
					h = h + m[x][y].offsetHeight + 15;
				}
			}
			if (h > t.max_height) t.max_height = h;
		}
		return t.max_height;
	};

	function add(matrix_item, col)
	{
		if(typeof(m[col]) == 'undefined') m[col] = [];

		matrix_item.style.left = col * col_width + 'px';

		var last_item = m[col].length - 1;
		if(last_item > -1)
		{
			var last_matrix_item = m[col][last_item];
			matrix_item.style.top = last_matrix_item.offsetTop + last_matrix_item.offsetHeight + margin_bottom + 'px';
		}
		else matrix_item.style.top = 0;

		m[col].push(matrix_item);
	}
}

function text_item_over(obj)
{
	obj.className = obj.className + ' text_item_over';
}

function text_item_out(obj)
{
	obj.className = obj.className.replace(/text_item_over/,'');
}

function init_flash_text()
{
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) return false;
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "false",
		allowScriptAccess: "always",
		bgcolor: "#FFFFFF",
		wmode: "transparent"
	};

	var h2s = document.getElementsByTagName("H2");
	var h2sLength = h2s.length;

	if(h2sLength)
	{
		var h2_flashvars = {};

		var h2_attributes = {};

		for(var i = 0; i < h2sLength; i++)
		{
			var h2 = h2s[i];
			h2.style.height = h2.offsetHeight + 'px';

			h2_flashvars[i] = {};
			h2_attributes[i] = {};

			h2_flashvars[i]['text'] = format_flash_text(h2, h2_vars);
			h2_flashvars[i]['size'] = h2_vars['size'];
			h2_flashvars[i]['leading'] = h2_vars['leading'];
			h2_flashvars[i]['color'] = h2_vars['color'];
			h2_flashvars[i]['letterSpacing'] = h2_vars['letterSpacing'];

			h2_attributes[i]['id'] = 'flash_text_h2_' + i;

			h2.innerHTML = '<span id="h2_' + i + '"></span>';

			swfobject.embedSWF(
				PATH + "swf/FlashText.swf?" + time(),
				'h2_' + i,
				"100%",
				"100%",
				"9.0.0",
				PATH + "swf/expressInstall.swf",
				h2_flashvars[i],
				params,
				h2_attributes[i]
			);
		}
	}

	var h3s = document.getElementsByTagName("H3");
	var h3sLength = h3s.length;

	if(h3sLength)
	{
		var h3_flashvars = {};

		var h3_attributes = {};

		for(i = 0; i < h3sLength; i++)
		{
			var h3 = h3s[i];
			h3.style.height = h3.offsetHeight + 'px';

			h3_flashvars[i] = {};
			h3_attributes[i] = {};

			h3_flashvars[i]['text'] = format_flash_text(h3, h3_vars);
			h3_flashvars[i]['size'] = h3_vars['size'];
			h3_flashvars[i]['leading'] = h3_vars['leading'];
			h3_flashvars[i]['color'] = h3_vars['color'];
			h3_flashvars[i]['letterSpacing'] = h3_vars['letterSpacing'];

			h3.innerHTML = '<span id="h3_' + i + '"></span>';

			swfobject.embedSWF(
				PATH + "swf/FlashText.swf?" + time(),
				'h3_' + i,
				"100%",
				"100%",
				"9.0.0",
				PATH + "swf/expressInstall.swf",
				h3_flashvars[i],
				params,
				h3_attributes[i]
			);
		}
	}
}

function format_flash_text(obj, vars)
{
	var text = obj.innerHTML.toLowerCase();
	var char_limit = vars['charLimit'];
	if(!char_limit) char_limit = 13;
	text = text.replace(/<br>/gi,'\n');
	var ex_text = text.split(' ');
	var word_count = ex_text.length;
	var word = '';
	for (var i = 0; i < word_count; i++)
	{
		word = ex_text[i];
		if(word.indexOf('/') > -1 && word.indexOf('\n') == -1 && word.length > char_limit)
		{
			ex_text[i] = word.replace(/\//gi,'/\n');
				obj.style.height = obj.offsetHeight + vars['size'] + 'px';
		}
	}
	text = ex_text.join(' ');
	if(vars['maxLength'])
	{
		var lines = text.split('\n');
		var lines_count = lines.length;
		var line = '';
		var over;
		for (var i = 0; i < lines_count; i++)
		{
			line = lines[i];
			if(line.length > vars['maxLength'])
			{
				over = Math.ceil(line.length / vars['maxLength']) - 1;
				obj.style.height = obj.offsetHeight + (vars['size'] * over) + 'px';
			}
		}
	}
	text = text.toUpperCase();
	return text;
}

function init_flash_nav()
{
	var flashvars = {
		jsonURL: PATH + 'json/nav_json.php'
	};
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "false",
		allowScriptAccess: "always",
		bgcolor: "#FFFFFF",
		wmode: "transparent"
	};
	var attributes = {
		id:"Nav"
	};
	swfobject.embedSWF(PATH + "swf/Nav.swf", "nav_inner", "100%", "100%", "9.0.0", PATH + "swf/expressInstall.swf", flashvars, params, attributes);
}

function get_mc_image(name)
{
	make_request(PATH+'get_image.php?i='+name, 'gallery_detail_holder');
	return false;
}

function ajax_finished(obj_id)
{
	if (obj_id == 'gallery_detail_holder')
	{
		var holder = $('gallery_detail_holder');
		holder.style.width = '662px';
		fade_in('gallery_detail_holder');
	}
	else if (obj_id == 'strip_col_b_inner')
	{
		init_mc_basket_scroll();
	}
}

function ie_loop()
{
	var empty = true;
	for (var i in intervals)
	{
		if (intervals[i])
		{
			eval(intervals[i]);
			empty = false;
		}
	}
	if (empty)
	{
		clearInterval(ie_interval);
		ie_interval = null;
	}
}

function opacity_ie(id, opacStart, opacEnd, duration)
{
	var speed = 50;
	var step = (opacEnd - opacStart) / (duration / speed);
    //speed for each frame
    var timer = 0;
	var opac = opacStart;

	opacities[id] = opac;

	$(id).style.display = "";

	do_opacity_ie(id, step);

	opacities[id] = opac;
	intervals[id] = "do_opacity_ie('"+id+"', "+step+");";

	if (!ie_interval) ie_interval = setInterval(ie_loop, speed);
}

function do_opacity_ie(id, step)
{
	var op = opacities[id] + step;

	if (op < 0 || op > 100)
	{
		intervals[id] = null;
		if (op < 0) op = 0;
		else if (op > 100) op = 100;
	}
	$(id).style.filter = "alpha(opacity=" + op + ")";
	if(op == 0 && step < 0) $(id).style.display = "none";
	opacities[id] = op;
}

function opacity(id, opacStart, opacEnd, duration, after_finish)
{
	var speed = 30;
	var step = (opacEnd - opacStart) / (duration / speed);
    //speed for each frame
    var timer = 0;
	var opac = opacStart;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) dir = "down";
	else dir = "up";

	changeOpac(opac, id, dir);

	for(var timer = speed; timer <= duration; timer+=speed)
	{
		opac += step;
		timeouts[timeouts.length] = setTimeout("changeOpac(" + Math.round(opac) + ",'" + id + "','" + dir + "')",timer);
	}

	if (timer != duration)
	{
		timeouts[timeouts.length] = setTimeout("changeOpac(" + opacEnd + ",'" + id + "','" + dir + "')",duration);
	}

	if(after_finish) timeouts[timeouts.length] = setTimeout(after_finish,duration);
}

//change the opacity for different browsers
function changeOpac(opacity, id, dir)
{
	//if (!id || !$(id)) return false;
    var object = $(id).style;

	if(opacity == 0 && dir == "up")
	{
		if($(id).tagName == "TR") object.display = "";
		else object.display = "block";
	}
    object.opacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity == 0 && dir == "down") object.display = "none";
}

function fade_out(obj,after_finish)
{
	if (browser == 'Internet Explorer') opacity_ie(obj, 100, 0, 500);
	else opacity(obj, 100, 0, 500, after_finish);
}

function fade_in(obj)
{
	if (browser == 'Internet Explorer') opacity_ie(obj, 0, 100, 500);
	else opacity(obj, 0, 100, 500);
}


function get_position(e)
{
	var left = 0;
	var top  = 0;

	while (e.offsetParent)
	{
		left += e.offsetLeft - e.offsetParent.scrollLeft;
		top  += e.offsetTop - e.offsetParent.scrollTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouse_coords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:(ev.clientX + document.body.scrollLeft - document.body.clientLeft),
		y:(ev.clientY + document.body.scrollTop  - document.body.clientTop)
	};
}

function mouse_move(ev)
{
	ev = ev || window.event;

	var mouse_pos = mouse_coords(ev);

	mousex = mouse_pos.x;
	mousey = mouse_pos.y;

	if (_scroll_anchor)
	{
		if (!is('offsetset'))
		{
			anchor_pos = get_position(_scroll_anchor);
			/*_scroll_bar = $('scroll_bar');
			_scroll_content = $('scroll_content');*/
			scroll_bar_pos = get_position(_scroll_bar);
			offset = {};
			offset.x = mouse_pos.x - anchor_pos.x;
			offset.y = mouse_pos.y - anchor_pos.y;
			offsetset = true;
		}
		else
		{
			var scroll_top = mouse_pos.y - scroll_bar_pos.y - offset.y;
			if (scroll_top < 0) scroll_top = 0;
			if (scroll_top + _scroll_anchor.offsetHeight > _scroll_bar.offsetHeight) scroll_top = _scroll_bar.offsetHeight - _scroll_anchor.offsetHeight;

			_scroll_anchor.style.top = scroll_top + "px";

			var percent = (100 / (_scroll_bar.offsetHeight - _scroll_anchor.offsetHeight)) * scroll_top;

			_scroll_content.style.top = (0 - ((percent / 100) * (_scroll_content.offsetHeight - _scroll_holder.offsetHeight)))+'px';
		}
	}
	return false;
}

function handle(delta)
{
	if (!delta) return false;
	/*if (typeof(_scroll_content) == "undefined") _scroll_content = $('scroll_content');
	if (typeof(_scroll_bar) == "undefined") _scroll_bar = $('scroll_bar');
	if (typeof(__scroll_anchor) == "undefined") __scroll_anchor = $('scroll_anchor');*/

	if (delta < 0)
	{
		scroll_top = _scroll_content.offsetTop-40;
		if (scroll_top < 0 - (_scroll_content.offsetHeight - _scroll_content.parentNode.offsetHeight))
			scroll_top = 0 - (_scroll_content.offsetHeight - _scroll_content.parentNode.offsetHeight);
	}
	else
	{
		scroll_top = _scroll_content.offsetTop+40;
		if (scroll_top > 0) scroll_top = 0;
	}

	_scroll_content.style.top = scroll_top+'px';

	var margin = _scroll_content.style.marginTop.replace('px', '') * 1;

	var percent = (100 / (_scroll_content.offsetHeight - _scroll_content.parentNode.offsetHeight)) * (_scroll_content.offsetTop - margin);

	__scroll_anchor.style.top = 0- Math.floor((percent / 100) * (_scroll_bar.offsetHeight - __scroll_anchor.offsetHeight)) + "px";
}

/** Event handler for mouse wheel event.
 */
function wheel(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)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code.
 * If you use your own event management code, change it as required.
 */
function initscroll(obj, init_mouse)
{
	if (!_scroll_bar) return false;
	if (init_mouse)
	{
		_scroll_anchor.style.backgroundColor = '#FB5656';
		$('wrap').onmousemove = mouse_move;
		$('wrap').onmouseup = function()
		{
			$('wrap').onmousemove = null;
			if (is('_scroll_anchor'))
			{
				_scroll_anchor.style.backgroundColor = '#293547';
				_scroll_anchor = false;
				offsetset = false;
			}
		}
	}

	var percent = (100 / _scroll_content.offsetHeight) * _scroll_content.parentNode.offsetHeight;
	if (percent > 100)
	{
		if (_scroll_bar) _scroll_bar.parentNode.removeChild(_scroll_bar);
		return false;
	}
	var scroll_bar_height = (percent / 100) * _scroll_bar.offsetHeight;
	__scroll_anchor.style.height = scroll_bar_height + "px";

	if (obj.addEventListener)
	        /** DOMMouseScroll is for mozilla. */
	        obj.addEventListener('DOMMouseScroll', wheel, false);
	/** IE/Opera. */
	obj.onmousewheel = wheel;
}

function init_mc_gallery_scroll(init_mouse)
{
	if (!init_mouse) mouse = 0;
	if (_scroll_anchor && _scroll_anchor != $('gallery_scrollanchor')) return false;
	__scroll_anchor = $('gallery_scrollanchor');
	_scroll_bar = $('media_gallery_scroller');
	_scroll_content = $('media_gallery_inner');
	_scroll_holder = $('media_gallery_wrap');
	initscroll($('media_gallery_outer'), init_mouse);
}

function init_mc_documents_scroll(init_mouse)
{
	if (!init_mouse) mouse = 0;
	if (_scroll_anchor && _scroll_anchor != $('gallery_scrollanchor')) return false;
	__scroll_anchor = $('gallery_scrollanchor');
	_scroll_bar = $('media_gallery_scroller');
	_scroll_content = $('media_text_inner');
	_scroll_holder = $('media_text_wrap');
	initscroll($('media_text_outer'), init_mouse);
}

function init_mc_basket_scroll(init_mouse)
{
	if (!init_mouse) mouse = 0;
	if (_scroll_anchor && _scroll_anchor != $('basket_scrollanchor')) return false;
	__scroll_anchor = $('basket_scrollanchor');
	_scroll_bar = $('basket_scroller');
	_scroll_content = $('basket_inner');
	_scroll_holder = $('basket_wrap');
	initscroll($('basket_outer'), init_mouse);
}

function init_text_scroll(init_mouse)
{
	if (!init_mouse) mouse = 0;
	if (_scroll_anchor && _scroll_anchor != $('text_scrollanchor')) return false;
	__scroll_anchor = $('text_scrollanchor');
	_scroll_bar = $('text_scroller');
	_scroll_content = $('text_wrap_inner');
	_scroll_holder = $('text_wrap');
	initscroll($('text_wrap_outer'), init_mouse);
}
/*
function click_scroll()
{
	__scroll_anchor = $('gallery_scrollanchor');
	_scroll_bar = $('gallery_scrollbar');
	_scroll_content = $('media_gallery_inner');
	scroll_anchor_pos = get_position(__scroll_anchor);
	scroll_bar_pos = get_position(_scroll_bar);
	if (mousey > scroll_bar_pos.y && mousey < scroll_anchor_pos.y)
	{
		_scroll_content.scrollTop = _scroll_content.scrollTop - _scroll_content.offsetHeight;
	}
	else if (mousey > scroll_anchor_pos.y + __scroll_anchor.offsetHeight && mousey < scroll_bar_pos.y + _scroll_bar.offsetHeight)
	{
		_scroll_content.scrollTop = _scroll_content.scrollTop + _scroll_content.offsetHeight;
	}

	var percent = (100 / (_scroll_content.scrollHeight - _scroll_content.offsetHeight)) * _scroll_content.scrollTop;

	__scroll_anchor.style.top = ((percent / 100) * (_scroll_bar.offsetHeight - __scroll_anchor.offsetHeight)) + "px";
}
 */
function time()
{
	var d = new Date();
	return d.getTime();
}

function debug(debug_id,debug_var)
{
	if (!(debug_element = document.getElementById('debug_'+debug_id)))
	{
		var debug_element = document.createElement("div");
		debug_element.style.position = "fixed";
		debug_element.style.right="10px";
		debug_element.style.height="15px";
		debug_element.style.backgroundColor="#E0E0E0";
		debug_element.style.font="13px/17px verdana";
		debug_element.id = "debug_"+debug_id;
		debug_element.style.bottom = 10+(debug_id*20)+"px";
		document.body.appendChild(debug_element);
	}
	debug_element.innerHTML = debug_var;
}

function is(element)
{
	if (typeof(window[element]) == 'undefined') return false;
	if (!window[element]) return false;
	if (element.length == 0) return false;
	return true;
}

function modify_input(input_name, obj)
{
	var input = obj.form[input_name].value;
	var bits = input.split(',');

	if (!bits.length) return false;
	for (var i in bits)
	{
		if (bits[i] == obj.value)
		{
			if (!obj.checked)
			{
				bits = array_remove(bits, i);
				obj.form[input_name].value = bits.join(',');
			}
			return false;
		}
	}
	if (obj.checked)
	{
		if (input) obj.form[input_name].value = input+','+obj.value;
		else obj.form[input_name].value = obj.value;
	}
	return false;
}

 function array_remove(array, from, to)
 {
  array.splice(from,
    !to ||
    1 + to - from + (!(to < 0 ^ from >= 0) && (to < 0 || -1) * array.length));
  return array;
 }

 function enable_button(button_id)
 {
	var button = $(button_id);
	if (!button) return false;
	var click = button.getAttribute('onclick');
	click = click.replace('return false;', '');
	button.onclick = function() {eval(click);};
	var omover = button.firstChild.getAttribute('onmouseover');
	button.firstChild.onmouseover = function() {eval(omover.replace('return false;', ''));};
	var omovout = button.firstChild.getAttribute('onmouseout');
	button.firstChild.onmouseout = function() {eval(omovout.replace('return false;', ''));};
	button.className = button.className.replace(' inactive', '');
 }

 function disable_button(button_id)
 {
	var button = $(button_id);
	if (!button) return false;
	button.onclick = 'return false;'+button.onclick;
	button.firstChild.onmouseover = 'return false;'+button.firstChild.onmouseover;
	button.firstChild.onmouseout = 'return false;'+button.firstChild.onmouseout;
	button.firstChild.src = PATH+'images/core/media_pack_add_btn2.gif';
	button.className += ' inactive';
 }

 function activate(obj, parent_id)
 {
	if (typeof(obj) != 'object') obj = $(obj);
	if (!obj) return false;
	a_list = $(parent_id).getElementsByTagName(obj.tagName);
	for (var a in a_list)
	{
		a_list[a].className = '';
	}
	obj.className = 'act';
 }

 function slide_left(obj_id, direction, position)
 {
	if (_sliding) return false;
	clear_all_timeouts();
	var content = $(obj_id),
		wrapper = content.parentNode,
		original_left = content.offsetLeft,
		new_left = original_left,
		distance_to_move,
		final_left,
		timeout = (browser == 'Internet Explorer' ? 15 : 30),
		timer = 0,
		percent_moved = 0,
		speed = 5,
		ease,
		loop_counter = 0,
		change;


	var step = Math.round(original_left / wrapper.offsetWidth);
	if (direction != 0)
	{
		if (direction > 0) step++;
		else step--;
	}

	if (direction != 0)
	{
		if (original_left != 0)
		{
			final_left = (step * wrapper.offsetWidth);
			distance_to_move = final_left - original_left;
		}
		else
		{
			distance_to_move = direction * wrapper.offsetWidth;
			final_left = original_left + distance_to_move;
		}
	}
	else
	{
		distance_to_move = position - original_left;
		final_left = position;
		if (final_left > original_left) direction = 1;
		else direction = -1;
	}

	if (final_left > 0 || final_left * -1 > content.offsetWidth - 1) return false;
	_sliding = true;

	var page_num = Math.round(final_left * -1 / wrapper.offsetWidth) + 1;
	activate('page_'+page_num, 'gallery_nav');

	while (new_left * direction < final_left * direction && loop_counter < 1000)
	{
		loop_counter++;
		timer += timeout;
		percent_moved = (new_left - original_left) / distance_to_move;
		ease = (Math.sin(percent_moved * 3.14159265))*20;
		//setTimeout("debug(1, "+ease+");", timer);
		change = Math.round(speed * ease);
		if (change < 2) change = 2;
		change = change * direction;
		//setTimeout("debug(2, "+change+");", timer);
		new_left += change;
		timeouts[timeouts.length] = setTimeout("move_left('"+obj_id+"', "+new_left+");", timer);
	}
	if (new_left != final_left)
	{
		timer += timeout;
		timeouts[timeouts.length] = setTimeout("move_left('"+obj_id+"', "+final_left+");", timer);
	}
	setTimeout("_sliding = false;", timer);
	return true;
 }

 function move_left(obj_id, left)
 {
	if ($(obj_id)) $(obj_id).style.left = left+'px';
 }

function clear_all_timeouts()
{
	if (timeouts) for (var i in timeouts) if (timeouts[i]) clearTimeout(timeouts[i]);
	timeouts = [];
}

function tc_popup(url)
{
	window.open(url, 'tc_popup', 'toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resizable=0, width=550, height=500');
	return false;
}