var gBajax = createRequest();

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

function splitResponse(response) {
	var responseArray = response.split('|~');
	var newArray = new Array();
	for( var i=0; i<responseArray.length; i++ ) {
		tempArray = responseArray[i].split(':~');
		newArray[tempArray[0]] = tempArray[1];
	}
	return newArray;
}

function changeHero(hero_id, direction) {
	
	gBajax.open('get','ajax.php?action=hero&id='+hero_id+'&direction='+direction);
	gBajax.onreadystatechange = changeHeroResponse;
   	gBajax.send(null);
	/*document.getElementById('hero_page').value = hero_id;
	document.hero.submit();*/
}
function changeHeroResponse(){
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;
		
       	//process the response
       	if( response ) {
			var hero = splitResponse(response);
			
			hero_image = '<div id="hero_img" style="background: url('+hero["image"]+');">';
			hero_image += '<div id="arrows">';
			hero_image += '<a href="javascript:void(null)" onclick="changeHero('+hero["id"]+', \'down\')"><img src="images/slideshow_left.gif" /></a><a href="javascript:void(null)" onclick="changeHero('+hero["id"]+', \'up\')"><img src="images/slideshow_right.gif" /></a>';
			hero_image += '</div>';
			hero_image += hero["image_link"];
			hero_image += '</div>';
			
			document.getElementById('hero_img_container').innerHTML = hero_image;
			
			
			if( hero["image"] ) {
				document.getElementById('hero_content').innerHTML = hero["desc"]
			}
			else {
				document.getElementById('hero_content').innerHTML = '';
			}
			
			
       	}
    }
}
/***********************************************************
* gallery remove functions
***********************************************************/
function galleryRemove(id) {
	check = confirm('Are you sure you want to remove this gallery image from the system?');
	if( check ) {
		gBajax.open('get','ajax_gallery_delete.php?id='+id);
		gBajax.onreadystatechange = galleryRemoveResponse;
		gBajax.send(null);
	}
}
function galleryRemoveResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
	       	document.getElementById('gallery_items').innerHTML = response;
	       	resort();
       	}
    }
}


/***********************************************************
* gallery sort functions
***********************************************************/
function saveSortOrder(order) {
	gBajax.open('get','ajax_gallery_sort.php?order='+order);
	gBajax.send(null);
}

/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryFolder(folder) {
	gBajax.open('get','ajax.php?action=selectLibraryFolder&folder='+folder);
	gBajax.onreadystatechange = selectLibraryFolderResponse;
	gBajax.send(null);
}
function selectLibraryFolderResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* select image in rte_image pop-up window
***********************************************************/
function selectLibraryImage(id) {
	gBajax.open('get','ajax.php?action=selectLibraryImage&id='+id);
	gBajax.onreadystatechange = selectLibraryImageResponse;
	gBajax.send(null);
}
function selectLibraryImageResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
			var res = response.split("|");
			/*
			r[0] = id of image or "no"
			r[1] = url of image
			r[2] = alt of image
			*/
			if( res[0] == 'no' ) {
				alert('The selected image could not be found. Please try again.');
			}
			else {
				document.getElementById('selected_image_url').value = res[1];
				document.getElementById('selected_image_alt').value = res[2];
				var tables = document.getElementById('thumbnails').getElementsByTagName('table');
				for( var t=0; t < tables.length; t++ ) {
					tables[t].className = '';
				}
				document.getElementById('image_' + res[0]).className = 'selected_image';
			}
       	}
    }
}


/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryParentCategory() {
	gBajax.open('get','ajax.php?action=selectLibraryParentCategory');
	gBajax.onreadystatechange = selectLibraryParentCategoryResponse;
	gBajax.send(null);
}
function selectLibraryParentCategoryResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* cycle through pages of images
***********************************************************/
function cycleLibraryImages(start) {
	gBajax.open('get','ajax.php?action=getLibraryThumbnails'+start);
	gBajax.onreadystatechange = cycleLibraryImagesResponse;
	gBajax.send(null);
}
function cycleLibraryImagesResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;
       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}
