var NapsTopPageGallery = function(main_div_path, sub_div_path) {
	this.main_div_path =main_div_path;
	this.sub_div_path = sub_div_path;
	this.top_gallery_main_box_label='top_gallery_main_box_';
	this.top_gallery_sub_box_label='top_gallery_sub_box_';
	this.main_images = jQuery(this.main_div_path+' ul li');
	this.sub_images = jQuery(this.sub_div_path+' ul li');
	this.sub_images_max_top_number=0;
	this.top_gallery_sub_scroll_up_button=jQuery("img#top_gallery_sub_scroll_up_button");
	this.top_gallery_sub_scroll_down_button=jQuery("img#top_gallery_sub_scroll_down_button");
	this.top_gallery_sub_scroll_button=jQuery("img.top_gallery_sub_scroll_button");
	this.current_image_number=0;
	this.timer_used=true;
	this.current_sub_image_top_number=0;
	this.current_sub_image_slide_bottom=true;
}
NapsTopPageGallery.prototype={
	setGalleryPath:function(galllery_image_paths){
		var self=this;
		this.main_images.each(function(index){
			jQuery(this).attr('id',self.top_gallery_main_box_label+index);
			if(index==0){
				jQuery(this).hide();
			}
			jQuery(jQuery(this).children('a')).children('img').attr('src',galllery_image_paths[index])
			jQuery(this).children('p').hide();
			if(index==0){
				jQuery(this).fadeIn(2000);
			}
		});
		this.sub_images.each(function(index){
			jQuery(this).attr('id',self.top_gallery_sub_box_label+index);
			if(index>=0&&index<=2){
				jQuery(this).hide();
			}
			jQuery(jQuery(this).children('a')).children('img').attr('src',galllery_image_paths[index]);
			if(index>=0&&index<=2){
				jQuery(this).fadeIn(2000);
			}
		});
		if(this.sub_images.length>3){
			this.sub_images_max_top_number=this.sub_images.length-3;
			this.top_gallery_sub_scroll_up_button.attr('src','/img/naps/top/sidebanner_slidebutton_up.png')
			this.top_gallery_sub_scroll_up_button.attr('alt','上のバナーを表示')
			this.top_gallery_sub_scroll_up_button.attr('title','上のバナーを表示')
			this.top_gallery_sub_scroll_up_button.click(
				function(){
					self.shift_sub_images(false);
				}
				);
			this.top_gallery_sub_scroll_down_button.attr('src','/img/naps/top/sidebanner_slidebutton.png')
			this.top_gallery_sub_scroll_down_button.attr('alt','下のバナーを表示')
			this.top_gallery_sub_scroll_down_button.attr('title','下のバナーを表示')
			this.top_gallery_sub_scroll_down_button.click(
				function(){
					self.shift_sub_images(true);
				}
				);
			this.top_gallery_sub_scroll_button.hover(
				function(){
					var currentTarget=jQuery(this);
					currentTarget.css("cursor","pointer").css("cursor","hand");
				},
				function(){
					var currentTarget=jQuery(this);
					currentTarget.css('cursor', 'default');
				}
				);
		}
		this.main_images.hover(
			function(){
				self.timer_used=false;
				var currentTarget=jQuery(this);
				currentTarget.children('p').stop().fadeTo(1000,0.8);
			},
			function(){
				self.timer_used=true;
				var currentTarget=jQuery(this);
				currentTarget.children('p').stop().fadeTo(1000,0);
			}
			);
		this.sub_images.hover(
			function(){
				var currentTarget=jQuery(this);
				self.timer_used=false;
				var id=currentTarget.attr('id');
				self.current_image_number=parseInt(id.match(/\d+$/));
				self.show_main_images(self.current_image_number);
				currentTarget.css("cursor","pointer").css("cursor","hand");
			},
			function(){
				var currentTarget=jQuery(this);
				self.timer_used=true;
				currentTarget.css('cursor', 'default');
			}
			);
	},
	rotate_main_images:function(){
		if(this.main_images.length>0){
			if(this.current_image_number<this.main_images.length-1){
				this.current_image_number++;
			}else{
				this.current_image_number=0;
			}
			this.show_main_images(this.current_image_number);
			if(!this.shift_sub_images(this.current_sub_image_slide_bottom)){
				this.current_sub_image_slide_bottom=!this.current_sub_image_slide_bottom;
				this.shift_sub_images(this.current_sub_image_slide_bottom);
			}
		}
	},
	show_main_images:function(number){
		this.main_images.fadeOut(400);
		jQuery(this.main_images[number]).fadeIn(2000);
	},
	slide_sub_images:function(slide_number){
		var sub_image_height=80;
		var scrollPos=sub_image_height*slide_number;
		var sub_image_box=jQuery(this.sub_div_path+' ul');
		sub_image_box.stop().animate({
			'scrollTop':scrollPos
		},400);
	},
	shift_sub_images:function(next){
		var shift;
		var ret=false;
		if(next){
			shift=1;
			if(this.sub_images_max_top_number>this.current_sub_image_top_number){
				this.current_sub_image_top_number+=shift;
				ret=true;
			}
		}else{
			shift=-1;
			if(this.current_sub_image_top_number>0){
				this.current_sub_image_top_number+=shift;
				ret=true;
			}
		}
		this.slide_sub_images(this.current_sub_image_top_number);
		return ret;
	}
}

jQuery(function(){
	var top_search_freeword_form=jQuery('#top_search_freeword_form');
	var ret=true;
	top_search_freeword_form.bind('submit',function(){
		if(jQuery('#top_search_freeword_text input').val()==''){ //初期化してしまったらsubmitを中止させる
			ret=false;
		}
		return ret;
	});
});

