/*
common.js
REQUIRES: prototype.js, scriptaculous.js
Description: 企業サイト構築用簡易CMSパッケージ付属スクリプト
Since: 2009-02-23
Author: Takuro Hishikawa
Updated: 2009-06-17
*/


// *************************************************
// ギャラリー
// *************************************************

jQuery.noConflict();

jQuery(document).ready(function() {
	jQuery("a.fancybox").fancybox();
	jQuery("a.openwindow").fancybox({
		'hideOnContentClick': false
	});
});



// *************************************************
// ページの読み込み時に起動
// *************************************************

Event.observe( window, 'load', function(){
// 	setRollOver('roll');
//	showGoogleMap('gmap',34.694044,135.224168,13,0);
	setFontSize('font_size_selector');
});



// *************************************************
// フォントサイズ切り替えボタン
// *************************************************

var cookiemanager = new CookieManager();

var ChangeFontSize = Class.create();
ChangeFontSize.prototype = {
	initialize: function(){
		this.btn_m = $('font_m');
		this.btn_l = $('font_l');
	},
	change: function(size){
		var body = document.body;
		if(size == 'm'){
			Element.removeClassName(body,'font_size_l');
			Element.addClassName(body,'font_size_m');
			cookiemanager.setCookie('fontSize','m');
		}else if(size == 'l'){
			Element.removeClassName(body,'font_size_m');
			Element.addClassName(body,'font_size_l');
			cookiemanager.setCookie('fontSize','l');
		}
	}
}

var fontsize = new ChangeFontSize();

function setFontSize(targetId){
	var target = targetId;
	var fontSize = cookiemanager.getCookie('fontSize');
	if(fontSize == 'm'){
		fontsize.change('m');
	}else if(fontSize == 'l'){
		fontsize.change('l');
	}else{
		cookiemanager.setCookie('fontSize','m');
		fontsize.change('m');
	}
	
	var btnhtml = document.createElement('div');
	btnhtml.innerHTML = '<ul><li id="font_l"><a href="javascript:fontsize.change(\'l\');">大きく</a></li><li id="font_m"><a href="javascript:fontsize.change(\'m\');">標準</a></li></ul>';
	$(target).appendChild(btnhtml);
}




// *************************************************
// 汎用関数
// *************************************************


// eventSetter
var isIE = isIE = (document.documentElement.getAttribute("style") == document.documentElement.style);

function eventSetter(obj,eventType,func){
	if(isIE) {
		obj.setAttribute(eventType,new Function(func));
	} else {
		obj.setAttribute(eventType,func);
	}
}


// open new window
function newWindow(uri,width,height){
	var myWindow = window.open(uri, 'newWindow', 'resizable=yes,scrollbars=yes,status=0,width='+width+',height='+height);
	if (myWindow.focus!=null) {
		myWindow.focus();
	}
}


// print window
function printWindow(){
	if(document.getElementById || document.layers){
		window.print();
	}
}


// Ajax recover UTF-8 (for Safari)
function recover_utf8(text){
	if(navigator.appVersion.indexOf('KHTML') > -1){
		var esc = escape(text);
		if(esc.indexOf('%u') < 0 && esc.indexOf('%') > -1){
			text = decodeURIComponent(esc);
		}
	}
	return text;
}


// input clear
function inputClear(id,txt){
	if ($(id).value == txt) $(id).value = '';
}

// scroll to top
function scrollToTop() {
	new Effect.ScrollTo("wrapper",{duration: 0.5});
}



// *************************************************
// 画像ロールオーバー
// *************************************************

var ImgRollOver = Class.create();
ImgRollOver.prototype = {
	initialize: function(img,type){
		this.img = img;
		this.outImgSrc = img.src;
		var imgSrcArray = img.src.split('.');
		var fileType = imgSrcArray[imgSrcArray.length - 1];
		this.overImgSrc = img.src.split('.' + fileType)[0] + '_o.' + fileType;
		if(type == "img"){
			Event.observe(
				img.parentNode,
				'mouseover',
				this.mouseOver.bindAsEventListener(this)
			);
			Event.observe(
				img.parentNode,
				'mouseout',
				this.mouseOut.bindAsEventListener(this)
			);
		}else if(type == "input"){
			Event.observe(
				img,
				'mouseover',
				this.mouseOver.bindAsEventListener(this)
			);
			Event.observe(
				img,
				'mouseout',
				this.mouseOut.bindAsEventListener(this)
			);
		}
		var preImage = new Image();
		preImage.src = this.overImgSrc;
	},
	mouseOver: function(){
		this.img.src = this.overImgSrc;
	},
	mouseOut: function(){
		this.img.src = this.outImgSrc;
	}
}

