/*<script>*/
/*
Inline Ajax Page
(C) Anthologyoi.com
*/

var d=document;
var last_page = [];
var cur_page = [];
var last_show = [];
var mrc = '';
var force = 0;
var info = [];

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(type=='addcomment' && extra){
		if(extra.match(';')){
			info = extra.split(';');
		}
		if(isNaN(last_extra[id])){last_extra[id] = 1;}
		if(isNaN(info[0])){
			info[0] = last_extra[id];
			info[1] = 0;
		}
	}else if(type=='content'){
		if (last_page[id] === 0 || isNaN(last_page[id])){
			if(isNaN(extra)){cur_page[id] = 2;}else{cur_page[id] = extra;}
			last_page[id] = 1;
			force = 1;
		}else{
			if(isNaN(extra) && cur_page[id] == 1){extra = 2;}else if (isNaN(extra) && cur_page[id] == 2){extra = 1;}
			last_page[id] = cur_page[id];
			cur_page[id] =extra;
		}
	}

	if (dwhere.innerHTML.length === 0 || force ==1){

					inaprequest = new sack('http://coderewind.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('extra', extra);
			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,comment_hide){
	if( the_id ){
		id =  the_id;}
	if( the_type ){
		type =  the_type;}
	if( hide_text ){
		hide =  hide_text;}
	if( show_text ){
		show =  show_text;}
	if( comment_hide ){
		com_hide = comment_hide;}

		try{d.getElementById('submit_'+id).disabled = true;} catch(e){}

		base = d.getElementById('post_addcomment_'+id).getElementsByTagName('input');


			inaprequest = new sack('http://coderewind.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');
		x = base.length;

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

	
			inaprequest.setVar('type', 'submit_form');
			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://coderewind.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;


	}
var last_extra = [];


function inap_toggle(){
var winHeight = window.innerHeight;
	if(!winHeight){
		//yet another IE fix
		winHeight = d.documentElement.clientHeight;
	}



	setTimeout("try{d.getElementById('throbber'+type+id).parentNode.removeChild(d.getElementById('throbber'+type+id));}catch(e){}",100);

 	if (dwhere.style.display == 'none' || force==1 || (last_extra[id] != info[0] && type == 'addcomment')){
 		style1 = 'block';
 		style2 = 'none';
		link =hide;
 		force=0;
		var style= '_hide';
 	}else{
 		 style1 = 'none';
 		 style2 = 'block';
 		 link =show;
		var style = '';
 	}

	if(type == 'content'){
		inap_toggle_content();

		var l = d.getElementById('post_content_link'+'_'+id);
		if(show == 'off'){
			d.getElementById('post_page_'+cur_page[id]+'_'+id+'_link').style.fontWeight = 'bold';
			if(last_page[id] !=cur_page[id] ){
			d.getElementById('post_page_'+last_page[id]+'_'+id+'_link').style.fontWeight = 'normal';}
		}else{

			if(l.firstChild.data == hide){
				l.firstChild.data = show;
				l.className = 'post_content_link';
			}else{
				l.firstChild.data = hide;
				l.className = 'post_content_link_hide';
			}

		}
	}else if(type == 'addcomment'){

		inap_toggle_addcomment();

		if(info[0]){
			if(style1 == 'none'){
				d.getElementById('post_addcomment_link'+info[0]+'_'+id).firstChild.data = show;
			}else{
				try{
					if(last_show[id]){
					d.getElementById('post_addcomment_link'+last_extra[id]+'_'+id).firstChild.data = last_show[id];}
				}catch(e){}
				d.getElementById('post_addcomment_link'+info[0]+'_'+id).firstChild.data = hide;
			}
			last_show[id] = show;
			last_extra[id] = info[0];
		}
	}else{
		if(d.getElementById('post_'+type+'_link'+'_'+id)){
			d.getElementById('post_'+type+'_link'+'_'+id).className = 'post_' + type + '_link' + style;
			d.getElementById('post_'+type+'_link'+'_'+id).firstChild.data = link;
		}
		inap_effects_pick();
	}



	if(type == 'comments' && mrc > 1){
		setTimeout("location.href= '#comment-'+mrc;",100);
	}
		if(dwhere.offsetTop > 0){
			window.parent.scrollTo(0,dwhere.offsetTop - winHeight/4);
		}
}

function inap_toggle_addcomment(){

		
	if(style1 == 'block' && force===0){
		dwhere.style.display = 'none';
		d.getElementById('post_addcomment_link'+info[0]+'_'+id).parentNode.insertBefore(dwhere, d.getElementById('post_addcomment_link'+info[0]+'_'+id).nextSibling);

		if(dwhere.style.display != 'block'){
	//	d.getElementById('post_addcomment_link'+info[0]+'_'+id).firstChild.data = show;
			inap_effects_pick();
		}
	}else{
		style1 = 'none';
		inap_effects_pick();
	}
try{d.getElementById('submit_'+id).disabled = false;} catch(e){}
}

function inap_toggle_content(){
			inap_effects('Fade');
	
}





function inap_effects_pick(){

			/*inap_effects();*/
		dwhere.style.display= style1;
	
}
function inap_loading(){

	var img = d.createElement('img');
	img.src="http://coderewind.com/blog/wp-content/plugins/inline-ajax-page/js/throbber.gif";
	img.alt="Please hold now loading";
	img.id = "throbber"+type+id;
	if(cur_page[id] && show == 'off'){
		try{d.getElementById('post_page_'+cur_page[id]+'_'+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://coderewind.com/blog/wp-content/plugins/inline-ajax-page/js/throbber.gif";
	img.alt="Please hold now loading";
	img.id = "throbbersubmit"+id;
		try{d.getElementById('submit_'+id).appendChild(img);}catch(e){}
}

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>|(Error)/)){
		var re = new RegExp('<p>(.*?)</p>','gmi');
		
		var m = re.exec(d.getElementById('submit_form_'+id).innerHTML);
		if(m){
		d.getElementById('submit_form_'+id).innerHTML = m[1];}
	}else{
		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 ='';}

// Set up for the reload.
		d.getElementById('post_comments_'+id).parentNode.insertBefore(d.getElementById('post_addcomment_'+id), d.getElementById('post_comments_'+id));
		info[1] = 0;
		force = 1;
		d.getElementById('post_addcomment_'+id).style.display='none';

		try{
			var i = d.getElementById('post_comments_'+id).innerHTML;
			hide = com_hide;
			type = 'comments';
			inap_request();
		}catch(e){}

	}

	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);
}


