/* 
	Function to encode an email
	@param: id = the id/class of the object to target
*/
function encodeEmail(id){
	$(document).ready(function(){
		var target = "."+id;
		var at = / at /;
		var dot = / dot /g;
		var addr = $(target).text().replace(at,"@").replace(dot,".");
		$(target).after('<a href="mailto:'+addr+'" title="Send an email to '+ addr +'">'+ addr +'</a>')
		.hover(function(){window.status="Send an email";}, function(){window.status="";});
		$(target).remove();
	});
}

/* 
	Function to create a slideshow 
	@param: id = the id/class of the object to target
	@param: slideFx = the effect used for the transition
	@param: slideSpeed = the speed of the transition
	@param: slideTimeOut = the internal in milliseconds between transitions
	@param: slidePause = whether to pause the slidedshow if the mouse is placed over it - 0 or 1
*/
function slideShow(id,slideFx,slideSpeed,slideTimeOut,slidePause){
	$(document).ready(function(){
		// Slideshow
		$(id).cycle({ 
			fx: slideFx,
			speed: slideSpeed,
			timeout: slideTimeOut,
			pause: slidePause,
			next: '#next',
			prev: '#prev'
		});
	});
}

/* 
	Function to fade in/out object
	@param: id = the id/class of the object to target
	@param: offState = the default opacity value of the object
	@param: onState = the hover opactity value of the object
	@param: speed = the speed of the transition in milliseconds
*/
function fadeObject(id,offState,onState,speed){
	$(document).ready(function(){
		// Set the default transparency
		$(id).each(function(){
			$(this).css('opacity',offState);
		});
		// Add mouseover
		$(id).mouseover(function(){
			// Fade in the div
			$(this).stop().animate({
			opacity: onState
			}, speed);
		});
		// Add mouseout
		$(id).mouseout(function(){
			// Fade in the div
			$(this).stop().animate({
			opacity: offState
			}, speed);
		});
	});
}


/*
	Function to insert jQuery swfObject
	@param: flashTarget = the id/class of the html container
	@param: flashContent = the directory path to the swf file
	@param: flashWidth = the width of the swf file
	@param: flashHeight = the height of the swf file
	@param: flashVersion = the Flash player version 
*/
function swfObject(id,flashContent,flashWidth,flashHeight,flashVersion){
	// Create the flash object
	var flashContent = 
		$.flash.create(
			{
				swf: flashContent,
				width: flashWidth,
				height: flashHeight, 
				hasVersion: flashVersion,
				expressInstaller: 'swf/expressInstaller.swf'
			}
		);
	// Insert it into the html
	$(document).ready(function(){
			// Hide the html content
			$(id).children().hide();
			// Insert the flash object
			$(id).html(flashContent);
		}
	);
}

/* ------------------------------------------------------------

	A set of functions to create the image gallery slideshow

------------------------------------------------------------ */

/* 
	Function to create the image gallery
	@param: thumbId = the id/class of the thumbnail container
	@param: imageId = the id/class of the image container
	@param: activeClass = the name of the active css class for the thumbnails
*/
function imageGallery(thumbId,imageId,activeClass){
	$(document).ready(function(){
	
		// Add the onclick event to each thumbnail image
		$(thumbId).click(function(){
		
			// Remove any selected states from each thumbnail
			$(thumbId).each(function(){
				$(this).removeClass(activeClass);
			});
			// Set the selected class for the current thumbnail 
			$(this).addClass(activeClass);
			
			// Fade out all the main images
			$(imageId).each(function(){
				$(this).fadeOut(100);
			});
			
			// Take the href from the thumbnail and use it to target & fade in the current image
			var currentImageId = $(this).attr('href');
			// Fade in the current image
			$(currentImageId).fadeIn(450);
			
			// Ensure that the thumbnail anchors don't make the page jump up 
			return false;
		});
		// Run the thumbnail carousel 
		imageGalleryCarousel("#gallery_thumbs");
		
	});
}

/* 
	Function to add the prev/next button functionality for the image gallery
	@param: carousel = the carousel object
*/
function imageGalleryControls(carousel){
	// Previous button
    jQuery('#gallery_prev').click(function(){
        carousel.prev();
        return false;
    });
	// Next button
    jQuery('#gallery_next').click(function(){
        carousel.next();
        return false;
    });
}

/* 
	Function to turn the thumbnail area into a carousel
	@param: thumbId = the id/class of the thumbnail container
*/
function imageGalleryCarousel(thumbId){
	$(thumbId).jcarousel({
	    scroll: 1,
	    animation: 250,
	    wrap: 'last',
	    initCallback: imageGalleryControls,
	    buttonNextHTML: null,
	    buttonPrevHTML: null
	});
}

/* Image Gallery on hover for location section */

$(document).ready(function(){
	/* Location List Hover */

	$('.hover_map ul li').hover(function(){
		$('.hover_map ul li').removeClass('active');
		$('.hover_map .image img').removeClass('active');
		$($(this).children("a").attr('href').replace("#", ".")).addClass('active');
		$(this).addClass('active');
	},
	function(){
		$($(this).children("a").attr('href').replace("#", ".")).not('img').removeClass('active');
		$('.hover_map ul li').removeClass('active');
	});			
	$('.hover_map ul li a').click(function(){
		return false;
	});
		
	/* Ideo Locator Hover */
	$('.hover_map .image div a').hover(function(){
		$(this).parent('div').addClass('active');				
		$($(this).attr('href')).addClass('active');
	},
	function(){
		$(this).parent('div').removeClass('active');
		$('.hover_map ul li').removeClass('active');
	});


});



/* 
	Modal window function
*/

$(document).ready(function() {    
  
    //select all the a tag with name equal to modal  
    $('a[name=modal]').click(function(e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        //Get the A tag  
        var id = $(this).attr('href');  
      
        //Get the screen height and width  
        var maskHeight = $(document).height();  
        var maskWidth = $(window).width();  
      
        //Set height and width to mask to fill up the whole screen  
        $('#mask').css({'width':maskWidth,'height':maskHeight});  
          
        //transition effect       
      	$('#mask').fadeTo("slow",0.8);  
     
      
        //Get the window height and width  
        var winH = $(window).height();  
        var winW = $(window).width();  
                
        //Set the popup window to center  
        $(id).css('top', 242);  
        $(id).css('left', 365);  
      
        //transition effect  
        $(id).fadeIn(250);   
      	  
    });  
      
    //if close button is clicked  
    $('.window .close').click(function (e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        $('#mask, .window').fadeOut(250);  
    });       
      
    //if mask is clicked  
    $('#mask').click(function () {  
        $(this).hide();  
        $('.window').hide();  
    });          
    
    //if escape is pressed  
    $(document).keyup(function(e) {
 	if (e.keyCode == 27) {
  		$('#mask, .window').fadeOut(250); }  		
	});
    
});  


/*
	Image Pre Loading 
*/

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$(['img/logo.gif','img/bg_footer.gif','img/footer_logo.gif']).preload();
