// <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;
var last_page = new Array();
var pagewhere = new Array();
var mrc = ''
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;
	if (pagewhere[id]){
		last_page[id] = pagewhere[id];
	} else if (!last_page[id]){
		last_page[id] ='post_page_1_'+id;
	}
	pagewhere[id] = '';
	if(type == 'page'){
		pagewhere[id] = 'post_page_'+extra+'_'+id;
		type = 'content';
	}
	if(type == 'paged'){
		where='inap_loop';
	}else{
		where = 'post_'+type+'_'+id;
	}



		dwhere = d.getElementById(where)

	if (dwhere.innerHTML.length == 0){
	 		inaprequest = new sack('http://www.closed-loop-marketing.com/blog/wp-content/plugins/inline-ajax-page/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){}

		//try{
			base = d.getElementById('post_addcomment_'+id).getElementsByTagName('input');
		//}catch(e){
		//	try{
		//		base = d.getElementById('post_comments_'+id)
		//	}catch(e){
			//	aelert('No comment form detected.');
		//	}
	//	}

			inaprequest = new sack('http://www.closed-loop-marketing.com/blog/wp-content/plugins/inline-ajax-page/inap-get-info.php');
			var x = base.length;
		var value = '';
		var name = '';
		for(i=0; i<x; i++){

			if(base[i].type != 'button'){
				if(base[i].type == 'text' || base[i].type == 'hidden' || base[i].type == 'password'){
						value =  base[i].value;
						name = base[i].name;
				}else if(base[i].type == 'checkbox'){
					if (base[i].checked) {
						value =  base[i].value;
						name = base[i].name;
					}
				}else if(base[i].type == 'select'){
						value =  base[i].value;
						name = base[i].name;
				}
			}
						inaprequest.setVar(name, value);
				}
	base = d.getElementById('post_addcomment_'+id).getElementsByTagName('textarea');
		var x = base.length;

		for(i=0; i<x; i++){
							inaprequest.setVar(base[i].name, base[i].value);
					
		}
	

	//	d.getElementById('commentform-'+id)
	 		
 		inaprequest.setVar('type', 'submit_form')
 		inaprequest.setVar('comment_post_ID', id);
 		inaprequest.element = 'submit_form_'+id;
		inaprequest.method = 'POST';
		inaprequest.onLoading = inap_submiting;
 		inaprequest.onCompletion = complete_submit;
 		inaprequest.runAJAX();
		inaprequest = null;

	}


function inap_paged(page,what){
	 		inaprequest = new sack('http://www.closed-loop-marketing.com/blog/wp-content/plugins/inline-ajax-page/inap-get-info.php');
 		inaprequest.method = 'POST';
 		inaprequest.setVar('type', 'paged')
 		inaprequest.setVar('pagenum', page);
		inaprequest.setVar('what', what)
 		inaprequest.element = 'inap_loop';
 		inaprequest.onCompletion = complete_paged;
 		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' && extra != '10101') || (last_extra[id] != extra && isNaN(extra)  == false && extra != '10101')){
		//This line removes the throbber, but slows it down a little so the reader is not confused.
		//We remove the throbber based on the relative parent of the throbber rather than a static parent.
		setTimeout("try{d.getElementById('throbber'+type+id).parentNode.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;
		if(dwhere.offsetTop > 0){
			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'){
		inap_toggle_content();
	}else{
						//inap_effects();
			dwhere.style.display= style1;
			}


	if(type == 'addcomment'){
		inap_toggle_addcomment();
	}
	if(!pagewhere[id]){
		try{d.getElementById('post_'+type+'_link_'+id).firstChild.data = link;}catch(e){}
	}else{
		try{d.getElementById('post_page_'+extra+'_'+id+'_link').style.fontWeight = 'bold';}catch(e){}
		if(last_page[id] !=pagewhere[id] )
			try{d.getElementById(last_page[id]+'_link').style.fontWeight = 'normal';}catch(e){}
	}

	if(type == 'comments' && mrc > 1){
		setTimeout("location.href= '#comment-'+mrc;",100);
	}
}


function inap_toggle_content(){
				dwhere.style.display= style1;
		d.getElementById('post_excerpt_'+id).style.display= style2;
			if(pagewhere[id]){

				d.getElementById(last_page[id]+'_link');

				d.getElementById(pagewhere[id]+'_link');

				d.getElementById(last_page[id]).style.display= 'none';

				d.getElementById(pagewhere[id]).style.display= 'block';

			}
		
}



function inap_toggle_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){}
}



function inap_loading(){
	var img = d.createElement('img')
	img.src="http://www.closed-loop-marketing.com/blog/wp-content/plugins/inline-ajax-page/throbber.gif";
	img.alt="Please hold now loading";
	img.id = "throbber"+type+id;
	if(pagewhere[id]){
		try{d.getElementById(pagewhere[id]+'_link').appendChild(img);}catch(e){}
	}else{
		try{d.getElementById('post_'+type+'_link_'+id).appendChild(img);}catch(e){}
	}
}

function inap_submiting(){
	var img = d.createElement('img')
	img.src="http://www.closed-loop-marketing.com/blog/wp-content/plugins/inline-ajax-page/throbber.gif";
	img.alt="Please hold now loading";
	img.id = "throbbersubmit"+id;
		try{d.getElementById('submit_'+id).appendChild(img);}catch(e){}
}
/**
*
*These are the built in effects.
*
**/

function complete_submit(){
setTimeout("complete_submit_2()",5);
}

function complete_submit_2(){

//if there was an error the message is really long...so:
	if(d.getElementById('submit_form_'+id).innerHTML.match(/title>/)){
	//let's parse the error message out
		var re = new RegExp('<p>(.*?)</p>','gmi');
		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.
		var message = d.getElementById('submit_form_'+id).innerHTML.split('-');
		d.getElementById('submit_form_'+id).innerHTML = message[0];
		mrc = message[1];
		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 =''}
	//alert(message[1])
	// 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 the submit button
try{d.getElementById('submit_'+id).disabled = false;} catch(e){}
try{d.getElementById('throbbersubmit'+id).parentNode.removeChild(d.getElementById('throbbersubmit'+id));}catch(e){};

}

function complete_paged(){
window.parent.scrollTo(0,d.getElementById('inap_loop').offsetTop);
}

 