/**
* 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(e){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);

/*--------------------------------------------------------------------------*/

var SlideShow = function (container, slides, delay, initial) {
    this.container = container;
    this.slides = slides;
    this.delay = delay;
    this.current = initial;
    this.togglers = this.container.find('.togglers li');
    this.setup();
};
SlideShow.prototype = {
    setup: function () {
        var self = this;
        this.showOnly($(this.slides[this.current]));
        $(this.togglers[this.current]).addClass('active');
        
        this.togglers.bind('click', function () {
            self.pause();
            self.goTo($(this).index());
        });
				
		this.start();
    },
    showOnly: function (el) {
        this.slides.hide();
        el.show();
    },
    goTo: function (idx) {
        var self = this;
		
        if (idx === this.current) {	
            return;
        }
        $(this.togglers[this.current]).removeClass('active');
        $(this.slides[this.current]).fadeOut('slow');
        this.current = idx;
		
        $(this.togglers[this.current]).addClass('active');
        $(this.slides[this.current]).fadeIn('slow');
		
        this.start();
    },
    start: function () {
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else {
            return;
        }
    },
    next: function() {
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};

/*--------------------------------------------------------------------------*/

var ToolTip = function (trigger, pop) {
	this.trigger = $(trigger);
	this.pop = $(pop);
    this.setup();
};
ToolTip.prototype = {
	setup: function () {
		var self = this;
		$(this.trigger).hoverIntent({
			interval: 100,
			over: function () {
				$(this).addClass('active');
				self.open();
			},
			out: function () {
				$(this).removeClass('active');
				self.close();
			}
		});
	},
	open: function () {
		this.pop.find('.inner').css("height", (this.pop.find('.content').height() - 214));
		this.pop.css("visibility", "visible");
		//this.pop.show();
	},
	close: function () {
		this.pop.css("visibility", "hidden");
	}
};

/*--------------------------------------------------------------------------*/

$(document).ready(function () {
	if ($.browser.msie && $.browser.version=="6.0") {
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch (e) {}
	}

	if ($('#boxtext li').length) {
		$('#boxtext li').each(function () {
			new ToolTip($(this), $(this).find('.popup'));
		});
	}
	
	if ($('#adstext').length) {
		new SlideShow($('#adstext'), $('#adstext .slide'), 5, 0);
	}
	
	if ($('#freeresourcetext').length) {
		new SlideShow($('#freeresourcetext'), $('#freeresourcetext .slide'), 5, 0);
	}	
});
