// JavaScript Document
function resizePanel()
{

	width = $(window).width();
	height = $(window).height();

	mask_width = width * $('.item').length;
		
	$('#debug').html(width  + ' ' + height + ' ' + mask_width);
		
	$('#wrapper, .item').css({width: width, height: height});
	$('#mask').css({width: mask_width, height: height});
	$('#wrapper').scrollTo($('a.selected').attr('name'), 0);
		
}


function update_user_box()
{	
	dataString = $.toJSON($('#step_2').serializeObject());

	$.post('/signup/step_2', {data: dataString}, function(response_data){
		var obj = $.evalJSON(response_data);
		
		if(obj.success === false) {				

			$("#result2").html(obj.message);
		} else {

			var user_box = document.getElementById("facebook_info");  
			user_box.innerHTML = obj.message;  
			
			FB.XFBML.Host.parseDomTree();

			$("#result2").html('');
			$("#step2_left").hide();
			$("#step2_right").hide('');
			
			$("#facebook_info").show();
			
			$("#facebook_info").append('<div id="loggedIntoFBC"><a href="#" onclick = "FB.Connect.logout(function() { FBCLogout(); }); return false;"><img id="fb_logout_image" src="http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif" alt="Connect"/></a></div>');
			
			
			//slideit("#step3");
		}
		
	});
	return false;
	
}


function FBCLogout() {
	$("#step2_left").show();
	$("#step2_right").show('');
	$("#facebook_info").hide();
	$("#errorbox").html('');

	// Make AJAX call to log user out of Facebook
	
	FB.XFBML.Host.parseDomTree();
}

function hide_right()
{
			$("#step2_right").hide('');
}

function checkAll(field,checkstatus){for(i=0;i<field.length;i++){field[i].checked=checkstatus.checked?true:false;}}



