var fcFunctions2 = {
    feature: function(o) {
        function hideLastRolled(lR, fC) {
            if (lR == fC) {
                element = "#" + $(lR).attr('id');
            }

            else {
                element = "#" + $(lR).attr('id') + "_div";
            }
            return element;
        }
    },

    ieHover: function(o) {
        var list = o.list;

        $(list).find("li").hover(
			function() {
			    if ($(this).hasClass("first")) {
			        $(this).removeClass("first");
			        $(this).addClass("first-hover");
			    }
			    if ($(this).hasClass("first-active")) {
			        $(this).removeClass("first-active");
			        $(this).addClass("first-active-hover");
			    }
			    if ($(this).hasClass("last")) {
			        $(this).removeClass("last");
			        $(this).addClass("last-hover");
			    }
			    if ($(this).hasClass("last-active")) {
			        $(this).removeClass("last-active");
			        $(this).addClass("last-active-hover");
			    }

			},
			function() {
			    if ($(this).hasClass("first-hover")) {
			        $(this).removeClass("first-hover");
			        $(this).addClass("first");
			    }
			    if ($(this).hasClass("first-active")) {
			        $(this).removeClass("first-active-hover");
			        $(this).addClass("first-active");
			    }
			    if ($(this).hasClass("last-active-hover")) {
			        $(this).removeClass("last-active-hover");
			        $(this).addClass("last-active");
			    }
			    if ($(this).hasClass("last-hover")) {
			        $(this).removeClass("last-hover");
			        $(this).addClass("last");
			    }
			}
		);
    },
    dropdownmenu: function() {
        var borderRight = $(window).width();
        $(window).resize(function() {
            borderRight = $(window).width();
        });
        $('#navigation > ul > *').each(function(i) {
            var thisWidth = $(this).width();
            var dropdownOffset = $(this).offset();
            var dropdownWidth = $(this).children('ul').width();
            var dropdownActual = (dropdownOffset.left + dropdownWidth + 10); //jQuery doesnt get the padding for some reason, so add in manually.
            var dropdownResetPos = (thisWidth - dropdownWidth - 10);



        });
        $('li a').bind('focus mouseenter', function() {
            $(this).parents('li').addClass('active');
            $("#navigation").css({ "z-index": "200" });
        });

        $('li a').bind('blur mouseleave', function() {
            $(this).parents('li').removeClass('active');
            $("#navigation").css({ "z-index": "10" });
        });
    },
    slideshowCount: 0,
    slideshow: function() {
        var carousel = $('div.carousel');
        var items = $('div.item', carousel);
        var numItems = ($(items).length);
        var isPauseSet = false;
        var firstItem = $(items).eq(0);
        var firstFormBoxM = $('div.formbox-m', firstItem);
        var ext = ".gif";

        carousel.addClass('js');
        $(items).css('display', 'none'); //hide all items
        $(firstItem).css('display', 'block'); //show the first

        //add the player controls and controls for each specific item
        var addControls = function() {
            $('div.carousel').append('<div class="overlay"><div class="padding clear"><ul class="controls-item clear"></ul></div></div>');

            var controlsItem = $('ul.controls-item', 'div.overlay');

            //add a control for each specific item
            for (i = 0; i < numItems; i++) {
                controlsItem.append('<li><a href="#"><img src="/new/images/carousel/carousel-control-item' + ext + '" width="21" height="6" alt="Item ' + i + '" /></a></li>');
            }
            $('div.padding', 'div.carousel').append('<ul class="controls clear"><li class="previous"><a href="#"><img src="/new/images/carousel/carousel-control-previous' + ext + '" width="20" height="22" alt="Previous" /></a></li><li class="pause"><a href="#"><img src="/new/images/carousel/carousel-control-pause' + ext + '" width="20" height="22" alt="Pause slideshow" /></a></li><li class="next"><a href="#"><img src="/new/images/carousel/carousel-control-next' + ext + '" width="20" height="22" alt="Next" /></a></li></ul>');

            var el = $('li:first-child', controlsItem);
            if (FC.hasIE6) {
                el.addClass('first');
            }

            //set first item to active
            //the active image has the same name with '-on'
            //split the src up to the bit before the extension and the bit after, but using the last occurance of '.'
            el.addClass('active');
            var imgEl = $('img', $(el));
            var imgSrc = $(imgEl)[0].src;
            var lastDot = imgSrc.lastIndexOf('.');
            var newSrcStart = imgSrc.substring(0, lastDot);
            var newSrcEnd = imgSrc.substring(lastDot, imgSrc.length);

            var newImgSrc = newSrcStart + "-on" + newSrcEnd;

            el.addClass('active');
            $(imgEl)[0].src = newImgSrc;
        };

        var performSwitch = function() {
            //numItems is zero indexed, so it's 1 more than fcFunctions2.slideshowCount
            if (fcFunctions2.slideshowCount < numItems - 1) {
                var nextItem = $(items).eq(fcFunctions2.slideshowCount + 1);

                $(items).css('display', 'none');
                $(nextItem).css('display', 'block');
                fcFunctions2.slideshowCount++;
            }

            else {
                //current item is the last one, so show the first one again
                fcFunctions2.slideshowCount = 0;
                var currentItem = $(items).eq(fcFunctions2.slideshowCount);

                $(items).css('display', 'none');
                $(currentItem).css('display', 'block');
            }
            activateItem();
        };

        // call performSwitch every 2000 ms for 12 times.  12 is equal to 3 complete run throughs
        //if isPauseSet is true, then pause button has been pressed,
        //so just switch to next item
        var switchItem = function() {
            var time = 5000;

            if (!isPauseSet) {
                $(document).everyTime(time, function() {
                    performSwitch();
                }, 0, 12);
            }
            else {
                performSwitch();
            }

        };

        //change currently active item to unactive
        var deactivateItem = function() {
            var el = $('ul.controls-item li.active');
            var imgEl = $('img', $(el));
            var imgSrc = $(imgEl)[0].src;

            $(el).removeClass('active');
            var partToRemove = imgSrc.indexOf('-on');
            var newSrcStart = imgSrc.substring(0, partToRemove);
            var newSrcEnd = imgSrc.substring(partToRemove + 3, imgSrc.length);
            var newImgSrc = newSrcStart + newSrcEnd;

            $(imgEl)[0].src = newImgSrc;
        };

        //change image item to active colour
        var activateItem = function() {
            deactivateItem(); //deactivate the currently active item before iterating and activating the newly selected item

            var el = $('ul.controls-item li').eq(fcFunctions2.slideshowCount);
            el.addClass('active');

            var imgEl = $('img', $(el));
            var imgSrc = $(imgEl)[0].src;
            var lastDot = imgSrc.lastIndexOf('.');
            var newSrcStart = imgSrc.substring(0, lastDot);
            var newSrcEnd = imgSrc.substring(lastDot, imgSrc.length);

            var newImgSrc = newSrcStart + "-on" + newSrcEnd;

            el.addClass('active');
            $(imgEl)[0].src = newImgSrc;

        };


        //a specific item control has been selected
        //this works finds the current item that's being displayed (fcFunctions2.slideshowCount)
        //and sets the specific item control equal to that number to active

        //i.e. if fcFunctions2.slideshowCount is 3, then the 3rd item control needs to be active
        var goToItem = function() {
            $(document).stopTime();
            var currentItem = $(items).eq(fcFunctions2.slideshowCount);
            $(items).css('display', 'none');

            $(currentItem).css('display', 'block');

            activateItem();
            if (!isPauseSet) {
                switchItem();
            }
        };

        //
        var addItemControls = function() {
            var index = $('ul.controls-item a').index();

            $('li', 'ul.controls-item').bind('click', function() {
                var index = $('ul.controls-item li').index(this);
                fcFunctions2.slideshowCount = index;
                goToItem();
                return false;
            });
        };


        //unset the isPauseSet variable, change the play icon back to pause
        //and switch to the next item
        var playHandler = function(el) {
            isPauseSet = false;
            $(el).removeClass('play').addClass('pause');
            $('img', $(el)).attr({
                src: '/new/images/carousel/carousel-control-pause' + ext,
                alt: 'Pause slideshow'
            });

            //unbind the previous click event and bind a new one
            $('li.pause', 'ul.controls').unbind().bind('click', function() {
                pauseHandler(this);
                return false;
            });
            switchItem();
        };

        //set the isPauseSet variable to true(so rotation stops)
        var pauseHandler = function(el) {
            isPauseSet = true;
            $(document).stopTime();

            //switch icon to the play icon
            $(el).removeClass('pause').addClass('play');
            $('img', $(el)).attr({
                src: '/new/images/carousel/carousel-control-play' + ext,
                alt: 'Resume slideshow'
            });

            //unbind the previous click event and bind a new one
            $('li.play', 'ul.controls').unbind().bind('click', function() {
                playHandler(this);
                return false;
            });

        };

        //iterate the current item variable and then call function to activate that item
        var nextHandler = function() {
            $(document).stopTime();

            //numItems is zero indexed, so it's 1 more than fcFunctions2.slideshowCount
            if (fcFunctions2.slideshowCount < numItems - 1) {
                fcFunctions2.slideshowCount++;
            }
            else {
                //the last item is being shown so set variable back to zero to show the firs item
                fcFunctions2.slideshowCount = 0;
            }
            goToItem();
        };

        //iterate current item variable and then call function to activate that item
        var previousHandler = function() {
            $(document).stopTime();
            //numItems is zero indexed, so it's 1 more than fcFunctions2.slideshowCount
            if (fcFunctions2.slideshowCount !== 0) {
                fcFunctions2.slideshowCount--;
            }
            else {
                //the last item is being shown so set variable back to zero to show the firs item
                fcFunctions2.slideshowCount = numItems - 1;
            }
            goToItem();

        };

        //stop iteration when hovering
        var mouseOverHandler = function() {
            $(document).stopTime();
        };

        //stop iteration when an anchor in the carousel has focus
        var focusHandler = function() {
            $(document).stopTime();
        };

        //restart rotation when an anchor inside the carousel has lost focus
        var blurHandler = function() {
            $(document).stopTime();
            if (!isPauseSet) {
                switchItem();
            }
        };

        //restart rotation when the mouse has left the carousel
        var mouseLeaveHandler = function() {
            $(document).stopTime();
            if (!isPauseSet) {
                switchItem();
            }
        };

        //add click and focus events to the controls
        var addControlFunctionality = function() {
            $('li.pause', 'ul.controls').unbind().bind('click', function() {
                pauseHandler(this);
                return false;
            });

            $('li.next', 'ul.controls').unbind().bind('click', function() {
                nextHandler();
                return false;
            });

            $('li.previous', 'ul.controls').unbind().bind('click', function() {
                previousHandler();
                return false;
            });
            $('div.carousel').bind('mouseenter', mouseOverHandler);
            $('div.carousel').bind('mouseleave', mouseLeaveHandler);
            $('div.carousel div.info a').bind('focus', focusHandler);
            $('div.carousel div.info a').bind('blur', blurHandler);
        };

        addControls();
        switchItem();
        addControlFunctionality();
        addItemControls();
    }
};

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  
};

$(document).ready(function() {
						   
							   
							
		if($("#tb-content").length>0){
			$("#tb-content").hide();	
		}						   
						   
    fcFunctions2.dropdownmenu();
	//navigation - toggle link 'all products'
	$('#nav-all > a').toggle(function() {$("#all-products").show(0);return false;},function(){$("#all-products").hide(0); return false;});
	$('#hide-arrow').click(function() {		  
	  $('#nav-all > a').click();  
	});
	
	if($('div.carousel').length) {
		fcFunctions2.slideshow();
	}
});
