function OllehKTBoard()
{

this.cfg = {
	wvurl:'http://ithwave.com/twboard.php',
	imgurl:'http://ithwave.com/custom/ollehkt/images/'
};

this.flags = {
	wid: '_brd',
	nodoctype:false,
	loaded:0,
	ticktime1:150,
	ticktime2:15,
	expand:0,
	ltimer:0
};
this.wpos = {
	animated: false,
	visible : false,
	wwidth : 450,
	wheight: 160,
	wminwidth: 290,
	target : 0,
	ohash: '',
	stime : 0,
	ttime : 0
	};

this.iframe_src = this.cfg['wvurl'] + "?ssrl=101";
this.init = function ()
{
	if(this.gel(this.wname('ollehkt_wcont'))) return false;

	var lhash = document.location.hash;
	if(navigator.userAgent.indexOf('MSIE') >= 0)
	{
		if((navigator.userAgent.indexOf('MSIE 6') >= 0) || (document.firstChild.nodeValue == null))
		{
			this.flags['nodoctype'] = true;
		}
	}

	if(lhash.substring(1,7) == 'wvshow')
	{
		this.flags['expand'] = 1;
	}

	this.icon_insert();
};

this.init_pos = function (obj, shift)
{
	obj.style.left = '-'+(this.wpos['wwidth'] - shift)+'px';
};

this.init_content = function ()
{
	var d = document;

	if(!this.gel(this.wname('ollehkt_cont')) )
	{
		var obj = d.createElement('iframe');
		obj.id = this.wname('ollehkt_cont');
		obj.name = this.wname('ollehkt_cont');
		obj.className = 'ollehkt_content';
		this.init_pos(obj, 0);
		obj.style.display = 'none';
		obj.scrolling = 'no';
		obj.frameBorder=0;
		obj.setAttribute("allowTransparency","true");
		this.add_el(obj);

		// important: after add_el
		obj.src = this.iframe_src;

		return true;
	}
	return false;
};

this.init_onload = function ()
{
	if(this.flags['loaded']) return;

	this.flags['loaded']++;

	if(!this.gel(this.wname('ollehkt_wcont'))) this.init();
	this.init_content();

	if(this.flags['expand'])
	{
		this.flags['expand'] = 0;
		this.icon_click();
	}

	this.set_timer(1);
};

this.remove_px = function (str)
{
	var pos = str.indexOf('px');
	if (pos > 0)
		return str.substring(0, pos);
	else return str;
}

this.slide_prop = function (el, setto)
{
	if(typeof setto == 'undefined')
	{
		var t = this.remove_px(el.style.left);
		return parseInt(t);
	}
	else
	{
		el.style.left = setto;
	}
};

this.check_timer = function ()
{
	var curtime = this.ctime();
	this.cmd_dispatch();

	if(this.wpos['animated'])
	{
		var ael = [this.wname('ollehkt_cont'), this.wname('ollehkt_over'), this.wname('ollehkt_wcont')];

		var tdiff = curtime - this.wpos['stime'];
		var pshift = 1;
		if(tdiff >= this.wpos['ttime'])
		{
			this.set_timer(1);
			this.wpos['animated'] = false;
			if(!this.wpos['visible'])
			{
				this.change_class(this.wname('ollehkt_wcont'), 'ollehkt_open', false);
			}
			this.wswitch_state(this.wpos['visible']);
			if (!this.wpos['visible']) this.refresh();
			// 	this.cmd_send('open_' + (this.wpos['visible']?1:0));

			document.location.hash = this.wpos['visible'] ? '#wvshow' : '#wvhide';
		}else
			pshift = tdiff/this.wpos['ttime'];

		pshift = ((-Math.cos(pshift * Math.PI)/2) + 0.5) * this.wpos['target'];

		for(var i=0;i<ael.length;i++)
			if(this.gel(ael[i]))
			{
				el = ael[i];

				if(typeof this.epos[el] == 'undefined')
				{
					this.epos[el] = this.slide_prop(this.gel(el));
					if(isNaN(this.epos[el]))
						this.epos[el] = 0;
				}
				this.slide_prop(this.gel(el), this.epos[el] + pshift + 'px');
			}
	}
};

this.wswitch_state = function (state)
{
	if(this.wpos['visible'] == state) return;

	if(!this.wpos['animated'])
	{
		this.set_timer(2);
		this.wpos['visible'] = state;
		this.wpos['target'] = this.wpos['wwidth'];
		if(!state)
			this.wpos['target'] = -this.wpos['target'];
		this.wpos['stime'] = this.ctime();
		this.wpos['ttime'] = 0.0001;

		this.epos = {};
		this.wpos['animated'] = true;

		if(this.wpos['visible'])
			this.change_class(this.wname('ollehkt_wcont'), 'ollehkt_open', true);
	}

}

this.cmd_dispatch = function()
{	
	var cmdpos = document.location.hash.indexOf('wvcmd_');
	if(cmdpos >= 0)
	{
		var cmd = document.location.hash.substring(cmdpos+6);
		document.location.hash = this.wpos['ohash'] ? this.wpos['ohash'] : '#';
		this.cmd_get(cmd);
	}
	if(this.wpos['ohash'] != document.location.hash)
		this.wpos['ohash'] = document.location.hash;
};

this.cmd_get = function(e)
{	
	if(e.data) 
		e = e.data;
	var psplit = e.split('_');
	var cmd = psplit[0];

	if(cmd.indexOf('brd') == 0)
		cmd = cmd.substring(3);

	switch(cmd)
	{
		case 'ready':
			this.change_class(this.wname('ollehkt_wcont'), 'ollehkt_n_load', false);
			this.wswitch_state(true);
			this.gel(this.wname('ollehkt_cont')).style.display = '';
			break;

		case 'close':
			this.wswitch_state(false);
			break;

	}
};

this.refresh = function()
{	
	this.gel(this.wname('ollehkt_cont')).src = this.iframe_src;
};

this.icon_click = function ()
{
	var d = document;
	this.set_widget_width(this.wpos['wwidth'], false);

	var cinit = this.init_content();
	if(!this.gel(this.wname('ollehkt_over')))
	{
		this.change_class(this.wname('ollehkt_wcont'), 'ollehkt_n_load', true);

		var obj = d.createElement('div');

		obj.id = this.wname('ollehkt_over');
		obj.className = 'ollehkt_overlay';
		this.init_pos(obj, 0);
		this.add_el(obj);

		if(!cinit)
		{
			if(document.location.hash.indexOf('wvcmd_brdready') < 0)
			{
				document.location.hash = document.location.hash+"#wvcmd_brdready";
			}
			// this.gel(this.wname('ollehkt_cont')).src = this.if_src('');
		}

		this.set_widget_width(this.wpos['wwidth'], false);
	}
	else
	{
		olleh_kt_brd.wswitch_state(!this.wpos['visible']);
	}

	return false;
};

this.gel = function (elid)
{
	return document.getElementById(elid);
};

this.wname = function (elid)
{
	return elid+'_brd';
};

this.icon_insert = function ()
{
	var d = document;

	var cssText = "";
	if (this.flags['nodoctype'])
	{
		cssText += " * { margin : 0; }";
		cssText += " html, body { height: 100%; overflow:auto; }";
	}
	cssText += " .ollehkt_passive_container { bottom:0; padding:0px; margin:0px; position:"+(this.flags['nodoctype']? "absolute" : "fixed")+"; top:35%; cursor:pointer; left:0px; padding:0px; margin:0px; height:101px; z-index:2000000000; background:none; line-height:normal; }";
	cssText += " #ollehkt_passive_twitter { background:none; width:21px; height:127px; position:absolute; left:15px; top: 19px; z-index:1; }";
	if (this.flags['nodoctype'])
	{
		cssText += " #ollehkt_passive_large { position:absolute; top:0px; left:0px; height:108px; width:35px; border: none; margin:0; padding:0; background:transparent url("+this.cfg['imgurl']+"ollehkt_tab.gif?1109) 0px 0px no-repeat; opacity:.95; }";
	}
	else 
	{
		cssText += " #ollehkt_passive_large { position:absolute; top:0px; left:0px; height:108px; width:35px; border: none; margin:0; padding:0; background:transparent url("+this.cfg['imgurl']+"ollehkt_tab.gif?1109) 0px 0px no-repeat; opacity:.95; }";
	}

	cssText += " .ollehkt_passive_container.ollehkt_open #ollehkt_passive_twitter { left:8px; }";
	cssText += " #ollehkt_passive_large.ollehkt_open { background-position: 0px -108px !important; }";
	cssText += " .ollehkt_passive_container.ollehkt_open #ollehkt_passive_large { background-position: -5px -216px !important; }";
	cssText += " .ollehkt_passive_container.ollehkt_open #ollehkt_passive_large.ollehkt_open { background-position: -5px -324px !important; }";
	if (this.flags['nodoctype'])
	{
		cssText += " .ollehkt_overlay { width: 450px; min-width:289px; z-index: 2000000000; background-color:#231f20; border-right:#231f20 1px solid ; position: absolute; top: 0px; bottom:0px; left:-450px; padding:0px; margin:0px; height: expression(document.documentElement.clientHeight); }";
		cssText += " body .ollehkt_content { z-index: 2000000000; margin: 0px; padding: 0px; position: absolute; width: 400px; scrolling: no; min-width:269px; top: 0px; bottom:0px; left:-440px; height: expression(document.documentElement.clientHeight - 15); border:none; }";
	}
	else
	{
		cssText += " .ollehkt_overlay { width: 450px; min-width:289px; z-index: 2000000000; background-color:#231f20; border-right:#231f20 1px solid ; position: fixed; top: 0px; left:-450px; padding:0px; margin:0px; height: 100%; }";
		cssText += " .ollehkt_content { z-index: 2000000000; margin: 0px; padding: 0px; position: fixed; width: 400px; scrolling: no; min-width:269px; top: 0px; left:-440px; height: 98%; border:none; }";
	}
	cssText += " #ollehkt_waitload { display:none; position:absolute; top:39px; left:11px; }";
	cssText += " .ollehkt_n_load { opacity:0.6; filter2: alpha(opacity=60); }";
	cssText += " .ollehkt_n_load #ollehkt_waitload { display:inline; }";
	cssText += " a:focus { outline: none; }";
	var styleText = d.createTextNode(cssText);
	var styleNode = d.createElement("style");
	styleNode.type = 'text/css';
	if(styleNode.styleSheet)
		styleNode.styleSheet.cssText = styleText.nodeValue;
	else
		styleNode.appendChild(styleText);
	d.getElementsByTagName("head")[0].appendChild(styleNode);

	if(!this.gel(this.wname('ollehkt_wcont')))
	{
		var obj = d.createElement('div');
		var html = '';
		html += "<div class=ollehkt_passive_container id=ollehkt_wcont_brd>";
		html += "<div onmouseover=\"olleh_kt_brd.icon_over('large')\" onclick=\"return olleh_kt_brd.icon_click()\" onmouseout=\"olleh_kt_brd.icon_out('large')\">";
		html += "<div id=ollehkt_passive_twitter></div>";
		html += "<div id=ollehkt_passive_large></div></div><img id=ollehkt_waitload src=\""+this.cfg['imgurl']+"ajax-loader.gif\"></div>";

		obj.innerHTML = html;
		obj.id = 'ITH_icon';
		obj.style.display = 'none';
		this.add_el(obj);

		d.body.replaceChild(this.gel(this.wname('ollehkt_wcont')), this.gel('ITH_icon'));
	}
};

this.icon_over = function(tp)
{
	this.change_class('ollehkt_passive_'+tp, 'ollehkt_open', true);
};

this.icon_out = function(tp)
{
	this.change_class('ollehkt_passive_'+tp, 'ollehkt_open', false);
};

this.add_onload = function(fnc)
{
	if(!this.add_event( 'load', fnc, true ))
	{
		if ( window.onload != null ) 
		{
			var oldOnload = window.onload;
			window.onload = function ( e ) 
			{
				oldOnload(e);
				window[fnc]();
			};
		}
		else
			window.onload = fnc;
	}
};

this.change_class = function (elid, cname, onoff) 
{
	var el = this.gel(elid);
	if(onoff && (onoff!=0))
	{
		if(el.className.indexOf(cname)<0)
			el.className += ' '+cname;
	}
	else
	{
		for(var cn='';cn!=el.className;)
		{
			cn = el.className;
			el.className = el.className.replace(cname,'');
		}
		el.className = el.className.replace(/ +/g, ' ');
	}
};

this.add_el = function (obj)
{
	var b = document.body;
	try {
		if(b.childNodes.length > 0)
			b.insertBefore(obj, b.childNodes[0]);
		else
			b.appendChild(obj);
	}catch(e) {
		return false;
	}

	return true;
};

this.set_timer = function (tp) 
{
	if(this.flags['ltimer'])
		window.clearInterval(this.flags['ltimer']);

	this.flags['ltimer'] = null;
	this.flags['ltimer'] = window.setInterval('olleh_kt_brd.check_timer()', this.flags['ticktime'+tp]);
};

this.add_event = function(ev, func, wnd)
{
	var scope = wnd ? window : document;
	if(typeof scope.addEventListener != 'undefined')
	{
		scope.addEventListener(ev, func, false);
	}
	else
	{
		if(typeof scope.attachEvent != 'undefined')
			scope.attachEvent('on'+ev, func);
		else
			return false;
	}

	return true;
};

this.set_widget_width = function (wd, mv) 
{
	wd = Math.min(parseInt(document.body.clientWidth*0.9), wd);
	wd = Math.max(olleh_kt_brd.wpos['wminwidth'], wd);
	olleh_kt_brd.wpos['wwidth'] = wd;

	var e1 = olleh_kt_brd.gel(this.wname('ollehkt_over'));
	var e2 = olleh_kt_brd.gel(this.wname('ollehkt_cont'));
	if(e1 && e2)
	{
		e1.style.width = (wd - 1) + 'px';
		e2.style.width = (wd - 20)+ 'px';
		// e2.style.width = (wd - 1) + 'px';

		var l1 = this.remove_px(e1.style.left);
		var l2 = this.remove_px(e2.style.left);

		if(parseInt(l1)<0) e1.style.left = '-'+(wd)+'px';
		if(parseInt(l2)<0) e2.style.left = '-'+(wd-10)+'px';
		// if(parseInt(l2)<0) e2.style.left = '-'+(wd)+'px';
	}

};

this.ctime = function(){ return +new Date;}

}

if(typeof olleh_kt_brd == 'undefined')
{
	var olleh_kt_brd = new OllehKTBoard();

	if(document.body)
	{
		olleh_kt_brd.init_onload();
	}
	olleh_kt_brd.add_onload(function(){olleh_kt_brd.init_onload();});
}
