$(function() {
	if($.browser.msie && $.browser.version<7) {
		$("link").remove();
	}
	
	$(".firstHidden").hide();
	
	SyntaxHighlighter.autoloader(
		'js jscript javascript  /scripts/shBrushJScript.js',
		'css                    /scripts/shBrushCss.js',
		'text plain             /scripts/shBrushPlain.js',
		'xml xhtml xslt html    /scripts/shBrushXml.js',
		'objc obj-c   					/scripts/shBrushObjectiveC.js'
	);
	SyntaxHighlighter.all();
	
	var cUrl = document.URL;
	if (cUrl.indexOf("/blog")!=-1) {
		$("#mainNavigation ul li a.blog").addClass("current");
	} else if (cUrl.indexOf("/news")!=-1) {
		$("#mainNavigation ul li a.news").addClass("current");
	} else if (cUrl.indexOf("/contact")!=-1) {
		$("#mainNavigation ul li a.contact").addClass("current");
	} else if (cUrl.indexOf("/tag")!=-1) {
	} else {
		$("#mainNavigation ul li a.home").addClass("current");
	}
	
	$("#bottomArea .content .entry .thumbnail img").before("<span class=\"cover\"></span>");
	$("#bottomArea").css({"height":($("#bottomArea .content .section").outerHeight())});
	
	//MainNavi
	$("#mainNavigation ul li a").mouseover(function() {
		if(!$(this).hasClass("current")) {
			$(this).addClass("hover");
		}
	});
	$("#mainNavigation ul li a").mouseout(function() {
		if(!$(this).hasClass("current")) {
			$(this).removeClass("hover");
		}
	});
	
	
	$("#mainNavigation ul li a.profile").click(function() {
	
		var dw = $(document).width();
		var dh = $(document).height();
		$("#profile").fadeIn("fast");
		var pos = $("#mainNavigation ul li a.profile").offset();
		$("#profile").css({"top":($(window).height()-$("#profile").outerHeight())*0.5,"left":(dw-$("#profile").outerWidth())*0.5});
		$("body").append("<div id=\"black\" />");
		$("#black").css({"width":dw,"height":dh});
		return false;
	});
	
	$("#bottomArea").append("<div id='lights' />");
	$("div#lights").append("<p class='narrow' />").append("<p class='wide' />")
	
	$("#profile").click(function() {
		$("#profile").fadeOut("fast");
		$("#black").remove();
	});
	$("#profile .close a").click(function() {
		$("#profile").fadeOut("fast");
		$("#black").remove();
	});
	
	function fukidashi(obj,str) {	
		obj.mouseover(function(){
			$("#titlePopup").append("<img src='/themes/penpale2011/images/popup.png' class='fukidashi' />"+str).addClass("open");
		});
		obj.mouseout(function(){
			$("#titlePopup").removeClass("open").text("");
		});
	}
	
	if($("body").attr("id")=="top") {
		$("#photos").after("<p id='penguin' />");
		$("#photos").after("<p id='titlePopup' />");
		var photos = $("#photos .topThumbs");
		var photoNum = photos.length;
		var i=0;
		photos.each(function() {
			i++;
			$(this).delay(500*i).animate({"margin-top":0},300).animate({"margin-top":20},200);
			fukidashi($(this),$(this).find("a").attr("title"));
		});
		$("#eventArea .social a").each(function() {
			fukidashi($(this),$(this).attr("title"));
		});
		$("#mainNavigation a").each(function() {
			fukidashi($(this),$(this).attr("title"));
		});
		setTimeout(faceupPenguin,2000);
	}
		
	var p = 1;
	function faceupPenguin() {
		faceupID = setInterval(faceup, 100);
	}
	function faceup() {
		p++;
		if(p<=4){
			$("#penguin").addClass("f"+p);
		} else {
			clearInterval(faceupID);
			$(document).bind('mousemove',function(e){
				if(e.pageY<300) {
					$("#penguin").removeClass().addClass("f4");
				}
				if(e.pageY>=280&&e.pageY<300) {
					$("#penguin").removeClass().addClass("f3");
				}
				if(e.pageY>=300&&e.pageY<330) {
					$("#penguin").removeClass().addClass("f2");
				}
				if(e.pageY>=330) {
					$("#penguin").removeClass().addClass("f1");
				}
			});
		}
	}
		
		/*var flickr = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=60a6fc7a7a5a4a836675232f83c8f8c8&photoset_id=72157627329253454&format=json&nojsoncallback=1&auth_token=72157627387081528-e4a6d4afc307ebde&api_sig=709568bffc67ee53c09c3887b91a635f";
		var photos = new Array();
		$.getJSON(flickr, function(data) {
			var items = [];
			$.each(data.photoset.photo, function(key, val) {
				items.push(val);
			});
			//http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}.jpg	
			var urls = [];
			var cache = [];
			var ssize = "_t.jpg";
			var zsize = "_z.jpg";
			$("#eventArea").append("<div id=\"thumb\" />");
			for(var i=0;i<items.length;i++) {
				urls.push("http://farm"+items[i].farm+".static.flickr.com/"+items[i].server+"/"+items[i].id+"_"+items[i].secret);
				var cacheImage = new Image();
				cacheImage.src = urls[i]+ssize;
				cache.push($(cacheImage).attr("id","i"+i));
				$("#eventArea #thumb").append(cache[i]);
				console.log($("#thumb").width()+"/"+$("#thumb").height());
				$("img#i"+i).css({"left":Math.random()*($("#thumb").width()-100),"top":Math.random()*($("#thumb").height()-150)+100});
			}
			$("#thumb").fadeIn(2000);
		});*/
	
 /* var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').not(".profile").each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }
	
	function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  }*/
	
});


