(function ($) {
    'use strict';
    
	$.widget( "ongig.job_posting", {
		// default options
		options: {
			token: null,
			identifier: null,
			job_id: null,
			company_string:null,
			company_name:null,
			comments_template : $('#comment_template'),
			hover_searching_template : $('#hover_searching_template'),
			hover_results_template : $('#hover_results_template'),
			hover_no_results_template : $('#hover_no_results_template'),
			connectability_template : $('#connectability_template'),
			hiring_manager_template : $('#connectability_template'),					
			preview: null,
			filled: null,
			submitted: null
		},

		// the constructor
		_create: function() {	
			this._init_video_and_pics();
			this._setBasicListeners();
			if(this.options.preview==1) this._init_job_preview();
			//if(this.options.filled==0) 
			this._init_job_open();
			
			$('.delete6').live('click',function(){
		    	$('.shadow').hide();
            	$('.delete_job_popup').hide();
			});
            $('.video-js-box').find('iframe').attr('height','360').attr('width','560');
            $('.video-js-box').find('iframe').attr('src', $('.preview').find('iframe').attr('src') + '?wmode=transparent');
						
			
		},
		_follow_job: function() {
			
		},
		_init_job_open: function() {
			this._applyListeners();
			this._followListeners();
			this._initTemplates();
			this._hoverSearch();
			this._getConnections();
			this._commentHandlers();
		},
		_init_job_preview: function() {
			var that = this;
			if(this.options.submitted==0) this._init_job_publish();
			else this._init_job_published();
			//Edit button
			$('.apply_div').html('<button class=\"edit\">Edit</button>');	
			$('.edit').click(function(){
				window.location = '/jobs/edit/' + that.options.company_name + '/' + that.options.identifier;
			});	
							
			
		},
		_init_job_publish: function() {
			
			var that = this;
			$('.follow_div').html('<button class=\"cupid-green6\">Publish</button>');	
			$('.cupid-green6').click(function(){
					//alert('here');
					$.ajax({
					   type: "POST",
					   url: "/php/migrate.php?id="  + that.options.job_id,
					   dataType:'json',	
					   beforeSend: function(jqXHR, settings){
					   		$('.follow_div').html('<button class=\"cupid-green6\">Publishing….</button>');
					   },	
					   erorr:function(data){
					   		//console.log(data);
					   },	     
					   success: function(data){	
					   		//console.log(data);
				       		if(data.response=='done'){
								$('.follow_div').html('<button class=\"cupid-green6\">Published</button>');
								$('.cupid-green6').unbind('click');
						    	$('.shadow').show();
				            	$('.delete_job_popup').center();
				            	$('.delete_job_popup').show();
								
				       		}
				       		else{
				       			$('.follow_div').html('<button class=\"cupid-green6\">Error</button>');
				       		}
					   }
					});								
			});
		},
		_init_job_published: function(){
				
				$('.follow_div').html('<button class=\"cupid-green6\">Published</button>');	
			
		},
		_init_video_and_pics: function() {
			
			
			VideoJS.setupAllWhenReady({
			      controlsBelow: false, // Display control bar below video instead of in front of
			      controlsHiding: false, // Hide controls when mouse is not over the video
			      defaultVolume: 0.85, // Will be overridden by user's last volume if available
			      flashPlayerVersion: 9 // Required flash version for fallback
				
			});
			$('#gallery a').lightBox(); 
		},
		_applyListeners: function() {
			var that = this;
			var timer2;
			//apply button hovers
			$('.apply_img').hover(function(){
					clearTimeout(timer2);
					var position = $(this).position();
				
					$('.follow_text').hide();
					$('.apply_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/apply_over.gif');
					$('.apply_hint').css('left',position.left);
					$('.apply_hint').css('top',position.top+60);
					$('.apply_hint').show();
			});	
			$('.apply_hint').hover(function(){
				clearTimeout(timer2);
			});
			$('.apply_hint').mouseleave(function(){
			    timer2 = setTimeout(function(){
			        $(".apply_hint").hide(); 
			    }, 300); 
			});	
			$('.apply_img').mouseleave(function(){
				$('.apply_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/apply.gif');
			    timer2 = setTimeout(function(){
			        $(".apply_hint").hide(); 
			    }, 300); 
			});
			$('.apply_img').click(function(){
				    $("#search_jobs_inline").hide(); 
					that.options.hover_searching_template.tmpl().appendTo("#search_jobs_inline");	
				
					$('.apply_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/apply_down.gif'); 
					if(that.options.token!='unset'){
						$('.apply_popup').fadeIn('fast',function(){
							$('embed, object, select').css({ 'visibility' : 'hidden' });
							$('.apply_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/apply.gif'); 	
						});
					}
					else{
						$('.apply_popup').fadeIn('fast',function(){
							$('embed, object, select').css({ 'visibility' : 'hidden' });
							$('.apply_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/apply.gif'); 
							$('.linkedin_hint').html('You are Anonymous right now. Please sign in to LinkedIn and you will be returned to this page to write a note to ' + that.options.company_name);					
							
							$('.linkedin_connect_form_apply').css('display','block');	
						});

														
						
					}
			});	
			
			if(this.options.token!='unset'){	
				//ajax form events set								
			    var options = { 
			        beforeSubmit:  this._showRequest,  // pre-submit callback 
			        success:       this._showResponse,  // post-submit callback 
			        dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
		
			    }; 
				
			    $('#letter_form').submit(function() { 
			        $(this).ajaxSubmit(options); 
			        return false; 
			    });
			}
			else{
				//form disabled
				$('#apply_submit').replaceWith('<img src=\"http://d1emgcifyo4rdp.cloudfront.net/erin_images/submit.gif\">');	
			}
			// apply form events
			
			$('#apply_cancel').click(function(){
					$('#apply_cancel').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/cancel_down.gif');
					$('.apply_popup').fadeOut('fast', function(){
						$('embed, object, select').css({ 'visibility' : 'visible' });
						$('#apply_cancel').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/cancel.gif');
					});
			});	
			$('#apply_cancel').hover(function(){
					$('#apply_cancel').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/cancel_over.gif');
			});	
			
			$('#apply_cancel').mouseout(function(){
					$('#apply_cancel').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/cancel.gif');
			});	
				
			$('#apply_submit').hover(function(){
					$('#apply_submit').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/submit_over.gif');
			});	
			
			$('#apply_submit').mouseout(function(){
					$('#apply_submit').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/submit.gif');
			});	
			
							
		},
		_followListeners: function(){
			var that = this;
	    	if(that.options.token!='unset'){
	    		$('.follow_text div').html("Follow this job &amp; you will get an email alert when there is a new comment or question or if the job is filled.  You can stop the alerts at any time."); 	
	    	}
			
			$.ajax({
			   type: "POST",
			   url: "php/posting.php",
			   dataType:'json',
			   data: "mode=get_follow_job&job_id=" + that.options.job_id + "&token=" + that.options.token,	
			   beforeSend: function(jqXHR, settings){
			   },	
			   error: function(data){
			   },	     
			   success: function(data){	
			   		//console.log(data);
		       		if(data.response.result=='success'){
		       			
		       				follow = data.response.follow;
							if(follow==0){
								
							}
							else{
								$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/unfollow.gif');						
		
							}
							$("#follow_job").button();
							$('#login').attr('href','./index.php?redirect=jobs/' + that.options.company_string + '/' + that.options.identifier);
							$('.follow_img').click(function(){
								$('.follow_text').hide();
								if(follow==0){
									$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/follow_down.gif');
									if(that.options.token=='unset'){
										$('#linkedin_connect_form_other').attr('action','jobs/follow/' + that.options.company_string + '/' + that.options.identifier);
										$('#linkedin_connect_form_other').submit();
									}
									else{
										$.ajax({
										   type: "POST",
										   url: "php/posting.php",
										   dataType:'json',
										   data: "mode=follow&job_id=" + that.options.job_id + "&token=" + that.options.token,	
										   beforeSend: function(jqXHR, settings){
										   		$(".follow_text").hide();
										   },		     
										   success: function(data){	
									       		if(data.response=='success'){
									       			follow=1;
									       			$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/unfollow.gif');
									       		}
										   }
										});		
									}
								}
								else{
									$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/unfollow_down.gif');
									$.ajax({
									   type: "POST",
									   url: "php/posting.php",
									   dataType:'json',
									   data: "mode=unfollow&job_id=" + that.options.job_id + "&token=" + that.options.token,	
									   beforeSend: function(jqXHR, settings){
									   },		     
									   success: function(data){	
								       		if(data.response=='success'){
								       			follow=0;
								       			$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/follow.gif');
								       		}
									   }
									});	
								}					   
							});	
							$('.follow_img').hover(function(){
								$('.apply_hint').hide();
								if(follow==0){
										$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/follow_over.gif');
										clearTimeout(timer);
										var position = $(this).position();
										$('.follow_text').css('left',position.left);
										$('.follow_text').css('top',position.top+60);
										$('.follow_text').show();									
								}
								else{
									$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/unfollow_over.gif');
			
								}					   
							});	
							$('.follow_text').hover(function(){
								clearTimeout(timer);								
							})
								
							$('.follow_text').mouseleave(function(){
							    timer = setTimeout(function(){
							        $(".follow_text").hide(); // regular hide
							    }, 300); // 1 second
								
							});	
							$('.follow_img').mouseleave(function(){
							    timer = setTimeout(function(){
							        $(".follow_text").hide(); // regular hide
							    }, 300); // 1 second
								if(follow==0){
									$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/follow.gif');
									
		
								}
								else{
									$('.follow_img').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/unfollow.gif');
			
								}					   
								
							});	
					}
			   }
			});			
		},
		_setBasicListeners: function() {
			if(this.options.token=='unset'){
				$('#apply_linked_in').show();	
			}
			
			$('.back_img').click(function(){
					window.location = '/jobs';
			});
			$('.cancel_share').click(function(){
					$('.embed_share').slideUp('slow');
			});
			$('.cancel_share2').click(function(){
					$('.embed_share2').slideUp('slow');
			});			
			$('.minimal7').click(function(){
					$('.embed_share').slideDown('slow');
			});
			$('.minimal8').click(function(){
					$('.embed_share2').slideDown('slow');
			});			
			
			
		},
		_showRequest: function(formData, jqForm, options) { 
			var that = $(document).data('job_posting');
			$('.loader').show();
			$('#apply_submit').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/submit_down.gif');
			if(!$('#letter').val()||$('#letter').val()=="Your introductory note to" + that.options.company_name + "..."){
				return false;
				$('.error2').show();
			}
			else{
			    var queryString = $.param(formData); 
			    //console.log(queryString);
				return true;
			}
		},	
		_showResponse: function(responseText, statusText, xhr, $form)  { 
			//console.log(responseText);
			//console.log(statusText);
			//console.log(xhr);

			var that = $(document).data('job_posting');
			if(responseText.response==1){
				$('.textarea').empty();					
				$('.textarea').html('<div class=\"success_apply\">Congratulations on taking the bold step to apply, ' + $('.account_preview_name').html() + '!  '+  that.options.company_name + ' will soon review your application and you will receive a follow-up.</div><br class="clear"/>');
				
				
			}
			else{
				$('.textarea').empty();					
				$('.textarea').html('<div class=\"success_apply\">You have already expressed interest.  You will hear back soon!</div><br class="clear"/>');
				
			}
			$('#letter_form').submit(function() { 
				return false;
				
			});
			$('#apply_submit').attr('src','http://d1emgcifyo4rdp.cloudfront.net/erin_images/submit.gif');
			$('.apply_popup').fadeOut(10000,function(){
				$('embed, object, select').css({ 'visibility' : 'visible' });	
			});
			$('.loader').hide();
		},	
        _initTemplates: function () {
            // Handle cases where the templates are defined
            // after the widget library has been included:
            if (this.options.hover_searching_template instanceof $ &&
                    !this.options.hover_searching_template.length) {
                this.options.hover_searching_template = $(
                    this.options.hover_searching_template.selector
                );
            }
            if (this.options.hover_results_template instanceof $ &&
                    !this.options.hover_results_template.length) {
                this.options.hover_results_template = $(
                    this.options.hover_results_template.selector
                );
            }
           if (this.options.hover_no_results_template instanceof $ &&
                    !this.options.hover_no_results_template.length) {
                this.options.hover_no_results_template = $(
                    this.options.hover_no_results_template.selector
                );
            }
           if (this.options.comments_template instanceof $ &&
                    !this.options.comments_template.length) {
                this.options.comments_template = $(
                    this.options.comments_template.selector
                );
            }
            
        },
        _hoverSearchEvents: function(){
        	if(this.options.preview!=1){
	        	var that = this;
			   	$('.all_jobs_list_PAGE li').click(function(){
					window.location = $(this).find("a.real_url").attr('href');
					//alert('here');
				});
		   		
				var timer4;
				//$('.key_tag4').unbind('hover');
				$('#search_jobs_inline').unbind('hover');
				$('.key_tag4').unbind('mouseleave');
				$('#search_jobs_inline').unbind('mouseleave');
				
				$('.key_tag4').hover(function(){
						clearTimeout(timer4);
				});	
				$('#search_jobs_inline').hover(function(){
					clearTimeout(timer4);
				});
				$('.delete7').live('click',function(){
				        $("#search_jobs_inline").hide(); 
						that.options.hover_searching_template.tmpl().appendTo("#search_jobs_inline");	
				});
				$('.key_tag4').mouseleave(function(){
				    timer4 = setTimeout(function(){
				        $("#search_jobs_inline").hide();
						that.options.hover_searching_template.tmpl().appendTo("#search_jobs_inline");
				         // regular hide
				    }, 300); // 1 second
				});	
				$('#search_jobs_inline').mouseleave(function(){
					
				    timer4 = setTimeout(function(){
				        $("#search_jobs_inline").hide(); 
						that.options.hover_searching_template.tmpl().appendTo("#search_jobs_inline");	
				    }, 300); // 1 second
				});	
        	}
        },
																										
		_hoverSearch: function() {
			if(this.options.preview!=1){
				var that = this;
				$('.key_tag4').hover(function(){
			 		var query = $(this).html();
					var position = $(this).position();
					$.ajax({
						   type: "POST",
						   url: "/php/search.php",
						   dataType:'json',
						   data: "mode=search_on_page&query=" + query + "&token=" + that.options.token,
						   beforeSend: function(jqXHR, settings){
								$('#search_jobs_inline').css('left',position.left-350);
								$('#search_jobs_inline').css('top',position.top+45);
								$('#search_jobs_inline').empty();
								that.options.hover_searching_template.tmpl().appendTo("#search_jobs_inline");
								$("#search_jobs_inline").show();
						   },	  
						   error: function(e){
						   },
						   success: function(data) {
						   		$('#search_jobs_inline').html('<span class=\'popup_triangle_side\'></span><div class=\"search_container\"><div class="delete7"><img src="https://s3.amazonaws.com/assets.talentheroes.com/img/close3.png"/></div><h1>Related Jobs For "' + query + '"</h1><ul class=\"all_jobs_list_PAGE\"></ul><br class=\"clear\"/></div>');
						   		if(data.response.display_num_jobs>6) var max=6;
						   		else var max = data.response.display_num_jobs;
						   		if(max>0){
								   	for(var i=0;i<max;i++){
								   		that.options.hover_results_template.tmpl(data.response.jobs[i]).appendTo(".all_jobs_list_PAGE");
					
								   	}
									that._hoverSearchEvents();
						   		}
						   		else{
									that.options.hover_no_results_template.tmpl().appendTo("#search_jobs_inline");								
									that._hoverSearchEvents();
						   			
						   		}
						   }
					});
					
				});	
			}		
		},
		_commentHandlers: function(){
			var that=this;
			//comment_form
			//console.log('comment handlers');
			if(that.options.token!='unset'){
				var commentor_image = $('#comment_form_image').attr('src');
				$('.comment_form').append("<textarea id=\"comment_text\" name=\"comment\" cols=\"26\" rows=\"5\"/></textarea><br/><label class=\"radio\"><input type=\"radio\" id=\"anonymous_redirect_button\" name=\"anonymous\" value=\"1\" />  Anonymous</label><br/><label class=\"radio\"><input type=\"radio\" id=\"linkedin_redirect_button\" name=\"anonymous\" checked=\"checked\" value=\"0\"/>  With linkedin profile</label><input class=\"white_button\" id=\"post_comment\" value=\"Post\" type=\"button\"/><div id=\"comment_loader_list\"><img height=\"30\" src=\"http://d1emgcifyo4rdp.cloudfront.net/img/loader32.gif\" id=\"comment_loader\"/>Loading...</div>");			
				$("#linkedin_redirect_button").click(function(){
					 $('#comment_form_image').attr('src',commentor_image);
				});
				$("#anonymous_redirect_button").click(function(){
					 $('#comment_form_image').attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
				});			
			}
			else{
				$('.comment_form').append("<textarea id=\"comment_text\" name=\"comment\" cols=\"26\" rows=\"5\"/></textarea><br/><label class=\"radio\"><input type=\"radio\" name=\"anonymous\" value=\"1\" checked=\"checked\"/>  Anonymous</label><br/><label class=\"radio\"><input type=\"radio\" id=\"linkedin_redirect_button\" name=\"anonymous\" value=\"0\"/>  With linkedin profile</label><input class=\"white_button\" id=\"post_comment\" value=\"Post\" type=\"button\"/><div id=\"comment_loader_list\"><img height=\"30\" src=\"http://d1emgcifyo4rdp.cloudfront.net/img/loader32.gif\" id=\"comment_loader\"/>Loading...</div>");	
				$("#linkedin_redirect_button").click(function(){
					$('#linkedin_connect_form_other').attr('action','jobs/' + that.options.company_string + '/' + identifier);
					$('#linkedin_connect_form_other').submit();
					
					// window.location = 'index.php?redirect=jobs/' + company_name + '/' + identifier;
				});						
			}
			if(preview=='0'){
				$("#post_comment").live('click', function(){
					var anonymous = $("input[@name=anonymous]:checked").val();
					var comment = $("#comment_text").val();
					var current = new Date();
					var Year = current.getFullYear();
					var Day = current.getDate();
					var Month = current.getMonth() + 1;
					var Hour = current.getHours();
					var Minutes = current.getMinutes();
					var Seconds = current.getSeconds();
					if(comment!=''){
						$.ajax({
						   type: "POST",
						   url: "php/posting.php",
						   dataType:'json',
						   data: {
						   		mode:"add_comment",
						   		job_id:that.options.job_id,
						   		token:that.options.token,
						   		anonymous:anonymous,
						   		comment: comment,
						   		date: Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minutes + ":" + Seconds
						   },
						   beforeSend: function(jqXHR, settings){
						   		$('#comment_loader_list').show();
						   		$('#comment_text').val('');
						   		//console.log("before");
						   },		     
						   success: function(data){	
								//console.log(data);
						   		
						   		$('#comment_loader_list').hide();			   	   
						   	 
						   	 	that._getComments();
						   },
						   error: function(e){
						   		//console.log(e);
						   }
						  
						});	
					}	
				});
			}	
			else{
				$("#post_comment").click(function(){
					alert('This functionality will be turned on when you publish this listing');
				});	
			}	
			//comment_reply
			
			var commentor_image = $('#comment_form_image').attr('src');
			$('.reply_comment_list img').attr('src',commentor_image);
			$('.comments_display_date').each(function(){
				var new_date = that.prettyDate($(this).text());
				$(this).html(new_date);
				
			});

		    $('.comments_profile_pic > a > img').error(function (){
		   			$(this).attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
		    });
		    $('.comments_profile_pic2 > a > img').error(function (){
		   			$(this).attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
		    });
		    
		    $('.linkedin_hover').error(function (){
		   			$(this).attr('src','https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg');
		    });
	   	 	
	   	 	if(preview=='0'){
		   	 	$('.comment_reply').live('click', function(){
		   	 		if($(this).html()=='Reply') $(this).html('Cancel');
		   	 		else $(this).html('Reply');
					$(this).parent().parent().parent().next().slideToggle('fast');
					$(this).parent().parent().parent().next().next().slideToggle('fast');
					$(this).parent().parent().parent().next().next().find('.reply_comment_button').click(function(){
							var id = $(this).parent().prev().prev().find('.comment_reply').attr('id').replace('comment_','');
							var answer = $(this).parent().prev().find('textarea').val();
							var $button = $(this);
							var current = new Date();
							var Year = current.getFullYear();
							var Day = current.getDate();
							var Month = current.getMonth() + 1;
							var Hour = current.getHours();
							var Minutes = current.getMinutes();
							var Seconds = current.getSeconds();
							
							$.ajax({
							   type: "POST",
							   url: "php/posting.php",
							   dataType:'json',
							   data: {
							   		mode:"reply_comment",
							   		job_id:that.options.job_id,
							   		token:that.options.token,
							   		comment_id:id,
							   		answer:answer,
							   		date: Year + "-" + Month + "-" + Day + " " + Hour + ":" + Minutes + ":" + Seconds
							   },	
							   beforeSend: function(jqXHR, settings){
									$button.parent().next().show()
							   },		     
							   success: function(data){
									that._getComments();

							   }
							});

					});	
									   	 		
		   	 	});
	   	 	}
	   	 	else{
	   	 		$('.comment_reply').live('click',function(){
	   	 			alert('This functionality will be turned on when you publish this listing');
	   	 		});	
	   	 	}			
		},
		_getComments: function(){
			var that = this;
			var commentor_image = $('#comment_form_image').attr('src');
			$.ajax({
			   type: "POST",
			   url: "php/posting.php",
			   dataType:'json',
			   data: "mode=get_comments&job_id=" + that.options.job_id + "&token=" + that.options.token,	
			   beforeSend: function(jqXHR, settings){
			   },		     
			   success: function(data){	
			   	    //////////console.log(data);
			   	 	var comments_body_string = '';
			   	 	comments_body_string += "<ul class=\"form4\"></ul>";
			   	 	$('.comments_body').html(comments_body_string);
			   	 	if(data.response.num_comments>0){
				        var options = {
				            owner: data.response.owner,
				            token: that.options.token,
				            prettyDate: that.prettyDate,				            				            
							commentor_image:commentor_image
						    
							
				        };
				   	 	for(var i=0;i<data.response.num_comments;i++){
				   	 		
							that.options.comments_template.tmpl(data.response.comments[i],options).appendTo('.comments_body ul');
				   	 	}
			   	 	}
			   	 	else{
			   	 		
			   	 		$('.comments_body ul').append("<li>No Comments</li>");
			   	 	}
			   }
			});

			   	 	
			
		},
		_connectabilityHandlers: function(){
				$('#login_button_connectability').click(function(){
								//$('#linkedin_connect_form').attr('action','jobs/follow/' + company_name.replace(" ","-") + '/' + identifier);
								$('#linkedin_connect_form_other').submit();
					
				});
				$('.linkedin_hover').mouseover(function(){
					//alert('here');
					    clearTimeout(timer);
						if($(this).attr('link_info')!=null){
							var url = $(this).attr('link_info');
						}
						else{
							var url = 'http://linkedin.com';
						}						
						$('#linkedin_popup').unbind('click');
						$('#linkedin_popup').html('<span class=\"popup_triangle2\"></span><div class=\"linked_in_popup_name\">' + $(this).attr('name_info') + '<br/><span>' + $(this).attr('position_info') + '</span></div>');
						var position = $(this).position();
						$('#linkedin_popup').css('left',position.left);
						$('#linkedin_popup').css('top',position.top+40);
						$('#linkedin_popup').show();
						$('#linkedin_popup').click(function(){
							window.open(url);
						});
				});	
				$('.linkedin_hover').click(function(){
					if($(this).attr('link_info')!=null){
						window.open($(this).attr('link_info'));
					}
					else{
						window.open('http://linkedin.com');
					}
				});
				$('#title_hiring_manager').click(function(){
					if($(this).attr('link_info')!=null){
						window.open($(this).attr('link_info'));
					}
					else{
						window.open('http://linkedin.com');							
					}
				});
				$('#linkedin_popup').hover(function(){
					 clearTimeout(timer);
				    //timer = setTimeout(function(){
				        //$("#linkedin_popup").show(); // regular hide
				    //}, 1000); // 1 second
				});	
				$('#linkedin_popup').mouseleave(function(){
					 
				    timer = setTimeout(function(){
				        $("#linkedin_popup").hide(); // regular hide
				    }, 1000); // 1 second
				});	
								
				$('.job_connect').mouseleave(function(){
					 
				    timer = setTimeout(function(){
				        $("#linkedin_popup").hide(); // regular hide
				    }, 1000); // 1 second
				});	
			
		},
		_getConnections: function(){
			var that = this;
			$.ajax({
			   type: "POST",
			   url: "php/posting.php",
			   dataType:'json',
			   data: "mode=get_connections&job_id=" + that.options.job_id + "&token=" + that.options.token,	
			   beforeSend: function(jqXHR, settings){
			   		//showLoading
			   },	
			   error:function(data){

			   },     
			   success: function(data){
			   		if(data.response.result=='success'){
						//alert('here');
						var connect_string = '<div class=\'hiring_manager_info\'>';
						if(data.response.manager_link!=null){
						connect_string += '<span class=\"hiring_manager_info_general\">Your connections to hiring manager</span>&nbsp;<span class=\"hiring_manager_info_green_arrow\">&raquo;</span>&nbsp;<a class=\"hiring_manager_link\" href=\"' +data.response.manager_link+'\"><span class=\"hiring_manager_info_name\">' + data.response.manager + '</span> </a></div>';
						}
						else{
							if(data.response.connect!='not set'){
								connect_string += '<span class=\"hiring_manager_info_general\">Hiring manager</span>&nbsp;<span class=\"hiring_manager_info_green_arrow\">&raquo;</span>&nbsp;<span class=\"hiring_manager_info_name\">' + data.response.manager + '</span></div>';
							}
							else{
								connect_string += '<span class=\"hiring_manager_info_general\">Hiring manager</span>&nbsp;<span class=\"hiring_manager_info_green_arrow\">&raquo;</span>&nbsp;<span class=\"hiring_manager_info_name\">' + data.response.manager + '</span></div>';
								
							}
							
						}
						if(access_token!='unset'){
							if(data.response.connect!=0&&data.response.connect!='not set'){
								//connect_string += '<li class=\"title\">Connection';
								if(data.response.connect['relation-to-viewer']){
									if(data.response.connect['relation-to-viewer'].distance==1){
										var distance = '1st Degree';
									} 
									else if(data.response.connect['relation-to-viewer'].distance==2){
										var distance = '2nd Degree';
									} 	
									else if(data.response.connect['relation-to-viewer'].distance==3){
										var distance = '3rd Degree';
									} 
									else if(data.response.connect['relation-to-viewer'].distance==-1){
										var distance = 'Out of Network';
									} 												
									else if(data.response.connect['relation-to-viewer'].distance==0){
										var distance = 'You own this job';
									}
									else if(data.response.connect['relation-to-viewer'].distance==100){
										var distance = 'Group Share';
									}
								}
								else{
									var distance = 'You are not connected to this job';
								}																							
								//connect_string += '<br/><span class=\"connect_info\">' + distance + '</span></li>';	
								
								var connection_index=0;
								if(data.response.connect['relation-to-viewer']){
	
									if(data.response.connect['relation-to-viewer'].connections){
										connect_string += '<ul> ';
										if(data.response.connect['relation-to-viewer'].connections.connection[connection_index]){
											while(data.response.connect['relation-to-viewer'].connections.connection[connection_index]){
												if(connection_index<8){
													if(connection_index==0){
														if(data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name']!='private'){
															if(data.response.connect_pics[connection_index]==''){
																connect_string += '<li><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\"class=\"linkedin_hover\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\" width=40px;/></li>';													
															}
															else{
																connect_string += '<li><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\"class=\"linkedin_hover\" src=\"' + data.response.connect_pics[connection_index] + '\" width=40px;/></li>';
															}
														}
													}
													else{
														if(data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name']!='private'){
															if(data.response.connect_pics[connection_index]==''){
																connect_string += '  <li><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\" class=\"linkedin_hover\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\" width=40px;/></li>';
															}
															else{
																connect_string += '  <li><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\" class=\"linkedin_hover\" src=\"' + data.response.connect_pics[connection_index] + '\" width=40px;/></li>';	
															}
														}
														
													}
													
												}
												else{
													
													if(connection_index==0){
														if(data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name']!='private'){
															if(data.response.connect_pics[connection_index]==''){
																connect_string += '<li class=\"connect_hide\"><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\"class=\"linkedin_hover\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\" width=40px;/></li>';													
															}
															else{
																connect_string += '<li class=\"connect_hide\"><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\"class=\"linkedin_hover\" src=\"' + data.response.connect_pics[connection_index] + '\" width=40px;/></li>';
															}
														}
													}
													else{
														if(data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name']!='private'){
															if(data.response.connect_pics[connection_index]==''){
																connect_string += '  <li class=\"connect_hide\"><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\" class=\"linkedin_hover\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\" width=40px;/></li>';
															}
															else{
																connect_string += '  <li class=\"connect_hide\"><img link_info=\"' + data.response.connect_link[connection_index] + '\" position_info=\"' + data.response.connect_headline[connection_index] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection[connection_index].person['last-name'] + '\" class=\"linkedin_hover\" src=\"' + data.response.connect_pics[connection_index] + '\" width=40px;/></li>';	
															}
														}
														
													}
													
												}											
												connection_index++;
											}
										}
										else{
											if(data.response.connect['relation-to-viewer'].connections.connection.person['first-name']!='private'){
												if(data.response.connect_pics[0]==''){
													connect_string += '<li><img link_info=\"' + data.response.connect_link[0] + '\" position_info=\"' + data.response.connect_headline[0] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection.person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection.person['last-name'] + '\" class=\"linkedin_hover\" src=\"https://s3.amazonaws.com/images.talentheroes.com/anonymous.jpg\" width=40px;/></li>';
												}
												else{
													connect_string += '<li><img link_info=\"' + data.response.connect_link[0] + '\" position_info=\"' + data.response.connect_headline[0] + '\" name_info=\"' + data.response.connect['relation-to-viewer'].connections.connection.person['first-name'] + ' ' + data.response.connect['relation-to-viewer'].connections.connection.person['last-name'] + '\" class=\"linkedin_hover\" src=\"' + data.response.connect_pics[0] + '\" width=40px;/></li>';	
												}
											
											}
										}
										connect_string += '</ul>';
									}
								}
								else{
									
								}
								if(connection_index>8){
									connect_string += '<div class=\"show_more_connects\">More</div>';
										
								}
								connect_string += '<br class=\"clear\"/>';
								$('.job_connect').html(connect_string);	
								$('.show_more_connects').click(function(){
										if($(this).text()=='More'){
											$(this).html('Hide');
											$('.connect_hide').show();
										}
										else{
											$(this).html('More');
											$('.connect_hide').hide();										
										}
								});
								
							}
							else{
								if(data.response.connect!='not set'){
									connect_string += '<ul><li class=\"not_connected\"> ';
									connect_string += 'Sorry, we don\'t show you connected to ' + data.response.manager + ' through LinkedIn.';
									connect_string += '</li></ul>';
									connect_string += '<br class=\"clear\"/>';
								}
								else{
									//connect_string += '<ul><li class=\"not_connected\"> ';
									//connect_string += 'Sorry, we don\'t show you connected to ' + data.response.manager + ' through LinkedIn.';
									//connect_string += '</li></ul>';
									connect_string += '<br class=\"clear\"/>';
									
								}
								$('.job_connect').html(connect_string);
							}
						}
						else{
								connect_string += '<ul><li> ';
								connect_string += '<div id=\"connect_not_signed\" class=\"connect_info\">Sign in to see your connectability</div>';
								connect_string += '<div id=\"connect_not_signed_image\"><img id=\"login_button_connectability\" type="image" src="img/linkedin-login-button.png" /></div>';
								connect_string += '</li></ul>';
								connect_string += '<br class=\"clear\"/>';
								$('.job_connect').html(connect_string);	
	
							
						}
						that._connectabilityHandlers();
	
			   		}
			   }
			});				
		},
	    prettyDate: function(time){
			var monthname = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
		
			
			var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
				diff = (((new Date()).getTime() - date.getTime()) / 1000),
				day_diff = Math.floor(diff / 86400);
					
			if ( isNaN(day_diff) || day_diff < 0)
				return;
			if(day_diff >= 31){
				var date_year = date.getFullYear();
				
				var month_name = monthname[date.getMonth()];
				var date_month = date.getMonth() + 1;
				if(date_month < 10){
					date_month = "0"+date_month;
				}
				var date_monthday = date.getDate();
				if(date_monthday < 10){
					date_monthday = "0"+date_monthday;
				}
				var mon_diff = (day_diff/31);
				mon_diff = Math.floor(mon_diff);
				if(mon_diff>=1&&mon_diff<=12){
					if(mon_diff==1){
						return "about " + mon_diff + " month ago";					
					}
					else{
						return "about " + mon_diff + " months ago";
					}
				}
				else{
					var year_diff = Math.floor(mon_diff/12);
					return "about " + year_diff + " years ago";
				}	
				
			}			
			return day_diff == 0 && (
					diff < 60 && "just now" ||
					diff < 120 && "1 minute ago" ||
					diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
					diff < 7200 && "1 hour ago" ||
					diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
				day_diff == 1 && "Yesterday" ||
				day_diff < 7 && day_diff + " days ago" ||
				day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
		}

	});
}(jQuery));	