// start effects/* <script>(to trick editors into using javascript syntax)*/
var the_count = 0;
var timeout = 100;
var effect = '';
/*
we use these whole function rather than a loop to keep it from freezing the browser window.
that and by splitting it into many functions it is extremely easy to add functionality.
Purpose: Shows and Hides the excerpt and content
*/
function inap_effects(efct){
effect = efct;

	if(type == 'content' && cur_page[id]){
		inap_effects_hide('pagedcontent');
		//inap_shrink('pagedcontent');
	}else if(style1 == 'block'){
		inap_effects_show();
	}else{
		inap_effects_hide();
	}
}

function inap_effects_hide(mode){
the_count += 1;

	if(the_count == 1){
		inap_effects_hide_f(mode);}

	inap_effects_do('neg', f);

	if(the_count < 10){
		setTimeout("inap_effects_hide('"+mode+"');", timeout);
	}else{
	the_count = 0;
	inap_effects_continue(mode, f);

	}
}

function inap_effects_hide_f(mode){
	if(mode == 'pagedcontent'){
		f = d.getElementById('post_page_'+last_page[id]+'_'+id);
	}else{
		f = dwhere;
	}
	hght = f.offsetHeight;
}

function inap_effects_continue(mode, f){
/* clean up:*/
if(effect =='SlideUp2'){
	f.style.height = hght+'px';}
if(effect == 'Expand'){
	f.style.lineHeight = '100%';}
		if(mode == 'pagedcontent'){
			f.style.display = 'none';
			inap_effects_show(mode);
		}else{
			dwhere.style.display = style1;
			inap_effects_show(mode);
		}
}

