/*
 * jQuery pack.js for NUPALS Ver.20110713
 *
 * Copyright (C) 2011 HOKUTO Co.,Ltd.
 *
 */
 
var imgAlign;
var photoList = new Array();
var timerID;
var areaLength = 0;
var imageList = new Array();

jQuery(document).ready(function(){
	
	$('#fontSize a').slice(2,3).css('margin-right',3);
	$('#fontSize a').slice(1,2).css('margin-right',3);
	$('#headerNavi ul li').slice(4,5).children('a').css('margin',0);
	
	$('#localNavi ul').each(function(){
	    $(this).children('li:last').css('border-bottom','none');
	})
	
	$(function(){
        $('body').css('font-size',$.cookie('fontsize'));
	    $('#fontSize').attr('class',$.cookie('classSet'));
	    var sizeID = $('#fontSize').attr('class');
	    if(sizeID=='large'){
	       $('#large').addClass("selected");
		   $('#listHanger dl dd').css('margin','-27px 0 0 115px');
		   $('#listHanger dl dt').css('background-position','0 12px');
		   if($.browser.msie && $.browser.version < 8){
		       $('#headerNavi li').slice(3,4).css('float','none');
		       $('#headerNavi li:last').css('float','none'); 
	       }
		   if($.os.name=='mac'){
			   $('#listHanger dl dd').css('margin','-27px 0 0 135px');
		   }
	    }
	    if(sizeID=='medium'||sizeID==''){
	       $('#medium').addClass("selected");
		   $('#listHanger dl dd').css('margin','-22px 0 0 107px;');
		   $('#listHanger dl dt').css('background-position','0 10px');
		   if($.browser.msie && $.browser.version < 8){
		       $('#headerNavi li').css('float','left');
	       }
		   if($.os.name=='mac'){
			   $('#listHanger dl dd').css('margin','-22px 0 0 117px;');
		   }
	    }
	    if(sizeID=='small'){
	       $('#small').addClass("selected");
		   $('#listHanger dl dd').css('margin','-18px 0 0 80px;');
		   $('#listHanger dl dt').css('background-position','0 9px');
		   if($.browser.msie && $.browser.version < 8){
		       $('#headerNavi li').css('float','left');
	       }
		   if($.os.name=='mac'){
			   $('#listHanger dl dd').css('margin','-18px 0 0 80px;');
		   }
	    }
    });
	
	$('.subCate:first').css('border-top','none');
	$('.subCate').prev().css('border-bottom','none');
	
	var now = location.href.split('/');
	var cate = now.slice(now.length-2,now.length-1);
	var cate02 = now.slice(now.length-3,now.length-2);
	var endDir = now.slice(now.length-1);
	var url01 = cate.toString();
	var url02 = endDir.toString();
	var url03 = cate02.toString();
	
	if(location.pathname != "/") {
	    $('#globalNavi li a[href$="'+url01+'/index.html"]').css({backgroundPosition: '0 -40px'});
		$('#globalNavi li a[href$="'+url03+'/index.html"]').css({backgroundPosition: '0 -40px'});
		if(url02=='index.html'){
		}else{
			$('#localNavi h4 a[href$="'+url02+'"]').css("background-color","#DEE1E4");
			$('#localNavi ul li a[href$="'+url02+'"]').css("background-color","#DEE1E4");
			$('#localNavi ul li a[href$="'+url02+'"]').parents('ul').attr('id','sub');
		}
		$('#sub').prev().children('a').css("background-color","#DEE1E4");
	}
	
	$('#localNavi h4').each(function(){
        var leng = $(this).next().length;
		if(leng==1){
		    var targetURL = $(this).children('a').attr('href').split('/');
	        var url = targetURL.slice(targetURL.length-1).toString();
	        var check = $(this).next().get(0).tagName;
	        if(url==url02){
		        if(check=='UL'){
			        $(this).next('ul').css('display','block');
			        $(this).children('a').addClass('selected');
		        }
	        }
		}
	});
	
	$('#localNavi ul li').each(function(){
	   var targetURL = $(this).children('a').attr('href').split('/');
	   var url = targetURL.slice(targetURL.length-1).toString();
	   if(url==url02){
		   $(this).parents('ul').css('display','block');
		   $(this).parents('ul').prev('h4').children('a').addClass('selected');
	   }
	});
	
	$('#localNavi h4').each(function(){
		var leng = $(this).next().length;
		if(leng==1){
			var a = $(this).next().get(0).tagName;
			if(a=='UL'){
		        $(this).click(function(){
			        if($(this).children('a').hasClass('selected')){
		                $(this).children('a').removeClass('selected');
		            }else{
		                $(this).children('a').addClass('selected');
		            }
			        $(this).next('ul').slideToggle('fast');
				    return false;
			    });
		    }
		}
	});
									
    $('#fontSize a').click(function(){
	    var ID = $(this).attr('id');
	    $('#fontSize a').removeClass("selected");
        $(this).addClass("selected");
	    if(ID=='large'){
	       fontSize('130%');
	       classSet(ID);
	    }
	    if(ID=='medium'){
	       fontSize('100%');
	       classSet(ID);
	    }
	    if(ID=='small'){
	       fontSize('75%');
	       classSet(ID);
	    }
		if(url01 == 'nupals'){
			visualPos();
		}
		window.location.reload();
	});

    function fontSize(size){
        $('body').css('font-size',size);
        $.cookie('fontsize',size,{expires:30,path:'/'});
    }

    function classSet(cla){
        $('#fontSize').attr('class',cla);
        $.cookie('classSet',cla,{expires:30,path:'/'});
    }
	
	function visualPos(){
		var photoPos = $('#photoHanger').position();
		$('#photoHanger ul li').css('top',photoPos.top);
		$('#thumbNails').css('top',photoPos.top+165);
	}
	
	/*$('#rightColumn p,#rightColumn div,#rightColumn li').each(function(){
		var check=0;
		$(this).children('img').each(function(){
			var imgAlign = $(this).attr('align');
			if(imgAlign=='left'){
				check++;
		    }
			if(imgAlign=='right'){
				check++;
		    }
			if(check==1){
				$(this).parent().append('<br clear="all" />');
			}
		});
	});*/
	$('#rightColumn p').each(function(){
		$(this).append('<br clear="all" />');
	});
	
	$('.pt').click(function(){
	    window.scrollTo(0,0);
        return false;
    });
	
	/*lightBox*/
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	var pageh = document.body.clientHeight;
	
	var scH = $('html').scrollTop();
	
	$(window).scroll(function () {
	   ipPos = $(this).scrollTop();
    });
	
	$('a.lightBox').each(function(){
		var preLoader = new Image();
		var imageurl =  $(this).attr('href');
		preLoader.src = imageurl;
		photoList.push(preLoader);
	});
	
	photoCheck();
	
	$('.lightBox').click(function(){
       $('body').append("<div id='overLayer'></div>");
	   $('body').append("<div id='lightBoxHnager'><div id='closeBtn'><img src='http://www.nupals.ac.jp/img/close.gif' /></div><div id='lbphoto'></div><div id='photoDescription'></div></div>");
	   var url =  $(this).attr('href');
	   /*var txt =  $(this).attr('name');
	   var Link =  $(this).attr('rel');*/
	   var Title =  $(this).attr('title');
	   $('#lbphoto').append('<img src="'+url+'" />');
	   /*$('#photoDescription').html('<a href="'+Link+'">'+txt+'</a>');*/
	   $('#photoDescription').html(Title);
	   if($.browser.msie && $.browser.version < 7){
	      $('html').css('overflow','hidden');
	   }
	   $('#overLayer').css({'width' : w, 'height' : pageh+100});
	   $('#overLayer').animate({"opacity": "0.85"},400,showImage);
	   return false;
    });
	
	function showImage(){
	  var imageW = $('#lbphoto').children('img').attr('width');
	  var imageH = $('#lbphoto').children('img').attr('height');
	  var newW = (w-imageW)/2;
	  var newH = (h-imageH)/2;
	  if($.browser.msie && $.browser.version < 7){
	      newH=ipPos+80;
	  }
	  if($.os.name == 'iphone') {
		  newH=ipPos+80;
	  } else if($.os.name == 'ipod') {
		  newH=ipPos+80;
	  } else if($.os.name == 'ipad') {
		  newH=ipPos+80;
	  }
	  $('#lightBoxHnager').css('left',newW);
	  $('#lightBoxHnager').css('top',newH-40);
	  showPhoto();
	}
	
	function layerRemove(){
	   $('#overLayer,#closeBtn img').click(function(){ 
	        $(this).animate({"opacity": "0"},300,Removes);
			$('#lightBoxHnager').animate({"opacity": "0"},300);
			if($.browser.msie && $.browser.version < 7){
	           $('html').css('overflow','auto');
	        }
	   });
	}
	
	function Removes(){
	   $('#overLayer').remove();
	   $('#lightBoxHnager').remove();
	}
	
	$(window).bind('resize', function(){
	  de = document.documentElement;
	  w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	  pageh = document.body.clientHeight;
	  $('#overLayer').css({'width' : w, 'height' : pageh});
	  showImage();
    });
	
	function showPhoto(){
	   $('#lightBoxHnager').animate({"opacity": "1"},300,layerRemove);
	}
	
	/*Pop up*/
	areaLength = $('area').length;
	var imgNum = 0;
	for(var a=1; a<areaLength; a++){
		if(a<10){
			imgNum = '0'+a;
		}else{
			imgNum = a;
		}
		var preLoader = new Image();
		var imageurl =  'http://www.nupals.ac.jp/about/img/cmp0'+imgNum+'s-l.jpg';
		preLoader.src = imageurl;
		imageList.push(preLoader);
	}
	checkFunc();
	$('.popup').each(function(){
        $(this).mouseenter(function(e){
			var x = getMousePosition(e).x;
            var y = getMousePosition(e).y;
		    var url = $(this).attr('href');
			var title = $(this).attr('alt');
			var disp = $(this).attr('name');
		    $('body').append('<div id="imageContainer"><img src="'+url+'" /><br /><div id="titleHanger">'+title+'</div><div id="dsipHanger">'+disp+'</div></div>');
			var newPos = $('#imageContainer').innerHeight();
			$('#imageContainer').css({'top' : y-newPos, 'left' : x-387});
		    $('#imageContainer').css('display','block');	
			$(this).mousemove(function(e){
				x = getMousePosition(e).x;
				y = getMousePosition(e).y;
				$('#imageContainer').css({'top' : y-newPos, 'left' : x-387});
			});
			$(this).click(function(){
			    return false;
			});	
        });
		$(this).mouseleave(function(){
	        $('#imageContainer').css('display','none');
			Reset();
	    });
	});
	
	$(document).bind("contextmenu",function(e){
		return false;
	});
	
});

