var featuredMugSecond = 0;
var isLooping = false;
var keepLooping = false;

jQuery("document").ready(function() {
	
	jQuery("ul.mug-selector li").mouseover(function () {
		jQuery(this).addClass("sfHover");
	});
	jQuery("ul.mug-selector li").mouseout(function () {
		jQuery(this).removeClass("sfHover");
	});
	
	jQuery("body").addClass("enhanced");
	
	pause();
	jQuery("ul.mug-selector li").filter(":first").addClass("current");

	jQuery.history.init(function (hash) {
	    if (hash.length == 0) {
            hash = window.location.hash.substr(1);
        }
        if (hash.length > 0) {
            var paramsAndValues = hash.split("/");
            var params = new Array();
            var param;
            var isParam = true;
            for (var i in paramsAndValues) {
                var paramOrValue = paramsAndValues[i];
                if (isParam) {
                    param = paramOrValue;
                } else {
                    params[param] = paramOrValue;
                }
                isParam = !isParam;
            }
            var feature = params["feature"];
            jQuery("ul.mug-selector li.current").removeClass("current");
            jQuery("ul.mug-selector li#feature-" + feature).addClass("current");
            var location = window.location.toString().split('#');
            location = location[0];
            jQuery("dl.mug-feature dt,dl.mug-feature dd").fadeOut(500, function () {
                jQuery("dl.mug-feature").empty();
                jQuery("dl.mug-feature").load(location + "?feature=" + feature + "&rand=" + randomString() + "&context=plain dl.mug-feature dt,dl.mug-feature dd");
                jQuery("dl.mug-feature dt,dl.mug-feature dd").fadeIn(500);
            });
        } else {
            var location = window.location.toString().split('?');
            if (location.length > 1) {
                sendLocation = location[1];
                sendLocation = sendLocation.replace('=', '&');
                var paramsAndValues = sendLocation.split("&");
                sendLocation = paramsAndValues.join('/');
                sendLocation = location[0] + "#" + sendLocation;
                window.location.href = sendLocation;
            } else {
                play();
            }
        }
    });

    jQuery.timer(1000, function (timer) {
        if (isLooping) {
            featuredMugSecond++;
            if (featuredMugSecond >= 5) {
                featuredMugSecond = 0;
                keepLooping = true;
                moveNext();
                keepLooping = false;
            }
	    }
    });

    jQuery("#previous").click(function() {
        pause();
        movePrevious();
    });
    jQuery("#play").click(function() {
        moveNext();
        play();
    });
    jQuery("#pause").click(function() {
        pause();
    });
    jQuery("#next").click(function() {
        pause();
        moveNext();
    });
    
    jQuery("ul.mug-selector li a").click(function() {
        if (keepLooping == false) {
            pause();
            scroll(0,0);
        }
    });
    
    jQuery("a[@rel='history']").click(function() {
        var href = jQuery(this).attr("href");
        var queryString = href.substring(href.indexOf('?') + 1, href.length);
        queryString = queryString.replace('=', '&');
        var paramsAndValues = queryString.split("&");
        jQuery.history.load(paramsAndValues.join("/"));
        return false;
    });
});

    function moveNext() {
        if (jQuery("ul.mug-selector li.current").next("li").size() == 0) {
            jQuery("ul.mug-selector li.current").removeClass("current");
            jQuery("ul.mug-selector li").filter(":first").addClass("current");
        } else {
            jQuery("ul.mug-selector li.current").removeClass("current").next("li").addClass("current");
        }
        selectCurrent();
    };
    function movePrevious() {
        if (jQuery("ul.mug-selector li.current").prev("li").size() == 0) {
            jQuery("ul.mug-selector li.current").removeClass("current");
            jQuery("ul.mug-selector li").filter(":last").addClass("current");
        } else {
            jQuery("ul.mug-selector li.current").removeClass("current").prev("li").addClass("current");
        }
        selectCurrent();
    };
    function selectCurrent() {
        jQuery("ul.mug-selector li.current a").click();
    }
    function play() {
        featuredMugSecond = 0;
        isLooping = true;
        keepLooping = false;
        jQuery("ul.controls li a#play").removeClass("active").addClass("inactive");
        jQuery("ul.controls li a#pause").removeClass("inactive").addClass("active");
    };
    function pause() {
        isLooping = false;
        keepLooping = false;
        jQuery("ul.controls li a#play").removeClass("inactive").addClass("active");
        jQuery("ul.controls li a#pause").removeClass("active").addClass("inactive");
    };

    function randomString() {
       var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
       var string_length = 8;
       var randomstring = '';
       for (var i=0; i<string_length; i++) {
           var rnum = Math.floor(Math.random() * chars.length);
           randomstring += chars.substring(rnum,rnum+1);
       }
       return randomstring;
   }