/*<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://www.gamedezigner.ru/wordpress/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){
	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){}

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


			inaprequest = new sack('http://www.gamedezigner.ru/wordpress/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://www.gamedezigner.ru/wordpress/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;
		link2 = show;
 		force=0;
 	}else{
 		 style1 = 'none';
 		 style2 = 'block';
 		 link  =show; link2 = hide;
 	}

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

		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{
			d.getElementById('post_content_link'+'_'+id).firstChild.data = link;
		}
	}else if(type == 'addcomment'){

		inap_toggle_addcomment();
		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];
// 		if(show != 'off'){
// 			if(info[0]){
// 			d.getElementById('post_addcomment_link'+info[0]+'_'+id).firstChild.data = link2;}
// 
// 			if(last_extra[id]){
// 			}
// 		}
	}else{
		
		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(){
				d.getElementById('post_page_'+last_page[id]+'_'+id).style.display= 'none';
			d.getElementById('post_page_'+cur_page[id]+'_'+id).style.display= 'block';
	
}





function inap_effects_pick(){

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

	var img = d.createElement('img')
	img.src="http://www.gamedezigner.ru/wordpress/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://www.gamedezigner.ru/wordpress/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;
		if(m = re.exec(d.getElementById('submit_form_'+id).innerHTML)){
		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 m = d.getElementById('post_comments_'+id).innerHTML
		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 quicktags// new edit toolbar used without permission
// 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]);
	}
}


function edSpell(the_id) {
if (the_id){
 id = the_id;}
var myField = d.getElementById('comment_'+id);
	var word = '';
	if (document.selection) {
		myField.focus();
	    var sel = document.selection.createRange();
		if (sel.text.length > 0) {
			word = sel.text;
		}
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		if (startPos != endPos) {
			word = myField.value.substring(startPos, endPos);
		}
	}
	if (word === '') {
		word = prompt('Enter a word to look up:', '');
	}
	if (word !== null && /^\w[\w ]*$/.test(word)) {
		window.open('http://www.answers.com/' + escape(word));
	}
}

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