/*creat random number and add random class to all titleMenu*/
$(function() {
  $("a.titleMenu").hide();
  aNum = $("a.titleMenu").length + 2;
/*"+3" means there are at least 2 other links before titleMenu. I'm trying to clear this problem around selector.*/
  for (i = 1; i <= aNum; i++) {
    var myRnd = Math.floor( Math.random() * 8 ) + 1;
    myClass = "text_" + myRnd;
    $('a.titleMenu:nth-child('+i+')').addClass("buttonText").addClass(myClass).hide("fast", function(){
      $('a.titleMenu:nth-child('+i+')').show("slow")}
    );
  }
  //$(function(){ //animate text link in random. Don't move this part to use var myRnd.
    //setInterval(function(){
      //$(".text_"+myRnd).animate({"lineHeight": "+=1em"}, 1000);
      //$(".text_"+myRnd).animate({"width": "+=40px"}, 800);
      //$(".text_"+myRnd).animate({"lineHeight": "-=1em"}, 1000);
      //$(".text_"+myRnd).animate({"width": "-=10px"}, 300);
      //},5000);
  //}); 
});




/*change background image.*/
function changeBg(){
	
	var pageBg = document.getElementsByTagName("body");
	var currenturl = window.location.href;

		  if(currenturl=="http://polytechnics.jp/"){
			var parentObject = document.getElementsByTagName("body")[0];
			var ele = document.createElement("img");
			ele.setAttribute("src", "http://polytechnics.jp/blog/wp-content/themes/titletile/img/background.jpg");
			ele.setAttribute("alt", "背景画像");
			ele.setAttribute("class", "bg");
			parentObject.insertBefore(ele,parentObject.firstChild);
		  }

	      else if(currenturl.match("http://polytechnics.jp/category/blog")){pageBg[0].setAttribute("class", "bg_blog");}
          else if(currenturl.match("http://polytechnics.jp/category/tweet")){pageBg[0].setAttribute("class", "bg_tweet");}
          else if(currenturl.match("http://polytechnics.jp/category/graphic")){pageBg[0].setAttribute("class", "bg_graphic");}
          else if(currenturl.match("http://polytechnics.jp/category/videoclip")){pageBg[0].setAttribute("class", "bg_movie");}
          else if(currenturl.match("http://polytechnics.jp/category/web")){pageBg[0].setAttribute("class", "bg_web");}
          else if(currenturl.match("http://polytechnics.jp/category/ask")){pageBg[0].setAttribute("class", "bg_ask");}
          else {pageBg.className = "";}
}

if(window.addEventListener) {
	window.addEventListener("load", changeBg, false);
}
else if(window.attachEvent) {
	window.attachEvent("onload", changeBg);
}



/*highlight current page using menu css.*/
function highlightpage(){
	var nav = document.getElementById("global_navigation1");
	var links = nav.getElementsByTagName("a");
	var currenturl = window.location.href;

	if (currenturl == "http:\/\/polytechnics\.jp\/") {
		links[0].className = "here";	//home
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/blog/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/blog/)) {
		links[1].className = "here";	//blog
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/tweet/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/tweet/)) {
		links[2].className = "here";	//tweet
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/graphic/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/graphic/)) {
		links[3].className = "here";	//graphic
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/videoclip/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/videoclip/)) {
		links[4].className = "here";	//movie
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/web/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/web/)) {
		links[5].className = "here";	//web
	} else if (currenturl.match(/^http:\/\/polytechnics\.jp\/ask/)||currenturl.match(/^http:\/\/polytechnics\.jp\/category\/ask/)) {
		links[6].className = "here";	//ask
	}
}

if(window.addEventListener) {
	window.addEventListener("load", highlightpage, false);
}
else if(window.attachEvent) {
	window.attachEvent("onload", highlightpage);
}

