/**
 * @author andrew
 */



jQuery(document).ready(function() {
	jsHover();	//activate rollover images 
});



//image rollover effect
//the image should be assigned class="jsHover" and a hover image must be availabe with the same name but with hover appended eg.filename_hover.jpg.   
function jsHover() {
	var rolloverImage = '';
	
	jQuery('.jsHover').each(function() {
    	rolloverImage = jQuery(this).attr('src').split('.');
		rolloverImage[rolloverImage.length-2] += '_hover';
		rolloverImage = rolloverImage.join('.');
		
		jQuery(this).mouseover(function() {
			jQuery(this).attr('src', rolloverImage);
		});

		jQuery(this).mouseout(function() {
			jQuery(this).attr('src', rolloverImage.replace('_hover', ''));
		});
  	});
}



//popup message window
var openDialogs = new Object();

function popupDialog(title, body, modal) {
	modal = (typeof modal == 'undefined') ? false : true;
	
	if (typeof body != 'undefined' && body.length > 0) {
		jQuery('#modalDialog').dialog('close').remove();	//remove any previous message
		jQuery('body').append('<div id="modalDialog" title="'+title+'"><p style="text-align:left;">'+body+'</p></div>');
		placeholder = jQuery('#modalDialog');
		openDialogs['#modalDialog'] = placeholder;
	} else {
		if (typeof openDialogs[title] != 'undefined') {
			openDialogs[title].dialog('open');	//re-open existing dialog
			return;
		}
		placeholder = jQuery(title);
		openDialogs[title] = placeholder;
	}

	if (placeholder.length == 0) {
		alert("couldn't locate the dialog placeholder " + title);
		return;
	}	
		
	//initialize the dialog
	placeholder.dialog({
		modal: modal,
		resizable: false,
		width: 'auto',
		show: 'slide',
		hide: 'slide',
		autoOpen: false
	})
		
	placeholder.dialog('open');	//open the dialog
	
	
}



//close any open popup dialogs
function closePopupDialogs() {
	var size = 0;
	for (key in openDialogs) {
        if (openDialogs.hasOwnProperty(key)) {
			if (openDialogs[key].dialog('isOpen')) {
log('closing dialog', key);
				openDialogs[key].dialog('close');
			}
		}
    }
}



function displayResult(selector) {
	jQuery(selector).css('display', 'inline')
		.css('opacity', 0.3)
		.css('fontSize', '0.3em')
		.animate({ fontSize: "1.5em", opacity: 1.0 }, 2000 ).animate({ opacity: 0.0 }, 8000 ).animate({ fontSize: "0em" }, 1000 );	//the fontsize reduction is because IE8 ignores the opacity fade
}






function animateResult(selector, className, message, cantWait) {
	var target = jQuery(selector);
	
	//if the result target doesn't exist then send the message to the general server-result box
	if (!target.length) {
		target = jQuery('#server-result');
	}

	//if there's already an animation running wait till it finishes 
	if (target.queue("fx").length && typeof cantWait == 'undefined') {
log('queue size', target.queue("fx").length)
	window.setTimeout ('animateResult("'+selector+'", "'+className+'", "'+message+'", true)',3000);
log('delay message by 3 secs', 'animateResult("'+selector+'", "'+className+'", "'+message+'", true)')
		return;
	} else {
		target.stop(true, true); 	//kill any queue and display this right away
	}
	
log('displaying', message);
	
	target.css('display', '');
	if ((typeof message != 'undefined' && message.length > 0)) {
		message = message.replace(/\+/g, ' ');	//fixes an incompatibility between php url encode and javascript decode
		message = decodeURIComponent(message);
		target.html(message);
	}	
	target.attr('class', className);
	target.css('opacity', 0.3);
	target.css('fontSize', '0.3em');

   	target.animate({ fontSize: "1.5em", opacity: 1.0 }, 2000 ).animate({ opacity: 0.0 }, 8000 ).animate({ fontSize: "0em" }, 1000 );	//the fontsize reduction is because IE8 ignores the opacity fade
}





function removeElements(jQuerySelector) {	
	jQuery(jQuerySelector).fadeOut('slow').slideUp('slow', function () {
      this.remove();
    });
}



//slowly fade out any form error messages after the page has loaded 
	addOnloadEvent(function() {
		jQuery('.onloadError').css('opacity', 0.3);
		jQuery('.onloadError').css('fontSize', '0.3em');
	
	   	jQuery('.onloadError').animate({ fontSize: "1.5em", opacity: 1.0 }, 1000 ).animate({ opacity: 0.0 }, 7000, function(){
			jQuery('.onloadError').remove();
		});

		jQuery('#busyAnimation').css('display', 'none');	//stop the busy animation once the page loads
	});



function shake(selector) {
 	jQuery(selector).fadeIn(100).animate({left:"-=20px"},100).animate({left:"+=20px"},100).animate({left:"-=20px"},100).animate({left:"+=20px"},100).animate({left:"-=20px"},100).animate({left:"+=20px"},100);
}




function fadeOut(targetId) {
	jQuery('#'+targetId).fadeOut('fast');
}


function fadeIn(targetId) {
	jQuery('#'+targetId).fadeIn('slow');
}


function reloadPage(url) {
	if (url) {
		window.open(url, '_self');
	} else {
		window.location.reload(true); //force a complete reload (not using the browser cache)
	}
	//if we're using ajax page loads then check for an update to the address bar url
	if (typeof window.checkHash == 'function') {
		checkHash(); //this causes any # anchors in the url to get handled by ajax
	}
}



//compares the current webpage 
function updateWebpage(container) {
	
	//create the temporary placeholder for the updated page
	if (!jQuery('#updatedPage').length) {
		jQuery('body').append('<div id="updatedPage" style="visibility:hidden"></div>');	
	}
	
	//re-load this page via ajax call into the placeholder
	jQuery('#updatedPage').load(window.location.href+' '+container, function() {
		
		//oncomplete ajax load step through elements that have changed
		var newElement;
		var oldElement;
		
		jQuery(container+' *[id]').each(function() {
		
			newContent = jQuery('#updatedPage #'+this.id);
			oldContent = jQuery(this);
				
log('comparing ids', this.id)
			
			//update changed element content
			if (oldContent.html() != newContent.html()) {
				oldContent.html(newContent.html());
log('updated content for id', this.id)
			}

			//update changed element class
			if (oldContent.attr('class') != newContent.attr('class')) {
				oldContent.attr('class', newContent.attr('class'));		//update changed content
log('updated class for id', this.id)
			}
		});
		jQuery('#updatedPage').remove();	//this page fragment causes duplicate ids which can interfere with other DOM scripts
	});
}




