jQuery(document).ready(function(){  
  
//$(document).scroll(function(){
	

//var tophat = $(document).scrollTop();

//  if(tophat > 120){
//	$('#menuback').css({'position':'fixed', 'top':'0', 'left':'0', 'z-index':'99999'});
//	$('#main').css({'margin-top':'110px'});
//}else{
//	$('#menuback').css({'position':'relative'});
//	$('#main').css({'margin-top':'0px'});
//}

//})
    $("#nav-menu li a").mouseover(function() { 

        $(this).parent().find("ul.subnav").slideDown('fast').show(); 
  
        $(this).parent().hover(function() {  
        }, function(){  
            $(this).parent().find("ul.subnav").stop(true, true).hide();
        });  
  
        });

			$('input[name=callback]').keyup(function(e) {

				var code = e.keyCode || e.which;
				 if(code == 13) {

				   call();

					return false;
				 }else{
					return false;
				}

			});	

				
				
});

function call(){
	var fail = 0;
	
	if($('input[name=callback]').val().length < 6){
		
		$('input[name=callback]').css({'borderColor':'#d73636', 'borderWidth':'2px', 'padding':'4px'});
		$('input[name=callback]').val('Num invalid');
		$('#button').css({'backgroundColor':'#f2bbbb', 'borderColor':'#d73636'});
		var fail = 1;
	}else{
		
		var number = $('input[name=callback]').val();
		
	}
	
	if(fail == 0){
	
	$('#button').html('<img src=\"images/loader.gif\" />');
	
	
	$.ajax({
		type: "POST",
		url: "process.php",
		data: "call=1&number=" + number,
		dataType: 'json',
		success: function(msg){
			
			if(msg.state == "fail"){
				
				if(msg.message == "invalid"){
					
						$('input[name=callback]').css({'borderColor':'#d73636', 'borderWidth':'2px', 'padding':'4px'});
						$('input[name=callback]').val('Num invalid');
						
						$('#button').css({'backgroundColor':'#f2bbbb', 'borderColor':'#d73636'});
						$('#button').html('Submit');
						
				}else if(msg.message == "fail"){
					
						$('input[name=callback]').css({'borderColor':'#aaaaaa', 'borderWidth':'1px', 'padding':'5px'});
						$('input[name=callback]').val('Try again');
						
						$('#button').css({'backgroundColor':'#f2bbbb', 'borderColor':'#d73636'});
						$('#button').html('Submit');
					
				}
			}else if(msg.state == "success"){
				
				$('input[name=callback]').css({'borderColor':'#aaaaaa', 'borderWidth':'1px', 'padding':'5px'});
				$('input[name=callback]').val('Sent!');
				
				$('#button').css({'backgroundColor':'#b6dab1', 'borderColor':'#70a868'});
				$('#button').html('Done!');
				$('#button').attr('onclick', '');
				
			}
		}
	});
  }
}

String.prototype.linkify = function() {  //Make links linkable
           return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/~.=]+/, function(m) {
       return m.link(m);
     });
   };



