/**
 * general.js - for ip20.co.jp
 * @requires jQuery v1.4.2
 *
 * Copyright (c) 2010 cyclops.co.,ltd.
 * $Date: 2010.4.8
 *
 */


/**
 *	Initialization
 */
$(document).ready(function(){
	for (module in cyclops) {
		if (cyclops[module].init){
			cyclops[module].init();
		}
	}
});


var cyclops = {};

/**
 *	clikable list (list option)
 *
 *	"clickable"クラスを持つUL要素のLI要素のエリア全面をクリックできるようにする
 *	クリックアクションは、LI要素に内包するAタグのHREF要素を呼び出す
 */
cyclops.clickable = {
	clickableClass:".clickable",
	init:function(){
		$(this.clickableClass, '#content').find('li').click(function(){
			window.location = $(this).find("a").attr("href");
		}).hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
	}
};


/**
 * hoverCaption (gallery option)
 *
 *	"gallery"を持つ、UL要素で、さらに"hover-caption"クラスを持つ場合、LI要素の:hoverで、キャプションを表示する
 *	キャプションは、imgタグのalt属性が表示される
 * alt属性で文字列"::"がある場合は、分割され後半が<B>タグで囲まれる
 **/
cyclops.hoverCaption = {
	hoverCaptionClass:".hover-caption",
	init:function(){
		$('li a', this.hoverCaptionClass).each(function(){
			if(!$('img', this).size() || !$('img', this).attr('alt') ) return;
			this.areaH = $(this).eq(0).height()+10;
		}).hover(function(){
			var h = this.areaH - $(this).find('.caption').outerHeight()-5;
			$(this).find('.caption').stop(false,true).animate({top:h}, 150);
		},function(){
			$(this).find('.caption').stop(false,true).animate({top:this.areaH}, 200);
		})
	}
};


/**
 * mailSubscribe
 *
 *	メールアドレス入力フォームにメッセージを表示する
 *	submitアクション時にフォームの値がデフォルトと同様の場合は中止する
 */
cyclops.mailSubscribe = {
	mailFormClass:".mail-form",
	mailInputClass:".mail-form-input",
	defaultVal:"メールアドレスを入力してくだい",
	init:function(){
		if(!$(this.mailFormClass).length) return false;
			this.setTextFill();
			this.setSubmitProxy();
	},
	setTextFill:function(){
		var self = this;
		$(this.mailInputClass).each(function(){
			if( $.trim($(this).val()) == '' ) $(this).val(self.defaultVal);
			$(this).focus( function(){
				if( $.trim($(this).val()) == self.defaultVal ) $(this).val(''); 
			});
			$(this).blur( function(){
				if( $.trim($(this).val()) == '' ) $(this).val(self.defaultVal);
			});
		});
	},
	setSubmitProxy:function(){
		var self = this;
		$(this.mailFormClass).submit(function(){
			if( $.trim($(self.mailInputClass, this).val()) == self.defaultVal) return false;
		});
	}
}


/**
 * auto play slider
 *
 *	複数あるリストの内容をスライド表示する
 *	delayに値がある場合は自動再生する
 *	画像部分にロールオーバー時は自動再生は一時停止
 *	ナビゲーションをクリックすると自動再生は停止
 */
cyclops.slider = {
	ID:"#campaign-display",
	delay:6000,
	current:null,
	len:null,
	timeoutID:null,
	defaultDelay:null,
	init:function(){
		if(!$(this.ID) || $('li', this.ID).size() < 2) return false;
		$('ul', this.ID).addClass('slider');
		this.len = $('li', this.ID).size();
		this.makeNav(this.len);
		this.setDisplayMask();
		this.update(0);
		this.defaultDelay = this.delay;
		if(this.delay) this.autoplay();
	},
	makeNav:function(len){
		var html = '', self = this;
		for(var i = 0; i < len; i++){ html += '<li><a href="#"></a></li>';}
		$('.slider', this.ID).after('<ul id="slider-nav">' + html + '</ul>');
		
		$('li', '#slider-nav').each(function(i){
			$(this).click(function(){
				self.delay = self.defaultDelay = 0;
				self.update(i);
				return false;
			})
		});
	},
	setDisplayMask:function(){
		var self = this;
		$('.slider', this.ID).mouseover(function(){
				self.stopplay();
				self.delay = 0;
			}).mouseout(function(){
				self.delay = self.defaultDelay;
				if(self.delay) self.autoplay();
			});
	},
	update:function(i){
		if(this.current == i) return false;
		var getout = this.current;
		this.current = i;
		$('li', '#slider-nav').removeClass('current').eq(i).addClass('current');
		$('li', this.ID).eq(getout).stop().animate({left:'-960px'}, 480);
		if(getout == null){
			$('li', this.ID).eq(i).stop().css({left:'0'});
		}else{
			$('li', this.ID).eq(i).stop().css({left:'960px'}).animate({left:'0px'}, 480);
		}
		this.stopplay();
		if(this.delay) this.autoplay();
	},
	autoplay:function(){
		var self = this;
		var next = (this.current+1 >= this.len) ? 0 : this.current+1;
		this.stopplay();
		this.timeoutID = window.setTimeout(function(){self.update(next)}, this.delay);
	},
	stopplay:function(){
		if(this.timeoutID) window.clearTimeout(this.timeoutID);
	}
};


