$(function () {

// loading is done.
$('#site_info').show();
$('#nav').show();
$('#content_main').fadeIn(500);
$('#content_sub').fadeIn(1500);
$('#loading').fadeOut(500); 


//set body_id
var body_id = $("body").attr("id");


// LinkScrubber Plugin
$("a").bind("focus",function(){if(this.blur)this.blur();});

//add class for layout
$("div#content").addClass("clear_children");


var height_nav = $("div#nav").height(); //which is the tallest
var height_main = $("div#content_main").height(); 
var height_sub = $("div#content_sub").height();


if(body_id!=='index' & height_main < 900){$("div#content_main").css("height","900px");}
$("div#content_main").addClass("cc_tallest");




//nav_main
$.fn.nav_main = function(delay) {
	return this.each(function(){
		var tid;
		var tul = $(this).find('ul');
		var tul_fix = $(this).find('ul.fix');
		$(this).hover(function(){
			tid = setTimeout(function(){$(tul).slideDown();} , delay);
		}, function(){
			$(tul).not(tul_fix).slideUp();
			clearTimeout(tid);
		});
	});
}
$('#nav_main li ul').hide(); 
$('#nav_main li.active ul').slideDown().addClass('fix'); 


//add pdf icon
$('ul.download li a[href*=".pdf"]').addClass("pdf").attr("target","_blank"); 


//for index
if(body_id == "index"){

	jQuery.getFeed({
		url: '/js/proxy.php/?url=http://seacology.jp/news/rss',
		success: function(feed){
			var html = '';
			 for(var i = 0; i < feed.items.length && i < 5; i++) {
				var item = feed.items[i];
				/* var date = item.updated.replace(' +0000',''); <span class="date">' + date + '</span> */
				html += '<li>'+ '<a href="'+ item.link+ '">'+ item.title + '</a></li>';
			}
			$('ul#news').append(html);
		}
	});

}else{

	//br delete
	$("#breadcrumb br").css("display","none");

	//add return link
	$('div#content_main').append('<p class="pagetop"><a href="#branding">このページの先頭へ</a></p>');
}

if(body_id == "news"){

	//set active nav_local
	var activeFile = $("#breadcrumb > li > a:last").attr("href");
	var nav_active_local = 'ul#nav_main li ul li a[href='+activeFile+']';
	$(nav_active_local).addClass("active");


	//getFeed
	jQuery.getFeed({
		url: '/js/proxy.php/?url=http://www.seacology.org/news/rss',
		success: function(feed){
			var html = '';
			 for(var i = 0; i < feed.items.length && i < 4; i++) {
				var item = feed.items[i];
				html += '<li>'+ '<a href="'+ item.link+ '">'+ item.title+ '</a></li>';
			}
			$('ul#rss_usa').append(html);
		}
	});

}
if(body_id == "sitemap"){
	$("#sitemap .col li:first-child").addClass("first");
	$("#content_main div.col ul > li:last-child").addClass("last");
}


//add ie6 alert alert
	$("div#branding")
	.append('<p id="alert_ie6">お使いの InternetExplorer のバージョンが古いようです。新しいブラウザを使うと、より快適にご覧いただけます。</p>');


//photo
$("#index p.main_visual > a").attr('rel','prettyPhoto'); 
$("ul#photo > li > a").attr('rel','prettyPhoto[photo]'); 
$("ol#activity > li > a").attr('rel','prettyPhoto[activity]'); 
$("ul#goods_list > li > a").attr('rel','prettyPhoto[goods]'); 
$("a[rel^='prettyPhoto']").prettyPhoto();


//iframe
$("#news iframe").wrap('<div id="iframe">'); 
$("#news #iframe").css('margin-left','40px'); 





//for not IE 6----------------------------------------------------------------------------------------
if ($.browser.msie && $.browser.version < 7) return;

//rich nav_main 
$('#nav_main > li').nav_main(400);

//exLink

	exLink1 = "#admin #content p a,#admin #content ul a,#admin #content table a"; 
	exLink2 = $('a[href*="http"]').not('a[href*="localhost"]').not('a[href*="seacology.jp"]'); 
	exLink3 = $('a[href*="sub/map"],a[href="http://picasaweb.google.co.jp/seacology.jp/"]'); 
	$(exLink1).addClass("exLink");
	$(exLink2).addClass("exLink");
	$(exLink3).addClass("exLink");
	$("a.exLink").attr("target","_blank");


//screenshots
$.fn.screenshots = function(){
		var serverURL = "http://capture.heartrails.com/128x128/shorten?";
		xOffset = 0;
		yOffset = 30;

		return this.each(function(){
		$(this).hover(
		function(e){
			var url = $(this).attr("href");
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? this.t : "";
			$("body").append($('<p>').attr("id","screenshot"));
			$("#screenshot").text(c).prepend($('<img />')
						.attr("src",serverURL+url)
						.attr("alt","site thumbnail")
						.attr("width","128")
						.attr("height","128")
						);
			$("#screenshot")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("slow"); 
			},
		function(){
			this.title = this.t;
			$("#screenshot").remove()
			});

		$(this).mousemove(function(e){
			$("#screenshot")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
			}
		);
	});
}
$(exLink2).screenshots();


//remove alert
	$("p#alert_ie6").remove();


//rich effects
$.fn.fadeFx = function(fadeIn,opacityIn,fadeOut,opacityOut) {
		$(this)
		.find('a')
		.css("position","relative")
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', opacityOut);
			$(this).hover(function () {
				$span.stop().fadeTo(fadeIn, opacityIn);
				}, function () {
				$span.stop().fadeTo(fadeOut, opacityOut);
				});
			$(this).click(function () {$(this).addClass("active")});
	});
}
$('#nav_main li, #content_sub p').fadeFx(500,1,500,0);
$('#nav_main li ul li').fadeFx(0,1,500,0);


//slideScroll
$("p.pagetop a").slideScroll(); 


});

