function vr_gal_form_val() {

	$('#vr_gal_form').submit(function() {
		var vr_gal_name = $('input[@name=vr_gal_name]').fieldValue();
		var vrimages = $('input[@name="vrimages[]"]').fieldValue();
		if ( !vr_gal_name[0] ) {
			alert('Please enter the Name for the Virtual Gallery'); 
			return false; 
		}
		if ( !vrimages[0] ) {
			alert('Please add at least one image to the Virtual Gallery'); 
			return false; 
		}
		/* $(this).ajaxSubmit();*/
		/* return true does the submit! */
		return true;
	});
};


function selectSMUser() {
	$("#smuser")
		.livequery(function(){
			$(this).change(function(){
				$("#sm-alb_and_gal_ajax").empty();
				$("#sm-alb_and_gal_ajax").append("Loading ...");
				$("#sm-alb_and_gal_ajax").load($(this).val());
				return false;
			});
		});
};

function refreshSMUser() {
	$("a.albreload")
		.livequery(function(){
			$(this).click(function(e){
				$("#sm-alb_and_gal_ajax").empty();
				$("#sm-alb_and_gal_ajax").append("Loading ...");
				$("#sm-alb_and_gal_ajax").load($(this).attr("href"));
				return false;
			});
		});
};

function haltSMAjaxDivs() {
	$("#sm-alb-ajax").animate({opacity:'toggle'}, 100);
	$("#sm-gal-ajax").empty();
	$("#sm-gal-ajax").append("Loading ...");
};

function unhaltSMAjaxDivs() {
	$("#sm-alb-ajax").animate({opacity:'toggle'}, 10);
};

function searchImages() {

    var options = { 
        target:        '#sm-gal-ajax',   // target element(s) to be updated with server response 
        beforeSubmit:  haltSMAjaxDivs,  // pre-submit callback 
        success:       unhaltSMAjaxDivs  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	$("#sm_img_search_header")
		.livequery(function(){
			$("#img_search_form").ajaxForm(options);
		});			
};

function saveAlbPass() {

    var options = { 
        target:        '#sm-gal-ajax',   // target element(s) to be updated with server response 
        beforeSubmit:  haltSMAjaxDivs,  // pre-submit callback 
        success:       unhaltSMAjaxDivs  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	$("#alb_pass_prompter")
		.livequery(function(){
			$("#alb_pass_form").ajaxForm(options);
		});			
};

 
function selectedGal(){	
	//ajaxBase = "http://www.ttljournal.com/sm/alblst.php";
	//	$("#sm-gal-ajax").load(ajaxBase +"?"+ $(this).attr("href"));
	$("a.imageload")
		.livequery(function(){
			$(this).click(function(e){
				haltSMAjaxDivs();
				randomnumber=Math.floor(Math.random()*1001);
				$("#sm-gal-ajax").load($(this).attr("href") + "&ie_sucks=" + randomnumber, function(){
					unhaltSMAjaxDivs();
				});
				return false;
			});
		});			
};

function copyImage(){
	$("#sm-gal-copy-from")
		.livequery(function(){
			$("a.preview").click(function(e){
				$("#vr-gal-help").remove();
				newImage = $(this).clone().attr('class', 'addedimg');
				imgId = $(this).attr("id");
				$("#vr-images").prepend('<input id="IVR_'+ imgId +'" name="vrimages[]" value="'+ imgId +'" type="hidden" />');
				$("div.vr-gal-list").prepend('<a href="#" id="VR_'+ imgId +'" class="addedimg"><div class="wp-caption alignleft" style="width: 115px; height: 115px;">'+ $(newImage).html() +'</div></a>');
				/*$("div.vr-gal-list").prepend(newImage);*/
				return false;
			});
		});
};

function removeImage(){
	$("a.addedimg")
		.livequery(function(){
			$(this).click(function(e){
				imgId = $(this).attr("id");
				$("#I" + imgId).remove();
				$(this).remove();
				return false;
			});
		});
};


// starting the script on page load
$(document).ready(function(){
	selectedGal();
	copyImage();
	removeImage();
	selectSMUser();
	refreshSMUser();
	saveAlbPass();
	searchImages();
	vr_gal_form_val();
});


