var login_checker;
var is_logged_in = false;
//oauth related functions 
function oauth_login(partner, type) {
	var type = type || false;
	oauth_popup(BASE_URL+'oauth_request/go/'+partner, '285', '575');
	login_checker = setInterval("check_login('"+type+"')", 1000);
}

//http://stackoverflow.com/questions/351499/how-to-create-a-pop-up-window-the-good-kind
function oauth_popup(url, height, width) {
		if (height == undefined) height = '200';
		if (width == undefined) width = '350';
    var newwindow=window.open(url,'name','height='+height+',width='+width);  
    if(!newwindow){
        alert('We have detected that you are using popup blocking software...');}
    if (window.focus) {newwindow.focus()}
    return false;
}
var i=0;
function check_login(type){
	i++;
	
	$.getJSON('/users/isLoggedIn', function(data){
	
		//times out after 10 minutes
		if((data != 'false' && data != false && is_logged_in == false) || i > 600){
//		if (data == 'false') { //TESTING!!
			clearInterval(login_checker);
			is_logged_in = true;
			
			if (type == 'widget') {
				//want to know if we're on the widget, i.e. logging in to comment, then we just reload
				reloadWindow();			
			} else if (type == 'login') {
   			if(data.type == 'Organization'){
   				window.location.href = '/organizations/my_nonprofit';
   			}else{
   				window.location.href = '/users/reviews';
   			}
				
			} else {
			
				data = $.parseJSON(data);
/*
				$('a.login_with_partners').remove();
				$('#login-success').fadeIn();
*/

				$('#require_login').val(1);
/*
				if(data.User.email_opt_marketing == 1){
					$('#subscribe_to_marketing_email').attr('checked', true);
				}else{
					$('#subscribe_to_marketing_email').attr('checked', false);
				}
				if(data.User.email_opt_message == 1){
					$('#subscribe_to_message_email').attr('checked', true);
				}else{
					$('#subscribe_to_message_email').attr('checked', false);
				}
*/
				

				$('form#form-reviews').unbind('submit', bindReviewValidation_preventDefault);
				$('form#form-reviews').submit();


			
			}
			
		}
	});
}


function openid_popup(provider) {

	oauth_popup('/users/openid_login/'+provider);
/*
 		$.getJSON('/users/openid_login/', function(data) {  		 		
 			if (data.result == 'success') {
	   		if (currentAction == 'write') {

					//google analytics tracking
					_gaq.push(['_trackEvent', 'Write A Review', 'Login w/ OpenID']);				

					//we're logged in... submit the review 
					$('form#form-reviews').unbind('submit', bindReviewValidation_preventDefault);
					$('form#form-reviews').submit();

	   		} else if (currentAction == 'login') {
	   			if(data.type == 'Organization'){
	   				window.location.href = '/organizations/my_nonprofit';
	   			}else{
	   				window.location.href = '/users/reviews';
	   			}
	   		} else {
		   		reloadWindow(); 		
	   		}   			
 			}
 		
 		});
*/
}

function openid_login(partner, type, username) {
	var type = type || false;
	var username = username || ''; 
	oauth_popup(BASE_URL+'openid/login/'+partner+'/'+username, '450', '625');
	//console.log(BASE_URL+'openid/login/'+partner+'/'+username);	
	login_checker = setInterval("check_login('"+type+"')", 1000);
}

