var xml_request = false;

function makeXMLRequest(url, parameters) {
	xml_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xml_request = new XMLHttpRequest();
		if (xml_request.overrideMimeType) {
			xml_request.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xml_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xml_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!xml_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
//	alert("sending request");
//	alert('url is '+url+parameters);
//	xml_request.open('GET', url + parameters);
//	xml_request.send(null);
	xml_request.onreadystatechange = fill_poses;
	xml_request.open("POST", url, true);
	xml_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xml_request.setRequestHeader("Content-length", parameters.length);
	xml_request.setRequestHeader("Connection", "close");
	xml_request.send(parameters);
}

function fill_poses() {
	//alert('ready state is '+xml_request.readyState+', status is '+xml_request.status);
	if(xml_request.readyState != 4) return false;
	if(xml_request.status != 200) return false;
	var text = xml_request.responseText;
	if(!text) {
		return false;
	}
	document.getElementById('pose_library').innerHTML = text;
}

var page = 1;
var category = -1;
var type = 'all';

function update_poses(pg, tp) {
	// If we change the category drop-down, then 
	// "page" gets reset back to it's initial value. 
	// However, "type" stays the same. 
	var cat = document.getElementById('pose_category');
	if (cat) {
		if (cat.value != category) {
			category = cat.value;
			page = 1;
		}
	}
	// If we change the type, then we reset the page and the 
	// category back to their initial values
	if (tp != '') {
		type = tp
		category = -1;
		page = 1;
	}
	// If we changed the page, then the others stay the same
	if (pg > 0) {
		page = pg
	} 
	document.getElementById('pose_library').innerHTML = "<h3>Please wait... Loading Poses...</h3><div class='clear' style='background-image: none;'><!-- --></div>";
	//alert('page is '+page); 1
	//alert('type is '+type); all 
	//alert('category is '+category); -1
	makeXMLRequest('/community/my_yoga_journal/my_sequences/', 'action=custom&yj=get_poses&page='+page+'&pose_type='+type+'&pose_category='+category);
}

update_poses(1,'all');


/* function try_ajax() { */
/* 	this.attempt++; */
/* 	if(this.attempt > 20){ */
/* 		clearInterval(this.tid); */
/* 	} */
/* 	if(fields.email){ */
/* 		clearInterval(this.tid); */
/* 		this.the_function(); */
/* 	} */
/* } */
/*  */
/* function wait(){ */
/* 	var myself = this; */
/* 	function callMethod() { */
/* 		myself.try_ajax(); */
/* 	} */
/* 	this.tid = setInterval(callMethod, 500); */
/* } */
/*  */
/* function Ajax(the_function){ */
/* 	if(typeof(the_function) != 'function') return false; */
/* 	if(logged_out) return false; */
/* 	if( (typeof(fields) == 'object') && fields['id']){ */
/* 		//if we aren't cached, we already have the user information, so immediately do the_function() */
/* 		//instead of starting an ajax call */
/* 		//alert('non-cached call to the_function()'); */
/* 		the_function(); */
/* 	} */
/* 	else { */
/* 		this.the_function = the_function; */
/* 		this.tid; */
/* 		this.attempt = 0; */
/* 		this.try_ajax = try_ajax; */
/* 		this.wait = wait; */
/* 		this.wait(); */
/* 	} */
/* } */

function flip_tabs(activate) {
	var tabs = new Array('all', 'intermediate', 'advanced');
	for (var i=0; i<tabs.length; i++) {
		var tab = document.getElementById('tab_'+tabs[i]);
		if (activate == tabs[i]) {
			tab.className = 'selected';
		} else {
			tab.className = '';
		}
	}
}

