// Tabs
$.fn.tabs = function () {
    return this.each(function () {
        $tabs = $(this);
        if (!$tabs.hasClass('tabsDone')) {
			$('> div > div', $tabs).hide();
			var pageurl = window.location.href;
			var tabURL = pageurl.split("#");
			var tIndex = (tabURL[1] != undefined)? $('> ul',$tabs).find('a[href="#'+tabURL[1]+'"]').parent().prevAll('li').length : 0;
			$('> ul li:eq(' + tIndex + ')', $tabs).addClass('active');
            $('> div > div:eq(' + tIndex + ')', $tabs).show();
            $('> ul li', $tabs).click(function () {
                var tabIndex = $(this).prevAll('li:not(.disabled)').length;
                $(this).siblings('li.active').removeClass('active');
                var $tabLi = $(this);
                $tabLi.addClass('active');
                $tabLi.parent().next().find('>div:visible').hide();
                $currentTab = $tabLi.parent().next().find('>div:eq(' + tabIndex + ')');
                $currentTab.show();
            });
        }
        $tabs.addClass('tabsDone');
    });
};

// Logo slider
$.fn.imageSlider = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul'),
                $items = $slider.find('> li'),
                $single = $items.filter(':first');
           
           var wrapperWidth = $(this).width() - parseInt($wrapper.css('margin-left')) - parseInt($wrapper.css('margin-right'));
           $wrapper.css('width', wrapperWidth);
                
                singleWidth = $single.outerWidth(),
                visible = Math.floor($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);

                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft((singleWidth * visible) - 1);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back"></a><a href="#" class="arrow forward"></a>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
};

// Tooltip
$.fn.simpletooltip = function(){
  return this.each(function() {
    var text = $(this).attr("title");
    $(this).attr("title", "");
    if(text != 'undefined' && text != '') {
      $(this).hover(function(e){
        $(this).attr("title", ""); 
        $("body").append('<div id="simpleTooltip" style="position: absolute; z-index: 100; display: none;"><div class="top"></div><div class="content clearfix">' + text + '</div><div class="bottom"></div></div>');
        var theight = $('#simpleTooltip').height();
        var tipX = e.pageX + 25;
        var tipY = e.pageY - parseInt(theight/2);
        $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn('fast').show('fast');
      }, function(){
        $("#simpleTooltip").remove();
        $(this).attr("title", text);
      });
      $(this).mousemove(function(e){
        var theight = $('#simpleTooltip').height();
        var tipX = e.pageX + 25;
        var tipY = e.pageY - parseInt(theight/2);
        var tipWidth = $("#simpleTooltip").outerWidth(true);
        var tipHeight = $("#simpleTooltip").outerHeight(true);
        if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
        if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
        $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn('fast').show('fast');
      });
    }
  });
};

