/* index.js
   Copyright(C)2011 hpservice.jp
   URL: http://hpservice.jp/
   E-Mail: info@hpservice.jp
 */
$(function(){
	
	// navigation
	$("#nav a").each( function() {
		var url = document.URL.split("#");
		if ( this == url[0] || this + "index.html" == url[0] || this == url[0] + "index.html") {
			//$(this).addClass('stay');	// global
			$(this).addClass( $(this).attr('class') + 'stay'); // for IE6
		}
	});

	// _blank and main
	$('a[rel="_blank"]').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
	$('a[rel="_self"]').click(function(){
		window.open(this.href, '_self');
		return false;
	});

	// navigation ifreame
	$("#nav a").click(function(){
		var $url = $(this).attr('href');
		if ($url == '#') return false;
		if ($(this).attr('rel')=='_blank') return;
		$("#main").attr('src', $url);
		setHeightMain();
		return false;
	});

  // Effect Images

	$("#sideMenu li a").fadeTo(0, 1.0, 0 );
	$("#sideMenu li a").hover(function(){$(this).fadeTo(0,0.7);}, function(){$(this).fadeTo(0,1.0);});

	// Preload Images

	$.preloadImages = function(){
		for(var i=0; i<arguments.length; i++){
			jQuery("<img>").attr("src", arguments[i]);
		}
	}

//	$.preloadImages("images/header_top.png", "images/nav.png", "images/navi_bg.png", "images/article.jpg");

	// Smooth Scroll
 $('a[href*=#]').click(function() {
  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
    var targetOffset = $target.offset().top;
    $('html,body').animate({scrollTop: targetOffset}, 300);
    return false;
   }
  }
 });

	// floating div
if ( $('#cargoBox').size() > 0 ) {
	var name = "#cargoBox";
	var menuYloc = null;

	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	$(window).scroll(function () { 
		offset = menuYloc+$(document).scrollTop()+"px";
		$(name).animate({top:offset},{
			duration: 0,
			queue: false
		});
	});
}
	// Change the color of alternating
/*
	$(".alternatingTable").each(function(){
		$(this).find("tr:odd").addClass("odd");
	});
	$(".alternatingUl").each(function(){
		$(this).find("li:odd").addClass("odd");
	});
	$(".alternatingDl").each(function(){
		$(this).find("dt:odd").addClass("odd");
		$(this).find("dd:odd").addClass("odd");
	});
*/

	// aside
	function setBackground() {
		var $last = $(".acc > li:last > a");
		if($last.hasClass("close"))
			$last.css("background-position", "left bottom");
		else
			$last.css("background-position", "left -30px");
	}

	$(".acc").each(function(){
		$("li > ul"            , this).wrap("<div></div>");
		$("li > div:not(:last)", this).append("<div class='notlast'>&nbsp;</div>");
		$("li > div:last"      , this).append("<div class='last'>&nbsp;</div>");

		$("li > a", this).each(function(index){
			var $this = $(this);

			if(index > 0) 
				$this.addClass("close").next().hide();
			else
				$this.css("background-position", "left top");

			setBackground();

			var prms = {height:"toggle", opacity:"toggle"};
			$this.click(function(){
				$(this).toggleClass("close").next().animate(prms, {duration:"fast"})
					.parent().siblings().children("div:visible").animate(prms, {duration:"fast"}).prev().addClass("close");

				// add class
				$("#sideMenu a").removeClass('stay');
				$this.addClass( $this.attr('class') + ' stay');

				// Setting iframe.
				var url = $(this).attr('href');
				if (url == '#' ) return false;
				$("#main").attr('src', url);
				
				// history 
//				window.location.replace( '?t='+url );

				// Height of iframe
//				setHeightMain();

				return false;
			});
		});
	});
	
	//
	// Others
	//

	//	setHeightMain();
		setHeightMain();
	// footer
	$(".lastModified span").text(document.lastModified);

	// Feed
	if ($('#blog').size()>0){
		$.getFeed({
			url: '_top/js/blog.php',
			success: function(feed){
				var html='';
				for(var i=0; i<feed.items.length && i<5; i++){
					var item = feed.items[i];
					html += '<dt>' + dateChanger(item.dcdate) + '<\/dt><dd><a href="' + item.link + '">' + item.title + '...<\/a><\/dd>';
				}
				$('#blog').append(html);
				$('#blog dt,#blog dd').wrapAll('<dl><\/dl>');
			}
		});
	
		var dateChanger = function(str){
			var mydate = parseISO8601(str);
			yy = mydate.getFullYear();
			mm = mydate.getMonth() + 1;
			dd = mydate.getDate();
			if (mm < 10) { mm = "0" + mm; }
			if (dd < 10) { dd = "0" + dd; }
			datestr = yy + "/" + mm + "/" + dd + " ";
			return datestr;
		};
	}

});

//@param timestamp An ISO-8601 timestamp in the form YYYY-MM-DDTHH:MM:SS賊HH:MM
//Note: Some other valid ISO-8601 timestamps are not accepted by this function
function parseISO8601(timestamp)
{
	var regex = new RegExp("^([\\d]{4})-([\\d]{2})-([\\d]{2})T([\\d]{2}):([\\d]{2}):([\\d]{2})([\\+\\-])([\\d]{2}):([\\d]{2})$");
	var matches = regex.exec(timestamp);
	if(matches != null)
	{
		var offset = parseInt(matches[8], 10) * 60 + parseInt(matches[9], 10);
		if(matches[7] == "-") offset = -offset;

		return new Date(
			Date.UTC(
				parseInt(matches[1], 10),
				parseInt(matches[2], 10)-1,
				parseInt(matches[3], 10),
				parseInt(matches[4], 10),
				parseInt(matches[5], 10),
				parseInt(matches[6], 10)
			) - offset*60*1000
		);
	}
	return null;
}

function setHeightMain()
{
	$('#main').load(function(){
		if(jQuery.browser.msie ){
			try {
				$(this).height( this.contentWindow.document.body.scrollHeight + 60 );
			}catch(e){
				if(e.number != -2146827864) throw e;
			}
		} else {
			$(this).height( this.contentWindow.document.documentElement.offsetHeight );
		}
	});
	$('#main').triggerHandler('load');
}