function inap_effects_show(mode){
	the_count += 1;

	if(the_count == 1){
	inap_effects_show_f(mode);}

	inap_effects_do('pos', f);

	if(the_count < 10){
		setTimeout("inap_effects_show('"+mode+"');", timeout);
	}else{
/*clean up*/
if(effect =='SlideUp'){
	f.style.height = hght+'px';}
if(effect == 'Expand'){
	f.style.lineHeight = '100%';}

	the_count = 0;

	}
}

function inap_effects_show_f(mode){
	if(mode == 'pagedcontent'){
		f = d.getElementById('post_page_'+cur_page[id]+'_'+id);
	}else{
		f = dwhere;
	}
	f.style.display = 'block';

/*These ensure that almoat instantaneously the new text has the style. These ensure that most users can't see the normal text.*/
	if(effect == 'ScrollLeft'){
		f.style.marginLeft=80+'%';
	}
	if(effect == 'Expand'){
		f.style.lineHeight = '300%';
		f.style.letterSpacing = '1em';
	}

	hght = f.offsetHeight;
}
function inap_effects_do(dir,f){
var min = 0;
switch (effect){
case 'Expand':
f.style.overflow = 'hidden';
	if(dir == 'neg'){
		f.style.letterSpacing = (the_count*3)+'px';
		f.style.lineHeight = 100+(the_count*3)*10+'% !important;';
	}else{
		f.style.letterSpacing = (10-the_count)*2+'px';
		f.style.lineHeight = (20-the_count)*10+'% !important;';

	}
break;
case 'SlideUp':
f.style.overflow = 'hidden';
	if(dir == 'neg'){
		f.style.height = (f.offsetHeight - Math.floor((hght)/(10-the_count)))+'px';
	}else{
		f.style.height = Math.floor((hght)/(10-the_count))+'px';
	}
break;
case 'ScrollLeft':
	if(!window.innerHeight){
		min = 2;}
	if(dir == 'neg'){
		f.style.marginLeft=((the_count - min)*10)+'%';
	}else{
		f.style.marginLeft=(80-(the_count)*8)+'%';
	}
break;
case 'Fade':
	if(dir == 'neg'){
		f.style.opacity = (10 -the_count)/10;
		f.style.filter = 'alpha(opacity='+(10 -the_count)/0.1+')';
	}else{
		f.style.opacity = the_count/10;
		f.style.filter = 'alpha(opacity='+(the_count)/0.1+')';
	}
break;
default :
	if(dir == 'neg'){
		f.style.opacity = (10 -the_count)/10;
		f.style.filter = 'alpha(opacity='+(10 -the_count)/0.1+')';
	}else{
		f.style.opacity = the_count/10;
		f.style.filter = 'alpha(opacity='+(the_count)/0.1+')';
	}
}


}// start quicktags// new edit toolbar used without permission
// This file has been edited from its original state
// The original author should not be held responsible for any bugs.
// Originally by Alex King
// http://www.alexking.org/


var edButtons = [];
var edOpenTags = [];

function edButton(i, display, tagStart, tagEnd, access, open) {
	this.id = i;				// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.access = access;		// access key
	this.open = open;			// set to -1 if tag does not need to be closed
}

function zeroise(number, threshold) {
	// FIXME: or we could use an implementation of printf in js here
	var str = number.toString();
	if (number < 0) { str = str.substr(1, str.length); }
	while (str.length < threshold) { str = "0" + str; }
	if (number < 0) { str = '-' + str; }
	return str;
}

var now = new Date();
var datetime = now.getUTCFullYear() + '-' +
zeroise(now.getUTCMonth() + 1, 2) + '-' +
zeroise(now.getUTCDate(), 2) + 'T' +
zeroise(now.getUTCHours(), 2) + ':' +
zeroise(now.getUTCMinutes(), 2) + ':' +
zeroise(now.getUTCSeconds() ,2) +
'+00:00';

