(function ($) {
    'use strict';
    
	$.widget( "ongig.profile", {
		// default options
		options: {
			token: null,
			logout_url: null,
			facebook_obj: null,
			twitter_obj: null,
			gthub_obj: null,
			github_follower_obj: null,
			github_following_obj: null,
			github_watched_obj: null,
			employer_obj: null,
			crunchbase_obj: null
		},

		// the constructor
		_create: function() {	
			this._retrieve_info();
			this._my_job_scorecard();
		},
		_retrieve_info : function(){
			
		    var that = this;
			var output = null;
			if(this.options.token!='unset'){
				$.ajax({
				   type: "POST",
				   url: "/php/profile.php",
				   dataType:'json',
				   async:false,
				   data: "mode=get_profile&token=" + that.options.token,	 
				   error: function(e){
				   		//console.log(e);
				   }, 
				   success: function(data){
				   	 	//console.log(data);
				   	  	if(data.response.linkedin){
				   	  		 if(data.response.linkedin){
				   	  		 	 that._parse_linked_in(data.response.linkedin);
				   	  		 }
				   	         if(data.response.fb) {
				   	         	that.options.facebook_obj = data.response.fb;
				   	         }
				   	         if(data.response.twitter){
				   	         	that.options.twitter_obj = data.response.twitter;	
				   	         }
				   	         if(data.response.github){
				   	         	that.options.github_obj = data.response.github;	
				   	         	if(that.options.github_obj!='fail'){
								    that.options.github_follower_obj = data.response.github_followers;
								    that.options.github_following_obj = data.response.github_following;
								    that.options.github_watched_obj = data.response.github_watched;			   	         		
				   	         	}
				   	         }	
				   	         if(data.response.company){
				   	         	that.options.employer_obj = data.response.company;	
				   	         	
				   	         }
				   	         if(data.response.company){
				   	         	that.options.crunchbase_obj = data.response.crunchbase;	
				   	         }	
				   	         if(data.response.uservoice_token){
	
				   	         }	
				   	  	}	   	         			   	         		   	         
				   	  		 
				   }
				   
				 });
			}
			else{
				//console.log('anonymous');
				var this_display_name = 'Anonymous User';
			 	$('.profile_img').attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
			 	$('.profile_img').css('width','80');
			 	$('.comment_form').prepend('<img id=\"comment_form_image\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\"/>');
				$('.profile_name').html(this_display_name);
			 	$('.profile_position_name').html('');
			 	$('.profile_position2_name').html('');
  				$('.profile_status').after('<div class=\"sign_in_help\"><div class=\"question_mark\">?</div></div>');
  				$('.profile_status2').after('<div class=\"sign_in_help\"><div class=\"question_mark\">?</div></div>');
  				$('.sign_in_help').after('<div class=\"why_linked_in\"><span class=\"popup_triangle\"></span><div>Ongig is cooler when you\'re signed in through LinkedIn -- You can see who you\'re connected to, "follow" and comment on jobs and share your LinkedIn background if you apply for a job - and none of this goes on your LinkedIn wall!</div></div>');
				var timer3;
				$('.question_mark').hover(function(){
						clearTimeout(timer3);
						var position = $('.sign_in_help').position();
						$('.why_linked_in').css('left',position.left);
						$('.why_linked_in').css('top',position.top);
						$('.why_linked_in').show();
				});	
				$('.why_linked_in').hover(function(){
					clearTimeout(timer3);
				});
				$('.why_linked_in').mouseleave(function(){
				    timer3 = setTimeout(function(){
				        $(".why_linked_in").hide(); // regular hide
				    }, 300); // 1 second
				});	
				$('.question_mark').mouseleave(function(){
					
				    timer3 = setTimeout(function(){
				        $(".why_linked_in").hide(); // regular hide
				    }, 300); // 1 second*/
				});  				
			    $('#login_button').show();
			    $('#login_button1').show();
			    $('#letter').Watermark("Please sign in with LinkedIn");
			    $('#letter').attr("disabled","true");
			    $('.account_preview_linkedin').hide();
				 			
			}
		},
		_my_job_scorecard : function(){
			var that = this;
			if(that.options.token!='unset'){
			    $.ajax({
				   type: "POST",
				   url: "/php/posting.php",
				   dataType:'json',
				   data: "mode=my_job_scorecard&token=" + that.options.token,
				   error: function(e){
				   		//console.log(e);
				   },
				   success: function(data) {
					
				   	$('.profile_comments').html(data.response.num_comments);
				    $('.profile_applications').html(data.response.num_applied);
				   	$('#popup_comments').html(data.response.num_comments + ' Comments');
				    $('#popup_apps').html(data.response.num_applied + ' Intros');
				    
				    
					$('.profile_pic').mouseover(function(){
						var position = $('.profile_pic').position();
						$('#profile_popup').css('left',position.left+32);
						$('#profile_popup').show();
					});
					$('.profile_pic').mouseout(function(){
						$('#profile_popup').hide();
					});
					
				   }
				 });			
			}	
			else{
				$('.profile_comments').html('0');
				$('.profile_applications').html('0');
			}	
		},		
		_parse_linked_in : function(obj){
			 //console.log(obj);
			 var this_display_name = obj['first-name'] + " " + obj['last-name'];
			 if(obj['picture-url']){
			 	$('.profile_img').attr('src',obj['picture-url']);
			 	$('.comment_form').prepend('<img id=\"comment_form_image\" src=\"' + obj['picture-url'] + '\"/>');
			    $('.account_preview_thumbnail').html('<img src=\"'+ obj['picture-url'] + '\"/>');		 	
			 }
			 else{
			 	$('.profile_img').attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
			 	$('.profile_img').attr('height','80px;');
			 	$('.comment_form').prepend('<img id=\"comment_form_image\" height=\"80px\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\"/>');
			 	$('.account_preview_thumbnail').html('<img src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\"/>');		 	
			 }
			 $('.profile_name').html(this_display_name);
			 $('.account_preview_name').html(this_display_name);		 
			 $('.profile_position_name').html(obj.positions.position[0].title + " at " + obj.positions.position[0].company.name);
			 $('.profile_position2_name').html(obj.positions.position[0].title + " at " + obj.positions.position[0].company.name);
			 $('.account_preview_position').html(obj.positions.position[0].title);
			 $('.account_preview_location').html(obj.location.name);
			 if(this.options.token!='unset'){
			 	if(this.options.logout_url!='unset'){
			 		$('.profile_status').html('<a href=\"/logout.php?redirect=' + this.options.logout_url + '\">Log Out</a>');
			 		$('.profile_status2').html('<a href=\"/logout.php?redirect=' + this.options.this_logout_url + '\">Log Out</a>');
			 	}
			 	else{
			 		$('.profile_status').html('<a href=\"/logout.php\">Log Out</a>');
			 		$('.profile_status2').html('<a href=\"/logout.php\">Log Out</a>');
			 		
			 	}
			 }
			 else{
				 			
			 }
			 $('#login_button').show();
			 $('#login_button1').show();
			 //console.log('here2');
			 this._check_my_jobs();
			 
			 $('.account_preview_linkedin .collapse').append("<ul></ul>");
			 if(typeof( obj.headline) != "undefined") $('.account_preview_linkedin .collapse ul').append("<li><span>Headline:</span></li><li class=\"indent\"> " + obj.headline + "</li>");
			 if(typeof( obj.industry) != "undefined") $('.account_preview_linkedin .collapse ul').append("<li><span>Industry: </span></li><li class=\"indent\"> " + obj.industry + "</li>");
			 $('.account_preview_linkedin .collapse ul').append("<li><span>Work History:</span></li>");
			 $.each(obj.positions.position, function(i, val) {
			 	
			 	$('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">"+ val.title + " at " + val.company.name +"</li>");
	
			 });
			 if(typeof( obj.specialties) != "undefined") $('.account_preview_linkedin .collapse ul').append("<li><span>Specialties: </span></li><li class=\"indent\"> " + obj.specialties + "</li>");		 
			 if(obj.educations.education.constructor==Array){
			 	$('.account_preview_linkedin .collapse ul').append("<li><span>Education:</span></li>");
			 	$.each(obj.educations.education, function(i, val) {
			 	 if((typeof(val.degree ) != "undefined")&&(typeof(val['field-of-study'] ) != "undefined")) $('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">"+ val.degree + " in " + val['field-of-study'] +  " at " + val['school-name'] + "</li>");
			 	 else $('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">" + val['school-name'] + "</li>");
			 	});
			 }
			 else{
			     $('.account_preview_linkedin .collapse ul').append("<li><span>Education:</span></li>");
			 	 if((typeof(obj.educations.education.degree ) != "undefined")&&(typeof(obj.educations.education['field-of-study'] ) != "undefined")){ 
			 	 	$('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">"+ obj.educations.education.degree + " in " + obj.educations.education['field-of-study'] +  " at " + obj.educations.education['school-name'] + "</li>"); }
			 	 else{ 
			 	 	$('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">" + obj.educations.education['school-name'] + "</li>"); 
			 	 }
			 	
			 }
			 
			 if(obj['twitter-accounts']['twitter-account'].constructor == Array){
			 	$('.account_preview_linkedin .collapse ul').append("<li><span>Twitter Account:</span></li>");
				 $.each(obj['twitter-accounts']['twitter-account'], function(i,val){
				 	
					$('.account_preview_linkedin .collapse ul').append("<li class=\"indent\">"+ val['provider-account-name'] + "</li>");
				 });
			 }
			 else{
			 	 $('.account_preview_linkedin .collapse ul').append("<li><span>Twitter Account:</span></li><li class=\"indent\"> "+ obj['twitter-accounts']['twitter-account']['provider-account-name'] + "</li>");
			 }	 
			 		
		},
		_check_my_jobs : function(){
			    //console.log('here');
				$.ajax({
				   type: "POST",
				   url: "/php/profile.php",
				   dataType:'json',
				   data: "mode=check_my_jobs&token=" + this.options.token,
				   error: function(e){
				   	  //console.log(e);
				   },	  
				   success: function(data){
				   	  //console.log(data);
				   	  if(data.response=='success'){
				   	  	if($(location).attr('href').indexOf('/account')!=-1){
				   	  		$('.profile_status').append('&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"/account\">Account</a>');
				   	  	}
				   	  	else{
				   	  		$('.profile_status').append('&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"/account\">Account</a>');
				   	  	}
				   	  }
				   	  else{
				   	  	
				   	  }
				   }
				   
				});
			
		},
		_register_job_form : function(){
			var that = this;
			if(this.options.token!='unset'){
				$.ajax({
				   type: "POST",
				   url: "/php/profile.php",
				   dataType:'json',
				   async:false,
				   data: "mode=get_profile&token=" + that.options.token,	  
				   success: function(data){
				   	    ////////console.log(data);
				   	  	if(data.response.linkedin){
				   	  		 $('#job_email_name').attr('value',data.response.email);
				   	  		 $('#job_city_name').attr('value',data.response.linkedin.location.name);
				   	  		 $('#job_company_name').attr('value',data.response.linkedin.positions.position[0].company.name);
				   	  		 $('#job_name_name').attr('value',data.response.linkedin['first-name'] + ' ' + data.response.linkedin['last-name']);
					   	}
				   }
				   
				});
			}
			else{
			}	
		}		
						
	});
}(jQuery));	
