// JavaScript Document

//---------------------------------------------------------------------------------general functions
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
			while(1) 
			{
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
	else if(obj.x)
			curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
			while(1)
			{
				curtop += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
	else if(obj.y)
			curtop += obj.y;
	return curtop;
}

function create_ajax()
{
	var ajax;
	try {  ajax = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) 
	{
		try {   ajax = new ActiveXObject('Microsoft.XMLHTTP');    }
		catch (e2) 
		{
			try {  ajax = new XMLHttpRequest();     }
			catch (e3) {  ajax = false;   }
		}
	}
	return ajax;
}

function ajax_post( ajax, post_value, url )
{
	ajax.open( (post_value? 'POST' : 'GET'), url, true );
	if( post_value )
	{
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Content-length", post_value.length);
		ajax.setRequestHeader("Connection", "close");
	}
	ajax.send( post_value );
}


//-------------------------------------------------------------------------------hiliting (turn down opacity on mouseover)
function hilite( div )
{
	//alert( div.className.indexOf('hilite') );
	if( div.className.indexOf('hilite') == -1 )
		div.className += ' hilite';
}
function unhilite( div )
{
	if( div.className.indexOf(' hilite') != -1 )
		div.className = div.className.substr( 0, div.className.length-7 );
}


//------------------------------------------------------------------------------------------rollover comment boxes for design tumble
function new_load_page( dest, target, hide )
{
	if( !document.getElementById || !dest || !target )
		return false;

	if( typeof(hide) !== 'object' )
		hide = Array();
	
	var ajax = create_ajax();
	ajax.onreadystatechange = function(){
		if( ajax.readyState == 4 ){
			if( ajax.status == 200 )
			{
				run = hide.length;
				target.innerHTML = ajax.responseText;
				for( i=0; i<run; i++ )
					document.getElementById(hide[i]).style.display = 'none';
				target.style.display = 'block';
			}
		}
	}
	ajax_post( ajax, 'code=index', dest );
}

function new_show_spill( clicked )
{
	if( clicked.getAttribute('single') )
		window.location.href = clicked.getAttribute('single');
/*	
	//can't do anything
	if( !document.getElementById || !clicked )
		return false;
		
	//-design spill
	switch( clicked.getAttribute('id') )
	{
		case 'design_comment_link':
			new_load_page( clicked.getAttribute('single'), document.getElementById('design_spill'), Array('bolg') );
			document.getElementById('bolg_link').className = 'button embolden';
		break;
		
		case 'tumble':
			//-if it's already up
			if( document.getElementById('single_content') && document.getElementById('single_content').getAttribute('post_id') == clicked.getAttribute('post_id') && document.getElementById('design_posts').style.display != 'none' )
			{
				document.getElementById('design_posts').style.display = 'none';
				document.getElementById('single_content').parentNode.parentNode.style.display = 'block';
			}
			else
			{
				if( !clicked.getAttribute('type') )
					return false;
				hide = Array('tweet_spill','bookface_spill','spaz_spill','design_posts');
				new_load_page( clicked.getAttribute('single'), document.getElementById(clicked.getAttribute('type')+'_spill'), hide );
			}
			document.getElementById('design_link').className = 'button embolden';
		break;
		
		case 'bolg_link':
			document.getElementById('design_spill').style.display = 'none';
			document.getElementById('bolg').style.display = 'block';
			document.getElementById('bolg_link').className = 'button';
		break;
		
		case 'design_link':
			document.getElementById('tweet_spill').style.display = 'none';
			document.getElementById('bookface_spill').style.display = 'none';
			document.getElementById('spaz_spill').style.display = 'none';
			document.getElementById('design_posts').style.display = 'block';
			document.getElementById('design_link').className = 'button';
		break;
	}
*/
}
/*
function enmousen( div )
{
	div.setAttribute('xyzy','true');
}
function demousen( div )
{
	div.setAttribute('xyzy','false');
}
*/
/*
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
function savemcs(e)
{
	window.mouseyX = mouseX(e);
	window.mouseyY = mouseY(e);
}

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = savemcs;
*/

function design_rollover( div )
{
	//console.log('rollover');
	if( document.getElementById )
	{
		if( window.who_has_dcl != div.getAttribute('post_id') )
		{
			if( document.getElementById('design_comment_link') )
				document.getElementById('design_comment_link').parentNode.removeChild(document.getElementById('design_comment_link'));
				
			if( window.new_div )
				new_div = window.new_div;
			else
			{
				new_div = document.createElement('div');
				new_div.id = 'design_comment_link';
				new_div.className = 'button hilite_more';
				new_div.onmouseover = function()
				{
					window.dcl_mo = true;
					//console.log('dcl_mo:' + window.dcl_mo);
				}
				new_div.onmouseout = function()
				{
					window.dcl_mo = false;
				}
				new_div.onclick = function()
				{
					new_show_spill(this);
				}
			}
			new_div.style.left = (parseInt(findPosX(div)) + 10) + 'px';
			new_div.style.top = (parseInt(findPosY(div)) + 10) + 'px';
			new_div.innerHTML = '&larr;' + (div.getAttribute('c_count') > 0? '&nbsp;'+div.getAttribute('c_count') : '');
			//new_div.setAttribute('xyzy','false');
			new_div.setAttribute('single',div.getAttribute('single'));
			
			div.appendChild( new_div );
			window.who_has_dcl = div.getAttribute('post_id');
			//console.log( window.who_has_dcl );
			//console.log( new_div.onclick );
		}
	}
}

function design_rollout( div )
{
	//console.log( window.mouseyY );
	/*
	console.log('rollout' + window.dcl_mo + ', ' + window.who_has_dcl );
	if( document.getElementById )
	{
		setTimeout( function()
			{
				if( window.dcl_mo != true && window.who_has_dcl == div.getAttribute('post_id') )
				{
					if( div == document.getElementById('design_comment_link').parentNode )
					{
						div.removeChild(document.getElementById('design_comment_link'));
					};
					window.who_has_dcl = null;
				}
			},
			75
		);
	*/
	/*
		setTimeout(
			function()
			{
				if( div.getAttribute('has_cl') == 'true' && document.getElementById('design_comment_link').getAttribute('xyzy') != 'true' )
				{
					if( div == document.getElementById('design_comment_link').parentNode )
						div.removeChild(document.getElementById('design_comment_link'));
					div.setAttribute('has_cl','false');
				}
			},
			100
		);
	*/
	//}
}


//-----------------------------------------------------------------------------------------hide and show tumbles
function load_page( clicked )
{
	target = document.getElementById( clicked.getAttribute('type')+'_spill' );
	if( target && clicked.getAttribute('single') )
	{
		var ajax = create_ajax();
		ajax.onreadystatechange = function(){
			if( ajax.readyState == 4 ){
				if( ajax.status == 200 )
				{
					//alert( ajax.responseText );
					document.getElementById('bookface_spill').innerHTML = '';
					document.getElementById('tweet_spill').innerHTML = '';
					document.getElementById('spaz_spill').innerHTML = '';
					target.innerHTML = ajax.responseText;
					document.getElementById('design_posts').style.display = 'none';
					document.getElementById('tweet_spill').style.display = 'none';
					document.getElementById('bookface_spill').style.display = 'none';
					document.getElementById('spaz_spill').style.display = 'none';
					target.style.display = 'block';
				}
			}
		}
		//alert( clicked.getAttribute('single') );
		ajax_post( ajax, null, clicked.getAttribute('single') );
	}
}

function show_spill( clicked )
{
	//can't do anything
	if( !document.getElementById || !clicked.getAttribute('type') )
		return null;
	//-if we're going to design
	if( clicked.getAttribute('type') == 'design' )
	{
		document.getElementById('tweet_spill').style.display = 'none';
		document.getElementById('bookface_spill').style.display = 'none';
		document.getElementById('spaz_spill').style.display = 'none';
		document.getElementById('design_posts').style.display = 'block';
	}
	else 
	{
		//-if the single content matches what's been clicked on, but we're viewing design, switch to single without loading
		if( document.getElementById('single_content') && document.getElementById('single_content').getAttribute('post_id') == clicked.getAttribute('post_id') && document.getElementById('design_posts').style.display != 'none' )
		{
			document.getElementById('design_posts').style.display = 'none';
			document.getElementById('single_content').parentNode.parentNode.style.display = 'block';
		}
		else //-if it doesn't match, no matter what, pull it up
			load_page( clicked );
	}
}

function submit_comment_form()
{
	if( document.getElementById && document.getElementById('commentform') )
	{
		//cform = document.getElementById('commentform');
		
		post_value = '';
		inputs = Array();
		inputs = document.getElementById('commentform').getElementsByTagName('input');
		run = inputs.length;
		for( i=0; i<run; i++ )
			post_value += '&' + encodeURIComponent(inputs[i].id) + '=' + encodeURIComponent(inputs[i].value);
		post_value += '&comment=' + encodeURIComponent( document.getElementById('comment').value );
		
		var ajax = create_ajax();
		ajax.onreadystatechange = function(){
			if( ajax.readyState == 4 ){
				if( ajax.status == 200 )
				{
					//alert( ajax.responseText );
					document.getElementById('commentform').parentNode.parentNode.parentNode.innerHTML = ajax.responseText;
					//window.location.href = window.location.href;
				}
			}
		}
		//alert( post_value );
		ajax_post( ajax, post_value, '/wp-comments-post.php' );
	}
}