/**
 * photo viewer
 */
cyclops.photoViewer = {
	viewerID:'#photo-viewer',
	viewerMainID:'#photo-viewer-main',
	viewerNavID:'#photo-viewer-nav',
	items:{},
	len:{},
	currentNum:null,
	init:function(){
		if(!$(this.viewerID).size()) return false;
		$('.display', this.viewerMainID).hide();
		$('.caption', this.viewerMainID).hide();
		this.getItemList();
		this.setOverlayBtn();
		this.update(0);
	},
	getItemList:function(){
		var self = this, temp = {},cache = [];
		this.len = $('li', this.viewerNavID).size();
		$('li', this.viewerNavID).each(function(i){
			temp[i] = {};
			temp[i]['id'] = i;
			temp[i]['photo'] = $('a', this).attr('href');
			temp[i]['caption'] = $('img', this).attr('alt');
			$(this).click(function(){
				self.update(i);
				return false;
			})
			var cacheImage = document.createElement('img');
			cacheImage.src = temp[i]['photo'];
			cache.push(cacheImage);
		});
		this.items = temp;
	},
	setOverlayBtn:function(){
		var self = this;
		$('#photo-viewer-prev, #photo-viewer-next').each(function(){
			$('span', this).hide();
		}).hover(function(){
			$('span', this).show();
		}, function(){
			$('span', this).hide();
		});
		
		$('#photo-viewer-prev').click(function(){
			self.btnUpdate(-1);
			return false;
		})
		$('#photo-viewer-next').click(function(){
			self.btnUpdate(1);
			return false;
		})
	},
	update:function(id){
		if(this.currentNum === id) return;
		this.currentNum = id;
		var self = this;
		var main = $('.display', this.viewerMainID);
		var cap = $('.caption', this.viewerMainID);
		
		main.fadeOut(200, function(){
			main.empty().append('<img src="'+ self.items[id]['photo'] +'" />').fadeIn(500);
		});
		
		cap.empty().hide();
		if(self.items[id]['caption']) cap.append(self.items[id]['caption']).delay(700).fadeIn(500);
		
		if(id == 0){
			$('#photo-viewer-prev').hide();
		}else{
			$('#photo-viewer-prev').show();
		}
		
		if((this.len-1) <= id){
			$('#photo-viewer-next').hide();
		}else{
			$('#photo-viewer-next').show();
		}
		
		$('li', this.viewerNavID).removeClass('current').eq(id).addClass('current');
		
	},
	btnUpdate:function(num){
		var nextID = this.currentNum + num;
		if(nextID < 0) nextID = ( this.len - 1);
		if(nextID >= this.len) nextID = 0;
		this.update(nextID);
	}
};


/** enlarge */
cyclops.enlarge = {
	init:function(){
		if(jQuery.isFunction($.fn.fancybox)){
			$("a[rel=enlarge]").fancybox({
				'padding':1,
				'titlePosition' 	: 'over',
				'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">' + title + '</span>';
				}
			});
		}
	}
};


/** gmapWriter */
cyclops.gmapWriter = {
	init:function(){
		if(jQuery.isFunction($.fn.gMap)){
			var isLarge = ($('#showroom').size());
			$('div.gmap').each(function(){
				this.title = $('dt', this).text();
				this.lat = $('dd.latitude', this).text();
				this.lon = $('dd.longitude', this).text();
				this.controls =  (isLarge) ? [] : false;
				$(this).empty().gMap({
					zoom: 14,
					controls: this.controls,
					scrollwheel: false,
					markers: [{ latitude: this.lat, longitude: this.lon }]
				});
			})
		}
	}
};
