<%
var hasError = false;
%>
<!DOCTYPE html>
<html>
  <head>
    <%- partial('header_partial') %>
    <link rel="stylesheet" href="css/settings-profile.css" />
    <script type='text/javascript' src='js/moment.min.js'></script>
    <script type='text/javascript'>
    $(function ()
    {

    	// trigger a analytics event when the page loads
    	window.parent._gaq.push(['_trackEvent', 'Locker', 'View  Profile', '<%= name %>']);

    	$("#publishedInfo .action-button").click(function ()
    	{
    		window.parent.location.hash = "";
    	});
    	$("#forceSync").click(function ()
    	{
    		// mehnimal
    		$("#forceSync").text("checking!");
    		$.getJSON("/synclets/github/run?id=repos&force=true", function ()
    		{
    			window.location.reload();
    		});
    	});

    	$("#submitPublish").click(function ()
    	{
    		$("#submitPublish").attr("disabled", "disabled").text("PUBLISHING...");
    		$("#publishedInfo a.issueLink").show();
    		$("#publishedInfo p").text("Publishing...");
    		$("#publishedInfo .action-button").hide();
    		$("#publishedInfo a.issueLink").hide();
    		$("#publishedInfo").show();
    		$("#publishOverlay").width($("body").width()).height($("body").height()).show();
    		$("body").scrollTop(0);

    		// trigger an analytics event when attempting to publish
    		window.parent._gaq.push(['_trackEvent', 'Locker', 'Publish Profile', '<%= name %>']);
    		$.getJSON("push/profile", function (info)
    		{
    			
    			$("#publishedInfo .action-button").show();
    			if (info.err)
    			{
    				if (info.reauth)
    				{
    					$("#publishedInfo p").addClass("error").text("There was an error publishing your profile, you may need to re-connect Github in the account settings.");
    				}
    				else
    				{
    					$("#publishedInfo p").addClass("error").html("There was an error publishing your profile, please <u><a href=\"https://github.com/sxux/profiles/issues/new?title=publish+fail&body=" + encodeURIComponent(info.err) + "\" target=_blank>file an issue</a></u>, thanks!");
    				}
    				$("#submitPublish").removeAttr("disabled");
    			}
    			else
    			{
    				$("#publishedInfo p").text("You successfully submitted \"<%=email%>\" to be published.  You can track its status on Github.");
    				$("#publishedInfo").show();
    				$("#publishedInfo a").show();
    				$("#publishedInfo .issueLink").attr("href", info.issue.html_url).text(info.issue.html_url).attr("target", "_blank");
    			}
    		}).error(function ()
    		{
    			$("#submitPublish").removeAttr("disabled");
    			$("#publishedInfo p").addClass("error").text("There was an error publishing your profile.  Please try again.");
    		});
    	});

function setPrivacyControls(obj)
{
  obj.click(function ()
  {
    if (this.className.indexOf('private')>=0) {
	  $(this).animate({opacity: 1.0}, 100);
	  $(this).toggleClass('private public');
	} else {
      $(this).animate({opacity: 0.5}, 100);
      $(this).toggleClass('public private');
    }
  });
}
    	var baseUrl = '';

    	var baseURL = baseUrl + '/synclets/linkedin/getCurrent/profile';
    	profilesEl = $("#myprofiles");

    	$.getJSON(baseURL, {}, function (profile)
    	{
          //console.log(profile[0]);

          profilesEl.append('<div class="synclet-logoname-container"><img class="synclet-logo" src="img/icons/32px/linkedin.png" /><span class="synclet-name">LinkedIN</span>');

          profilesEl.append('<h3>Summary</h3>');
          sum = $('<div/>', {'class': 'public'});
          sum.append(profile[0].summary); 
          setPrivacyControls(sum);
          profilesEl.append(sum);

          spe = $('<div/>', {'class': 'public'});
          spe.append('<h4>Specialties</h4>'); 
          spe.append('<p>'+profile[0].specialties+'</p>'); 
          setPrivacyControls(spe);
          profilesEl.append(spe);

          profilesEl.append('<h3>Experience</h3>');

          positions=profile[0].positions.values;

          for (var i in positions)
          {
            p = positions[i];
		    pos = $('<p/>', {'class': 'public'});
            pos.append('<div><strong>'+positions[i].title+'</strong></div>'); 
            pos.append('<div><strong>'+positions[i].company.name+'</strong></div>'); 

            if (positions[i].summary)
			  pos.append('<div><strong>'+positions[i].summary+'</strong></div>'); 
			
            ago = moment(positions[i].startDate.year+"-"+positions[i].startDate.month+"-01", 
              "YYYY-MM-DD").fromNow();
            start = moment(positions[i].startDate.year+"-"+positions[i].startDate.month+"-01","YYYY-MM-DD");
            if (positions[i].endDate) 
              end = moment(positions[i].endDate.year+"-"+positions[i].endDate.month+"-01","YYYY-MM-DD");
			else
			  end = new Date();
            duration = moment(start-end);
			pos.append('<span class="ago">'+ago+'</span>');
			pos.append('<span class="duration">'+duration+'</span>');
			pos.append('</span>');

            setPrivacyControls(pos);
     	    profilesEl.append(pos);
		  }
 

          profilesEl.append('<h3>Education</h3>');
          edus=profile[0].educations.values;

          for (var i in edus)
          {
            e = edus[i];
		    edu = $('<p/>', {'class': 'public'});
            edu.append(edus[i].schoolName); 
            setPrivacyControls(edu);
     	    profilesEl.append(edu);
		  }
		
          profilesEl.append('<h3>Skills & Expertise</h3>');

		  interests = profile[0].interests;
				
		  skills = profile[0].skills.values;
		
          for (var i in skills)
          {
		    skl = $('<div/>', {'class': 'public tag'});
            skl.append(skills[i].skill.name);
            setPrivacyControls(skl);
     	    profilesEl.append(skl);
          }

          profilesEl.append('<h3>Recommendations</h3>');

		  recs = profile[0].recommendationsReceived.values;
          for (var i in recs)
          {
            rec = $('<div/>', {'class': 'public'});
            rec.append('"'+recs[i].recommendationText); 
            rec.append('" - '+recs[i].recommender.firstName+' '+recs[i].recommender.lastName); 
            setPrivacyControls(rec);
            profilesEl.append(rec);
          }

          profilesEl.append('<h3>Additional Information - Associations & Group Memberships</h3>');

          ass = $('<div/>', {'class': 'public tag'});
          // we should really split by , separator
          ass.append(profile[0].associations); 
          setPrivacyControls(ass);
          profilesEl.append(ass);
          grps = profile[0].groupMemberships.values;
          for (var i in grps)
          {
            grp = $('<div/>', {'class': 'public tag'});
            grp.append(grps[i].group.name); 
            setPrivacyControls(grp);
            profilesEl.append(grp);
          }
 
    	});

 
    	var baseURL = baseUrl + '/Me/contacts';
    	var sort = '\'{"firstnamesort":1}\'';
    	var terms = "[firstnamesort:\"a\"+]";

    	contactsEl = $("#mycontacts");

    	$.getJSON(baseURL, {
    		limit: 500,
    		sort: sort
    	}, function (contacts)
    	{
    		for (var i in contacts)
    		{
                if (contacts[i] && contacts[i].photos) {
    			avatarImg = $('<img/>', {
    				'src': contacts[i].photos[0],
					'width':  '50px',
					'height': '50px',
    				'class': 'public',
    				'title': contacts[i].name
    			});
    			avatarImg.click(function ()
    			{
    				if (this.className == 'private')
    				{
    					$(this).animate(
    					{
    						opacity: 1.0
    					}, 100);
    					$(this).toggleClass('private public');
    				}
    				else
    				{
    					$(this).animate(
    					{
    						opacity: 0.1
    					}, 100);
    					$(this).toggleClass('public private');

    				}
    			});
    			contactsEl.append(avatarImg);
                }
    		}
            $("#mycontacts-count").append(' ('+contacts.length+')');
    	});


    	var baseURL = baseUrl + '/Me/photos';
    	photosEl = $("#myphotos");

    	$.getJSON(baseURL, {
    		limit: 500
    	}, function (photos)
    	{
			var myphotos=0;
    		for (var i in photos)
    		{
    			if (true || photos[i].me) {
				    myphotos++;
    			    photo = $('<img/>', {
    				'src': photos[i].thumbnail,
					'height': '80px',
    				'class': 'public',
    				'title': photos[i].title
    			    });
    			    photo.click(function ()
    			    {
    				  if (this.className == 'private')
    				  {
    					$(this).animate(
    					{
    						opacity: 1.0
    					}, 100);
    					$(this).toggleClass('private public');
    				}
    				else
    				{
    					$(this).animate(
    					{
    						opacity: 0.1
    					}, 100);
    					$(this).toggleClass('public private');
    				}

    			});
    			photosEl.append(photo);
				}
    		}
            $("#myphotos-count").append(' ('+myphotos+'-'+photos.length+')');

    	});


    	var baseURL = baseUrl + '/Me/places';

    	placesEl = $("#myplaces");

    	$.getJSON(baseURL, {
    		limit: 500
    	}, function (places)
    	{
			var myplaces=0;
            //console.log(places);
     		for (var i in places)
    		{
                console.log(places[i]);                
                if (places[i] )
                {
					myplaces++;
    			    place = $('<div/>', {
    				    'class': 'public'
    			    });
                    console.log(places[i].title);
                    place.append(places[i].title);
    			    place.click(function ()
    			    {
   					    if (this.className == 'private')
    				    {
    					    $(this).animate(
    					    {
    						    opacity: 1.0
    					    }, 100);
    					    $(this).toggleClass('private public');
    				     }
    				     else
    				     {
    					      $(this).animate(
    					      {
    						    opacity: 0.1
    					      }, 100);
    					      $(this).toggleClass('public private');
    				     }
                    });
                	placesEl.append(place);
                }
    		}
            $("#myplaces-count").append(' ('+myplaces+'-'+places.length+')');

		});

	   	var baseURL = baseUrl + '/Me/links';

	    	linksEl = $("#mylinks");

	    	$.getJSON(baseURL, {
	    		limit: 500
	    	}, function (links)
	    	{
	            var mylinks=0;
	     		for (var i in links)
	    		{
	                if (true || links[i].me)
	                {
						mylinks++;
	    			    link = $('<div/>', {
	    				    'class': 'public'
	    			    });
	                    link.append(links[i].title);
	    			    link.click(function ()
	    			    {
	   					    if (this.className == 'private')
	    				    {
	    					    $(this).animate(
	    					    {
	    						    opacity: 1.0
	    					    }, 100);
	    					    $(this).toggleClass('private public');
	    				     }
	    				     else
	    				     {
	    					      $(this).animate(
	    					      {
	    						    opacity: 0.1
	    					      }, 100);
	    					      $(this).toggleClass('public private');
	    				     }
	                    });
	                	linksEl.append(link);
	                }
	    		}
                $("#mylinks-count").append(' ('+mylinks+'-'+links.length+')');
			});
		
    });
  </script>
  </head>
  <body>
    <div id='publishOverlay'>
      <div id='publishedInfo'>
        <p>You successfully submitted "<%=email%>" to be published.</p>
        <a class='issueLink' href='#'>...</a>
        <a class='action-button' href='/dashboard/settings#Settings-Profile'>GOT IT!</a>
      </div>
    </div>
    <content class="body">
      <p class='publishInfo'>
        <p><button id='submitPublish' type="submit" <%=(hasError ? "disabled='disabled'" : "")%> class="publish" style='margin:20px'>PUBLISH</button></p>
