var imgdir = '/wp-content/themes/cm4/images/product_photos/';

$(document).ready(function(){	
	
	$(".collection_colors .collection_colorblock").click(function() {
		var id = $(this).closest("div.collection_item").attr("id");
   		var photoContainer = $('#'+id);
		var name = $(this).attr('name');
		$("#" + id + " img.collection_product").attr('src', imgdir+id+"/"+name+".gif");
	});
	
	/* Home Page Slideshow */
   $('ul#homeSlideshow').slickslide({
		speed: 1000,
		timeout: 6000,
		singleHeight:"451px",
		numbers:false 
	}); 

});

/* =========================================================

// jquery.slickslide.js

// Date: 2009-10-28
// Author: Cody Stoltman


 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').slickslide({ 
 *	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
 *	  timeout: Time between the fades in milliseconds (Default: '2000'), 
 * 	  singleHeight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  numbers: True or False (adds clickable numbers below the slideshow)
 *  }); 
 *

// ========================================================= */


(function($) {

    $.fn.slickslide = function(options) {
        return this.each(function() {   
            $.slickslide(this, options);
        });
    };

    $.slickslide = function(container, options) {
        var settings = {
        	'speed':            'normal',
            'timeout':          2000,
			'numbers':			false, // Adds clickable numbers True or False
			'singleHeight':			'auto',
			'timeoutID':        0,
			'current':          0,
			'next':             1
        };

        if (options){
            $.extend(settings, options);
		}
		
		// Sets elements to the children of the passed in container
        var elements = $(container).children();

        if (elements.length > 1) {
			
			// Sets the CSS to relative and the height to the height of 1 slide
            $(container).css('position', 'relative').css('height', settings.singleHeight);

			// Loops through and sets the z-index on all the elements
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };

			// If numbers is true then add the clickable numbers
			if (settings.numbers == true) {
				
				message = "<ul id='slickslide_count'>";
				
				for (var i = 0; i < elements.length; i++) {
					// add active class to currently selected number
					if (i == 0) {
						message += "<li class='count_" + (i+1) + " active'>" + (i+1) + "</li>\n";
					} else {
						message += "<li class='count_" + (i+1) + "'>" + (i+1) + "</li>\n";
					}
				};

				message += "</ul>";
			
				// Adds the number list to manually click to an element
				$(container).after(message);
			
				// Sets the click events for the number list items
				$.slickslide.setClickEvents(elements, settings);
				
			}
			
			// Starts the slide show
			$.slickslide.startShow(elements, settings);

		}
	};
	
	/******************************************************************
	/************* Set Button events to manually forward the slideshow
	/****************************************************************/
    $.slickslide.setClickEvents=function(elements, settings){
		$("ul#slickslide_count li").each(function(){
			
			$(this).click(function(){
			
				button_id=$(this).attr("class");
				split_button_id_string=button_id.split("_");
				button_id_string=split_button_id_string.pop();
				current=parseFloat(button_id_string);
				
				//Runs the skip function to advance the slide
				$.slickslide.stopTimeout(elements, settings, current)
			})
		})
	};
	
	/******************************************************************
	/************* Show the first element and send to timeout function
	/****************************************************************/
	$.slickslide.startShow = function(elements, settings) {
			$(elements[0]).show();
			$.slickslide.timeout(elements, settings);
		
	}

	/********************************************************************
	/************* Main function that advances the slide through a fade
	/*******************************************************************/
	$.slickslide.advance = function(elements, settings){		

		$(elements[settings.current]).fadeOut(settings.speed);
		$(elements[settings.next]).fadeIn(settings.speed); 
		active_class = ".count_" + (settings.next + 1);
		
		// If numbers is true set the active class on current number
		if(settings.numbers == true) {
						
			$("ul#slickslide_count li").each(function(){
				
				if ($(this).is('.active')){
					$(this).removeClass('active');
				}
			
				if ($(this).is(active_class)) {
					$(this).addClass('active');
				}
			});
			
		}
		
		current = settings.current;
		next = settings.next;
				
		// Sets the next element
		if ((current + 1) == (elements.length)){
			current = 0;
			next++;
		}else if ((next + 1) < elements.length) {
			next++;
			current++;
		} else {
			current = (elements.length - 1);
			next = 0;
		} 
		
		settings.current = current;
		settings.next = next;
					
		// Sends it back to our timeout function
		$.slickslide.timeout(elements, settings);
	}
	
	
	/**********************************************************
	/************* Controls the timeout (delay) between slides
	/*********************************************************/
	$.slickslide.timeout = function(elements, settings) {
		
		settings.timeoutID = setTimeout(function(){
			$.slickslide.advance(elements, settings);
		}, settings.timeout); 
		
	}
	
	/********************************************************************
	/************* Stops the timeout when a number is clicked and sends
	/************* new current and next settings to advance function
	/*******************************************************************/
	$.slickslide.stopTimeout = function(elements, settings, current) {
		
		clearTimeout(settings.timeoutID);
		
		// Fades out all of the elements
		for(var i=0;i<elements.length;i++){
			$(elements[i]).fadeOut(settings.speed);
		}
		
		// Adjust for use in an array
		settings.current = current - 2;
		
		
		// Sets the current and next slide
		if((settings.current + 1) > elements.length) {
			settings.next = 1;
		} else if ((settings.current) < 0) {
			settings.next = 0;
			settings.current = elements.length - 1;
		} else {
			settings.next = settings.current + 1;
		}
		

		$.slickslide.advance(elements, settings);
		
	}


})(jQuery);