$(document).ready(function(){

	$("#accordion").accordion({
		active: '.selected', 
	    active: 0,
		autoHeight: false }
	);

	$("#home #nav li a").hover(
  		function () {
  			var test = $(this).parent().hasClass('current');
   			if (!test) $(this).css("color","#303e3f");
  		},
  		function () {
  			var test = $(this).parent().hasClass('current');
   			if (!test) $(this).css("color","#717c7d");
  		}
	);

	$('.archive div ul').hide();

	$('#archive-text').text('Open Archive');

	$('#archive').attr('title', 'Open Archive');

	$("#archive").toggle(
		function() {
  			$('.archive div ul').slideDown(500, 
  				function() {
  					$('#archive-text').text('Close Archive');
  					$('#archive').attr('title', 'Close Archive' );
  				}
  			);
  	  	}, 
		function() {
  			$('.archive div ul').slideUp(500, 
  				function() {
  					$('#archive-text').text('Open Archive');
  					$('#archive').attr('title', 'Open Archive');
  				}
  			);
		}
	);

	function twitterDate(time){
		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 || day_diff >= 31 )
			return "";
			
		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";
	}
	
	var url = "http://twitter.com/status/user_timeline/kaibrach.json?count=20&callback=?";
	
	$.getJSON(url, 
		function(data){
			$.each(data, 
				function(i, item) {
      				in_reply_to = item.in_reply_to_screen_name;
      	
      				if (in_reply_to != null) 
      					return true;
      	
      				created = twitterDate(item.created_at);
      				tweet = item.text
      					.replace(/(https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/, 
      						function (t) {
            					return '<a href="' + t + '">' + t + '</a>';
          					}
          				)
          				.replace(/@([a-zA-Z0-9_]+)/g, '@<a href="http://twitter.com/$1">$1</a>')
          				.replace(/(?:^|\s)#([^\s\.\+:!]+)/g, 
          					function (a,t) {
            					return ' <a href="http://twitter.com/search?q=' + encodeURIComponent(t) + '">#' + t + '</a>';
          					}
          				);
        
        			tweet = "&ldquo;"+tweet+"&rdquo; <span>- I said "+created+"</span>";
          	
      				$(".tweet").html(tweet);
      				
      				return false;
      			}
      		);
    	}
	);



 	var l = $("#logo_1");
 

	var arrsections = new Array();
	
	arrsections[0] = new Array('#logo_1','top');
	arrsections[1] = new Array('#logo_2','work');
	arrsections[2] = new Array('#logo_3','blog');
	arrsections[3] = new Array('#logo_4','about');
	arrsections[4] = new Array('#logo_5','projects');
	arrsections[5] = new Array('#logo_6','contact');
	
	$("#logo_2").hide();
	$("#logo_3").hide();
	$("#logo_4").hide();
	$("#logo_5").hide();
	$("#logo_6").hide();
	
	
	function animateMenuLogo(logo, menu) {
		var scrollposition = $(window).scrollTop();
		var top = $("a[name='"+ menu +"']").offset().top;
		var sectionheight = $("a[name='"+ menu +"']").next().outerHeight();
		
		if (((top-100) < scrollposition) && ((top+sectionheight-200) > scrollposition)) {
			$(logo).fadeIn(500);
			$("a[href='#"+ menu +"']").css({ backgroundColor: "#303e3f", color: "#ffffff" });
			$("a[href='#"+ menu +"']").parent().addClass("current");
		} else {
			$(logo).fadeOut(500);
			$("a[href='#"+ menu +"']").css("background-color","transparent");
			$("a[href='#"+ menu +"']").css("color","#717c7d");$("a[href='#"+ menu +"']").parent().removeClass("current");
		}		
	}


    $(window).scroll(
    	function() {
    		
    		var top = 0;
    		
    		top = $(window).scrollTop();
    		
            if (top > 110) {
               $("#home #nav").css("top", 60);
            } else {
           	   $("#home #nav").css("top", 170-top);
            } 
            
            if (top > 510) {
            	$("#comment-form").css("top", 60);
            } else {
                $("#comment-form").css("top", 570-top);
            }
           
            for (var i = 0; i < arrsections.length; i++) {
            	animateMenuLogo(arrsections[i][0],arrsections[i][1]);
            } 
            
    	}
   	);  
        

    var selected_item = 0;
    var count_items = $("#slideshow img").size();
    
    for (i=0; i < count_items; i++) {
    	if (i>0)
    		$("#slideshow img:eq("+ i +")").css("display","none");
    		
    	$("#slideshow img:eq("+ i +")").css("z-index", i+100);
    	$("#slideshow img:eq("+ i +")").css("position", "absolute");
    }
        
	function animation() {
		$("#slideshow img:visible").fadeOut(2000, function() {});
		$("#slideshow img:eq("+ selected_item +")").fadeIn(2000, function() {});
	}
	
	 
	
	$(document).everyTime(6000,						   
		function () { 
			selected_item++;
			if (!(selected_item < count_items))
				selected_item = 0;
			animation();
		}					
	);

	
	$(document).oneTime(10000,						   
		function () { 
				$("#footer").stop().animate({ bottom: "-60px" }, 2000, function() {});
			}					
	);
	
	$("#footer").mouseover(
		function() {
			$("#footer").stop().animate({bottom: "0px" }, {duration:250});
		}
	);
			
	$("#footer").mouseout(
		function() {
			$("#footer").stop().animate({ bottom: "-60px" }, {duration:2000});
		}
	);
 

	$("#nav ul a").click(
		function(event) {
			event.preventDefault();
			var loc = $(this).attr("href");    
			var loc = loc.substring(1);
			var dest = 0;
			if (loc == "") {
				dest = 0;
			} else {
				dest = $("a[name='"+loc+"']").offset().top;	
			}
			$("body, html").stop().animate({scrollTop: dest}, {duration:2000});
		}
	);

	$("a[href='#comments']").click(
		function(event) {
			event.preventDefault();
			var dest = $("a[name='comments']").offset().top;	
			$("body, html").stop().animate({scrollTop: dest}, {duration:2000});
		}
	);
  
    
    
	$(".google").css({backgroundPosition: "-140px 18px"});
	
	$(".google").mouseover(
		function() {
			$(".google").stop().animate({backgroundPosition: "0px 18px"}, {duration: 1000});
		}
	);
		
	$(".google").mouseout(
		function(){
			$(".google").stop().animate({backgroundPosition: "-140px 18px"}, {duration: 1000});
		}
	);


   	$("a[title='Previous']").hide(); 
   
   	$("a[title='Next']").click(
   		function(event) {
   			event.preventDefault();
			slidepanel = $(this).next(".slide-show").children(" .slide-panel");
			ctr_images = $(slidepanel).children("img").size();

			if ($(slidepanel).position().left == -((ctr_images-1) * 636)) {
				pos = $(slidepanel).position().left; 0;
			} else {
	    		pos = $(slidepanel).position().left - 636;
			}
	
			if (pos == -((ctr_images-1) * 636)) 
				$(this).hide();
			
			if (ctr_images > 1) 
				$(this).prev().show();

			$(slidepanel).animate({left: pos}, {duration:1000});
		}
   	);

   	$("a[title='Previous']").click(
   		function(event) {
			event.preventDefault();
			slidepanel = $(this).next().next(".slide-show").children(" .slide-panel");
			ctr_images = $(slidepanel).children("img").size();
			
			if ($(slidepanel).position().left == 0) {
				pos = $(slidepanel).position().left; -((ctr_images-1) * 636);
			} else {
	    		pos = $(slidepanel).position().left + 636;
			}
			if (pos == 0)
				$(this).hide();
				
			if (ctr_images > 1)
				$(this).next().show();
			$(slidepanel).animate({left: pos}, {duration:1000});	
		}
	);
	
	  

	var h_comment = $("#comment-form-panel").height();
	
	$("#comment-form-panel").css("overflow","hidden");
	$("#comment-form-panel").css("height","0px");
	$("#comment-form-panel").hide();
	
	$("#comment-form button").click(
		function(event) {
			event.preventDefault();
			if ($('#comment-form-panel').is(":visible")) {
				$('#comment-form').submit();
			} else {
				$("#comment-form-panel").show();
				$('#comment-form-panel').animate({height: h_comment}, {duration:500});	
			}
  		}
  	);
	
	$("#contactform button").click(
		function(event){
			event.preventDefault();
			$("#ajax").val('1');
			var str = $('#contactform').serialize();
			$("#contactform label").removeClass("error");
			$.ajax({
				type: "POST",
				url: $("#contactform").attr('action'),
				data: str,
				dataType: 'json',
				success: function(msg) {
					if (msg.type == 'OK') {
						$("#contactform").html('<div>'+ msg.messages + '</div>');		
					} else {
						for (var i = 0; i < msg.messages.length; i++)
							$("#contactform label[for='" + msg.messages[i][0] + "']").addClass("error");
					}
				}
			});
		}
	);

});
