//http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);
function openWindow(_URL,_width,_height,_scrollbars) {
    var _left=(screen.availWidth/2)-(_width/2);
    var _top=(screen.availHeight/2)-(_height/2);
    window.open(_URL,"popupWindow","directories=no,menubar=no,location=yes,resizable=yes,scrollbars="+_scrollbars+",status=no,toolbar=no,height="+_height+",width="+_width+",left="+_left+",top="+_top);
}

var inputtext = {};
var submenuvisible = false;
var submenucleanup = false;
$(document).ready(function(){

	$.preLoadImages(
		'assets/images/nav-home_hover.png',
		'assets/images/nav-ministries_hover.png',
		'assets/images/nav-resources_hover.png',
		'assets/images/nav-training_hover.png',
		'assets/images/nav-events_hover.png',
		'assets/images/nav-about_hover.png',
		'assets/images/nav-contact_hover.png',
		'assets/images/nav-donate_hover.png'
	);

	// Rollover Images
	$('.hover').each(function(){
		$(this)
			.attr('title',$('a',this).text())
			.click(function(){
				src = $(this).css('background-image');
				$(this).css('background-image',src.replace(/_hover\.png/,'.png'));
				document.location.href=$('a',this).attr('href');
			})
			.hover(
				function() {
					src = $(this).css('background-image');
					$(this).css('background-image',src.replace(/\.png/,'_hover.png'));
				},
				function() {
					src = $(this).css('background-image');
					$(this).css('background-image',src.replace(/_hover\.png/,'.png'));
				}
			)
			.css('cursor','pointer')
		;
	});

	$('#nav-site-dropdown,#nav-site-dropdown-hover')
		.hover(
			function(){
				clearTimeout(submenucleanup);
				submenucleanup = null;
				if(submenuvisible===false) {
					submenuvisible = true;
					$('#nav-site-dropdown').css('background','url(/assets/images/nav-view-chapters_hover.png)');
					$('#nav-site-dropdown-hover').slideDown();
				}
			},
			function() {
				clearTimeout(submenucleanup);
				submenucleanup = null;
				submenucleanup = setTimeout(function(){
					submenuvisible = false;
					$('#nav-site-dropdown-hover').slideUp(function(){$('#nav-site-dropdown').css('background','url(/assets/images/nav-view-chapters.png)');});
				},500);
			}
		);

	$('.cycle').css('cursor','pointer').cycle({
		cleartype:!$.support.opacity,
		cleartypeNoBg:false,
        random:true,
		pause:true,
        timeout:5000,
        speed:400,
        requeueOnImageNotLoaded: true
    });
	
	$('input.text').focus(function(){
		id = $(this).attr('id');
		if(inputtext[id] === undefined) {
			inputtext[id] = $(this).val();
		}
		if($(this).val() === inputtext[id]) {
			$(this).val('');
		}
	}).blur(function(){
		id = $(this).attr('id');
		if($(this).val() === '') {
			$(this).val(inputtext[id]);
		}
	});

	//$('#logo').click(function(){window.location.href='/';}).css('cursor','pointer');

});