function doFade(text) {
	var base = $(text);
	var h4 = base.find('h4');
	var div = base.find('.cols');
	$('#lbox-inner .lbox-header').append(h4);
	$('#lbox-inner .lbox-content .wrapper').append(div);
	
	returnCur('#lbox-inner .lbox-content .wrapper .cols')();
	returnCur('#lbox-inner .lbox-header h4')();
	$('#lbox form.fancybox').ajaxForm(function(data){
		doFade(data);
	});
}


function do_lbox() {
	
	$('#lbox form.fancybox').ajaxForm(function(data){
		doFade(data);
	});
	
	$('#lbox-pagi a, #lbox a.fancybox').die();
	$('#lbox-pagi a, #lbox a.fancybox').live('click', function(e){
		e.preventDefault();
		
		var node = $(this);
		// If we clicked on an arrow node
		if ( $(this).parent(':not(.dot)').size() > 0 ) {
			node = $('#lbox-pagi .dot a[href=' + $(this).attr('href') + ']');
		}
		
		$('#lbox-pagi a').removeClass('lbox-current');
		
		node.addClass('lbox-current');
		if ( node.parent().prev('#lbox-prev').size() > 0 ) {
			$('#lbox-prev').html('<span>Previous</span>');
		} else {
			$('#lbox-prev').html('<a href="' + node.parent().prev().children().attr('href') + '">Previous</a>');
		}
		
		if ( node.parent().next('#lbox-next').size() > 0 ) {
			$('#lbox-next').html('<span>Next</span>');
		} else {
			$('#lbox-next').html('<a href="' + node.parent().next().children().attr('href') + '">Next</a>');
		}
		
		$.get(this.href, null, function(data){
			doFade(data);
		});
	});
}

function returnCur( sel ) {
	return function() {
		$(sel + ':last').hide().css({
			position: 'absolute', 
			left: 0, 
			top: '20px',
			background: '#fff'
		});
		$(sel + ':first').fadeOut('normal',function(){
			$(this).remove();
		});
		$(sel + ':last').fadeIn('normal', function(){
			$(this).attr('style','');
		});				
	}
}

jQuery(function($){
  var opts = { 
  	'frameWidth':  ( window.location.pathname.length > 1 ? 748 : 675 ),
  	'frameHeight': 499,
  	'callbackOnShow' : do_lbox,
  	'hideOnContentClick' : false
  }

  $('#tour-tabs .feature-list a, a#assistant, .fancybox').fancybox(opts); 
});

jQuery(function($){
  $('#tour-screens .scroller-wrapper').scroller('.scroller', '.m-left', '.m-right', 'a');
  $('#tour-screens a.t-img').click(function(e){
    if ( e ) { e.preventDefault(); }
    $('#tour-slides .scroller-wrapper').scroller().moveTo(this.href.toString().replace(/^.*#/,''));
  });
  $('#tour-slides .scroller-wrapper').scroller('.scroller', '.m-left', '.m-right', 'div.t-slide');
  $('#home-feature').scroller('.scroller', '#hf-left a', '#hf-right a', '.slide');
  
});