$(document).ready(function() {	


	ZeroClipboard.setMoviePath( 'http://www.actbig.com/js/ZeroClipboard.swf' );
	
	var clip = new ZeroClipboard.Client();

	clip.setText( '' ); // will be set later on mouseDown
	clip.setHandCursor( true );
	clip.setCSSEffects( true );
	
	clip.addEventListener( 'load', function(client) {
			// alert( "movie is loaded" );
	} );
	
	clip.addEventListener( 'complete', function(client, text) {
			//alert("Copied text to clipboard: " + text );
	} );

	clip.addEventListener('mouseDown', function (client) {
		// update the text on mouse over
		
		clip.setText(document.getElementById('ref_link').value);
	});
	
	clip.glue( 'd_clip_button', 'd_clip_container' );


	$("#step_3_import").hide();
	$("#step_3_manual").hide();
	
	$("#loading").html('');
	
	$("#loading_img").hide();

	$('#step_1').submit(function() {

		$("#loading").html('<img src="/img/loading.gif" alt="Wait" />');
				
		dataString = $.toJSON($('#step_1').serializeObject());

		$.post('/signup/step_1', {data: dataString}, function(response_data){
			var obj = $.evalJSON(response_data);
			if(obj.success === false) {				
				$("#result").html(obj.message);
			} else {
				//alert(obj.message);
				$("#result").html('');	

				$('#step1').removeClass('selected');				
				$(obj.go_to_step).addClass('selected');
				
				slideit(obj.go_to_step);
				$('#ref_link').attr('value', obj.ref_link);				
			}
			
			$("#loading").html('');
		});
		return false;
	});


	$('#step_2').submit(function() {

		$("#loading2").html('<img src="/img/loading.gif" alt="Wait" />');
				
		dataString = $.toJSON($('#step_2').serializeObject());

		$.post('/signup/step_2', {data: dataString}, function(response_data){
			var obj = $.evalJSON(response_data);
			
			if(obj.success === false) {				
	
				$("#result2").html(obj.message);
			} else {

				//alert(obj.message);

				$("#result2").html('');
				$('#ref_link').attr('value', obj.ref_link);

				$('#step2').removeClass('selected');
				$('#step3').addClass('selected');
				
				slideit("#step3");
			}
			
			$("#loading2").html('');
		});
		return false;
	});
	
	
	$('#step_3_import').submit(function() {

		$("#loading3a").html('<img src="/img/loading2.gif" alt="Wait" />');
				
		dataString = $.toJSON($('#step_3_import').serializeObject());

		$.post('/signup/step_3_import', {data: dataString}, function(response_data){
			var obj = $.evalJSON(response_data);
						
			if(obj.success === false) {				
			
				$("#result3").html(obj.message);
				$("#loading3a").html('');
				
			} else {

				$("#result3").html('');
				

				$('#step3').removeClass('selected');
				$('#step4').addClass('selected');				
				slideit("#step4");
				
				var newHTML='';
				$.each(obj.friend_emails, function( objIndex, objValue ) {
					 newHTML+='<tr><td><input type="checkbox" id="" name="checkID" value="'+ objIndex +'" checked="checked">'+ objValue +'</td> <td>('+ objIndex +')</td></tr>'
					});
					
				$('#addressbook').append(newHTML);
			$("#loading3a").html('');
			}
		});
		
		return false;
	});	
	
	

	$('#step_3_manual').submit(function() {

		$("#loading3b").html('<img src="/img/loading2.gif" alt="Wait" />');
				
		dataString = $.toJSON($('#step_3_manual').serializeObject());
		
		$.post('/signup/step_3_manual', {data: dataString}, function(response_data){
			var obj = $.evalJSON(response_data);			
			if(obj.success === false) {				
			
				$("#result3b").html(obj.message);
				$("#loading3b").html('');
				
			} else {

				$("#result3b").html('');
				

				$('#step3').removeClass('selected');
				$('#step4').addClass('selected');				
				slideit("#step4");
				
				var newHTML='';
				$.each(obj.friend_emails, function( objIndex, objValue ) {
					 newHTML+='<tr><td><input type="checkbox" id="" name="checkID" value="'+ objIndex +'" checked="checked">'+ objValue +'</td> <td>('+ objIndex +')</td></tr>'
					});
					
				$('#addressbook').append(newHTML);
			$("#loading3b").html('');
			}
		});
		
		return false;
	});		
	


	$('#step_4').submit(function() {

		$("#loading4").html('<img src="/img/loading.gif" alt="Wait" />');
				
		dataString = $.toJSON($('#step_4').serializeObject());
		
		$.post('/signup/step_4', {data: dataString}, function(response_data){
		
			//alert(response_data);
			var obj = $.evalJSON(response_data);
			
			if(obj.success === false) {				
	
				$("#result4").html(obj.message);
			} else {

				$("#result4").html('');
				
				$('#step4').removeClass('selected');
				$('#step5').addClass('selected');					
				
				slideit("#step5");
				var newHTML='';
				
				$('#bucks').html('$' + obj.email_sent.length * 5);
				
				
				if(obj.email_sent.length > 0) 
				{ 
					$('#invites_sent').html('<p style="text-align:center; color:#6c655e; font-weight:bold; margin-bottom:0; padding-bottom:10px;">Bonus!! We have credited you with $' + obj.email_sent.length * 5 +' ActBig bucks!</p>');

					$('#invites_sent').append('<p style="text-align:center; color:#6c655e; font-weight:bold; margin-bottom:0; padding-bottom:10px;"> Invites were sent to: </p>');
				} else {
					$('#total_bucks').html('');
				}
				 
				$.each(obj.email_sent, function(key, objValue) {
    				  $('#invites_sent').append('<p style="text-align:center; color:#6c655e; font-weight:bold;">'+ objValue  +'</p>');
				});
				
				 if(obj.before_invited.length > 0)
				 {
				 	$('#before_invited').html('<p style="text-align:center; color:#6c655e; font-weight:bold;">Sorry invites were not 			sent to the below list because they are currently in our mail list: </p>');
				 }
				$.each(obj.before_invited, function(key, objValue) {
    				  $('#before_invited').append('<p style="text-align:center; color:#6c655e; font-weight:bold;">'+ objValue  +'</p>');
				});
				
			}
			
			$("#loading4").html('');
		});
		return false;
	});
	
	 $('#invite_login').toggle(function()
	 {
		$("#step_3_import").show();
		$("#step_3_manual").hide();
	 },function() {
		$("#step_3_import").hide();
	 });


	 $('#plaxo_form').toggle(function()
	 {
		$("#step_3_import").hide();
		$("#step_3_manual").show();		
		
	 },function() {
		$("#step_3_manual").hide();
	 });	

	$('.invite_more').click(function () {
 
		$('#step5').removeClass('selected');
		$('#step3').addClass('selected');	
		
		$('#addressbook').html('');
						
		slideit("#step3");		
		return false;
	});


	$('#back_to_step1').click(function () {
 
		$('#step5').removeClass('selected');
		$('#step1').addClass('selected');	
				
		slideit("#step1");		
		return false;
	});


	$.fn.serializeObject = function()
	{
		var o = {};
		var a = this.serializeArray();
		$.each(a, function() {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	};

	function slideit(location)
	{		
		
		$('#wrapper').scrollTo($(location), 800);	
	
		$('.content').fadeTo(300, 0.5, function() {
			  // Animation complete.
		});
		$('.content').fadeTo(300, 1, function() {
			  // Animation complete.
		});			
		
		return false;
	}

	$(window).resize(function () {
		resizePanel();
	});
	
});