function setRollOver(className){
	var imgs = document.getElementsByTagName('img');
	var inputs = document.getElementsByTagName('input');
	var preImages = new Array();
	var preInputs = new Array();
	if(imgs){
		for(var i = 0,num = imgs.length;i < num;i++){
			img = imgs[i];
			if(Element.hasClassName(img, className)){
				new ImgRollOver(img,"img");
			}
		}
	}
	if(inputs){
		for(var i = 0,num = inputs.length;i < num;i++){
			input = inputs[i];
			if(input.type == 'image' && Element.hasClassName(input, className)){
				new ImgRollOver(input,"input");
			}
		}
	}
}


// *************************************************
// プルダウンメニュー
// *************************************************

var PullDownMenu = Class.create();
PullDownMenu.prototype = {
	initialize: function(menu,subnavi){
		this.menu = menu;
		this.img = this.menu.firstChild.firstChild;
		this.outImgSrc = this.img.src;
		this.subNavi = this.menu.getElementsByClassName(subnavi)[0];
		var imgSrcArray = this.img.src.split('.');
		var fileType = imgSrcArray[imgSrcArray.length - 1];
		this.overImgSrc = this.img.src.split('.' + fileType)[0] + '_o.' + fileType;
		Event.observe(
			this.menu,
			'mouseover',
			this.mouseOver.bindAsEventListener(this)
		);
		Event.observe(
			this.menu,
			'mouseout',
			this.mouseOut.bindAsEventListener(this)
		);
		var preImage = new Image();
		preImage.src = this.overImgSrc;
	},
	mouseOver: function(){
		this.img.src = this.overImgSrc;
		this.subNavi.style.display = 'block';
	},
	mouseOut: function(){
		this.img.src = this.outImgSrc;
		this.subNavi.style.display = 'none';
	}
}

function setPullDownMenu(wrapperID,navi,subnavi){
	if($(wrapperID)){
		var gnavi = $(wrapperID);
		var menus = gnavi.getElementsByClassName(navi);
		for(var i = 0,num = menus.length;i < num;i++){
			menu = menus[i];
			new PullDownMenu(menu,subnavi);
		}
	}
}



// *************************************************
// GoogleMap表示クラス
// *************************************************

var GoogleMapObj = Class.create();
GoogleMapObj.prototype = {
	initialize: function(id,lat,lng,zoom,control){
		if(GBrowserIsCompatible()){
			//コンテナの取得
			this.container = $(id);
		
			//地図の作成
			this.map = new GMap2(this.container);
		
			//地図の表示
			this.point = new GLatLng(lat,lng);
			this.zoomlevel = zoom;
			this.resetCenter();
			
			//マーカーの表示
			this.addMarker(lat,lng);
			
			//コントローラが必要であればつける
			if(control){
				this.setController();
			}
			Event.observe(window, 'unload', GUnload);
		}
	},
	setController: function(){
			//マップコントローラを付ける
			this.map.addControl(new GLargeMapControl());
		
			//マップタイプコントローラを付ける
			this.map.addControl(new GMapTypeControl());
		
			//オーバービューマップコントローラを付ける
			this.map.addControl(new GOverviewMapControl());
	},
	resetCenter: function(){
		this.map.setCenter(this.point, this.zoomlevel);
	},
	changeCenter: function(lat,lng,zoom){
		this.map.setCenter(new GLatLng(lat,lng), zoom);
	},
	addMarker: function(lat,lng){
		var cr_lat = lat;
		var cr_lng = lng;
		var mpoint = new GLatLng(cr_lat,cr_lng);
		
		//マーカーを作成
		var marker = new GMarker(mpoint,this.icon);
		
		//マーカーの追加
		this.map.addOverlay(marker);
	}
}

function showGoogleMap(id,lat,lng,zoom,control){
	if($(id)){
		new GoogleMapObj(id,lat,lng,zoom,control);
	}
}



// ペラペラ

function FullOpen(strURL) {
var options;
 
	if (navigator.appName.charAt(0)=='M')
		options = "fullscreen=1,scrollbars=0";
	else if (navigator.appName.charAt(0)=='N')
		options = "scrollbars=0,left=0,top=0"
				+ ",width=" + screen.width
                + ",height=" + screen.height;
 
	window.open(strURL, "", options);
}

