
function popupShow( id ) {
	popupCenter( id );
	
	$("#"+id).show();
	$("#"+id+" .popup-container-background").css("height", $(document).height() );
	
	if($('.invite_button').length > 1) { activateInvites(); };
	
}

function popupHide( id ) {
	$("#"+id).hide();
}

function activateInvites() {

	$('.invite_button').click(function() {
		
		$('.invite_button').removeClass('selected');
			
		$(this).addClass('selected');
		
		var new_provider = $(this).attr('id');
		
		new_provider = new_provider.split("invite_mode_");
		
		new_provider = new_provider[1];
		
		$('#invite_provider').val(new_provider);
		
		$('#invite_cur_pv b').html(new_provider);
		
		$('#invite_cur_pv').fadeIn();
			
	});
	
	$('#invite_username, #invite_password').focus(function() {
		
		if($(this).css('color')=='rgb(204, 204, 204)') {
		
			$(this).val('');
		
		}
		
		$(this).css('color','#000');
		
	});
	
	$('#invite_go').click(function() {
		
		if($('#invite_username').val() != '' && $('#invite_password').val() != '') {
	
			importInviteSource();
	
		}
	
	})
	
}

var invite_listed = Array();
		
function importInviteSource() {
	
	var ajax_email = $('#invite_username').val();

	var ajax_passw = $('#invite_password').val();

	var ajax_pvider = $('#invite_provider').val();
	
	if($('#invite_username').css('color')=='#CCC' || $('#invite_password').css('color')=='#CCC') {
	
		alert("Please enter a valid username and password");
	
	}else if($('#invite_provider').val() == '') {
		
		alert("Please select a provider");
		
	}else{
	
	var ajax_isdone = $.inArray(ajax_pvider, invite_listed);
	
	if($.inArray(ajax_pvider, invite_listed) >= 0) {
		
		alert("You have already imported contacts from "+ajax_pvider+" account");
		
	}else{
		
		$('#invite_list_loader').fadeIn();
	
		invite_listed.push(ajax_pvider);
	
		var invite_count = 0;
	
		function process_list(data) {
		
			var count = data.split('#');
		
			if( parseInt(count[0]) >= 0) {
		
				var list = count[1].split("[n]");
		
				count = count[0];
			
				$('#invite_list').html($('#invite_list').html()+'<div class="invite_list_provider"><b>'+ajax_pvider+'</b></div>');
			
				for (var i = 0; i < (list.length-1); i++) {
				
					var user_ret = list[i].split(",");
				
					var link_string = $('#invite_list').html()+'<li><input type="checkbox" class="invite_list_selected" value="" />';
				
					link_string += user_ret[0];
				
					link_string += '</li>';
				
					$('#invite_list').html(link_string);
				
				}
		
		}
			
		$('#invite_list_loader').fadeOut();
		
		$('#invite_list li').unbind('click').toggle(function() {
			
			$(this).addClass('selected');
			$(this).find('input').attr('checked',true);
			
			invite_count_total();
			
		},function() {
			
			$(this).removeClass('selected');
			$(this).find('input').attr('checked',false);
			
			invite_count_total();
			
		});
	}
	
	function invite_count_total() {
		
		invite_count = $('#invite_list li.selected').size();
			
		$('#invite_list_count').html('invitee(s) '+invite_count);
		
	}
	
	var json_req = 'email_box='+ajax_email+'&password_box='+ajax_passw+'&provider_box='+ajax_pvider;
	
	console.log(json_req);
	
	$.ajax({
        url: baseUrl + 'ajax/invite/all',
        type: 'POST',
        dataType: 'json',
        data: json_req,
        success: function(data) {
            console.log(data);
			//process_list(data.events);
        }
    });
	
	/*$.ajax({
	
		type: "POST",
		url: "",
		data: "email_box="+ajax_email+"&password_box="+ajax_passw+"&provider_box="+ajax_pvider,
		success: function(data) {
			process_list(data);
		}
		
	});*/
	
	}
	
	}
	
}

function popupResize( id, width, height ) {
	$("#"+id+" .popup").css("width", width);
	$("#"+id+" .popup").css("height", height);

	$("#"+id+" .pod-container-content").css("width", width - 66);
	$("#"+id+" .pod-container-content").css("height", height - 93);
}

function popupCenter( id ) {
	var w = parseInt( $("#"+id+" .popup").css("width") );
	var h = parseInt( $("#"+id+" .popup").css("height") );
	
	var x = Math.round( ( $(window).width() - w ) / 2 );
	var y = Math.round( ( $(window).height() - h ) / 2 ) + $(window).scrollTop();
	
	$("#"+id+" .popup").css("left", x );
	$("#"+id+" .popup").css("top", y );
}

function popupTitle( id, title ) {	
	$("#"+id+" .pod-container-header h2").html( title );
}

function popupInit( id, title, width, height ) {
	$("#"+id+" .popup-container-background").click( function( e ) {
		popupHide( id );
	} );

	$(window).resize( function() {
		popupCenter( id );
	} );

	$(window).scroll( function() {
		popupCenter( id );
	} );	
	
	popupTitle( id, title );
	
	popupResize( id, width, height );
}

function update_category_page(obj) {
					
	var ref_ind = obj.selectedIndex;
	
	var ref_opts = obj.options;
	
	window.location.href = "http://www.small-world.com.au/store?cat="+ref_opts[ref_ind].value;
	
}
