/*
*
* Generic Carousel Builder
* Wrote by Ashley Taylor
* For Candi International
*
* Copyright (c) 2009 Ashley Taylor
*
* Date: 2009-09-17 11:49:21 GMT (Thu, 17 Sept 2009)
*/
/*	Glossary
\
`--> Paddles:
|    These are the left and right arrows on the side of the 
|    carousel enabling previous and next functionality.
|
`--> Animation:
|    This is the name given to the automatic scrolling through 
|    the items and is disabled upon intervention.
|    
`--> Thumbnails:
|    In the carousel navigation, instead of 1, 2, 3, 4, 5 - it 
|    will display images instead. The images are set to fade in 
|    and fade out unlike 1, 2, 3, 4, 5 which just switch styles.
|
`--> Intro:
|    This is the first frame that is shown, if enabled. This 
|    frame is not included in the navigation and is only shown 
|    once and is unable to be accessed again except on page load.
|    
`--> Frame:
|    Name given to one of the heros in the carousel.
|    
`--> Speed:
|    This is the speed of the transition between frames.
|    If thumbnails are enabled they will also obey this speed.
|
`--> Pause:
|    The length of time it takes to start fading, display the image
|    and then prepare for the next frame. So 6000 (6 seconds) if you
|    want it visible for 4.5 seconds with a speed set to 1500.
|    Example: pause = non-animating time + transition length;
|
`--> $(carousel).data
|    We use this tool in jQuery to store our data so that if the
|    carousel is ever removed from the DOM, so are our settings
|    making the carousel self contained and having the ability
|    to have more than one carousel on each page.
|
`--------------------------------------------------// END //---->
*/
if (jQuery) {
    $(function() {
        function cInit(carousel) {
            function gup(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);
                if (results == null) {
                    return "";
                } else {
                    return results[1];
                }
            }
            if (gup('console') == "show") {
                $(carousel + ' #console').css('width', $(carousel + ' div.carouselWrap').css('width'));
                $(carousel).data("showConsole", true);
                $(carousel + " #console").fadeIn("slow");
                cDebugError("Loading console...");
            }
            function cDebugError(string) {
                if ($(carousel + " #console").length > 0 && $(carousel).data("showConsole") === true) {
                    $(carousel + " #console").prepend(string + "<br />");
                }
            }
            /*
            `--> Disable the script if the carousel doesn't exist.
            */
            if ($(carousel).length == 0) {
                cDebugError('<span style="color:red;font-weight:bold;">Carousel not found. Exiting carousel.</span>');
                return;
            } else {
                cDebugError('Loading carousel...');
            }
            /*
            `--> Checking to see if variables have already been defined.
            NOTE: These are PER CAROUSEL variables.
            */
            if ($(carousel).data("hasAnimation") == undefined) {
                if ($(carousel).hasClass("has-animation")) {
                    $(carousel).data("hasAnimation", true);
                } else {
                    $(carousel).data("hasAnimation", false);
                }
            }
            cDebugError('Has animation: <strong>' + $(carousel).data("hasAnimation").toString() + '</strong>');

            if ($(carousel).data("hasThumbnails") == undefined) {
                if ($(carousel).hasClass("no-thumbnails")) {
                    $(carousel).data("hasThumbnails", false);
                } else {
                    $(carousel).data("hasThumbnails", true);
                }
            }
            cDebugError('Has thumbs: <strong>' + $(carousel).data("hasThumbnails").toString() + '</strong>');

            if ($(carousel).data("hasIntro") == undefined) {
                if ($(carousel).hasClass("no-intro")) {
                    $(carousel).data("hasIntro", false);
                } else {
                    $(carousel).data("hasIntro", true);
                }
            }
            cDebugError('Has intro: <strong>' + $(carousel).data("hasIntro").toString() + '</strong>');

            if ($(carousel).data("hasPaddles") == undefined) {
                if ($(carousel).hasClass("has-paddles")) {
                    $(carousel).data("hasPaddles", true);
                } else {
                    $(carousel).data("hasPaddles", false);
                }
            }
            cDebugError('Has paddles: <strong>' + $(carousel).data("hasPaddles").toString() + '</strong>');

            if ($(carousel).data("cSpeed") == undefined) {
                $(carousel).data("cSpeed", 1000);
            }
            cDebugError('Carousel speed: <strong>' + $(carousel).data("cSpeed") + '</strong>');

            if ($(carousel).data("cPause") == undefined) {
                $(carousel).data("cPause", 5000);
            }
            cDebugError('Carousel pause: <strong>' + $(carousel).data("cPause") + '</strong>');
            /* Lets set up the carousel
            \
            `--> 1. First we need to check if there is an intro frame;
            |    if so, disable the very first navigation option.
            |    Otherwise - make it selected.
            |    The value determing if we need an intro frame is stored in
            |    $(carousel).data("hasIntro") as a true or false value.
            |
            `--> 2. Secondly, we need to find out how many items are actually
            |    in the carousel before we bother animating or binding
            |    functions to elements. We also check how many items are
            |    actually in the carousel just incase there is only one item
            |    and if thats the case we stop it altogether.
            |    Also setting the current, previous and next values here too.
            |
            `--> 3. Setting up the function that will move the carousel and
            |    the navigation when it is triggered.
            |
            `--> 4. Configuring the left and right paddles to scroll and to
            |    become visible on mouseover.
            |
            `--> 5. Make the navigation actually clickable, and visible.
            |
            `--> 6. Auto-scrolling if enabled!
            |
            `--------------------------------------------------// END //---->
            */
            /* -- Step 1 -- */
            if ($(carousel).data("hasIntro")) {
                $(carousel + ' li.pagination-1').css("display", "none");
                cDebugError('Hiding intro navigation item');
            } else {
                $(carousel + ' li.pagination-1').addClass("selected");
                cDebugError('Selecting first navigation item');
            }
            /* -- Step 2 -- */
            $(carousel).data("cItems", $(carousel + " ul.carouselItem li").length);
            if ($(carousel).data("cItems") == 1) {
                cDebugError('<span style="color:red;font-weight:bold;">Only 1 item. Exiting carousel.</span>');
                return;
            } else {
                $(carousel).data("cCurrent", 1);
                $(carousel).data("cNext", 2);
                $(carousel).data("cPrevious", $(carousel).data("cItems"));
                cDebugError('Carousel items: <strong>' + $(carousel).data("cItems") + '</strong>');
            }
            /* -- Step 3 -- */
            function cFade(fadeTo, fadeFrom) {
                if (fadeTo != $(carousel).data("cCurrent")) {
                    cDebugError('Fading from: ' + fadeFrom + ' to: ' + fadeTo);
                    $(carousel + " li.carouselItem-" + fadeTo).fadeIn($(carousel).data("cSpeed"));
                    $(carousel + " li.carouselItem-" + fadeFrom).fadeOut($(carousel).data("cSpeed"));
                    if ($(carousel).data("hasThumbnails")) {
                        $(carousel + " li.pagination-" + fadeTo + " img").fadeIn($(carousel).data("cSpeed"));
                        $(carousel + " li.pagination-" + fadeFrom + " img").fadeOut($(carousel).data("cSpeed"));
                    } else {
                        $(carousel + ' li.pagination-' + fadeFrom).removeClass("selected");
                        $(carousel + ' li.pagination-' + fadeTo).addClass("selected");
                    }
                    $(carousel).data("cCurrent", fadeTo);
                    if ($(carousel).data("hasIntro") === true) {
                        if ($(carousel).data("cCurrent") == 2) {
                            $(carousel).data("cPrevious", $(carousel).data("cItems"));
                            $(carousel).data("cNext", 3);
                        } else if ($(carousel).data("cCurrent") == $(carousel).data("cItems")) {
                            $(carousel).data("cPrevious", $(carousel).data("cItems") - 1);
                            $(carousel).data("cNext", 2);
                        } else {
                            $(carousel).data("cNext", $(carousel).data("cCurrent") + 1);
                            $(carousel).data("cPrevious", $(carousel).data("cCurrent") - 1);
                        }
                    } else {
                        if ($(carousel).data("cCurrent") == 1) {
                            $(carousel).data("cPrevious", $(carousel).data("cItems"));
                            $(carousel).data("cNext", 2);
                        } else if ($(carousel).data("cCurrent") == $(carousel).data("cItems")) {
                            $(carousel).data("cPrevious", $(carousel).data("cItems") - 1);
                            $(carousel).data("cNext", 1);
                        } else {
                            $(carousel).data("cNext", $(carousel).data("cCurrent") + 1);
                            $(carousel).data("cPrevious", $(carousel).data("cCurrent") - 1);
                        }
                    }
                } else {
                    cDebugError('<span style="color:red;font-weight:bold;">Attempting to fade to itself!</span>');
                }
            }
            function cNavFade(fadeTo) {
                var fadeFrom = $(carousel).data("cCurrent");
                cFade(fadeTo, fadeFrom);
            }
            /* -- Step 4 -- */
            if ($(carousel).data("hasPaddles")) {
				if ($(carousel).data("cItems") >= 2) {
					$(carousel + " div.carouselWrap").hover(function() {
						$(carousel + " div.cNext").fadeIn("fast");
						$(carousel + " div.cPrevious").fadeIn("fast");
						cDebugError('Showing paddles');
					}, function() {
						$(carousel + " div.cNext").fadeOut("fast");
						$(carousel + " div.cPrevious").fadeOut("fast");
						cDebugError('Hiding paddles');
					});
					$(carousel + " div.cNext").click(function() {
						cFade($(carousel).data("cNext"), $(carousel).data("cCurrent"));
						if ($(carousel).data("hasAnimation")) {
							$(carousel).data("hasAnimation", false);
							cDebugError('User intervention, animation disabled.');
						}
						return false;
					});
					$(carousel + " div.cPrevious").click(function() {
						cFade($(carousel).data("cPrevious"), $(carousel).data("cCurrent"));
						return false;
						if ($(carousel).data("hasAnimation")) {
							$(carousel).data("hasAnimation", false);
							cDebugError('User intervention, animation disabled.');
						}
					});
				} else {
					cDebugError('<span style="color:red;font-weight:bold;">There is only one carousel item. Disabling paddles.</span>');
				}
            }
            /* -- Step 5 -- */
            $(carousel + ' li.pagination-1').addClass("selected");
            $(carousel + ' ul.pagination').fadeIn("fast");
            var tmp = "";
            $(carousel + ' ul.pagination li').click(function() {
                tmp = $(this).attr("class").replace(/pagination-/g, "");
                tmp = tmp.replace(/ selected/g, "");
                cNavFade(tmp);
                if ($(carousel).data("hasAnimation")) {
                    $(carousel).data("hasAnimation", false);
                    cDebugError('User intervention, animation disabled.');
                }
                return false;
            });
            /* -- Step 6 -- */
            if ($(carousel).data("hasAnimation")) {
                $(carousel).everyTime($(carousel).data("cPause"), function() {
                    if ($(carousel).data("hasAnimation")) {
                        cFade($(carousel).data("cNext"), $(carousel).data("cCurrent"));
                    }
                });
            }
        }
        cInit("#carouselBlock");
    });
}
