
// <script>(to trick editors into using javascript syntax)
/*
Inline Ajax (front) page
(C) Anthologyoi.com
You can find the latest information about this plugin and javascript:
http://anthologyoi.com/plugins/

As an aside, my theory with javascript is that if it can possibly fail, some user will manage to do it.
So I try and catch most everything.

The actual file for this has php mixed throughout.
*/
var d=document;
function inap_request(the_id,the_type,show_text,hide_text,extras){
	if( the_id )
		id =  the_id;
	if( the_type )
		type =  the_type;
	if( hide_text )
		hide =  hide_text;
	if( show_text )
		show =  show_text;
		extra =  extras;

	where = 'post_'+type+'_'+id;
	dwhere = d.getElementById(where)

	if (dwhere.innerHTML.length == 0){
	 		inaprequest = new sack('http://www.ambientacionyeventos.com/wp-content/plugins/inap/get_info.php');
 		inaprequest.method = 'POST';
 		inaprequest.setVar('id', id)
 		inaprequest.setVar('type', type)
 		inaprequest.setVar('show', show_text)
 		inaprequest.setVar('hide', hide_text)
 		inaprequest.element = where;
 		inaprequest.onLoading = inap_loading;
 		inaprequest.onCompletion = inap_toggle;
 		inaprequest.runAJAX();
 		inaprequest = null;
		}else{
		inap_toggle();
	}
}

function submit_form(the_id,the_type,show_text,hide_text){
	if( the_id )
		id =  the_id;
	if( the_type )
		type =  the_type;
	if( hide_text )
		hide =  hide_text;
	if( show_text )
		show =  show_text;
		try{d.getElementById('submit_'+id).disabled = true;} catch(e){}
	 		inaprequest = new sack('http://www.ambientacionyeventos.com/wp-content/plugins/inap/get_info.php');
 		inaprequest.method = 'POST';
 	// These have to be in a try because otherwise we have to check if the user is logged in.
 	// This is easier.
		try{
			inaprequest.setVar('author', d.getElementById('author_'+id).value);
			inaprequest.setVar('email', d.getElementById('email_'+id).value);
			inaprequest.setVar('url', d.getElementById('url_'+id).value);
		} catch(e){}
 		inaprequest.setVar('comment', d.getElementById('comment_'+id).value);
 		inaprequest.setVar('type', 'submit_form')
 		inaprequest.setVar('comment_post_ID', id);
 		inaprequest.element = 'submit_form_'+id;
 		inaprequest.onCompletion = complete_submit;
 		inaprequest.runAJAX();
		inaprequest = null;

	}

/**
* This is the universal toggler.
* All elements that need to be toggled go through here
* The 'extra' is just a way to test which link the add comment used.
* For everything else it is always the same so there is no problems.
**/
var last_extra = new Array(); 


function inap_toggle(){
var winHeight = window.innerHeight;
	if(!winHeight){
		//yet another IE fix
		winHeight = d.documentElement.clientHeight;
	}
	
	if (dwhere.style.display == 'none' || (last_extra[id] != extra && isNaN(extra)  == false)){
		setTimeout("try{d.getElementById('post_'+type+'_link_'+id).removeChild(d.getElementById('throbber'+type+id))}catch(e){}",50);
		 style1 = 'block';
		 style2 = 'none';
		var link = link2 =hide;
	}else{
		 style1 = 'none';
		 style2 = 'block';
		var link = link2 =show;
		window.parent.scrollTo(0,dwhere.offsetTop - winHeight/4);
	}

	// If we are doing content it has two different methods and two divs rather than one
	if(type == 'content'){
					dwhere.style.display= style1;
			d.getElementById('post_excerpt_'+id).style.display= style2;
			}else{
						//inap_effects(); 

			dwhere.style.display= style1;
			}


	if(type == 'addcomment'){
		if(isNaN(extra)  == false){
			last_extra[id] = extra;
		}

		if(extra == 2){
			link = show;
			dwhere.parentNode.insertBefore(dwhere, d.getElementById('post_comments_'+id).nextSibling);
		}else{
			link2 = show;
			try{dwhere.parentNode.insertBefore(dwhere, d.getElementById('post_comments_'+id));}catch(e){}
		}

		try{d.getElementById('submit_'+id).disabled = false;} catch(e){}
		try{d.getElementById('post_'+type+'_link2_'+id).firstChild.data = link2;}catch(e){}
	}

	d.getElementById('post_'+type+'_link_'+id).firstChild.data = link;
}

function inap_loading(){
	var img = d.createElement('img')
	img.src="http://www.ambientacionyeventos.com/wp-content/plugins/inap/throbber.gif";
	img.alt="Please hold now loading";
	img.id = "throbber"+type+id;
	d.getElementById('post_'+type+'_link_'+id).appendChild(img);
}


function complete_submit(){

//if there was an error the message is really long...so:
	if(d.getElementById('submit_form_'+id).innerHTML.length > 100){
	//let's parse the error message out
		var re = new RegExp('<p>(.*?)</p>');
		var m = re.exec(d.getElementById('submit_form_'+id).innerHTML);
		d.getElementById('submit_form_'+id).innerHTML = m[1]
	}else{
		//Okay it worked, lets move on.
		try{
			if (d.getElementById('post_comments_none_'+id).style.display != 'none'){
			
				d.getElementById('post_comments_none_'+id).style.display = 'none';
				
				if(show){
					d.getElementById('post_comments_link_'+id).innerHTML = show;
				}else{
					d.getElementById('post_comments_link_'+id).innerHTML = 'Show Comments';
				}
			}

		}catch(e){ var string =''}
	
	// Get rid of the box.
		type = 'addcomment';
		last_extra[id] = 'undefined';
		inap_request();
		
		d.getElementById('post_comments_'+id).innerHTML = '';
		d.getElementById('post_comments_'+id).style.display = 'none';

		type = 'comments';
		inap_request();
	}

// no matter what we want to reenable to submit button
try{d.getElementById('submit_'+id).disabled = false;	} catch(e){}

}