/*
    ---------------------------
	SHARED JAVASCRIPT FUNCTIONS
	---------------------------
	KANZO 2009
	---------------------------
*/

/*
		-----------------------------------
		
		Text > image replacement
		
		Usage:
		Replaces HTML container with image
		Container must have class="imageReplace"
		id is replacement image name
		classes of container are inherited by new image
		ID of container inherited by new image
				
		-----------------------------------
	*/

var imageReplace = new Class({
	    Implements: Events,
	    doReplace: function(){
	        $$('.imageReplace').each(function(rep) {						  
			var id = rep.getProperty('id');
			var classes = rep.getProperty('class');
			var src = '/_img/' + id;
			var newImg = new  Element('img').replaces(rep);
			newImg.src = src;
			newImg.set ('class',classes);
			newImg.set ('id',id);
	        });
			this.fireEvent('complete');
	    }
});
	
replaceImages = new imageReplace();

window.addEvent('domready', function() {
	replaceImages.doReplace();
});

window.addEvent('domready', function() {
		
	/*
		-----------------------------------
	
		Add image roll-overs and preloaders
	
		Usage:
		assign class 'goodDog' to image
		default image name must end '-off'
		hover image name must end '-on'
	
		-----------------------------------
	*/
	$$('img.goodDog','input.goodDog').each(function(img) {
			var src = img.getProperty('src');
			var extension = src.substring(src.lastIndexOf('.'),src.length);
			var preloadImg = new Image(100,25); 
			preloadImg.src = src.replace('off' + extension,'on' + extension);
			img.addEvent('mouseenter', function() { img.setProperty('src',preloadImg.src); });
			img.addEvent('mouseleave', function() { img.setProperty('src',src); });
	});
	
	/*
		-----------------------------------
		
		Open links in a new window
		
		Usage:
		Automatically opens links begining http:// or https://
		in a new window. Add a class to the anchor to open other 
		links in a new window. set class name below or leave 
		as default
				
		-----------------------------------
	*/
	// Set class sttribute
	var newWinClass = 'newWindow';
	/*
		Add class to links beginning http:// or https://
	*/
	$(document.body).getElements('a[href^=http://]').addClass(newWinClass);
	$(document.body).getElements('a[href^=https://]').addClass(newWinClass);
	/*
		Open specific links in a new window
	*/
	$$('.'+newWinClass).each(function(el) {
		el.addEvent('click', function(e) {
			e.stop();
			window.open (el.href);
		});
	});
	
	

	
	
	

});