<p>
Toggle the fields below to select what to include in your public profile, then click to publish.
        </p>
      </p>
        <ul class="publish-list">
<!--          <li><p>Name
          <% if (name) { %>
            <p class="indented">
            <%= name %>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              Your name is mandatory.
            </p>
            <% } %>
            </p>
          </li>
          <li><p>email
          <% if (email) { %>
            <p class="indented">
            <%= email %>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You primary email address is mandatory.
            </p>
            <% } %>
            </p>
          </li>
           <li><p>Avatar</p>
          <% if (profileImage) { %>
          <div class="avatar"><img src="<%= profileImage %>"/></div>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You must include an advatar , please refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
          </li>
           <li><p>Background</p>
          <% if (profileBackground) { %>
          <div class="preview"><img src="<%= profileBackground %>"/></div>
            <% } else { %>
              <% hasError = true; %>
            <p class='indented error'>
              You must include an advatar , please refer to the
              <a href='/dashboard/develop#Develop-Publishing' target="_top">
                publishing documentation
              </a>
              for more details.
            <% } %>
          </li>-->
         <li><h1 id="myprofile-count">Profiles</h1>
         <div id="myprofiles"></div>
         </li>
         <li><h1 id="mycontacts-count">Contacts</h1>
         <div id="mycontacts"></div>
         </li>
         <li><h1 id="myphotos-count">Photos</h1>
         <div id="myphotos"></div>
         </li>
         <li><h1 id="myplaces-count" >Places</h1>
         <div id="myplaces"></div>
         </li>
         <li><h1 id="mylinks-count">Links</h1>
         <div id="mylinks"></div>
         </li>
        </ul>


        <p><button id='submitPublish' type="submit" <%=(hasError ? "disabled='disabled'" : "")%> class="publish" style='margin:20px'>PUBLISH</button></p>
    </content>
  </body>
</html>


