/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

; (function($) {
    $.fn.superfish = function(op) {

        var sf = $.fn.superfish,
        c = sf.c,
        $arrow = $(['<span class="', c.arrowClass, '"> &#187;</span>'].join('')),
        over = function() {
            var $$ = $(this),
            menu = getMenu($$);
            clearTimeout(menu.sfTimer);
            $$.showSuperfishUl().siblings().hideSuperfishUl();
        },
        out = function() {
            var $$ = $(this),
            menu = getMenu($$),
            o = sf.op;
            clearTimeout(menu.sfTimer);
            menu.sfTimer = setTimeout(function() {
                o.retainPath = ($.inArray($$[0], o.$path) > -1);
                $$.hideSuperfishUl();
                if (o.$path.length && $$.parents(['li.', o.hoverClass].join('')).length < 1) {
                    over.call(o.$path);
                }
            },
            o.delay);
        },
        getMenu = function($menu) {
            var menu = $menu.parents(['ul.', c.menuClass, ':first'].join(''))[0];
            sf.op = sf.o[menu.serial];
            return menu;
        },
        addArrow = function($a) {
            $a.addClass(c.anchorClass).append($arrow.clone());
        };

        return this.each(function() {
            var s = this.serial = sf.o.length;
            var o = $.extend({},
            sf.defaults, op);
            o.$path = $('li.' + o.pathClass, this).slice(0, o.pathLevels).each(function() {
                $(this).addClass([o.hoverClass, c.bcClass].join(' '))
                .filter('li:has(ul)').removeClass(o.pathClass);
            });
            sf.o[s] = sf.op = o;

            $('li:has(ul)', this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent': 'hover'](over, out).each(function() {
                if (o.autoArrows) addArrow($('>a:first-child', this));
            })
            .not('.' + c.bcClass)
            .hideSuperfishUl();

            var $a = $('a', this);
            $a.each(function(i) {
                var $li = $a.eq(i).parents('li');
                $a.eq(i).focus(function() {
                    over.call($li);
                }).blur(function() {
                    out.call($li);
                });
            });
            o.onInit.call(this);

        }).each(function() {
            var menuClasses = [c.menuClass];
            if (sf.op.dropShadows && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
            $(this).addClass(menuClasses.join(' '));
        });
    };

    var sf = $.fn.superfish;
    sf.o = [];
    sf.op = {};
    sf.IE7fix = function() {
        var o = sf.op;
        if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity != undefined)
        this.toggleClass(sf.c.shadowClass + '-off');
    };
    sf.c = {
        bcClass: 'sf-breadcrumb',
        menuClass: 'sf-js-enabled',
        anchorClass: 'sf-with-ul',
        arrowClass: 'sf-sub-indicator',
        shadowClass: 'sf-shadow'
    };
    sf.defaults = {
        hoverClass: 'sfHover',
        pathClass: 'overideThisToUse',
        pathLevels: 1,
        delay: 800,
        animation: {
            opacity: 'show'
        },
        speed: 'normal',
        autoArrows: true,
        dropShadows: true,
        disableHI: false,
        // true disables hoverIntent detection
        onInit: function() {},
        // callback functions
        onBeforeShow: function() {},
        onShow: function() {},
        onHide: function() {}
    };
    $.fn.extend({
        hideSuperfishUl: function() {
            var o = sf.op,
            not = (o.retainPath === true) ? o.$path: '';
            o.retainPath = false;
            var $ul = $(['li.', o.hoverClass].join(''), this).add(this).not(not).removeClass(o.hoverClass)
            .find('>ul').hide().css('visibility', 'hidden');
            o.onHide.call($ul);
            return this;
        },
        showSuperfishUl: function() {
            var o = sf.op,
            sh = sf.c.shadowClass + '-off',
            $ul = this.addClass(o.hoverClass)
            .find('>ul:hidden').css('visibility', 'visible');
            sf.IE7fix.call($ul);
            o.onBeforeShow.call($ul);
            $ul.animate(o.animation, o.speed,
            function() {
                sf.IE7fix.call($ul);
                o.onShow.call($ul);
            });
            return this;
        }
    });

})(jQuery);

// nundge effect
jQuery.fn.nudge = function(params) {
    //set default parameters
    params = jQuery.extend({
        amount: 20,
        //amount of pixels to pad / marginize
        duration: 300,
        //amount of milliseconds to take
        property: 'padding',
        //the property to animate (could also use margin)
        direction: 'left',
        //direction to animate (could also use right)
        toCallback: function() {},
        //function to execute when MO animation completes
        fromCallback: function() {}
        //function to execute when MOut animation completes
    },
    params);
    //For every element meant to nudge...
    this.each(function() {
        //variables
        var $t = jQuery(this);
        var $p = params;
        var dir = $p.direction;
        var prop = $p.property + dir.substring(0, 1).toUpperCase() + dir.substring(1, dir.length);
        var initialValue = $t.css(prop);
        /* fx */
        var go = {};
        go[prop] = parseInt($p.amount) + parseInt(initialValue);
        var bk = {};
        bk[prop] = initialValue;
        //Proceed to nudge on hover
        $t.hover(function() {
            $t.stop().animate(go, $p.duration, '', $p.toCallback);
        },
        function() {
            $t.stop().animate(bk, $p.duration, '', $p.fromCallback);
        });
    });
    return this;
};

/* "clearField" by Stijn Van Minnebruggen
   http://www.donotfold.be  */
 (function(jQuery) {
    jQuery.fn.clearField = function(settings) {
        settings = jQuery.extend({
            blurClass: 'clearFieldBlurred',
            activeClass: 'clearFieldActive'
        },
        settings);
        jQuery(this).each(function() {
            var el = jQuery(this);
            if (el.attr('rel') == undefined) {
                el.attr('rel', el.val()).addClass(settings.blurClass);
            }
            el.focus(function() {
                if (el.val() == el.attr('rel')) {
                    var v = '';
                    if (el.attr('name') == 'url') v = 'http://';
                    el.val(v).removeClass(settings.blurClass).addClass(settings.activeClass);
                }
            });
            el.blur(function() {
                if ((el.val() == ('')) || ((el.attr('name') == 'url') && (el.val() == ('http://')))) {
                    el.val(el.attr('rel')).removeClass(settings.activeClass).addClass(settings.blurClass);
                }
            });
        });
        return jQuery;
    };
})(jQuery);

jQuery(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */
	
	var totWidth=0;
	var positions = new Array();
	
	jQuery('#slides .slide').each(function(i){
		
		/* Traverse through all the slides and store their accumulative widths in totWidth */
		
		positions[i]= totWidth;
		totWidth += jQuery(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!jQuery(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
		
	});
	
	jQuery('#slides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	jQuery('#menu ul li a').click(function(e,keepScroll){

			/* On a thumbnail click */

			jQuery('li.menuItem').removeClass('act').addClass('inact');
			jQuery(this).parent().addClass('act');
			
			var pos = jQuery(this).parent().prevAll('.menuItem').length;
			
			jQuery('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450);
			/* Start the sliding animation */
			
			e.preventDefault();
			/* Prevent the default action of the link */
			
			
			// Stopping the auto-advance if an icon has been clicked:
			if(!keepScroll) clearInterval(itvl);
	});
	
	jQuery('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ****/
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		jQuery('#menu ul li a').eq(current%jQuery('#menu ul li a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 10;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);

	/* End of customizations */
});




jQuery(function() {
    jQuery("#menu").superfish();
});
