///////////////////////////////////////////////////////////////////////////////////////////////////

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function f_size() {
	$("#font_m").bind("click", function(){setActiveStyleSheet('alternate1')});
	$("#font_l").bind("click", function(){setActiveStyleSheet('alternate2')});
}

///////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function () {
	$("#front_menu img").hover(
		function(){
			$(this).fadeTo("fast", 0);
		},
		function(){
			$(this).fadeTo("fast", 1.0);
		}
	);
});

///////////////////////////////////////////////////////////////////////////////////////////////////

function dir() {
	var arrayPath = document.URL.split("/");
	var dir = arrayPath[3];

	var mnimgs_hv = new Array();
	var mnimgs_dwn = new Array();
	var mnimgs = new Array();

	$(".main_navi img").each(function(i){			
		mnimgs_hv[i] = new Image();
		mnimgs_dwn[i] = new Image();
		mnimgs[i] = new Image();

		mnimgs_hv[i].src = "/images/mainNavi_hv_" + i +".gif";
		mnimgs_dwn[i].src = "/images/mainNavi_dwn_" + i +".gif";
		mnimgs[i].src = "/images/mainNavi_" + i +".gif";
		
		function Over(){$(this).attr("src", mnimgs_hv[i].src)};
		function Out(){$(this).attr("src", mnimgs[i].src)};
		function Down(){$(this).attr("src", mnimgs_dwn[i].src )};
		
		if(arrayPath[3] == "guide") {
			if (i != 0) {
				$(this).hover(Over,Out); $(this).mousedown(Down);
			}
		}
		else if (arrayPath[3] == "dep"  || arrayPath[3] ==  "dentistory" ) {
			if (i != 1) {
				$(this).hover(Over,Out); $(this).mousedown(Down);
			}
		}
		else if (arrayPath[3] == "about") {
			if (i != 2) {
				$(this).hover(Over,Out); $(this).mousedown(Down);
			}
		}
		else if (arrayPath[3] == "student") {
			if (i != 3) {
				$(this).hover(Over,Out); $(this).mousedown(Down);
			}
		}
		else if (arrayPath[3] == "inochi_kurashi") {
			if (i != 4) {
				$(this).hover(Over,Out); $(this).mousedown(Down);
			}
		} else {
			$(this).hover(Over,Out); $(this).mousedown(Down);
		}
	})
}


////////////////////////////////////////////////////////////////////

function aBlur() {
	$("a").focus(function(){this.blur();})
}

////////////////////////////////////////////////////////////////////

function sNavi() {
	if (sNavi_ini == "") return false;
	$("#sNavi" + sNavi_ini + " > a").css({"fontWeight":"bold", "backgroundColor":"#e8e3db"});
}

////////////////////////////////////////////////////////////////////

function b_ent_navi() {
	$(".blog_ent_navi a, .st_ent_navi a").attr("class","tip_p");	
}
function tip() {
	$(".tip_p, .header .logo img").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		fade: 250
	});
}

////////////////////////////////////////////////////////////////////

function blog_entry() {
	$(".entry_body a[target='_blank']").addClass("open_win");
	$(".entry_body a[target!='_blank']").addClass("goto");
	$(".entry_body .mt-enclosure-image a").attr("target", "_blank");
	$(".entry_body .mt-enclosure-image a").attr("class","");
	
	$(".entry_body a[onclick]").attr("class","");
	
	$(".entry_body .mt-enclosure-file a").attr({"target":"_blank","class":"open_win"});
	$(".entry_body .mt-enclosure-file a[href$='pdf']").attr({"target":"_blank","class":"pdf"});
}

////////////////////////////////////////////////////////////////////

function modalOpen (dialog) {
	dialog.overlay.fadeIn('normal', function () {
		dialog.container.fadeIn('normal', function(){
			dialog.data.fadeIn('slow');
		});
	});
}
$(document).ready(function () {
	$('#detail_data_btn').click(function (e) {
		e.preventDefault();
		$('#detail_data').modal({onOpen: modalOpen});
		$('#detail_data').css('height',ddheight);
		$('#modalContainer').css('height',ddheight + 40 + "px");
	});
});

////////////////////////////////////////////////////////////////////

$(document).ready(function () {
	$(".index_list a img, .guide_op_list h2 a img, .index_section a img").hover(
		function(){
			$(this).fadeTo("fast", 0.9);
			$(this).parent().parent().css("backgroundColor","#9cb118");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","#ffffff");
		}
	);
});

$(document).ready(function () {
	$(".op_dep_list a img").hover(
		function(){
			$(this).fadeTo("fast", 0.93);
			$(this).parent().css("backgroundColor","#3c9cce");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().css("backgroundColor","#ffffff");
		}
	);
});

$(document).ready(function () {
	$(".navi_fltl a img").hover(
		function(){
			$(this).fadeTo("fast", 0.9);
			$(this).parent().parent().css("backgroundColor","#3c9cce");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","#ffffff");
		}
	);
});

$(document).ready(function () {
	$(".contact_disp a img").hover(
		function(){
			$(this).fadeTo("fast", 0.85);
			$(this).parent().css("backgroundColor","#574637");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().css("backgroundColor","transparent");
		}
	);
});

$(document).ready(function () {
	$(".training_banner a img").hover(
		function(){
			$(this).fadeTo("fast", 0.9);
			$(this).parent().parent().css("backgroundColor","#9cb118");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","transparent");
		}
	);
});

$(document).ready(function () {
	$(".application a img").hover(
		function(){
			$(this).fadeTo("fast", 0.9);
			$(this).parent().parent().css("backgroundColor","#3c9cce");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","transparent");
		}
	);
});

