
//======================= home tabs ============================
$(document).ready(function() { 
	
	$('.infiniteCarousel').infiniteCarousel();
	
	theRotator();
	
	$(".tabcontent").hide(); //Hide all content
	$("ul.tabs li:first").addClass("activeLink").show(); //Activate first tab
	$(".tabcontent:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").mouseover(function() {
		$("ul.tabs li").removeClass("activeLink"); //Remove any "active" class
		$(this).addClass("activeLink"); //Add "active" class to selected tab
		$(".tabcontent").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show(); //Fade in the active ID content
		return false;
	});
	
	$('#boxtext1').hide();
	$('#boxtext2').hide();
	$('#boxtext3').hide();
	
	$('#ahover1').mouseover(function(){
		$('#boximage1').hide();
		$('#boxtext1').show();
		}).mouseleave(function(){
		$('#boxtext1').hide();
		$('#boximage1').show();
	});
	$('#ahover2').mouseover(function(){
		$('#boximage2').hide();
		$('#boxtext2').show();
		}).mouseleave(function(){
		$('#boxtext2').hide();
		$('#boximage2').show();
	});
	$('#ahover3').mouseover(function(){
		$('#boximage3').hide();
		$('#boxtext3').show();
		}).mouseleave(function(){
		$('#boxtext3').hide();
		$('#boximage3').show();
	});
	
	// for Booklet
	var length = $('#rotator2 ul').children('li').length;
	
		var k=0;
		for (k=0;k<=length;k++)
		{
		$('#ahover1' + k).mouseover(function(){
			$('#rotator2').css({'background-color': '#d3c2ae'}) 
			}).mouseleave(function(){
			$('#rotator2').css({'background-color': '#AA8868'}) 
		});
		}
	
	
	/*
	$("#boximage1").mouseover(function() {
		$('#boxtext1').show();
		}).mouseleave(function() {
		$('#boxtext1').hide();
	});
	$("#boxtext1").mouseover(function() {
		$(this).show();
		$('#boximage1').hide();
		}).mouseleave(function() {
		$(this).hide();
	});
	
	
	$("#boximage2").mouseover(function() {
		$('#boxtext2').show();
		}).mouseleave(function() {
		$('#boxtext2').hide();
	});
	$("#boxtext2").mouseover(function() {
		$(this).show();
		}).mouseleave(function() {
		$(this).hide();
	});
	
	$("#boximage3").mouseover(function() {
		$('#boxtext3').show();
		}).mouseleave(function() {
		$('#boxtext3').hide();
	});
	$("#boxtext3").mouseover(function() {
		$(this).show();
		}).mouseleave(function() {
		$(this).hide();
	});
	*/
});



//================ Rotator for Testimonials ===================

function theRotator() {
	$('div#rotator ul li').hide();
	$('div#rotator ul li:first').show();
	
	
	$('div#rotator2 ul li').hide();
	$('div#rotator2 ul li:first').show();
	
	setInterval('rotate()', 5000);
}

function rotate() {
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));
	
	var current2 = ($('div#rotator2 ul li.show')?  $('div#rotator2 ul li.show') : $('div#rotator2 ul li:first'));
	var next2 = ((current2.next().length) ? ((current2.next().hasClass('show')) ? $('div#rotator2 ul li:first') :current2.next()) : $('div#rotator2 ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	//next.hide().fadeIn('slow').addClass('show');
	//next2.hide().fadeIn('slow').addClass('show');
	next.show().addClass('show');
	next2.show().addClass('show');

	//Hide the current image
	//current.fadeOut('slow').removeClass('show');
	//current2.fadeOut('slow').removeClass('show');
	current.hide().removeClass('show');
	current2.hide().removeClass('show');
};


$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $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'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 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
            }, 700, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};
