	/* -------------------------------
	   © 2008, MH2 Design
	   Project: mh2_shoutbox
	   Type: Component
	   Date: 09/20/2008
	   Change Log:
	   ------------------------------- */

	// Initialize all variables
	// Display variables
	var mh2SBwidth		= '200px';	// Main Width 
	var mh2SBheight		= '400px';	// Main Height
	var mh2SBspeed		= 2;				// Main Speed
	var mh2SBpause		= 1;				// Allow Pause
	
	var mh2SBdataready = false;
	
	var mh2SBGEThttp	= '';				// HTTP Get object 
	var mh2SBPUThttp	= '';				// HTTP Put object 
	var mh2SBbasedir 	= '';				// Base Directory to the component
	var mh2SBGetPHP 	= '';				// Getshouts php file
	var mh2EmotePath 	= '';				// Passed Emoticon Path 
	var mh2temptext		= '';				// Text Storage
	
	var mh2SBmarquee	= '';				// Storage pointer to the div
	var mh2SBspeed		= 2;				// Specify marquee scroll speed (larger is faster 1-10)
	var mh2SBpause		= 1;				// Pause marquee onMousever (0=no. 1=yes)
	var mh2SBnewshout = 0;
	
	var	mh2refresh		= false;
	var lengthtest1		= 0;
	var lengthtest2		= 0;
	

	var runningspeed	= 0;				// Curent Running Speed
	var pausespeed		= 0;				// Pause Speed
	var actualheight	= 0;				// Actual Height of the Shout Text
	var direction			= 1;				// 1 = Forward, 0 = Reverse


	// -----------------------------------
	// -- Set the main options
	// -----------------------------------
	function _mh2_setOptions(p_livepath, p_path, p_speed, p_pause, p_newshouts)
	{
		mh2SBbasedir	= base64_decode(p_livepath);
		mh2EmotePath 	= p_path; // + 'images/emoticons/';
		mh2SBGetPHP 	= mh2SBbasedir + 'mh2shoutbox.getshouts.php?mh2emotes=';
		mh2SBPutPHP 	= mh2SBbasedir + 'mh2shoutbox.putshouts.php';
		mh2SBmarquee	= document.getElementById("mh2vmarquee");
		mh2SBspeed		= p_speed;
		runningspeed  = mh2SBspeed;
		mh2SBpause		= p_pause;
		mh2SBnewshout = p_newshouts;
		pausespeed		= ((mh2SBpause==0)? mh2SBspeed: 0);

		mh2SBGEThttp 	= getHTTPObject();
		mh2SBPUThttp 	= getHTTPObject();
	
		if (window.addEventListener)
			window.addEventListener("load", initializemarquee, false);
		else if (window.attachEvent)
			window.attachEvent("onload", initializemarquee);
		else if (document.getElementById)
			window.onload=initializemarquee();
	}

	// -----------------------------------
	// -- Initialize the Marquee system
	// -----------------------------------
	function initializemarquee()
	{

		mh2SBmarquee						= document.getElementById("mh2vmarquee");
		mh2SBmarquee.style.top	= 0;
		marqueeheight						= document.getElementById("mh2marqueecontainer").offsetHeight;
		actualheight						= mh2SBmarquee.offsetHeight;
		mh2temptext 						= '';
		
		if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1)
		{ 
			mh2SBmarquee.style.height 	= marqueeheight + 'px';
			mh2SBmarquee.style.overflow	= 'scroll';
			return;
		}
//		_mh2getshouts();
		setTimeout('lefttime = setInterval("mh2scrollmarquee()",30)', 500); 
	}

	// -----------------------------------
	// -- Receive the Chat Text
	// -----------------------------------
	function _mh2receivetext()
	{
  	if (mh2SBGEThttp.readyState == 4)
  	{
			mh2temptext = mh2SBGEThttp.responseText;

			lengthtest2	= mh2temptext.length
			if ((mh2SBnewshout == 1) && (lengthtest1 != lengthtest2 ) && (mh2temptext.length > 0))
				mh2refresh = true;
			
			mh2SBGEThttp.close;
		}
	}
	
	// -----------------------------------
	// -- Execute the Get Shouts Function
	// -----------------------------------
	function _mh2getshouts()
	{
	  if ((mh2SBGEThttp.readyState == 0) || (mh2SBGEThttp.readyState == 4))
	  {
			mh2SBGEThttp.open('GET', mh2SBGetPHP + mh2EmotePath, true); 
			mh2SBGEThttp.onreadystatechange = _mh2receivetext;
			mh2SBGEThttp.send(null); 
		}
	}

	
