// Search Form Focus using idlefield and focusfield css styles
// Relies on JQuery Library at http://jqueryjs.googlecode.com/files/jquery-1.3.2js

		$(document).ready(function() {
			$('input[type="text"]').addClass("idleField");
       		$('input[type="text"]').focus(function() {
       			$(this).removeClass("idleField").addClass("focusField");
    		    if (this.value == this.defaultValue){ 
    		    	this.value = '';
				}
				if(this.value != this.defaultValue){
	    			this.select();
	    		}
    		});
    		$('input[type="text"]').blur(function() {
    			$(this).removeClass("focusField").addClass("idleField");
    		    if ($.trim(this.value) == ''){
			    	this.value = (this.defaultValue ? this.defaultValue : '');
				}
    		});
		});			

// Menu script very simply hides and shows an unoordered list
// Dependant on drop-down.css for menu positioning

 $(document).ready(function(){
  $('#cssdropdown li.headlink').hover(
   function() { $('ul', this).css('display', 'block'); },
   function() { $('ul', this).css('display', 'none'); });
 });


/*
 * 	 imBannerRotater - a JQuery Plugin
 * 	 @author Les Green
 * 	 Copyright (C) 2009 Intriguing Minds, Inc.
 *   Version 0.5
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *   Demo and Documentation can be found at:   
 *   http://www.grasshopperpebbles.com
 *   
 */
 
;(function($) {
	$.fn.extend({
        imBannerRotater: function(options) { 
        	opts = $.extend({}, $.bannerRotater.defaults, options);
			return this.each(function() {
				new $.bannerRotater(this, opts);
			});
        }
    });	

$.bannerRotater = function(obj, opts) {
	var $this = $(obj);
	var imgCnt = 0;
	var ttlImg = 0;
	if (opts.image_url) {
		var d = getDataString();
		doAjax('GET', opts.image_url, d, '', doCreate);
	} else {
		doCreate(opts.images);
	}
	
	function getDataString() {
		var str = '';
		$.each(opts.data, function(i, itm) {
			str += itm.name + "=" + itm.value + "&";							
		});
		//remove last "&"
		str = str.substr(0, (str.length-1));
		return str;
	};
		
	function doAjax(t, u, d, fnBefore, fnSuccess) {
		var dt = (opts.return_type == 'json') ? 'json' : 'text';
		$.ajax({
			type: t,
			url: u,
			data: d,
			dataType: dt,
			beforeSend: fnBefore, //function(){$("#loading").show("fast");}, //show loading just when link is clicked
			//complete: function(){ $("#loading").hide("fast");}, //stop showing loading when the process is complete
			success: fnSuccess,
			error: showError
	 	}); //close $.ajax(
	};
	
	function showError(XMLHttpRequest, textStatus, errorThrown) {
		console.log(textStatus);
	};
	
	function doCreate(data) {
		//var tbl, tr;
		var img, pic;
		if (opts.return_type == 'list') {
			var daAR = data.split(',');
		} else {
			var daAR = new Array();
			$.each(data, function(i, itm) {
				if (opts.mode == 'random') {
					if (opts.data_map.url_name) {
						daAR[i] = new Array(itm[opts.data_map.image_name], itm[opts.data_map.url_name]);
					} else {
						daAR[i] = itm[opts.data_map.image_name];
					}	
				} else {
					daAR[i] = itm[opts.data_map.image_name];
				}	
			});
		}
		if (opts.mode == 'random') {
			img = new Image();
			if (opts.data_map.url_name) {
				var tgt = (opts.data_map.url_target) ? opts.data_map.url_target : '_blank';
				var sel = daAR[Math.floor(Math.random()*daAR.length)];
				pic = opts.base_path + sel[0];
				var url = sel[1];
				$this.append($('<a></a>').attr({'href': url, 'target': tgt}).append($(img).attr({ src: pic, alt: pic})));
			} else {
				pic = opts.base_path + daAR[Math.floor(Math.random()*daAR.length)];
				$this.append($(img).attr({ src: pic, alt: pic}));
			}	
		} else {
			ttlImg = daAR.length;
			for (var i = 0; i < ttlImg; i++) {
				pic = opts.base_path + daAR[i];
				img = new Image();
				$this.append($(img).attr({ src: pic, alt: pic}).css('display', 'none'));
			}
			imgFadeIn();
		}
	};
	
	function imgFadeIn() {
		$("img:hidden:eq("+imgCnt+")", $this).fadeIn(opts.speed, function(){
			imgFadeOut();
		});
	};
	
	function imgFadeOut() {
		$("img:eq("+imgCnt+")", $this).fadeOut(opts.speed, function(){
			imgCnt = (imgCnt == ttlImg-1) ? 0 : imgCnt + 1;
			imgFadeIn();
		});
	};
};

$.bannerRotater.defaults = {
	mode: 'random',//rotate
	image_url: '',
	data: '',
	images: '',//can be used instead of image_url. contains comma delimited list of images
	return_type: 'list', //list, json
	base_path: '',
	data_map: '', //{image_name: '', url_name: '', url_target: '_blank'}
	speed: 1500
};
})(jQuery);
		
$(document).ready(function(){
	$("#header-insert").imBannerRotater({
//		image_url: 'random.php',
		base_path: '/images/header-images/',
		return_type: 'list',
//		data_map: {image_name: 'name', url_name: 'url'},
	    images: '01.png,02.png,03.png,04.png,05.png,06.png,07.png,08.png' //can be used instead of image_url. contains comma delimited list of images
	})
});

// The code to fix the IE<8 png transparency bug 

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'transparent.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};

// For #header-insert drop-ins etc.
// calls script and inserts shim

$(document).ready(function(){
 $('#header-insert, body').supersleight({shim: '/images/transparent.gif'});
});


// Preload images 

$(document).ready(function() {
   vCycleImages = setInterval(function() {
      var bImgLoaded = true;
      var images = $("body img");
 
      for (var i = 0; i < images.length; i++) {
         var img = images[i];
         if (img.complete == false)
            bImgLoaded = false;
      }
 
      if (bImgLoaded) {
         $("body").cycle({ delay: 1000, speed: 1000 });
         clearInterval(vCycleImages);
      }
   }, 1000);
})