edButtons[edButtons.length] = new edButton('ed_strong','b','<strong>','</strong>','b');
edButtons[edButtons.length] = new edButton('ed_em','i','<em>','</em>','i');
edButtons[edButtons.length] = new edButton('ed_link','link','','</a>','a'); // special case
edButtons[edButtons.length] = new edButton('ed_block','b-quote','\n\n<blockquote>','</blockquote>\n\n','q');
edButtons[edButtons.length] = new edButton('ed_img','img','','','m',-1); // special case
edButtons[edButtons.length] = new edButton('ed_ul','ul','<ul>\n','</ul>\n\n','u');
edButtons[edButtons.length] = new edButton('ed_ol','ol','<ol>\n','</ol>\n\n','o');
edButtons[edButtons.length] = new edButton('ed_li','li','\t<li>','</li>\n','l');
edButtons[edButtons.length] = new edButton('ed_code','code','<code>','</code>','c');
edButtons[edButtons.length] = new edButton('ed_quote','Quote','<q>','</q>','');


function edAddTag(button) {
	if(!edOpenTags[id]){
		edOpenTags[id] = [];}

	if (edButtons[button].tagEnd !== '') {
		edOpenTags[id][edOpenTags[id].length] = button;
		document.getElementById(edButtons[button].id + '_'+ id).value = '/' + document.getElementById(edButtons[button].id + '_'+ id).value;
	}
}

function edRemoveTag(button) {

	for (i = 0; i < edOpenTags[id].length; i++) {
		if (edOpenTags[id][i]== button) {
			edOpenTags[id].splice(i, 1);
			document.getElementById(edButtons[button].id + '_'+ id).value = 		document.getElementById(edButtons[button].id + '_'+ id).value.replace(/\//g, '');
		}
	}
}

function edCheckOpenTags(button) {
	if(!edOpenTags[id]){
		edOpenTags[id] = [];}

	var tag = 0;
	for (i = 0; i < edOpenTags[id].length; i++) {
		if (edOpenTags[id][i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}

function edCloseAllTags(the_id) {
	if( the_id){
		id = the_id;}
	if(!edOpenTags[id]){
		edOpenTags[id] = [];}

	var count = edOpenTags[id].length;
	for (o = 0; o < count; o++) {
		edInsertTag(id,edOpenTags[id][edOpenTags[id].length - 1]);
	}
}

// insertion code
function edInsertTag(the_id,i) {
if (the_id){
 id = the_id;}

var myField = document.getElementById('comment_'+id);

	//IE support
	if (document.selection) {
		myField.focus();
	    sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd === '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;
		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
			              + edButtons[i].tagStart
			              + myField.value.substring(startPos, endPos)
			              + edButtons[i].tagEnd
			              + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(i) || edButtons[i].tagEnd === '') {
				myField.value = myField.value.substring(0, startPos)
				              + edButtons[i].tagStart
				              + myField.value.substring(endPos, myField.value.length);
				edAddTag(i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos) +
								edButtons[i].tagEnd  +
								myField.value.substring(endPos, myField.value.length);
				edRemoveTag(i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		if (!edCheckOpenTags(i) || edButtons[i].tagEnd === '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(i);
		}
		myField.focus();
	}
}

function edInsertContent(myValue) {
var myField = d.getElementById('comment_'+id);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		              + myValue
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}

function edInsertLink(the_id, i) {
if (the_id){
 id = the_id;}

	if (!edCheckOpenTags(i)) {
		var URL = prompt('Enter the URL' ,'http://');
		if (URL) {
			edButtons[i].tagStart = '<a href="' + URL + '">';
			edInsertTag(the_id,i);
		}
	}
	else {
		edInsertTag(the_id,i);
	}
}

function edInsertImage(the_id,myField) {
	if (the_id){
  id = the_id;}

	var myValue = prompt('Enter the URL of the image', 'http://');
	if (myValue) {
		myValue = '<img src="'
				+ myValue
				+ '" alt="' + prompt('Enter a description of the image', '')
				+ '" />';
		edInsertContent(myValue);
	}
}