$(document).ready(function () {
	$(".section_navi p a img, .left_banner a img").hover(
		function(){
			$(this).fadeTo("fast", 0.85);
			$(this).parent().parent().css("backgroundColor","#574637");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","transparent");
		}
	);
});

$(document).ready(function () {
	$(".notice a img, .nrs_rec a img").hover(
		function(){
			$(this).fadeTo("fast", 0.85);
			$(this).parent().parent().css("backgroundColor","#dfa0b9");
		},
		function(){
			$(this).fadeTo("fast", 1.0);
			$(this).parent().parent().css("backgroundColor","transparent");
		}
	);
});

////////////////////////////////////////////////////////////////////

function macFireFox() {
	if(navigator.platform.indexOf("Win") != -1) return false;
	if($.browser.mozilla) { 
		$("body").addClass("body_mac_moz");
	}
}

////////////////////////////////////////////////////////////////////

try { 
	document.execCommand('BackgroundImageCache', false, true); 
} catch(e) {} 

////////////////////////////////////////////////////////////////////
/**
 * scrollsmoothly.js
 * Copyright (c) 2008 KAZUMiX
 * http://d.hatena.ne.jp/KAZUMiX/
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

(function(){
    var easing = 0.25;
    var interval = 30;
    var d = document;
    var targetX = 0;
    var targetY = 0;
    var targetHash = '';
    var scrolling = false;
    var splitHref = location.href.split('#');
    var currentHref_WOHash = splitHref[0];
    var incomingHash = splitHref[1];
    var prevX = null;
    var prevY = null;

    addEvent(window, 'load', init);

    function init(){
        setOnClickHandler();
        if(incomingHash){
            if(window.attachEvent && !window.opera){
                setTimeout(function(){scrollTo(0,0);setScroll('#'+incomingHash);},50);
            }else{
                scrollTo(0, 0);
                setScroll('#'+incomingHash);
            }
        }
    }

    function addEvent(eventTarget, eventName, func){
        if(eventTarget.addEventListener){
            eventTarget.addEventListener(eventName, func, false);
        }else if(window.attachEvent){
            eventTarget.attachEvent('on'+eventName, function(){func.apply(eventTarget);});
        }
    }
    
    function setOnClickHandler(){
        var links = d.links;
        for(var i=0; i<links.length; i++){
            var link = links[i];
            var splitLinkHref = link.href.split('#');
            if(currentHref_WOHash == splitLinkHref[0] && d.getElementById(splitLinkHref[1])){
                addEvent(link, 'click', startScroll);
            }
        }
    }

    function startScroll(event){
        if(event){
            event.preventDefault();
        }else if(window.event){ 
            window.event.returnValue = false;
        }
        setScroll(this.hash);
    }

    function setScroll(hash){
        var targetEle = d.getElementById(hash.substr(1));
        if(!targetEle)return;
        var ele = targetEle
        var x = 0;
        var y = 0;
        while(ele){
            x += ele.offsetLeft;
            y += ele.offsetTop;
            ele = ele.offsetParent;
        }
        targetX = x;
        targetY = y;
        targetHash = hash;
        if(!scrolling){
            scrolling = true;
            scroll();
        }
    }

    function scroll(){
        var currentX = d.documentElement.scrollLeft||d.body.scrollLeft;
        var currentY = d.documentElement.scrollTop||d.body.scrollTop;
        var vx = (targetX - currentX) * easing;
        var vy = (targetY - currentY) * easing;
        var nextX = currentX + vx;
        var nextY = currentY + vy;
        if((Math.abs(vx) < 1 && Math.abs(vy) < 1)
           || (prevX === currentX && prevY === currentY)){
            scrollTo(targetX, targetY);
            scrolling = false;
            location.hash = targetHash;
            prevX = prevY = null;
            return;
        }else{
            scrollTo(parseInt(nextX), parseInt(nextY));
            prevX = currentX;
            prevY = currentY;
            var scope = this;
            setTimeout(function(){scroll.apply(scope)},interval);
        }
    }

}());

////////////////////////////////////////////////////////////////////

window.onload = function() {
	f_size();
	dir();
	aBlur();
	sNavi();
	b_ent_navi();
	tip();
	blog_entry();
	macFireFox();
}

////////////////////////////////////////////////////////////////////

var Prart = {};

Prart.util = function(){
	return {
		addEvent: function(elm, type, fn){
			try{
				elm.addEventListener(type, fn, false);
			}catch(e){
				elm.attachEvent('on' + type, function() {
					fn.call(elm, window.event);
				});
			}
		}
	}
}();

Prart.rollOver = function(){
	var over = "_on.", out = "_off.";
	var overImgList = document.images;
	var overIptList = document.getElementsByTagName("input");
	var main = function() {
		if(overImgList.length) {
			for(var i = 0, len = overImgList.length; i < len; i++){
				if(overImgList[i].getAttribute("src") && overImgList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overImgList[i].src.replace(out, over);
					Prart.util.addEvent(overImgList[i], "mouseover", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overImgList[i], "mouseout", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
		if(overIptList.length) {
			for(var i = 0, len = overIptList.length; i < len; i++){
				if(overIptList[i].getAttribute("src") && overIptList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overIptList[i].src.replace(out, over);
					Prart.util.addEvent(overIptList[i], "mouseover", function() {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overIptList[i], "mouseout", function() {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
	}();
}
Prart.util.addEvent(window, "load", Prart.rollOver);

////////////////////////////////////////////////////////////////////

