(function($)
{
    jQuery.fn.slidify = function(settings)
	{
        var config =
		{
		    slideSelector: "li",
		    offsetWidth: 970,
		    animationDuration: 750,
		    currentIndex: 0,
		    lastIndex: null,
		    easingMethod: "swing",
		    enforceContainerHeight: false,
		    slideScenario: "services",
		    lastEvent: 0
		};

        if(settings)
		{
            jQuery.extend(config, settings);
        }

        this.each(function() {
            // Set a reference of the object
            obj = jQuery(this);

            // Set the last index variable
            config.lastIndex = jQuery(this).children(config.slideSelector).length - 1;

			// Loop through all of the indices
			obj.children(config.slideSelector).each(function()
			{
				// Set the CSS properties of the slides
				jQuery(this).css({display: "block", left: (config.offsetWidth * config.currentIndex) + "px"}).addClass("slide_" + config.currentIndex);

				// Increment the slide index
				config.currentIndex++;
			});

			// Reset the current index
			config.currentIndex = 0;
            
			// Set the height of the container if we're looking to enforce the container height
            if(config.enforceContainerHeight == true)
			{
				if(config.slideScenario != "ourWork")
				{
					obj.css({height: jQuery(this).children("li.slide_" + config.currentIndex).height() + "px"});
				}
            }
			
            // Figure out our bindings
            switch(config.slideScenario)
			{
                case "services":
					// Prep the story anchor elements
					jQuery("a.story").each(function()
					{
						// Assign the link a numerical class
						jQuery(this).addClass(String("slide_" + config.currentIndex));
						
						// Increment the storyIndex property
						config.currentIndex++;
					});
					
					// Reset the index counter
                    config.currentIndex = 0;
					
					// Scrolling handlers
					jQuery("img#scrollRight").click(function(event)
					{
						if(event.timeStamp > (config.lastEvent + config.animationDuration))
						{
							if(config.currentIndex < config.lastIndex)
							{
								// Set the lastEvent timestamp
								config.lastEvent = event.timeStamp;
								
								// Animate
								obj.children(config.slideSelector).animate({left: "-=970px"}, {duration: (config.animationDuration * 5), easing: config.easingMethod});
								
								// Increment the story index
								config.currentIndex++;
								
								// Remove the highlight class on all the story links
								jQuery("a.story").removeClass("highlight");
								
								// Add the highlight class to the focused link
								jQuery("a.slide_" + config.currentIndex).addClass("highlight");
								
								// Render control state
								renderControlState(config.currentIndex, config.lastIndex, obj, config.slideSelector, "img#scrollLeft", "img#scrollRight")
							}
						}
					});
					
					jQuery("img#scrollLeft").click(function(event)
					{
						if(event.timeStamp > (config.lastEvent + config.animationDuration))
						{
							if(config.currentIndex > 0)
							{
								// Set the lastEvent timestamp
								config.lastEvent = event.timeStamp;

								// Animate
								obj.children(config.slideSelector).animate({left: "+=970px"}, {duration: (config.animationDuration * 5), easing: config.easingMethod});
								
								// Decrement the story index
								config.currentIndex--;
								
								// Remove the highlight class on all the story links
								jQuery("a.story").removeClass("highlight");
								
								// Add the highlight class to the focused link
								jQuery("a.slide_" + config.currentIndex).addClass("highlight");
								
								// Render control state
								renderControlState(config.currentIndex, config.lastIndex, obj, config.slideSelector, "img#scrollLeft", "img#scrollRight")
							}
						}
					});
					
					// Index-specific handler
					jQuery("a.story").click(function(event)
					{
						if(event.timeStamp > (config.lastEvent + config.animationDuration))
						{
							// Grab the class string from this element
							var linkClassString = jQuery(this).attr("class");
							
							// Split the link class string into an array
							var linkClassArr = linkClassString.split(" ");
							
							// If this element exists, let's do our magic
							if(jQuery("li." + linkClassArr[2]).length > 0)
							{
								// Set the lastEvent timestamp
								config.lastEvent = event.timeStamp;
								
								// Split the index class by an underscore to get the story index number
								var targetIndexArr = linkClassArr[2].split("_");
								
								// Figure out the animation multiplier
								var animationMultiplier = Math.abs(targetIndexArr[1] - config.currentIndex);
								
								if(animationMultiplier < 4)
								{
									animationMultiplier = 5;
								}
								
								// Set the storyIndex property
								config.currentIndex = targetIndexArr[1];
								
								// Remove the highlight class on all the story links
								jQuery("a.story").removeClass("highlight");
								
								// Add the highlight class to the focused link
								jQuery("a.slide_" + config.currentIndex).addClass("highlight")
								
								// Find out the index's current position
								var currentIndexPosition = parseInt(jQuery("li." + linkClassArr[2]).css("left"));
								
								// If the current index position is not zero (visible), then take action
								if(currentIndexPosition != 0)
								{
									// Checking for positive or negative indexing position
									if(currentIndexPosition < 0)
									{
										// Negative index position
										obj.children(config.slideSelector).animate({left: "+=" + (0 - currentIndexPosition)}, {duration: (config.animationDuration * animationMultiplier), easing: config.easingMethod});
									}
									else if(currentIndexPosition > 0)
									{
										// Positive index position
										obj.children(config.slideSelector).animate({left: "-=" + (0 + currentIndexPosition)}, {duration: (config.animationDuration * animationMultiplier), easing: config.easingMethod});
									}
								}
								
								// Render control state
								renderControlState(config.currentIndex, config.lastIndex, obj, config.slideSelector, "img#scrollLeft", "img#scrollRight")
							}
						}
					});
                break;

                case "staffing":

                break;

                case "news":
                    // Show the first news list
                    jQuery("div.newsList").eq(0).show();

                    // Highlight the first news list year link
                    jQuery("a.newsYear").eq(0).removeClass("orange10").addClass("green10");

                    // Create a counter
                    config.currentIndex = 0;

                    // Assign unique identifiers to each slide anchor
                    jQuery("a.newsItem").each(function() {
                        // Assign the slide number
                        jQuery(this).attr({ id: "slide_" + config.currentIndex });

                        // Increment the counter
                        config.currentIndex++;
                    });

                    // Reset the index counter
                    config.currentIndex = 0;

                    // Assign click handlers to the news lists
                    jQuery("a.newsYear").click(function() {
                        // Get the link ID
                        var linkID = jQuery(this).attr("rel");

                        // Split the link ID
                        var linkIDArr = linkID.split("_");

                        // Hide all lists
                        jQuery("div.newsList").hide();

                        // Restore all link states
                        jQuery("a.newsYear").removeClass("green10").removeClass("orange10").addClass("orange10");

                        // Highlight this link
                        jQuery(this).removeClass("orange10").addClass("green10");

                        // Show the appropriate list
                        //jQuery("div#list_" + linkIDArr[1]).show();
                        jQuery("div[rel=list_" + linkIDArr[1] + "]").show();
                    });

                    // Assign a click event
                    jQuery("a.newsItem").click(function(event)
					{

                    if(event.timeStamp > (config.lastEvent + config.animationDuration)) {
                        
                            // Grab the ID from this link
                            var linkID = jQuery(this).attr("id");
                            
                            // Split the ID into an array
                            var linkIDArr = linkID.split("_");
                            
                            // Make sure the slide exists
                            if(jQuery("li." + linkID).length > 0) {
                                // Set the lastEvent timestamp
                                config.lastEvent = event.timeStamp;

                                // Remove all link highlights
                                jQuery("a.newsItem").removeClass("highlight");

                                // Highlight the clicked link
                                jQuery(this).addClass("highlight");

                                // Find out the index's current position
                                var currentIndexPosition = parseInt(jQuery("li." + linkID).css("left"));

                                // Set the current index
                                config.currentIndex = linkIDArr[1];

                                // If the current index position is not zero (visible), then take action
                                if(currentIndexPosition != 0) {
                                    // Checking for positive or negative indexing position
                                    if(currentIndexPosition < 0) {
                                        // Negative index position
                                        obj.children(config.slideSelector).animate({left: "+=" + (0 - currentIndexPosition) }, {duration: config.animationDuration, easing: config.easingMethod});
                                    }
                                    else if(currentIndexPosition > 0) {
                                        // Positive index position
                                        obj.children(config.slideSelector).animate({left: "-=" + (0 + currentIndexPosition) }, {duration: config.animationDuration, easing: config.easingMethod});
                                    }
                                }

                                // Enforce container height size (if need be)
                                if(config.enforceContainerHeight == true) {
                                    obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
                                }
                            }
                        }
                    });
                break;

                case "home":
                    jQuery("img#scrollLeft").click(function()
					{
						if(config.currentIndex > 0)
						{
							obj.children(config.slideSelector).animate({left: "+=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
							config.currentIndex--;

							// Enforce container height size (if need be)
							if(config.enforceContainerHeight == true) {
								obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
							}
							
							// Render the control state
							renderControlState(config.currentIndex, config.lastIndex, obj, config.slideSelector, "img#scrollLeft", "img#scrollRight");
						}
                    });

                    jQuery("img#scrollRight").click(function()
					{
						if(config.currentIndex < (obj.children(config.slideSelector).length - 1))
						{
							obj.children(config.slideSelector).animate({left: "-=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
							config.currentIndex++;

							// Enforce container height size (if need be)
							if(config.enforceContainerHeight == true) {
								obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
							}
							
							// Render the control state
							renderControlState(config.currentIndex, config.lastIndex, obj, config.slideSelector, "img#scrollLeft", "img#scrollRight");
						}
                    });
                break;

                case "caseStudies":
                    jQuery("img.goLeft").click(function()
					{
                        obj.children(config.slideSelector).animate({left: "+=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        config.currentIndex--;

                        // Enforce container height size (if need be)
                        if(config.enforceContainerHeight == true)
						{
                            obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        }
                    });

                    jQuery("img.goRight").click(function()
					{
                        obj.children(config.slideSelector).animate({left: "-=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        config.currentIndex++;

                        // Enforce container height size (if need be)
                        if(config.enforceContainerHeight == true)
						{
                            obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        }
                    });
                break;

                case "process":
                    jQuery("a.slideToggle").click(function()
					{
                        // Determine scrolling direction
                        if(jQuery(this).attr("id") == "left")
						{
                            obj.children(config.slideSelector).animate({left: "-=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
                            config.currentIndex++;
                        }
                        else
						{
                            obj.children(config.slideSelector).animate({left: "+=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
                            config.currentIndex--;
                        }

                        // Enforce container height size (if need be)
                        if(config.enforceContainerHeight == true)
						{
                            obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        }
                    });
                break;

                case "ourWork":
                    jQuery("a.seeMore").click(function()
					{
                        if(jQuery(this).attr("id") == "left")
						{
                            obj.children(config.slideSelector).animate({left: "-=970px"}, {duration: config.animationDuration, easing: config.easingMethod});
                            config.currentIndex++;

                            if(config.currentIndex == 1)
							{
                                jQuery(this).html("<img src=\"~/media/Images/Our Work/see_full_client_list.ashx\" title=\"See Full Client List\" alt=\"See Full Client List\" border=\"0\" />");
                            }
                            else if(config.currentIndex == 2)
							{
                                jQuery(this).attr("id", "right").html("<img src=\"~/media/Images/Our Work/see_more_text.ashx\" title=\"See More Work\" alt=\"See More Work\" border=\"0\" />");
                            }
                        }
                        else
						{
                            obj.children(config.slideSelector).animate({left: "+=1940px"}, {duration: config.animationDuration, easing: config.easingMethod});
                            config.currentIndex = 0;

                            jQuery(this).attr("id", "left");
                        }

                        // Enforce container height size (if need be)
                        if(config.enforceContainerHeight == true) {
                            obj.animate({ height: obj.children("li.slide_" + config.currentIndex).height() + "px"}, {duration: config.animationDuration, easing: config.easingMethod});
                        }
                    });
                break;
            }
        });
		
		function renderControlState(currentIndex, lastIndex, pluginObj, slideSelector, leftImageSelector, rightImageSelector)
		{
			if(currentIndex == 0)
			{
				jQuery(leftImageSelector).attr({src: jQuery.arrowLeftHalfFaded.src}).css({cursor: "default"});
				jQuery(rightImageSelector).attr({src: jQuery.arrowRightHalf.src}).css({cursor: "pointer"});
			}
			else if(currentIndex == lastIndex)
			{
				jQuery(leftImageSelector).attr({src: jQuery.arrowLeftHalf.src}).css({cursor: "pointer"});
				jQuery(rightImageSelector).attr({src: jQuery.arrowRightHalfFaded.src}).css({cursor: "default"});
			}
			else
			{
				jQuery(leftImageSelector).attr({src: jQuery.arrowLeftHalf.src}).css({cursor: "pointer"});
				jQuery(rightImageSelector).attr({src: jQuery.arrowRightHalf.src}).css({cursor: "pointer"});
			}
		}
		
        return this;
    };
})(jQuery);