function relative_time(time_value, is_relative) {
        var values = time_value.split(" "),
            parsed_date = Date.parse(values[1] + " " + values[2] + ", " + values[5] + " " + values[3] + " UTC"),
            date = new Date(parsed_date),
            relative_to = new Date(),
            r = '',
            delta = parseInt((relative_to.getTime() - date.getTime()) / 1000);

        var seconds = {
          'from' : {
            'minutes' : function(v) { return v * 60; },
            'hours'   : function(v) { return this.minutes(v) * 60; },
            'days'    : function(v) { return this.hours(v) * 24; },
            'weeks'   : function(v) { return this.days(v) * 7; },
            'months'  : function(v) { return this.weeks(v) * 4.34812141; },
            'years'   : function(v) { return this.months(v) * 12; }
          },
          'to' : {
            'minutes' : function(v) { return v / 60; },
            'hours'   : function(v) { return this.minutes(v) / 60; },
            'days'    : function(v) { return this.hours(v) / 24; },
            'weeks'   : function(v) { return this.days(v) / 7; },
            'months'  : function(v) { return this.weeks(v) / 4.34812141; },
            'years'   : function(v) { return this.months(v) / 12; }
          }
        };

        if (!is_relative)
          return formatTime(date) + ' ' + formatDate(date);

        if (delta < 30) 
          return 'less than a minute ago';
        var minutes = parseInt(seconds.to.minutes(delta)+0.5);
        if (minutes <= 1) 
          return 'about a minute ago';
        var hours = parseInt(seconds.to.hours(delta)+0.5);
        if (hours < 1) 
          return minutes + ' minutes ago';
        if (hours == 1) 
          return 'about an hour ago';
        var days = parseInt(seconds.to.days(delta)+0.5);
        if (days < 1) 
          return hours + ' hours ago';
        if (days==1) 
          return formatTime(date) + ' yesterday';
        var weeks = parseInt(seconds.to.weeks(delta)+0.5);
        if (weeks < 2) 
          return formatTime(date) + ' ' + days + ' days ago';
        var months = parseInt(seconds.to.months(delta)+0.5);
        if (months < 2) 
          return weeks + ' weeks ago';
        var years = parseInt(seconds.to.years(delta)+0.5);
        if (years < 2) 
          return months + ' months ago';
        return years + ' years ago';

        function formatTime(date) {
            var hour = date.getHours(),
                min = date.getMinutes() + "",
                ampm = 'AM';

            if (hour >= 12) ampm = 'PM';
            if (hour > 12) hour -= 12;

            if (min.length == 1) {
                min = '0' + min;
            }

            return hour + ':' + min + ' ' + ampm;
        }
};

function twitFetch(){
	
	$('.twitterfeed').html("<center><img src=\"images/loadinfo.gif\" width=\"24px\" height=\"24px\" style=\"margin:10px 0;\"/></center>");
	$('.facebookfeed').html("<center><img src=\"images/loadinfo.gif\" width=\"24px\" height=\"24px\" style=\"margin:10px 0;\"/></center>");
	
  $.getJSON('http://twitter.com/statuses/user_timeline/PrettyKlicks.json?count=3&callback=?',function(json){
      var html = "<ul>";
    $.each(json,function(i,tweet){
         html += "<li>" + tweet.text.linkify() + "<span class=\"time\"><abbr title=\"" + tweet.created_at + "\">" + relative_time(tweet.created_at,true) +"</abbr></span></li>";    
    });
            html += "</ul>";
			html += "<div class=\"counter twit\">Join our " + json[0].user.followers_count + " followers!</div>";
               $('.twitterfeed').animate({opacity:0}, 500, function(){
								$('.twitterfeed').html(html);
								});

		    $('.twitterfeed').animate({opacity:1}, 500);
  });
	
	
       var url = "https://graph.facebook.com/prettyklicks/posts?limit=5&"+access+"&callback=?";
	  
	$.getJSON(url,function(json){
	    var html = "<ul>";
	    $.each(json.data,function(i,fb){
			//XSS ATTACK FIX
			//if(fb.from.name == "Pretty Klicks"){
				html += "<li>" + $('<div>').html(fb.message).text().linkify() + "</li>";
				//html += "<li>" + fb.message.linkify() + "</li>";
			//}
	    });
	    html += "</ul>";
	
	var url = "https://graph.facebook.com/prettyklicks?"+access+"&callback=?";
	
		$.getJSON(url,function(json){
			
		  html += "<div class=\"counter fb\">" + json.likes + " people like Pretty Klicks.</div>";
		
		  });
	
	
		$('.facebookfeed').animate({opacity:0}, 500, function(){
											
											$('.facebookfeed').html(html);
												
													});
															
	    $('.facebookfeed').animate({opacity:1}, 500);
	  });
	
		
		
		
		
		 
};