/*
Javascript Ready State
0 = open has not yet been called
1 = send has not yet been called but open has been called
2 = send has been called but no response from server
3 = data is in the process of being received from the server
4 = response from server has arrived
*/

	// -----------------------------------
	// -- Scroll the data
	// -----------------------------------
	function mh2scrollmarquee()
	{
		if (direction == 1)
		{
			if (parseInt(mh2SBmarquee.style.top) > (actualheight*(-1)+8))
				mh2SBmarquee.style.top = parseInt(mh2SBmarquee.style.top)- runningspeed + 'px';
			else
				mh2SBmarquee.style.top = parseInt(marqueeheight) + 8 +'px';

			
			if ((parseInt(mh2SBmarquee.style.top) <= (actualheight*(-1)+8)) || (mh2refresh == true) ) 
			{
				mh2SBmarquee.style.top	= parseInt(marqueeheight)+8+'px';
				
				if (mh2temptext.length > 0)
					mh2SBmarquee.innerHTML	= mh2temptext;
					
				mh2SBmarquee						= document.getElementById("mh2vmarquee");
				actualheight						= mh2SBmarquee.offsetHeight;
				mh2temptext							= '';
				mh2refresh							= false;
				lengthtest1 						= lengthtest2;
				_mh2getshouts();
			}
		} else {
			if (parseInt(mh2SBmarquee.style.top) < marqueeheight - 8) 
			{
				if (parseInt(mh2SBmarquee.style.top) < marqueeheight)
					mh2SBmarquee.style.top = parseInt(mh2SBmarquee.style.top) +  runningspeed + 'px';
				else
					mh2SBmarquee.style.top = parseInt(marqueeheight) - 8 + 'px';
			} else 
				document.getElementById("mh2errordisp").innerHTML = 'Top of Data';
			
		}
	}

	// -----------------------------------
	// -- Post the shout
	// -----------------------------------
	function _mh2postshout(p_uid)
	{
		var uname 		= document.getElementById("mh2uname").value;
		var theshout	= document.getElementById("mh2sb_input").value;
		var tempurl = 'mh2uid=' + p_uid + '&mh2uname=' + base64_encode(uname) +'&mh2shout=' + base64_encode(theshout);
		
		theshout.replace(/\r|\n|\r\n/g, "");
		
		if ((theshout.length > 0) && (uname.length > 0))
		{
  		mh2SBPUThttp.open('POST', mh2SBPutPHP, true);  
			mh2SBPUThttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 			mh2SBPUThttp.onreadystatechange = _mh2closeshout; 
  		mh2SBPUThttp.send(tempurl);
  	} else {
  		
			if (theshout.length == 0) 
			{
				document.getElementById("mh2sb_input").value = '';
				document.getElementById("mh2errordisp").innerHTML = 'No Input';
			}
			if (uname.length == 0)
				document.getElementById("mh2errordisp").innerHTML = 'No Name';
  	}
  	
	}

	// -----------------------------------
	// -- Close the shout object
	// -----------------------------------
	function _mh2closeshout()
	{
		mh2SBPUThttp.close;
 		document.getElementById("mh2sb_input").value = '';
 		_mh2countchars();
 		_mh2getshouts();
	}

	// -----------------------------------
	// -- Count the characters in the shout
	// -----------------------------------
	function _mh2countchars(p_max)
	{
		var counttext;
		var uname 
				
		uname 		= document.getElementById("mh2uname").value;
		counttext = document.getElementById("mh2sb_input").value;

		if (uname.length == 0)
			document.getElementById("mh2errordisp").innerHTML = 'No Name';
		else
			document.getElementById("mh2errordisp").innerHTML = '';
		
		if (p_max > 0)
		{
			if (counttext.length > p_max)
			{
				document.getElementById("mh2sb_input").value = counttext.substring(0,p_max);
				document.getElementById("mh2countshout").innerHTML = '<span class="mh2sb_errors">MAXIMUM<span>';
				return;
			}
		}
		
		if (counttext.length > 0)
			document.getElementById("mh2countshout").innerHTML = counttext.length + ((p_max >0)?'/'+p_max:'');
		else
			document.getElementById("mh2countshout").innerHTML = '';
		
	}

	// -----------------------------------
	// -- Check for Enter
	// -----------------------------------
	function _mh2checkenter(p_event, p_userid)
	{
		if (p_event.keyCode == 13)
			_mh2postshout(p_userid);
	}	
	

	// -----------------------------------
	// -- Direction set
	// -----------------------------------
	function _mh2switchdirection()
	{
		if (direction == 1)
			direction = 2;
		else
			direction = 1;
			
		document.getElementById("mh2errordisp").innerHTML = '';
			
	}
	
	// -----------------------------------
	// -- Speed set
	// -----------------------------------
	function _mh2speed(p_type)
	{
		if (p_type == 'pause')
		{
			if (runningspeed == 0)
				runningspeed = mh2SBspeed;
			else 
				runningspeed = 0;
		}
		
		if (p_type == 'faster')
		{
			if (mh2SBspeed < 10)
			{
				mh2SBspeed = mh2SBspeed + 1;
				runningspeed = mh2SBspeed;
			}
		}

		if (p_type == 'slower')
		{
			if (mh2SBspeed > 1)
			{
				mh2SBspeed = mh2SBspeed - 1;
				runningspeed = mh2SBspeed;
			}
		}
		document.getElementById("mh2speed").innerHTML = '( ' + parseInt(runningspeed) + ' )';
	}	

	// -----------------------------------
	// -- Show Hide Emoticons
	// -----------------------------------
	function _mh2emoticons()
	{
		var currentstate;
		
		currentstate = document.getElementById("mh2emoticons").style.display;
		
		if (currentstate == 'block')
			document.getElementById("mh2emoticons").style.display = 'none'; 
		else
			document.getElementById("mh2emoticons").style.display = 'block'; 
	}	
	
	// -----------------------------------
	// -- Show Hide Emoticons
	// -----------------------------------
	function _mh2useemoticon(p_variable)
	{
		document.getElementById("mh2sb_input").value += ' ' + base64_decode(p_variable) + ' ';
		document.getElementById("mh2emoticons").style.display = 'none'; 
		_mh2countchars();
		document.getElementById("mh2sb_input").focus();
	}
	
	// -----------------------------------
	// -- Get the HTTP Object
	// -----------------------------------
	function getHTTPObject() {
	  var xmlhttp;
	  
		try 
		{ 
		 var xmlhttp = new XMLHttpRequest(); 
		} 
		catch (error) 
		{ 
		 try 
		 { 
		   var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		 } 
		 catch (error) 
		 { 
		   return false; 
		 } 
		}  
	  return xmlhttp;
	}

	// -----------------------------------
	// -- Emulate the PHP base64_encode
	// -----------------------------------
	function base64_encode( data ) 
	{
        
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    data = utf8_encode(data);
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
    return enc;
	}
	
	// -----------------------------------
	// -- Emulate the PHP base64_decode
	// -----------------------------------
	function base64_decode( data ) 
	{
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
 
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof window['btoa'] == 'function') {
    //    return btoa(data);
    //}
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
 
    data += '';
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
 
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
 
    return dec;
	}

	// -----------------------------------
	// -- Emulate the PHP base64_encode UTF
	// -----------------------------------
	function utf8_encode ( string ) 
	{
    // Encodes an ISO-8859-1 string to UTF-8
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_utf8_encode/
    // +       version: 810.621
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'

    string = (string+'').replace(/\r\n/g,"\n");
    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
	}

	// -----------------------------------
	// -- Emulate the PHP base64_decode UTF
	// -----------------------------------
	function utf8_decode ( str_data ) 
	{
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;
 
    str_data += '';
 
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
	}