function stripHash(str){
	return str.replace(/#.+/g, '');
}

function numberOnly(str){
	if (str) {
		str = str.replace(/\D/g, '');
	} else {
		str = null;
	}
	return parseInt(str);
}

// this function will find the highest 'No-#' id on the page and return it
// accepts a high limit to start testing from as an argument
function highestId(str){
	if (str) {
		$i = str;
	} else {
		$i = 99;	
	}
	while ($i>0) {
		if (document.getElementById('No-' + $i)) {
			return $i;
		}
		$i=$i-1;
	}
}

// http://codingforums.com/archive/index.php?t-102962.html
function getNearestId(str) {
	while (str && str.id == "") {
		str = str.parentNode;
	}
	return str.id;
}

// http://www.thescripts.com/forum/thread587235.html
function navigateNext(str) {
	var newAnchor;
	if (navigateNext.from) {
		newAnchor = numberOnly(navigateNext.from) + 1;
	}
	if (isNaN(newAnchor) && str) {
		str = getNearestId(str);
		newAnchor = numberOnly(str) + 1;
	}
	if (isNaN(newAnchor)) {
		newAnchor = numberOnly(window.location.hash.substring(1)) + 1;
		if (isNaN(newAnchor)) {
			return;
			// uncomment next line, comment previous line if id order is ascendeing
			// newAnchor = 2;
		}
	}
	newAnchor = "No-" + newAnchor;
	if (document.getElementById(newAnchor)) {
		HtinyScrolling.currentBlock = document.getElementById(newAnchor);   
		if(!HtinyScrolling.currentBlock) return;
		HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock); 
		window.scrollTo(HtinyScrolling.requestedX, 0);
		if(typeof XULDocument != 'undefined') {
			window.location.replace(stripHash(window.location.href) + "#" + newAnchor);
		}
		navigateNext.from = newAnchor;
	} else {
		void(0);
	}
}

function navigatePrevious(str) {
	var newAnchor;
	if (navigateNext.from) {
		newAnchor = numberOnly(navigateNext.from) - 1;
	}
	if (isNaN(newAnchor) && str) {
		str = getNearestId(str);
		newAnchor = numberOnly(str) - 1;
	}
	if (isNaN(newAnchor)) {
		newAnchor = numberOnly(window.location.hash.substring(1)) - 1;
		if (isNaN(newAnchor)) {
			newAnchor = highestId() - 1;
			// uncomment next line, comment previous lines if id order is ascendeing
			// newAnchor = 1;
		}
	}
	newAnchor = "No-" + newAnchor;
	if (document.getElementById(newAnchor)) {
		HtinyScrolling.currentBlock = document.getElementById(newAnchor);   
		if(!HtinyScrolling.currentBlock) return;
		HtinyScrolling.requestedX = HtinyScrolling.getElementXpos(HtinyScrolling.currentBlock); 
		window.scrollTo(HtinyScrolling.requestedX, 0);
		if(typeof XULDocument != 'undefined') {
			window.location.replace(stripHash(window.location.href) + "#" + newAnchor);
		}
		navigateNext.from = newAnchor;
	} else {
		void(0);
	}
}

// SHORTCUTS

shortcut.add("left",function() {
	navigateNext();
},{
	'type':'keydown',
	'disable_in_input':true,
	'target':document
});

shortcut.add("right",function() {
	navigatePrevious();
},{
	'type':'keydown',
	'disable_in_input':true,
	'target':document
});


// AJAX

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(action) {
    http.open('get', 'flip.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}