/****************************
 *	Home page image rotator	*
 ****************************/
var homeImages = {
	/* preferences */
	delay : 5000,
	fadeDuration : 300,
	/* end preferences */
	
	imgLink : Object,
	imgLinkImage : Object,
	imageSet : { "images" : [] },
	preloadImages : Array,
	currentImageIndex : 1,
	imageCount : 0,
	
	init : function() {
		// set local object vars here
		if($(".homePromo").length > 0){
			this.imgLink = $(".homePromo a");
			this.imgLinkImage = $(".homePromo img");
			this.setUpSlides();
			this.imageCount = this.imageSet.images.length;
			this.preload();
			
			var papaThis = this;
			setTimeout(function(){ papaThis.rotate() },this.delay);
		}
	},
	
	// Preload Images
	preload : function() {
		var papaThis = this;
		$(this.imageSet.images).each(function(index){
			papaThis.preloadImages[index] = $('<img />').attr('src', this.img);
		});
	},
	
	setUpSlides : function() {
		var papaThis = this;
		var $moreSlides = $(".moreSlides a");
		
		//add primary image/link to image set
		this.addImage(this.imgLink);
		
		//add additional slides to image set
		$moreSlides.each( function() {
			papaThis.addImage(this);
		});
	},
	
	addImage : function(imgLink) {
		var $image = $(imgLink).children("img");
		var curImage = $image.attr("src");
		var curAlt = $image.attr("alt") || "";
		var curTitle = $image.attr("title") || "";
		var curLink = $(imgLink).attr("href");
		
		this.imageSet.images.push(
			{ "img" : curImage,
				"alt" : curAlt,
				"href" : curLink,
				"title" : curTitle
			}
		);
	},
	
	rotate : function() {
		var currentImage = this.imageSet.images[this.currentImageIndex].img;
		var currentAlt = this.imageSet.images[this.currentImageIndex].alt;
		var currentHref = this.imageSet.images[this.currentImageIndex].href;
		var currentTitle = this.imageSet.images[this.currentImageIndex].title;
		
		this.replaceImage(currentImage);
			
		this.imgLink.attr('href', currentHref);
		this.imgLinkImage.attr('alt', currentAlt);
		this.imgLinkImage.attr('title', currentTitle);
		//increment counter, or set back to zero if last image was reached
		
		if(this.currentImageIndex == this.imageCount - 1){
			this.currentImageIndex = 0;
		}else{
			this.currentImageIndex++;
		}
				
		//restart timer
		var papaThis = this;
		setTimeout(function() { papaThis.rotate() },this.delay);
	},
	
	replaceImage : function(currentImage) {
		var papaThis = this;
		this.imgLinkImage.fadeTo(this.fadeDuration,0,function(){
			papaThis.loadNextImage(currentImage);
			
		});
	},
	
	loadNextImage : function(currentImage) {
		this.imgLinkImage.attr('src', currentImage);
		this.fadeImageIn();
	},
	
	fadeImageIn : function(){
		this.imgLinkImage.fadeTo(this.fadeDuration,1);
	}
}

/****************************
 *	Product Detail Gallery	*
 ****************************/
var productGallery = {
	imageSet : Array,
		
	init : function() {
		// on document ready
		if($(".productGallery").length > 0){
			// preload gallery images
			this.preload();
			
			// Swap large image when a thumbnail is hovered over in a product gallery
			// and disable thumbnail link
			$(".productDetail .thumbnails img").live("mouseover",function(){ productGallery.swap(this) });

			// Don't follow link to large image
			$(".productDetail .thumbnails a").live("click",function(){ return false; });
		}
	},
	
	// grab all thumbnail images in div.thumbnail, replace
	// "thumb" with "large" for each, and turn the result
	// into a preloaded jquery object in the "imageSet" array
	preload : function() {
		$(".thumbnails img").each(function(index){
			var thumby = $(this).attr("src");
			var imgLarge = productGallery.getLargeImage(thumby);
			
			productGallery.imageSet[index] = $('<img />').attr('src', imgLarge);
		});
	},
	
	// Swap Large Image
	swap : function(thumb) {
		var thumbImg = $(thumb).attr("src");
		var thumbTitle = $(thumb).attr("title");
		$("div.largeImage img").attr("src",this.getLargeImage(thumbImg));
		$("div.largeImage img").attr("title",this.getLargeImage(thumbTitle));
	},
	
	// Taks thumbnail src, returns large image src
	getLargeImage : function(thumb){
		return thumb.replace("thumb","large");
	}
}

/****************************
 *	Contact Us Linkitizer	*
 ****************************/
var contactLinkitizer = {
	$contactLink : $("a[href$=#email]") || null,
		
	linkitize : function() {
		// on document ready
		if(this.$contactLink != null){
			this.$contactLink.addClass("emailLink");
			this.$contactLink.attr("href","mailto:info@AT-95.com?subject=Found your web site");
		}
	}
}

/****************************
 *	Email List Joinerator	*
 ****************************/
var emailJoinerator = {
	$email : $("a[href$=#mailing-list]") || null,
		
	joinerize : function() {
		// on document ready
		if(this.$email != null){
			this.$email.bind("click",function() { 
				window.open("http://www.americanfurnishings.com/tinc?key=4d5qH6TL&RegistrationFormID=49538","joinEmailList","height=350,width=350,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes");
				return false;
			});
		}
	}
}

/***********************
 *	On Document Ready  *
 ***********************/
$(function(){
	// Start home page banner rotation if on the home page
	homeImages.init();
	
	// Preload product gallery images if on a product detail page
	productGallery.init();
	
	// turn contact us link into a mailto link
	contactLinkitizer.linkitize();
	
	//join email link
	emailJoinerator.joinerize();
});
