/**
 * Implementation of jcarousel slideshows for various pages and modules
 * @author Edgar Morales
 * @version 2010.03.07
 */

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(ex){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

function renderSlideShow(inst){
	jQuery(inst).jcarousel({
        scroll: 1,                  //-- how many items to scroll at a time
        auto: 5,                    //-- seconds before auto-scrolling
        wrap: "both",               //-- it should wrap when scrolling in which direction
        buttonNextHTML: null,       //-- remove auto-generated next button
        buttonPrevHTML: null,       //-- remove auto-generated previous button
        animation: 0,               //-- 0 turns off animation, which would slide the stage by default
        itemFirstInCallback: function(jc, item, idx, state) {
            var parent = inst;
            //-- toggle the style of the selected control --//
            jQuery(parent + ' .controls .nav.active').removeClass('active');
            jQuery(parent + ' .controls .nav').eq(idx - 1).addClass('active');
        },
        itemLoadCallback: {
            onBeforeAnimation: function(jc, state, evt) {
                var parent = inst;
                //-- hide the main stage elements --//
                jQuery(parent + ' .jcarousel-item img').hide();
                jQuery(parent + ' .jcarousel-item .title-overlay').hide();
            },
            onAfterAnimation: function(jc, state, evt) {
                var parent = inst;
                //-- ... and then fade them back into view --//
                jQuery(parent + ' .jcarousel-item img').fadeIn(200);
                jQuery(parent + ' .jcarousel-item .title-overlay').css('filter','alpha(opacity=80)').fadeIn(150);
            }
        },
        initCallback: function(jc, state) {
            if (state == 'init') {
                var t;
                jc.startAutoOrig = jc.startAuto;
                jc.startAuto = function() {
                    if (!jc.paused) {
                    	// console.log("autostarting...");
                        jc.startAutoOrig();
                    }
                };
                jc.pause = function() {
                    if(jc.paused !== true) {
                        // console.log("pausing...");
                        jc.paused = true;
                        jc.stopAuto();
                    }
                };
                jc.play = function() {
                    // console.log("back in effect!");
                    jc.paused = false;
                    jc.startNow();
                };
                jQuery(inst)
                    .mouseleave( function() {
                        if(jc.paused === true) {
                            // console.log("0noz, you've left!");
                            clearTimeout(t);
                            t = setTimeout(jc.play, 5000);
                        }
                    })
                    .mouseenter( function() {
                        if(jc.paused === true) {
                            clearTimeout(t);
                             // console.log("Ahhhh, you're back!");
                        }
                    }
                );
                //-- Assigns functionality to our custom controls --//
                jQuery(inst + ' .controls .nav').hoverIntent({
                    interval: 50,
                    out:    function() {},
                    over:   function() {
                        jc.pause();
                        var index = jQuery.jcarousel.intval(jQuery(this).attr('id').replace('nav-',''));
                        //console.log(jc.animating);
                        if (jc.animating === false){
                            jc.scroll(index);
                        } 
                        return false;
                    }
                });
                jQuery(inst + ' .controls .nav').each( function() {
                    var curIdx = jQuery(inst + ' .controls nav').index(this);
                    //var lnkHref = jQuery(inst + ' .jcarousel-item a').eq(curIdx).attr('href');
                    //jQuery(this).attr('href', lnkHref);
                });
            }
        }
	});
}