    function showLoading(text, rand){
		return "<div id='ajax_"+rand+"' class='ajax_load'><div class='ajax_loader'>"+text+"</div></div>";
    }
    
    function showMedLoading(text, rand){
		return "<div id='ajax_"+rand+"' class='ajax_med_load'><div class='ajax_med_loader'>"+text+"</div></div>";
    }
    
    function showBigLoading(text, rand){
		return "<div id='ajax_"+rand+"' class='ajax_big_load'><div class='ajax_big_loader'>"+text+"</div></div>";
    }
	
	function randomNumber(){
		return Math.floor(Math.random()*9999);
	}
    
    function filterAjaxValues(text){
        text = encodeURIComponent(text);
        return text;
    }
    
    function decodeHtml(text){
        return $("<div/>").html(text).text();
    }
    
	function setCenterBoxHeight(){
		var height = $(document).height();
		$("#center_contain").height(height);
	}
	
	function loadCenterBox(){
		var height = $(window).height();
		var pad_top = Math.round(height / 10);
		if(pad_top < 5) pad_top = 5;
		
		$("body").prepend("<div id='center_contain' style='padding-top: "+pad_top+"px;'><div id='center_border' style='margin-top: "+pad_top+"px;'><div id='center_header'></div><div id='center_close'>X</div><div class='clear'></div><div id='center_page'></div></div></div>");
		setCenterBoxHeight();
	}
	
	function updateInstructorImage(){
        rand = randomNumber();
		$("#instructorPhoto").html(showMedLoading("Loading Photo", rand));
        
        var photo = $("#photo_id").val();
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=load_photo",
			data: "photoId="+photo+"&width=250",
            dataType: "json",
			success: function(json){
                if(json.result == 1){
                    $("#instructorPhoto").html(json.source);
                }else{
                    $("#ajax_"+rand).html("Failed to load: "+json.error);
                }
			},
			error: function(){
                $("#ajax_"+rand).html("Failed to load");
			}
		});
	}
    
	function updatePhotoUploadView(assignTo, obj, width){
	   
        var photo = $("#"+assignTo).val();
        if(photo > 0){
            rand = randomNumber();
    		$("#"+obj).html(showMedLoading("Loading Photo", rand));
            
    		$.ajax({
    			type: "POST",
    			url: "ajax.php?pg=load_photo",
    			data: "photoId="+photo+"&width="+width,
                dataType: "json",
    			success: function(json){
                    if(json.result == 1){
                        $("#"+obj).html(json.source);
                    }else{
                        $("#ajax_"+rand).html("Failed to load: "+json.error);
                    }
    			},
    			error: function(){
                    $("#ajax_"+rand).html("Failed to load");
    			}
    		});
        }
	}
	
	function newPhotoUpload(assignTo, albumType){
        rand = randomNumber();
		$("body").prepend("<div id='photoUploader'>"+showLoading("Loading Photo Uploader", rand)+"</div>");
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=center_box&subpg=photo_upload",
			data: "assignTo="+assignTo+"&header=Upload+New+Photo&albumType="+albumType,
			success: function(html){
				$("#photoUploader").remove();
                loadCenterBox();
				$("#center_contain").html(html);
				$("#center_close").live("click", function(){
					$("#center_contain").remove();
				});
			},
			error: function(){
				$("#center_contain").remove();
			}
		});
	}
    
    function updateFieldValue(assignTo, value){
        $("#"+assignTo).val(value);
        var parent = $("#"+assignTo).parent();
        $(".popup", parent).removeClass("openPopup");
    }
    
    function searchInstructorPhotos(assignTo, obj){
        
        var rand = randomNumber();
        $("#"+obj+" .results").html(showLoading("Searching Photos", rand));
        
        var keyword = filterAjaxValues($("#"+obj+" .search_keyword").val());
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=instructors&subpg=photo_search",
			data: "search="+keyword+"&assignTo="+assignTo,
			dataType: "json",
			success: function(json){
			 //alert(json);
				if(json.result == "1"){
				    $("#"+obj+" .results").html(decodeHtml(json.source));
				}else{
					$("#ajax_"+rand).html("Failed to load: "+json.error);
					setTimeout('$("#ajax_'+rand+'").remove()', 3000);
				}
			},
			error: function(){
				$("#ajax_"+rand).html("Failed to load.  Please try again.");
				setTimeout('$("#ajax_'+rand+'").remove()', 3000);
			}
		}); 
        
    }
    
    function setPhotoCommentsPage(page){
        
        var rand = randomNumber();
        $(".photoComments").prepend(showMedLoading("Loading Comments", rand));
        
        var photo = $("#photoId").val();
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=instructor&subpg=photo&photo="+photo+"&sub=get_comments&comments_pgn="+page,
			//data: "body="+comment,
			dataType: "json",
			success: function(json){
			 //alert(json);
				if(json.result == "1"){
                    $(".photoComments").html(decodeHtml(json.source));
				}else{
					$("#ajax_"+rand).html("Failed to load: "+json.error+". <a href='javascript:void(0);' onclick='setPhotoCommentsPage(1);'>Click here to try again</a>.");
					setTimeout('$("#ajax_'+rand+'").remove()', 3000);
				}
			},
			error: function(){
				$("#ajax_"+rand).html("Failed to load.  <a href='javascript:void(0);' onclick='setPhotoCommentsPage(1);'>Click here to try again</a>.");
				setTimeout('$("#ajax_'+rand+'").remove()', 3000);
			}
		}); 
        
    }    
    
    function postPhotoComment(){
        
        var rand = randomNumber();
        $("#postPhotoComment .body").prepend(showLoading("Posting Comment", rand));
        
        var comment = filterAjaxValues($("#comment_body").val());
        var photo = $("#photoId").val();
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=instructor&subpg=photo&photo="+photo+"&sub=post_comment",
			data: "body="+comment,
			dataType: "json",
			success: function(json){
			 //alert(json);
				if(json.result == "1"){
				    $("#postPhotoComment").removeClass("openPopup");
				    $("#ajax_"+rand).remove();
                    $(".photoComments .body").prepend(decodeHtml(json.source));
				}else{
					$("#ajax_"+rand).html("Failed to post: "+json.error);
					setTimeout('$("#ajax_'+rand+'").remove()', 3000);
				}
			},
			error: function(){
				$("#ajax_"+rand).html("Failed to post.  Please try again.");
				setTimeout('$("#ajax_'+rand+'").remove()', 3000);
			}
		}); 
        
    }
        
    function deletePhotoComment(comment){
        
        var rand = randomNumber();
        $("#photoComment"+comment).prepend(showLoading("Deleting Comment", rand));
        
        var photo = $("#photoId").val();
        
		$.ajax({
			type: "POST",
			url: "ajax.php?pg=instructor&subpg=photo&photo="+photo+"&sub=delete_comment",
			data: "comment="+comment,
			dataType: "json",
			success: function(json){
			 //alert(json);
				if(json.result == "1"){
				    $("#photoComment"+comment).remove();
				}else{
					$("#ajax_"+rand).html("Failed to delete comment: "+json.error);
					setTimeout('$("#ajax_'+rand+'").remove()', 3000);
				}
			},
			error: function(){
				$("#ajax_"+rand).html("Failed to delete comment.  Please try again.");
				setTimeout('$("#ajax_'+rand+'").remove()', 3000);
			}
		}); 
        
    }
    
    $(document).ready(function(){
        
        $(".popup .cartSubmit").live("click", function(){
            var parent = $(this).parent();
            if($(parent).hasClass("openPopup")){
                $(parent).removeClass("openPopup");
            }else{
                $(parent).addClass("openPopup");
            }
        });
        $(".popup .close").live("click", function(){
            var parent = $(this).parent().parent();
            $(parent).removeClass("openPopup"); 
        }); 
        
        var buttons = "#bookatrip, #mytrips";
        $(buttons).click(function(){
           $(buttons).removeClass(); 
           var id = $(this).attr("id");
           $("#"+id).addClass(id+"Active");
           $(".panelBody .panels").hide();
           $("#"+id+"Content").show(); 
        });
        
        if($(".photoComments").length > 0) setPhotoCommentsPage(1);
        
    
        $(".cartButton").mouseenter(function(){
           $(this).addClass("cartButtonHover"); 
        }).mouseleave(function(){
           $(this).removeClass("cartButtonHover"); 
        });
    });
