// <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.dolalay.com/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.dolalay.com/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.dolalay.com/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.dolalay.com/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.dolalay.com/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);
}

 var delay = 0;
function inap_cleanhtml(text){


return text;
}
	var smilies = new Array('\\:mrgreen\\:', '\\:neutral\\:', '\\:twisted\\:', '\\:arrow\\:', '\\:shock\\:', '\\:smile\\:', '\\:\\?\\?\\?\\:', '\\:cool\\:', '\\:evil\\:', '\\:grin\\:', '\\:idea\\:', '\\:oops\\:', '\\:razz\\:', '\\:roll\\:', '\\:wink\\:', '\\:cry\\:', '\\:eek\\:', '\\:lol\\:', '\\:mad\\:', '\\:sad\\:', '8-?\\)', '8-?O', '\\:-?\\(', '\\:-?\\)', '\\:-?\\?', '\\:-?D', '\\:-?P', '\\:-?o', '\\:-?x', '\\:-?\\|', ';-?\\)', '\\:\\!\\:', '\\:\\?\\:');
	var smiliesfiles = new Array('mrgreen', 'neutral', 'twisted', 'arrow', 'eek', 'smile', 'confused', 'cool', 'evil', 'biggrin', 'idea', 'redface', 'razz', 'rolleyes', 'wink', 'cry', 'surprised', 'lol', 'mad', 'sad', 'cool', 'eek', 'sad', 'smile', 'confused', 'biggrin', 'razz', 'surprised', 'mad', 'neutral', 'wink', 'exclaim', 'question');
	var smiliesalt = new Array(':mrgreen:', ':neutral:', ':twisted:', ':arrow:', ':shock:', ':smile:', ':???:', ':cool:', ':evil:', ':grin:', ':idea:', ':oops:', ':razz:', ':roll:', ':wink:', ':cry:', ':eek:', ':lol:', ':mad:', ':sad:', '8-)', '8-O', ':-(', ':-)', ':-?', ':-D', ':-P', ':-o', ':-x', ':-|', ';-)', ':!:', ':?:');

	var x = smiliescount = smilies.length;
	var smil_reg = new Array();

	while(x--){
			smil_reg[x] = new RegExp('(>|\\s|^)'+smilies[x]+'(\\s|$|<)', "gm")
	}

	function inap_convertsmilies(text){
	var x = smiliescount;
;var start = new Date()
		while(x--){
			if(text.match(smil_reg[x])){
				text = text.replace(smil_reg[x],'$1<img src="http://www.dolalay.com/wp-includes/images/smilies/icon_'+smiliesfiles[x]+'.gif" alt="'+smiliesalt[x]+'" class="wp-smiley" />$2');
			}
		}
//text += 'Smilies:'+((new Date()).getTime() - start.getTime());
	return text;
	}


		var characters = new Array('---', ' -- ', '--', 'xn&#8211;', '\\.\\.\\.', '\\s\\(tm\\)','(\\d+)"',"(\\d+)'","(\\w)('{2}|\")",'(`{2}|")(\\w)',"(\\w)'","'([\\s.]|\\w)");
		var replacements = new Array('&#8212;', ' &#8212; ', '&#8211;', 'xn--', '&#8230;',' &#8482;','$1&#8243;', '$1&#8242;','$1&#8221;','&#8220;$2','$1&#8216;','&#8217;$1');
		var char_regex = new Array()
		var charcount =characters.length
		for(x=0; x< charcount; x++){
			char_regex[x] = new RegExp(characters[x], "g")
		}

//
// Direct translation of wptexturize from php to javascript
// Cleaned and optimized for speed and actual usage.
function  inap_js_wptexturize(text) {
	var next = true;
	var output = '';
	var curl = '';
var start = new Date()
		text = text.replace(/(<[^>]*>)/g, '@%@%@$1@%@%@');
 		var textarr = text.split('@%@%@');
 		var stop = textarr.length;
		var i = 0;
	while (stop > i) {
		curl = textarr[i];
			if (curl.charAt(0) != '<' && next) { // If it's not a tag

				var x = charcount;
				while(x--){
					if(curl.match(char_regex[x])){
						curl = curl.replace(char_regex[x],replacements[x]);
					}
				}
			} else if ( curl.indexOf('<code') == 0 || curl.indexOf('<pre') == 0) {
				next = false;
			} else {
				next = true;
			}
		curl = curl.replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/g', '&#038;$1');
		output += curl;
		i++;
	}
//output += 'texturize:'+((new Date()).getTime() - start.getTime());
return output;

}
function inap_js_wpautop(pee) {

var start = new Date()
       pee = pee + "\n\n";
       pee = pee.replace(/<br \/>\s*<br \/>/gi, "\n\n");
       pee = pee.replace(/(<(?:dl|dd|dt|ul|ol|li|pre|blockquote|p|h[1-6])[^>]*>)/gi, "\n$1");
       pee = pee.replace(/(<\/(?:dl|dd|dt|ul|ol|li|pre|blockquote|p|h[1-6])>)/gi, "$1\n\n");
       pee = pee.replace(/\r\n|\r/g, "\n");
       pee = pee.replace(/\n\s*\n+/g, "\n\n");
       pee = pee.replace(/([\s\S]+?)\n\n/gm, '<p>$1 </p>\n');
       pee = pee.replace(/<p>\s*?<\/p>/gi, '');
       pee = pee.replace(/(<p>)*\s*(<\/?(?:dl|dd|dt|ul|ol|li|pre|blockquote|p|h[1-6]|hr)[^>]*>)\s*(<\/p>)*/gi, "$2");
       pee = pee.replace(/<p>(<li.+?)<\/p>/i, "$1");
       pee = pee.replace(/<p><blockquote([^>]*)>/gi, "<blockquote$1><p>");
       pee = pee.replace(/<\/blockquote><\/p>/gi, '</p></blockquote>');
       pee = pee.replace(/\s*\n/gi, " <br />\n");
       pee = pee.replace(/(<\/?(?:dl|dd|dt|ul|ol|li|pre|blockquote|p|h[1-6])[^>]*>)\s*<br \/>/gi, "$1");
       pee = pee.replace(/'<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1');
       pee = pee.replace(/^((?:&nbsp;)*)\s/gm, '$1&nbsp;');

//pee += 'pee:'+((new Date()).getTime() - start.getTime());
     return pee;
}

function inap_update_preview(pid){
	var comment = '';
		try{	comment = document.getElementById('comment_'+pid).value;}catch(e){}
		if(comment != ''){
			comment = inap_js_wpautop(comment);
			comment = inap_js_wptexturize(comment);
			comment = inap_cleanhtml(comment);
			comment = inap_convertsmilies(comment);							document.getElementById('add_comment_live_preview_'+pid).innerHTML = comment;
		}

}

function inap_live_preview(pid) {
	if(delay >= 0){
		inap_update_preview(pid);
		delay = 0;
	}else{
		delay++;
	}
}


