/*
$Rev: 16 $
*/
(function($){

/* -------------------- V.S. Bug in Firefox with SWFAddress -------------------- */

(function(){
	var initialValue = SWFAddress.getValue();
	var hash = (location.hash || '').replace(/^#/, '');
	if (!hash) {
		location.replace('#/');
	}
	if (initialValue.indexOf('/') != 0 && initialValue.indexOf('#') != 0) {
		location.replace('#/' + initialValue);
	}
})();


/* -------------------- CWAVE.ContentController -------------------- */
/**
*
*/
CWAVE.ContentController = function () {
	//console.log('ready');
	this.contents = null;
	this.contentIds = [];
	this.isBusy = false;
	this.scrollSubNavTimer = null;
	this.init();
};

/**
 * 
 */
CWAVE.ContentController.prototype.init = function () {
	this.contents = new CWAVE.Contents();
	this.currentPage = null;
	this.currentPage_tmp = null;
	
	this.navigation = new CWAVE.ContentNavigation($('#global-navigation'));

	$('#articles .article-body').css('min-height','0');

	this.scroller = new CWAVE.Scroller($("html"));
	this.scroller.addCallback("onComplete", this.scrollCompleteHandler, this);
	this.scroller.addCallback("onAbort", this.scrollAbortHandler, this);

	this.adjustArticleHeight();

	this.subNavController = new CWAVE.SubNavigationController();
	this.subNavController.addCallback('onLoad', this.setFirstPosition, this);

	this.baloon = new CWAVE.BaloonMarker();
	this.baloon.appendTo($('#articles'));
	
	this.contact_form = new CWAVE.ContactForm($('#contact-form-wrappper'));
	this.contact_form.addCallback('loadFormComplete',this.adjustArticleHeight,this);

	var self =this;
	$('#header .title a, #articles a.to-content').click(function(evt){
		evt.preventDefault();
		evt.stopPropagation();
		self.changeContentById($(this).attr('href').replace(/^#/,""));
	});
	
	this.slide_contents = [];
	$('#about-us .slide-content,#services .slide-content,#privacy-policy .slide-content').each(function(){
		var slide_content = new CWAVE.SlideContent($(this));
		slide_content.addCallback('onChange',function(){
			console.log(arguments[0]);
			var page = this.contents.getPageById(arguments[0]);
			this.setTitle(page.getTitle());
			this.changePath(page.getPath());
			this.currentPage = page;
		},self);
		self.slide_contents.push(slide_content);
	});
};

/**
 * 
 */
CWAVE.ContentController.prototype.setFirstPosition = function (){
	var page = this.contents.getPageByPath(SWFAddress.getValue());

	this.changeContentById(page.getId());
};

/**
 * 
 * @param contentId
 */
CWAVE.ContentController.prototype.changeContentById = function ( id ) {
	var page = this.contents.getPageById(id);
	this.currentPage_tmp = page;

	var parentId = page.getParentId();

	var scrollId = (parentId == '' || parentId == 'root') ? page.getId() : parentId;
	if ( scrollId == 'root' ) scrollId = 'home';

	var d = (this.currentPage)? this.currentPage.posid : 0;
	var adjust = -125;
	if ( $.browser.msie && $.browser.version < 7 ) {
		adjust = -45;
	}
	this.isBusy = true;

	this.scroller.scrollToNode($("#"+scrollId), 800 * Math.max(1, Math.abs(page.posid-d)), adjust);


	if ( parentId != 'root' ) {
		//tmp;
		this.scroller.addCallback('onComplete', function(){
			//console.log(scrollId);
			$("#"+this.currentPage.getParentId()).find('.nav a[href=#'+ this.currentPage.getId() +']').click();
		},this,'disposable');
	}

	this.navigation.setStay(scrollId);
};

/**
 * 
 */
CWAVE.ContentController.prototype.scrollCompleteHandler = function () {
	//console.log('scrollCompleteHandler')
	this.isBusy = false;
	this.currentPage = this.currentPage_tmp;
	this.currentPage_tmp = null;
	this.setTitle(this.currentPage.getTitle());
	this.changePath(this.currentPage.getPath());

	var parentId = this.currentPage.getParentId();
	var scrollId = (parentId == '' || parentId == 'root') ? this.currentPage.getId() : parentId;
	this.subNavController.changeId(scrollId);
	//console.log('setPos scrollCompleteHandler : ' +this.currentPage.getId());
	this.baloon.setPos(scrollId);
};

/**
 * 
 */
CWAVE.ContentController.prototype.scrollAbortHandler = function () {
	//console.log('scrollAbortHandler')
	this.isBusy = false;
	this.changeContentByPosition();
};

CWAVE.ContentController.prototype.scrollHandler = function (evt) {
	this.changeContentByPosition();
};

/**
 * 
 * @param evt
 */
CWAVE.ContentController.prototype.changeContentByPosition = function () {
	if ( this.isBusy ) return;
	//console.log('changeContentByPosition',this.isBusy);

	var geom = CWAVE.GetGeometry();
	var pageMiddle = geom.scrollY + geom.windowH / 2;
	var page = null;

	jQuery.each(this.contents.getPages(), function(i, obj){
		if ( i == '/' || i.match(/about-us-|privacy-policy-/) ) return;
		var node       = $('#'+obj.getId());

		var nodeTop    = node.offset().top;
		var nodeBottom = nodeTop + node.height();
		if (obj.getId() == "home") {
			if (pageMiddle < nodeBottom) {
				page = obj;
				return false;
			}
			else {
				return;
			}
		}
		else if (obj.getId() == "about-us") {
			if (nodeTop <= pageMiddle && pageMiddle < nodeBottom) {
				page = obj;
				return false;
			}
			else {
				return;
			}
		}
		else {
			if (nodeTop <= pageMiddle && pageMiddle < nodeBottom) {
				page = obj;
				return false;
			}
			else {
				return;
			}
		}
	});

	if ( page && this.currentPage != page ) {
		var parentId = page.getParentId();
		var setId = (parentId == '' || parentId == 'root') ? page.getId() : parentId;

		this.navigation.setStay(setId);
		this.setTitle(page.getTitle());
		this.currentPage = page;

		this.baloon.setPos(setId);
		this.subNavController.changeId(setId);
	}
};

/**
 * 
 * @param arg
 */
CWAVE.ContentController.prototype.setTitle = function(arg) {
	var title = arg;
	if (title != SWFAddress.getTitle()) {
		SWFAddress.setTitle(title);
	}
};

/**
 * 
 * @param href
 */
CWAVE.ContentController.prototype.changePath = function(path) {
	if (path != SWFAddress.getValue()) {
		SWFAddress.setValue(path);
	}
};

CWAVE.ContentController.prototype.adjustArticleHeight = function () {
	var $articles = $('#articles .article');
	
	var geom = CWAVE.GetGeometry();
	var baseH = geom.windowH - 80;
	if ( $.browser.msie && $.browser.version < 7 ) {
		baseH = geom.windowH;
	}
	
	$articles.each(function(){
		var body = $(this).find('.article-body');
		body.css('height','auto');
		var h = baseH;
		if ($(this).attr('id') == 'home' ) {
			h = h + 45;
		}
		else if ($(this).attr('id') == 'privacy-policy' ) {
			h = h - 40 - $('#footer').outerHeight();
		}
		if( h > $(this).outerHeight() ) {
			var diff = h-$(this).outerHeight();
			body.height(body.height()+diff);
		}
	});
};

/*
 *
 */
CWAVE.ContentController.prototype.resizeHandler = function ( evt ) {
	this.adjustArticleHeight();
	
	this.clearScrollSubNavTImer();
	this.scrollSubNavTimer = setTimeout(CWAVE.Delegate(function(){
		if ( this.currentPage ) {
			var parentId = this.currentPage.getParentId();
			var setId = (parentId == '' || parentId == 'root') ? this.currentPage.getId() : parentId;
			this.subNavController.changeId(setId);
			//console.log('setPos resizeHandler');
			this.baloon.setPos(setId);
		}
	},this), 1000);
	;
};
CWAVE.ContentController.prototype.adjustHeader = function ( evt ) {
	var geom = CWAVE.GetGeometry();
	$('#header').css('top', geom.scrollY + 'px');
}
CWAVE.ContentController.prototype.clearScrollSubNavTImer = function(){
	if ( this.scrollSubNavTimer ) {
		clearTimeout(this.scrollSubNavTimer);
		this.scrollSubNavTimer = null;
	}
};
/**
 * for ie6
 */


/* -------------------- CWAVE.Contents -------------------- */
/**
 * 
 */
CWAVE.Contents = function () {
	this.current = null;
	this.pages = {};
	this.init();
};
CWAVE.Contents.prototype.init = function () {
	var tmp,self = this;
	var addChildren = function ( children, parent ) {
		for ( var j = 0; j<children.length; j++) {
			var child = new CWAVE.Page(children[j]);

			if ( child.getTitle() == '' ) {
				child.setTitle(tmp.title);
			}
			else {
				child.setTitle(child.getTitle() + ' | ' + parent.title);
			}
			child.setParentId(parent.id);
			self.pages[child.path] = child;
			if ( children[j].children && children[j].children.length ) arguments.callee(children[j].children, child);
		}
	};
	
	for ( var i = 0; i<CWAVE.settings.Contents.length; i++) {
		tmp = CWAVE.settings.Contents[i];
		this.pages[tmp.path] = new CWAVE.Page(tmp);
		addChildren(tmp.children, tmp);
	}
};
CWAVE.Contents.prototype.getPages = function () {
	return this.pages;
};
CWAVE.Contents.prototype.getPageByPath = function (path) {
	if (!path || path == '' ) return;
	//console.log(this.pages[path],this.pages,path)
	return this.pages[path];
};
CWAVE.Contents.prototype.getPageById = function (id) {
	if (!id || id == '' ) return;
	var tmp = null;
	$.each(this.pages, function(key, page){
		if ( page.getId() == id ) {
			tmp = page;
			return false;
		}
	});
	
	return tmp;
};

/* -------------------- CWAVE.Page -------------------- */
/**
 * 
 */
CWAVE.Page = function ( obj ) {
	this.parentId = '';
	this.childrens = [];
	this.path = '';
	this.title = '';
	this.init(obj);
};
CWAVE.Page.prototype.init = function (obj) {
	this.path = obj.path;
	this.id = obj.id;
	this.title = obj.title;
	this.childrens = obj.children;
	this.posid = obj.posid;
};
CWAVE.Page.prototype.setParentId = function ( id ) {
	this.parentId = id;
};
CWAVE.Page.prototype.getParentId = function ( id ) {
	return this.parentId;
};
CWAVE.Page.prototype.setTitle = function (str) {
	this.title = str;
};
CWAVE.Page.prototype.getTitle = function () {
	return this.title;
};
CWAVE.Page.prototype.getPath = function () {
	return this.path;
};
CWAVE.Page.prototype.getId = function () {
	return this.id;
};
CWAVE.Page.prototype.posId = function () {
	return this.posid;
};


/* -------------------- CWAVE.ContentNavigation -------------------- */
/**
 * 
 */
CWAVE.ContentNavigation = function (node) {
	this.node = $(node).get(0);
	this.buttons = null;
	this.stayId = '';
	this.buttonNumberMap = {};
	this.contentIds = [];
	this.currentTitle = '';
	this.init();
};

/**
 * 
 */
CWAVE.ContentNavigation.prototype.init = function () {
	this.buttons = $(this.node).find("li a");
	this.buttons.click(function(evt){
		evt.preventDefault();
		evt.stopPropagation();
		window.contentController.changeContentById($(this).attr('href').replace(/^#/,""));
	});

	var l = this.buttons.length;
	var b;
	for ( var i= 0; i<l; i++ ) {
		b = this.buttons.eq(i);
		this.buttonNumberMap[b.attr('href').replace(/^#/,"")] = i;
		this.contentIds.push(b.attr('href').replace(/^#/,""));
		CWAVE.setRollOver(b.find('img').get(0));
	}
};

/**
 * 
 * @param contentId
 */
CWAVE.ContentNavigation.prototype.setStay = function ( contentId ) {
	if ( !contentId || contentId== '' || this.stayId == contentId ) return;
	if ( this.stayId != '' ) {
		if ( this.buttonNumberMap[this.stayId] != undefined ) {
			var b = this.buttons.eq(this.buttonNumberMap[this.stayId]);
			b.parent().removeClass("stay");
			var bImg = b.find('img');
			bImg.attr('src', bImg.get(0)._orgSrc);
			CWAVE.setRollOver(bImg.get(0));
		}
	}
	if ( this.buttonNumberMap[contentId] != undefined ) {
		var cur = this.buttons.eq(this.buttonNumberMap[contentId]);
		cur.parent().addClass("stay");
		var curImg = cur.find('img');
		curImg.unbind("mouseenter").unbind("mouseleave");
		curImg.attr('src', curImg.get(0)._overSrc);
		this.currentTitle = cur.text();
	}
	this.stayId = contentId;
};


/* -------------------- CWAVE.SubNavigation -------------------- */
/**
 * 
 */
CWAVE.SubNavigationController = function (){
	this.curId = 'home';
	this.navs = {};
	this.loadCount = 0;
	this.isReady = false;
	this.initialize();
};
CWAVE.SubNavigationController.prototype = new CWAVE.Observable();
CWAVE.SubNavigationController.prototype.initialize = function () {
	this.settings = [
		{
			number: "1",
			id: "home",
			alt: "home"
		},
		{
			number: "2",
			id: "about-us",
			alt: "about us"
		},
		{
			number: "3",
			id: "services",
			alt: "services"
		},
		{
			number: "4",
			id: "offices",
			alt: "home"
		},
		{
			number: "5",
			id: "recruit",
			alt: "recruit"
		},
		{
			number: "6",
			id: "contact",
			alt: "contact"
		},
		{
			number: "7",
			id: "privacy-policy",
			alt: "privacy policy"
		}
	];
	
	for ( var i=0;i<this.settings.length;i++) {
		var snav = new CWAVE.SubNavigationItem(this.settings[i]);
		snav.appendTo($('#articles'));
		snav.addCallback('onLoad',function(){
			this.loadHandler();
		}, this);
		this.navs[this.settings[i].id] = snav;
	}
};
CWAVE.SubNavigationController.prototype.loadHandler = function() {
	this.loadCount++;
	if ( this.loadCount == this.settings.length ) {
		this.isReady = true;
		this.doCallback('onLoad');
	}
};
CWAVE.SubNavigationController.prototype.changeId = function (id) {
	if ( !this.isReady ) return;
	//console.log('CWAVE.SubNavigationController.prototype.changeId')
	if ( id == 'root' ) id = 'home';
	var n = this.navs[id].getNumber();
	
	for ( var i=0; i<this.settings.length; i++) {
		var s = this.settings[i];
		if ( s.number == n ) {
			this.navs[s.id].setMode("cur");
			this.navs[s.id].followTo($("#"+id), 1000);
		}
		else if (s.number < n){
			this.navs[s.id].setMode("up",1000);
			this.navs[s.id].followTo($("#"+id), 1000);
		}
		else if (s.number > n) {
			this.navs[s.id].setMode("down",1000);
			this.navs[s.id].followTo($("#"+id), 1000);
		}
	}
};

/**
 * 
 * @param config
 */
CWAVE.SubNavigationItem = function (config){
	this.upNode = null;
	this.downNode = null;
	this.mode = 'down';
	this.config = null;
	this.timer = null;
	this.timer2 = null;
	this.to = 0;
	this.imagesCount = 0;
	this.imagesLoadCount = 0;
	this.initialize(config);
};
CWAVE.SubNavigationItem.prototype = new CWAVE.Observable();
CWAVE.SubNavigationItem.prototype.initialize = function (config) {
	this.config = config;
	this.upNode = $("<div>",{
		'class':'subnav-item',
		'id': "subnav0"+config.number+'-up'
	});
	this.downNode = $("<div>",{
		'class':'subnav-item',
		'id': "subnav0"+config.number+'-down'
	}).css("top","-150px");
	
	var clickFunc = function (evt) {
		evt.preventDefault();
		evt.stopPropagation();
		window.contentController.changeContentById(config.id);
	};
	
	if ( config.id != 'home' ) {
		$("<img>",{'src':'assets/img/subnav_0'+ config.number +'_down.png'})
			.appendTo(this.downNode)
			.click(clickFunc)
			.hover(function(){$(this).css("opacity","0.7");},function(){$(this).css("opacity","1");})
			.load(function(evt){
				self.loadHandler(evt);
				$(evt.target).unbind('load');
			});
		this.imagesCount++;
	}
	
	var self = this;
	if ( config.id != 'privacy-policy' ) {
		$("<img>",{'src':'assets/img/subnav_0'+ config.number +'_up.png'})
			.appendTo(this.upNode)
			.click(clickFunc)
			.hover(function(){$(this).css("opacity","0.7");},function(){$(this).css("opacity","1");})
			.load(function(evt){
				self.loadHandler(evt);
				self.upNode.show();
				var uH = self.upNode.parent().height();
				self.upNode.css('top',uH +'px');
				$(evt.target).unbind('load');
			});
		this.imagesCount++;
	}

	self.upNode.hide();
};
CWAVE.SubNavigationItem.prototype.loadHandler = function (evt) {
	this.imagesLoadCount++;
	if ( this.imagesLoadCount == this.imagesCount ) {
		this.doCallback('onLoad');
	}
};
CWAVE.SubNavigationItem.prototype.followTo = function (target,delay) {
	if ( !delay ) delay = 0;
	var p = target.position();
	var options = {
			duration : 1500
			, easing : "easeInOutQuad"
	};
	this.to = 10;
	this.toTarget = target;
	
	this.upTo   = p.top-28;
	this.downTo = p.top + target.outerHeight() - 65 - this.downNode.height();
	this.upNodeAdjust   = false;
	this.downNodeAdjust = false;
	
	var self = this;
	this.clearTimeOut();
	switch(this.mode) {
		case "up":
			if ( this.upTo == this.upNode.position().top ) break;
			this.timer = setTimeout(function(){
				//console.log(self.to , self.upNode.position().top)	
				options.duration = Math.ceil(Math.abs(self.upTo - self.upNode.position().top) / 500) * 1000;
				//console.log(options.duration);
				self.upNode.animate({ 'top': self.upTo}, options);
			}, delay);
			break;
		case "down":
			//console.log(this.to, this.downNode.position().top);
			if ( this.downTo == this.downNode.position().top ) break;
			this.timer = setTimeout(function(){
				//console.log(self.to , self.upNode.position().top)
				options.duration = Math.ceil(Math.abs(self.downTo - self.downNode.position().top) / 500) * 1000;
				//console.log(options.duration);
				self.downNode.animate({ 'top': self.downTo}, options);
			}, delay);
			break;
		case "cur":
			this.upTo   = p.top + target.outerHeight()-28;
			this.downTo = p.top  - 65 - this.downNode.height()
			if ( this.upTo   >= this.upNode.position().top ) this.upNodeAdjust = true;
			if ( this.downTo <= this.downNode.position().top ) this.downNodeAdjust = true;
			
			this.timer = setTimeout(function(){
				if ( self.upNodeAdjust ) self.upNode.animate({ 'top': self.upTo}, options);
				if ( self.downNodeAdjust ) self.downNode.animate({ 'top': self.downTo}, options);
			}, delay);
			break;
			
	}
	
};
CWAVE.SubNavigationItem.prototype.setMode = function(mode, delay) {
	this.mode = mode;
};
CWAVE.SubNavigationItem.prototype.getNumber = function () {
	return this.config.number;
};
CWAVE.SubNavigationItem.prototype.appendTo = function (target) {
	this.upNode.appendTo(target);
	this.downNode.appendTo(target);
};
CWAVE.SubNavigationItem.prototype.clearTimeOut = function () {
	if ( this.timer ) {
		clearTimeout(this.timer);
		this.timer = null;
	}
};


/**
 * CWAVE.SlideContent
 */
CWAVE.SlideContent = function (node) {
	this.initialize(node);
};
CWAVE.SlideContent.prototype = new CWAVE.Observable();
CWAVE.SlideContent.prototype.initialize = function (node) {
	this.node = node;
	
	this.node.addClass('slide-content-activated');

	this.setItems(this.node.find('.slide-item'));
	this.setNav(this.node.find('.nav'));

	this.setCurrent(this.nav.find('a:first').attr('href').replace(/^#/,''), true,0);
};
CWAVE.SlideContent.prototype.setNav = function (node) {
	this.nav = node.appendTo(this.node);

	var self = this;
	node.find('a').click(function(evt){
		evt.preventDefault();
		evt.stopPropagation();
		self.setCurrent($(this).attr('href').replace(/^#/,''),false,$(this).data('index'));
	})
	.each(function(index, node){
		$(this).data('index',index)
	});

	if ( $.browser.msie && $.browser.version < 7) {
		this.nav.find('li:first').css('margin-left',0);
	}
};
CWAVE.SlideContent.prototype.setItems = function (nodes) {
	var $viewport = $('<div />').addClass('slide-item-viewport');
	$('<div />').addClass('slide-item-group').appendTo($viewport);
	nodes.wrapAll($viewport);
	this.itemGroup = this.node.find('.slide-item-group');

};
CWAVE.SlideContent.prototype.slide = function( immidiately ){
	var p = this.node.find("#"+this.curId).position();

	if ( immidiately ) {
		this.itemGroup.css('left', '-' + p.left+'px');
	}
	else {
		var options = {
			  duration : 800
			, easing   : "easeInOutQuad"
		};
		this.itemGroup.animate({'left': '-' + p.left},options);
	}
};
CWAVE.SlideContent.prototype.setCurrent = function(id, immidiately, curIndex){
	if(this.curId == id ) return;
	this.curId = id;

	this.slide(immidiately);
	
	this.nav
		.find('li').removeClass('current').end()
		.find('a[href=#'+id+']').parent().addClass('current');
	this.nav.find('.marker').remove();
	
	if ( curIndex != undefined ) {
		this.nav.find('a').each(function(index,node){
			if ( index == curIndex ) return;
			$img = $('<img class="marker">');
			if ( index < curIndex ) {
				$img.attr('src', 'assets/img/icon_arrow_left_01.png').addClass('marker-prev')
			}
			else if ( index > curIndex ) {
				$img.attr('src', 'assets/img/icon_arrow_right_01.png').addClass('marker-next')
			}
			$img.appendTo(this).hide().fadeIn('linear');
		});
	};
	
	this.doCallback('onChange', id);
};


/*
 * 
 */
CWAVE.OfficesMap = function (node){
	this.pos = [
		//latitue, logitude
		['35.169765', '136.916736'] //本社
		, ['35.693555','139.752795'] //東京
		, ['34.966955','138.395278'] //静岡
		, ['34.765244','137.38247'] //豊橋
		, ['34.953973','137.164641'] //豊橋
	];

	this.initialize(node);
};
CWAVE.OfficesMap.prototype.initialize = function(node){
	this.node = node;

	this.controller = node.find('.map-controller');
	this.wide_button = node.find('.map-controller-wide a');
	this.mapArea = node.find('.map-area');
	
	var ch = this.controller.outerHeight();
	var mh = this.mapArea.outerHeight();
	var sh = Math.max(ch,mh);
	
	var d = 0;
	if (ch < sh ) {
		d = sh -ch;
		this.controller.height(this.controller.height()+d);
	}
	if ( mh < sh) {
		d = sh -mh;
		this.mapArea.height(this.mapArea.height()+d);
	}

	this.setMap();
	this.setController();
};
CWAVE.OfficesMap.prototype.setMap = function () {
	var mapOptions = {
			zoom: 7,
			center: new google.maps.LatLng('35.88905','138.164063'),
			mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = this.map = new google.maps.Map(this.mapArea.get(0), mapOptions);

	var pos = this.pos;

	var markers = [];
	var infowindow = [];
	var setMaker = function ( num ) {
		var point = pos[num];

		var markerLatlng = new google.maps.LatLng(point[0],point[1]);

		var markerImg = new google.maps.MarkerImage('assets/img/offices/icon_map-marker_01.png',
			new google.maps.Size(37, 44),
			new google.maps.Point(0, 0),
			new google.maps.Point(15, 44));

		markers[num] = new google.maps.Marker({
			clickable: false
			, icon: markerImg
			, position: markerLatlng
			//, shadow: shadowImg
		});
		markers[num].setMap(map);

	};
	
	var self = this;
	google.maps.event.addListener(map, 'zoom_changed', function(evt){
		if ( self.map.getZoom() <= 7 ) self.wide_button.css('color','#c2c2c2');
		else self.wide_button.css('color','#666');
	});

	for ( var i = 0; i< pos.length; i++ ) {
		setMaker(i);
	}
};
CWAVE.OfficesMap.prototype.setController = function(){
	var self = this;
	this.controller.find('.item').each(function(index){
		var pos = self.pos;
		var p = pos[index];
		$(this)
			.css('cursor','pointer')
			.data('lat',p[0])
			.data('log',p[1])
			.bind('mouseenter',function(evt){
				self.map.panTo(new google.maps.LatLng($(this).data('lat'),$(this).data('log')));
				$(this).css('background','#5e5e5e');
			})
			.bind('mouseleave',function(evt){
				$(this).css('background','none');
			})
			.click(function(evt){
				evt.preventDefault();
				evt.stopPropagation();
				self.map.setZoom(17);
			});
	});

	this.wide_button.click(function(evt){
		evt.preventDefault();
		evt.stopPropagation();
		self.map.setZoom(7);
		self.map.panTo(new google.maps.LatLng('35.88905','138.164063'));
	}).css('color','#c2c2c2');
};


CWAVE.BaloonMarker = function () {
	this.pos = {
		'home': {id:0, x: 627, yadjust:25}
		, 'about-us': {id:1, x: 667, yadjust:-20}
		, 'services': {id:2, x: 707, yadjust:-20}
		, 'offices': {id:3, x: 747, yadjust:-20}
		, 'recruit': {id:4, x: 787, yadjust:-20}
		, 'contact': {id:5, x: 827, yadjust:-20}
		, 'privacy-policy': {id:6, x: 867, yadjust:0}
	};
	this.curPosKey = '';

	this.initialize();
}
CWAVE.BaloonMarker.prototype.initialize = function () {
	this.node = $('<div />',{'id':'baloonMarker'});
	var $img,n;
	for ( var i = 0; i< 7; i++ ) {
		n = i+1;
		$img = $('<img />',{
			'src': 'assets/img/baloon-marker_0'+n+'.png'
			, 'width': '145'
			, 'height': '145'
			, 'id': 'baloonImage' + i
			, 'class': 'baloonImage'
		}).appendTo(this.node);
	}
	this.node.find('.baloonImage').hide()
}
CWAVE.BaloonMarker.prototype.appendTo = function ( target ) {
	this.node.appendTo(target);
}
CWAVE.BaloonMarker.prototype.setPos = function ( key ) {
	if ( key == 'root' ) key = 'home';
	var p = this.pos[key];
	var pp = (this.curPosKey) ? this.pos[this.curPosKey] : p;

	var self =this;
	var options = {
		  duration : 1000 * Math.abs(p.id - pp.id)
		, easing   : "easeInOutQuad"
		, complete : function() {
			self.isBusy = false;
		}
	};

	if ( this.curPosKey != key ) {
		this.changeImage(p.id);
		this.isBusy = false;
	}

	var toTop = $('#'+key).position().top + p.yadjust;
	if ( this.node.position().top != toTop || this.isBusy == false ) {
		this.isBusy = true;
		this.node.animate({'left': p.x, 'top': toTop},options);
	}
	this.curPosKey = key;
}
CWAVE.BaloonMarker.prototype.changeImage = function (num) {
	this.node
		.find('.baloonImage').hide().end()
		.find('#baloonImage'+num).fadeIn();
};


/*****
 *
 */
CWAVE.ContactForm = function (node){
	this.initialize(node);
};
CWAVE.ContactForm.prototype = new CWAVE.Observable();
CWAVE.ContactForm.prototype.initialize = function (node ){
	this.node = node;
	this.request_method = 'GET';
	this.request_url = 'contact/index.php';
	this.request_data = [];
	this.loadForm();
}
CWAVE.ContactForm.prototype.loadForm = function (){
	this.tempElm = this.node.children();
	this.node
		.html('<p>読み込み中...</p>');

	this.xhr = $.ajax({
		  type     : this.request_method
		, url      : this.request_url
		, dataType : 'html'
		, data     : this.request_data
		, success  : function(data, textStatus, XMLHttpRequest){
			this.loadFormSuccessHandler(data, textStatus, XMLHttpRequest);
		  }
		, error    : function(XMLHttpRequest, textStatus, errorThrown){
			this.loadFormErrorHandler(XMLHttpRequest, textStatus, errorThrown);
		  }
		, complete : function(XMLHttpRequest, textStatus){
			this.loadFormCompleteHandler(XMLHttpRequest, textStatus);
		  }
		, context  : this
	});
};
CWAVE.ContactForm.prototype.loadFormSuccessHandler = function (data, textStatus, XMLHttpRequest) {
	this.node.empty()
	$(data).find('#contact .right').children()
		.appendTo(this.node)
		.hide().fadeIn();
	this.prepareFormElm();
};
CWAVE.ContactForm.prototype.loadFormErrorHandler = function (XMLHttpRequest, textStatus, errorThrown) {
	this.node.html('<p style="color: red">読み込みに失敗しました。</p>');
};
CWAVE.ContactForm.prototype.loadFormCompleteHandler = function (XMLHttpRequest, textStatus) {
	this.doCallback('loadFormComplete');
};
CWAVE.ContactForm.prototype.prepareFormElm = function () {
	var self = this;
	if ( this.node.find('form').length) {
		this.node.find('form').submit(function(evt){
			return false;
		});
		this.node.find('.controll input').click(function(evt){
			evt.preventDefault();
			self.request_data = self.node.find('form').serialize();
			self.request_method = self.node.find('form').attr('method');
			self.request_data += '&action='+$(this).val();
			self.loadForm();
		});
	}
	else {
		this.node.find('#re').click(function(evt){
			evt.preventDefault();
			self.request_data = [];
			self.request_method = 'GET';
			self.loadForm();
		});
	}
};

$(function(){
	window.contentController = new CWAVE.ContentController();
	$(window).bind('scroll', CWAVE.Delegate(window.contentController.scrollHandler, window.contentController));
	$(window).bind('resize', CWAVE.Delegate(window.contentController.resizeHandler, window.contentController));
});

})(jQuery);