//printer friendly
function replaceCSS(id,url){
   if(!document.getElementById) return false;
   var element = document.getElementById(id);
   if(!element || !element.cloneNode) return false;
   var new_node = element.cloneNode(true);
   new_node.href = url;
   element.parentNode.replaceChild(new_node,element);
   return true;
}

function changeCSS(){
   setTimeout("replaceCSS('forPrint','http://www.nupals.ac.jp/css/print_def.css')",5000);
}

function photoCheck(){   
    var count = 0;
    for(var i = 0; i < photoList.length; i++){
      if(photoList[i].complete) count++;
    }
	timerID = setTimeout("photoCheck()", 50);
	if(count == imageList.length){
	  clearTimeout(timerID);
    }	
}

function checkFunc(){
	var count = 0;
    for(var a = 0; a < imageList.length; a++){
      if(imageList[a].complete) count++;
    }
	checkID = setTimeout("checkFunc()", 50);
	if(count == imageList.length){
	  clearTimeout(checkID);
    }
}

function getMousePosition(e){
    var obj = new Object(); 
    if(e) {
        obj.x = e.pageX;
        obj.y = e.pageY;
    }else{
        obj.x = event.x + document.body.scrollLeft;
        obj.y = event.y + document.body.scrollTop;
    }
	return obj;
}

function Reset(){
    $('#imageContainer').remove();
}
