/*
 * © 2010 themodule.com
 * All rights reserved
 * www.themodule.com
 * */
var timer = null;
var time = 15000;
/* var time = 1500000; */

$(document).ready(function(){
    initHPControls();
    initHPTabs();
});

function initHPControls(){
    $('#next-testimonial').click(function(){
        //    Hide Current content
        showNextTestimonial();
        return false;
    });
    
    $('#show-more-scripts').click(function(){
        $('#script-integrations').html('<div class="loading">Loading scripts.<br/>Please wait...</div>');
        $('#script-integrations').addClass('loading');
        $('#script-integrations').load('more-scripts.html', '', function(responseText, textStatus, XMLHttpRequest){
            $('#script-integrations').removeClass('loading');           
            if(textStatus == 'error'){
                $('#script-integrations').html('Failed to load content from server.');
            }
        });
        return false;
    });
}


function initHPTabs()
{
	timer = setTimeout('slideShow()', time);
	
	$('.billboard a.next, .billboard a.prev').click(function(){
		if(timer != null){
			clearTimeout(timer);
			timer = null;
		}
		
		var currObj = $('#tabs-links li.current');
		var nextObj = null;		
		if ($(this).hasClass('prev')){
			nextObj = currObj.prev();
			if ($('#tabs-links li:first').hasClass('current')){ 
				nextObj = $('#tabs-links li:last');
			}
		} else {
			nextObj = currObj.next();
			if ($('#tabs-links li:last').hasClass('current')){ 
				nextObj = $('#tabs-links li:first');
			}			
		}
				
    	if($(nextObj).children('a').attr('rel') == '' || !$('#' + $(nextObj).children('a').attr('rel')).length) { return true; }
		$('#tabs-links LI').removeClass('current');    	
    	$(nextObj).addClass('current');
    	$(nextObj).css('opacity', 0.4);
    	$(nextObj).animate({opacity: 1}, 'fast');
    	openTab($(nextObj).children('a').attr('rel'));  
		
		timer = setTimeout('slideShow()', time);		
    	return false;    
    });	
	
    $('#tabs-links LI A').click(function(){
    	if($(this).attr('rel') == '' || !$('#' + $(this).attr('rel')).length) { return true; }
		
		if(timer != null){
			clearTimeout(timer);
			timer = null;
		}
		
    	$('#tabs-links LI').removeClass('current');
    	$(this).parent().addClass('current');
    	$(this).parent().css('opacity', 0.4);
    	$(this).parent().animate({opacity: 1}, 'fast');
    	openTab($(this).attr('rel'));   

		timer = setTimeout('slideShow()', time);		
    	return false;    
    });
}

function slideShow(){
	var currObj = $('#tabs-links li.current');
	var nextObj = currObj.next();
	if ($('#tabs-links li:last').hasClass('current')){ 
		nextObj = $('#tabs-links li:first');
	}
			
	if($(nextObj).children('a').attr('rel') == '' || !$('#' + $(nextObj).children('a').attr('rel')).length) { return true; }
	$('#tabs-links LI').removeClass('current');    	
	$(nextObj).addClass('current');
	$(nextObj).css('opacity', 0.4);
	$(nextObj).animate({opacity: 1}, 'fast');
	openTab($(nextObj).children('a').attr('rel')); 
	timer = setTimeout('slideShow()', time);
}

function openTab(tabId)
{
	var tabObj = $('#' + tabId);
	if(!tabObj.length) { return; }
	
	var tabLink = $('A[rel="' + tabId + '"]');
	if(tabLink.length > 0){
		if(!tabLink.parent().hasClass('current')){
			$('#tabs-links LI').removeClass('current');
			tabLink.parent().addClass('current');
		}
	}
	
	var newPos = (parseInt(tabId.replace('tab-', '')) - 1) * (-940);
    $('#tabs').animate({left: newPos}, 'normal');    
}
