// JavaScript Document

$(document).ready(function(){

//MENU
var menuids=["menu-header-menu"]

function buildsubmenus_horizontal(){
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ 
			ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" 
			ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ 
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" 
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
    }
    }
  }
}

if (window.addEventListener)
window.addEventListener("load", buildsubmenus_horizontal, false)
else if (window.attachEvent)
window.attachEvent("onload", buildsubmenus_horizontal)

    //FEATURE 
	$.featureList(
				$("#tabs li a"),
				$("#output li"), {
					start_item	:	1
				});
	
    //SCROLL TOP
	$('a.topOfPage').click(function(){
	$.scrollTo( 0, 2000);
	return false;
	});
	
	//HOME SCROLLER
	$('#mask-home').css({'height':$('#panel-home-1').height()});	
	$('#panel-home').width(parseInt($('#mask-home').width() * $('#panel-home div').length));
	$('#panel-home div').width($('#mask-home').width());
	$('a[rel=panel-home]').click(function () {
		var panelheight = $($(this).attr('href')).height();
		$('a[rel=panel-home]').removeClass('selected');
		$(this).addClass('selected');
		$('#mask-home').animate({'height':panelheight},{queue:false, duration:500});			
		$('#mask-home').scrollTo($(this).attr('href'), 800);		
		return false;
	});
	
	//PAGE SCROLLER
	$('#mask').css({'height':$('#panel-1').height()});	
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	$('#panel div').width($('#mask').width());
	$('a[rel=panel]').click(function () {
		var panelheight = $($(this).attr('href')).height();
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		$('#mask').scrollTo($(this).attr('href'), 800);		
		return false;
	});
	
	//SOCIAL
	$.getJSON("https://picasaweb.google.com/data/feed/api/user/kafgrofovi?kind=album&max-results=6&alt=json&callback=?", function(data){
      $.each(data.feed.entry, function(index, item){
        var title = item['title']['$t'];
		$("<img/>").attr("src",item.media$group.media$thumbnail[0].url).appendTo("#picasa")
            .wrap("<a href='" + item.link[1].href + "' title='" +title+ "'></a>");
      });
    });

	$.getJSON('http://twitter.com/status/user_timeline/grofovi.json?count=4&callback=?', function(data){		
		$.each(data, function(index, item){
			$('#twitter').append('<div class="tweet"><p>' + item.text.linkify().linkuser() + '</br><strong>' + relative_time(item.created_at) + '</strong></p></div>');		
		});	
	});

	$.getJSON("http://gdata.youtube.com/feeds/api/users/grofovi/uploads?alt=json-in-script&callback=?&max-results=6&start-index=1", function(data){
    $.each(data.feed.entry, function(index, item){
    var title = item['title']['$t'];
    var video = item['id']['$t'];
    video = video.replace('http://gdata.youtube.com/feeds/api/videos/','http://www.youtube.com/watch?v=');
    videoID = video.replace('http://www.youtube.com/watch?v=','');    
          
        $("<img/>").attr("src",item.media$group.media$thumbnail[0].url).appendTo("#youtube")
            .wrap("<a href='" +video+ "' title='" +title+ "'></a>");
    });
    });

	//TWITTER FIX
	function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);	  
	  var r = '';
	  if (delta < 60) {
		r = 'a minute ago';
	  } else if(delta < 120) {
		r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
		r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
		r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
		r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
		r = '1 day ago';
	  } else {
		r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }	  
	  return r;
	}
	
	String.prototype.linkify = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};	
	String.prototype.linkuser = function () {
        return this.replace(/[@]+[A-Za-z0-9-_]+/, function (u) {
            var username = u.replace("@", "")
            return u.link("http://twitter.com/" + username);
        });
    };
		
});
