//preload all images
function onLoadTasks(){
	//checkSchoolMenu();
}

function preload(){
	var images = document.getElementsByTagName('img');
    var preload_images = Array();
    for (var i=0; i< images.length; i++) {
        images[i].over_src = images[i].src;
        //alert(images[i].over_src);
        preload_images[i] = images[i].over_src;
    }
    //preload the images
    document.preloaded_images = Array();
    //alert(preload_images.length);
    for (var i=0; i<preload_images.length; i++) {
          document.preloaded_images[i] = new Image;
          document.preloaded_images[i].src = preload_images[i];
          //alert(document.preloaded_images[i].src);
    }
}

function clear_cookie(){
	var mydate = new Date();
	mydate.setTime(mydate.getTime() - 1);
	document.cookie = "TreeMenuBranchStatus=; expires=" + mydate.toGMTString();
	//location.reload(true);
	location.reload();
	//location.href(location.href);
}
function do_hovers() {
      //This function takes care of preloading and setting mouse events
      if (document.getElementsByTagName) {
            var images = document.getElementsByTagName('img');
            var preload_images = Array();
            for (var i=0; i< images.length; i++) {
                  if (images[i].className == 'rollover') {
                        cur_src = images[i].src;
                        images[i].off_src = cur_src;
                        images[i].over_src = cur_src.replace(/_inactive/g, '_active');
                        //alert(images[i].over_src);
                        preload_images[i] = images[i].over_src;
                        images[i].onmouseover = function() { this.src = this.over_src; };
                        images[i].onmouseout = function() { this.src = this.off_src; };
                  }
            }
            //preload the images
            document.preloaded_images = Array();
            for (var i=0; i<preload_images.length; i++) {
                  document.preloaded_images[i] = new Image;
                  document.preloaded_images[i].src = preload_images[i];
            }
      }
}

//SCHOOL_PAGES STAR ROLLOVERS

Rollimage = new Array();

Rollimage[0]= new Image(121,153);
Rollimage[0].src = "/images/buttons/star.gif";

Rollimage[1] = new Image(121,153);
Rollimage[1].src = "/images/buttons/star-over.gif";

function SwapOut(id){
	id = id + 1;
	for(var i=1; i < id; i++){
		var star_src = document.getElementById('star' + i);
		star_src.src = Rollimage[1].src;
	}
}
function SwapBack(id){
	id = id + 1;
	for(var i=1; i < id; i++){
		var star_src = document.getElementById('star' + i);
		star_src.src = Rollimage[0].src;
	}
}

//AJAX FUNCTIONS

function suggest_search_name(user_id, page)
{
	var search = document.getElementById('words').value;
	if(search == ''){
		document.getElementById('suggest_name_response').innerHTML = '';
	}else{
		getContent('/includes/ajax/suggest.name.php?search=' + search + '&user_id=' + user_id + '&page=' + page, 'suggest_name_response', 1);
	}
}
function suggest_search_city()
{
	var search = document.getElementById('city').value;
	if(search == ''){
		document.getElementById('suggest_city_response').innerHTML = '';
	}else{
		getContent('/includes/ajax/suggest.city.php?search=' + search, 'suggest_city_response', 1);
	}
}

/* bof image functions */
function onUploadComplete(table, id, action){
	getContent('/includes/ajax/get_images.php?table=' + table + '&id=' + id + '&action=' + action, 'imageUploadComplete', 1);
}
function changePageImage(table, id, image_id, school_id, owner_id){
	getContent('/includes/ajax/school_pages_default_image.php?table=' + table + '&id=' + id + '&image_id=' + image_id + '&school_id=' + school_id + '&owner_id=' + owner_id, 'default_image', 1);
}
function imageAdmin(owner_id, school_id, page_id, image_id){
	getContent('/includes/ajax/school_pages_image_admin.php?owner_id=' + owner_id + '&school_id=' + school_id + '&page_id=' + page_id + '&image_id=' + image_id, 'image_admin', 0);
}
function updateImageCounter(image_number, image_total){
	getContent('/includes/ajax/school_pages_image_counter.php?school_id=' + school_id + '&image_total=' + image_total, 'image_counter', 0);
}
function loadSchoolImageThumbnails(table, id, school_id, owner_id, position){
	getContent('/includes/ajax/school_pages_thumbnails.php?table=' + table + '&id=' + id + '&school_id=' + school_id + '&owner_id=' + owner_id + '&position=' + position, 'thumbnails', 1);
}
function nextImage(table, id, image_id, school_id, owner_id, action){
	getContent('/includes/ajax/school_pages_default_image.php?table=' + table + '&id=' + id + '&image_id=' + image_id + '&school_id=' + school_id + '&owner_id=' + owner_id + '&action=' + action, 'default_image', 1);
}
/* eof image functions */

/* bof rating functions */
function storeRating(rating, page_id){
	getContent('/includes/ajax/school_pages_rating.php?page_id=' + page_id + '&rating=' + rating, 'rating_response', 1); 
}
function refreshRating(page_id){
	getContent('/includes/ajax/school_pages_update_rating.php?page_id=' + page_id, 'rating', 1);
}
/* eof rating functions */

/* bof menu display functions */
function updateMenuDisplay(school_id, section_id, display){
	getContent('/includes/ajax/school_menu.php?school_id=' + school_id + '&section_id=' + section_id + '&display=' + display, 'navResponse', 0);
}
/* eof menu display functions */

function getSchoolPages(table, school_id, catID){
	getContent('/includes/ajax/school_pages_list.php?table=' + table + '&school_id=' + school_id + '&cat_id=' + catID, 'schoolPages', 0);
}

