var currSlides;
var slideshow;
var stop = false;
var cur_mt = 0;
var cur_st = 0;
var last = null;
var stop2 = false;

$(document).ready(function() {

//center the main and sub tabs since their width won't be 
//computed until the document is ready
	centerSubTabs('#main-list');
	centerSubTabs('#m-list-hc');
	centerSubTabs('#m-list-ai');
	centerSubTabs('#m-list-ae');
	
//hide anything outside of the slideshow
	$("#slideshow").css("overflow", "hidden");

// init and stop the inner slideshows
    var inners = $('.main-tab').cycle().cycle('stop');
	
	
// init main slideshow
    slideshow = $('#slideshow').cycle({
        fx: 'scrollHorz',
        speed: 300,
        timeout: 0,
		slideExpr: '.main-tab',
        before: function(curr, next, opts) {
			
            // stop all inner slideshows
            inners.cycle('stop');
			
			// start the new slide's slideshow
            currSlides = $(this).cycle({
                fx: 'fade',
				//@fixme: put this back to 10000 once all changes are final
                timeout: 8000,  //time each slide is shown
                autostop: true,
				pause: true,
				before: updateTabs,
				after: function(curr, next, opts) {
					cur_st = opts.currSlide;
					$('#output').html(cur_mt + "-" + cur_st+"--"+opts.nextSlide);
				},
				slideExpr: '.sub-tab',
                end: function() {
                    // when inner slideshow ends, advance the outer slideshow
                    slideshow.cycle('next');
                }
            });
        },
		after: function (curr, next, opts) {
			cur_mt = opts.currSlide;
			$('#output').html(cur_mt);
		}
    });
	
	//testimonial slideshow code
	$("#testimonial-slides").cycle({
		fx: 'fade',
		timeout: 10000,
		pause: true,
		autostop: false,
		random: true,
		speed:600,
		before: centerSlide
	});
});

function updateTabs() {
	//$('#output').html("Tab ID:<br>" + this.id);
	if (stop2) {
		stop2 = false;
		slideshow.cycle("next");
		return;
	}
	if (stop) {
		stop2 = true;
		stop = false;
	}
	var this_main = this.id.slice(0,2);
	var last_main = "";
	if (last) {
			last_main = last.id.slice(0,2);
			$('#m-' + last.id).toggleClass("tab current-tab cur-m-" + last_main);		
	}
	if (this_main != last_main) {
				$('#m-' + last_main).toggleClass("main-tab-reg main-tab-cur cur-m-" + last_main);
				$('#m-' + this_main).toggleClass("main-tab-reg main-tab-cur cur-m-" + this_main);
				$('#m-list-' + last_main).toggleClass("hidden");
				$('#m-list-' + this_main).toggleClass("hidden");
				
	}
	$('#m-' + this.id).toggleClass("tab current-tab cur-m-" + this_main);
	
	last = this;
}

function centerSubTabs(sub) {
	var wid = $(sub).width();
	var lm = (620 - wid) / 2;
	$(sub).css("margin-left", lm);
}
function tabClick(id) { 
	if (cur_mt == id) return true;
    $('#slideshow').cycle(id);  
    return false;  
}
function subtabClick(id, s) { 
	stop = (typeof(s) != 'undefined');
	stop2 = false;
	if (cur_st == id) return true;
    currSlides.cycle(id);  
    return false;  
}

function centerSlide( curr, next, opts ) {
	var id = "#" + next.id;
	var l = -5 + ($(id).parent().width() - $(id).width()) / 2;
	var t = -10 + ($(id).parent().height() - $(id).height()) / 2;
	$(id).css("margin-left", l+"px");
	$(id).css("margin-top", t+"px");
}