/*bof click tracking */
function trackClick(words, school_id, page_id){
	getContent('/includes/ajax/track_click.php?words=' + words + '&school_id=' + school_id + '&page_id=' + page_id, 'debugClick', 0);
}
/* eof click tracking */

/*bof profile menu*/
function updateContributions(section, entry_id, user_id, action){
	getContent('/includes/ajax/update_contributions.php?section=' + section + '&entry_id=' + entry_id + '&user_id=' + user_id + '&action=' + action, 'contributions', 1);
}
/*eof profile menu*/
/*bof registration*/
function showMoreReg(choice)
{
	if(choice == 1 || choice == 2){
		document.getElementById('show_school_wrap').style.display = 'block';
		getContent('/includes/ajax/reg_find_school.php', 'reg_suggest_name_response', 1);
	}else{
		document.getElementById('show_school_wrap').style.display = 'none';
		document.reg_form.email.value = '';
	}
}
function loadSchoolNameReg(name, id){
	if(document.reg_form.role.options[document.reg_form.role.selectedIndex].value == 1){
		if(ocument.reg_form.email.value == ''){
			document.reg_form.email.value = 'make sure to use your ".edu" address';
		}
		getContent('/includes/ajax/reg_show_school.php?school=' + name + '&school_id=' + id, 'reg_suggest_name_response', 1);
	}else{
		getContent('/includes/ajax/reg_show_school.php?school=' + name + '&school_id=' + id, 'reg_suggest_name_response', 1);
	}
}
/*bof registration*/

function getHTTPObject()
{
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
	try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
		xmlhttp = false;
	  }
	}
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	try {
	  xmlhttp = new XMLHttpRequest();
	} catch (e) {
	  xmlhttp = false;
	}
  }
  return xmlhttp;
}

function getContent(url,location,showLoader) {
	//add ajax loader
	if(showLoader){
		document.getElementById(location).innerHTML = '<img src="/images/icons/ajax-loader.gif" styles="vertical-align: middle">';
	}
	xObj = false;
	xObj = getHTTPObject();
	xObj.open('GET', url,true);
	xObj.onreadystatechange=function() {
		if (xObj.readyState==4) {
			document.getElementById(location).innerHTML = xObj.responseText;
			if(xObj.responseText.indexOf('<' + 'script') != -1) {
				beginning = xObj.responseText.indexOf('<' + 'script');
				beginning = xObj.responseText.indexOf('>',beginning) + 1;
				ending = xObj.responseText.indexOf('</' + 'script')-1;
				scriptText = xObj.responseText.substring(beginning, ending);
				eval(scriptText);
			}
		}
	}
	xObj.send(null)
}
//END AJAX

function email_page_pop(user_id, the_page) {
	open_popup('/includes/modules/invite_friend.php?user_id=' + user_id, 'email_window', 'width=400,height=350,scrollbars=no,resizable=yes,location=no,menubar=no,status=no,toolbar=no,directories=no,personalbar=no');
}

function showLargeImage(section, folderId, imageName, width, height) {
	open_popup('/includes/modules/show_large_image.php?section=' + section + '&folder_id=' + folderId + '&image_name=' + imageName, 'email_window', 'width=' + width +',height=' + height + ',scrollbars=no,resizable=yes,location=no,menubar=no,status=no,toolbar=no,directories=no,personalbar=no');
}

function video_player(videoId, width, height) {
	open_popup('/includes/video_player.php?video_id=' + videoId, 'video_player', 'width=' + (width + 50) + ',height=' + (height + 175) + ',scrollbars=no,resizable=yes,location=no,menubar=no,status=no,toolbar=no,directories=no,personalbar=no');
}

var first_click = '';
function clearField(theField)
{
	if(first_click == 1)
	{
		theField.value = '';
	}
	return false;
}

//ver 1.1
var popup_array = Array();
function open_popup(url, popup_name, options) {
	var cur_popup = popup_array[popup_name];
	var is_popup_defined = (typeof(cur_popup) == 'object');
	var is_popup_open = (is_popup_defined)
		? !(cur_popup.closed)
		: false;

	if ( is_popup_defined && is_popup_open ) {
		cur_popup.document.write (' ');
		cur_popup.focus();
		cur_popup.location.href = url;
		cur_popup.focus();
	} else if ((is_popup_defined && !is_popup_open) || !is_popup_defined) {
		popup_array[popup_name] = window.open(url, popup_name, options);
	}
}


//SCHOOL NAV
function hide_show_menu(school_id, section_id){
	var elem = document.getElementById('section_pages_' + section_id);
	if(elem.style.display == 'none'){
		elem.style.display = 'block';
		updateMenuDisplay(school_id, section_id, 'block');
	}else{
		elem.style.display = 'none';
		updateMenuDisplay(school_id, section_id, 'none');
	}
}

//Global add image
function selectPages(school_id){
	var category = document.catFrom.categories.selectedIndex;
	var catId = document.catFrom.categories.options[category].value;
	getSchoolPages('schools', school_id, catId);
}
function showUploader(school_id){
	var page = document.formPages.schoolPages.selectedIndex;
	var pageId = document.formPages.schoolPages.options[page].value;
	location.href = "/schools/add_image.html?school_id=" + school_id + "&page_id=" + pageId + "&add";
}
function hideUploader(school_id, cat_id){
	location.href = "/schools/add_image.html?school_id=" + school_id + "&cat=" + cat_id + "&hide";
}
function openwindow(url, width, height, scrollbars, resizable, multiple){
    var winname = "mywindow";
    if(multiple == "yes"){
        winname = Math.floor(Math.random()*9999999)
    }
    window.open("" + url + "","" + winname + "","toolbar=no,location=no,status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + width + ",height=" + height);
}
