// JavaScript Document

/* ===============================================
  ロールオーバー
  
  <img />,<input type="image" />タグに「class="rover"」とつけると
  「～～.gif」,「～～,jpg」を「～～_on.gif」,「～～_on,jpg」に切り替える。
=============================================== */
	function initRollOverImages(){
		var image_cache = new Object();
		$("img.rover,input.rover").each(function(i){
			var imgsrc = this.src;
			var dot = this.src.lastIndexOf('.');
			var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
			image_cache[this.src] = new Image();
			image_cache[this.src].src = imgsrc_ro;
			$(this).hover(function(){
				this.src = imgsrc_ro;
			},function(){
				this.src = imgsrc;
			});
		});
	}

/* ===============================================
  メニューアクティブ
  
  画面ロード時にimgタグを_onの付いた画像に張り替える
=============================================== */
	function Menu_active(id){
		if(id != ''){
			var target_img = "img#"+id;
			$(target_img).each(function(i){
				dot = this.src.lastIndexOf('.');
				var imgsrc_ro = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
				this.src = imgsrc_ro;
				//既に割り当てられているロールオーバー関数を上書き
				$(this).hover(function(){
					this.src = imgsrc_ro;
				},function(){
					this.src = imgsrc_ro;
				});
			});
		}
	}

/* ===============================================
  メニューアクティブ化(テキスﾄ)
  
  画面ロード時にliタグ or ddタグにクラス[active]を追加する
=============================================== */
	function localMenu_active(id){
		if(id != ''){
			var target_img = "#"+id;
			$(target_img).each(function(i){
				$(this).find('a').addClass("active");
			});
		}
	}

/* ===============================================
  スムーススクロール
  
  アンカーリンクをエフェクトしながら動く
=============================================== */
	function SmoothnessScroll(){
		$('a[href^="#"]').click(function(){
			$(this).scrollTo(500);
			return false;
		});
	}
	
	jQuery.fn.extend({
		scrollTo : function(speed, easing){
			if(!$(this)[0].hash || $(this)[0].hash == "#"){
				return false;
			}
			return this.each(function() {
				var targetOffset = $($(this)[0].hash).offset().top;
				$('html,body').animate({scrollTop: targetOffset}, speed, easing);
			});
		}
	});

/* ===============================================
  input,textareaタグをフィーカスする
  
  input,textareaタグにカーソルが乗ると、[input-focus]クラス名が追加される。
  デフォルト時は[input-usually]が自動的に追加される。
=============================================== */
	function InputFocus(){
		$('input[type=text],input[type=password]').addClass('input-usually');
		$('.input-usually').focus(function(){
			$(this).addClass('input-focus');
		});
		$('.input-usually').blur(function(){
			if($(this).find('.input-focus')){
				$(this).removeClass('input-focus');
			}
		});
	}

/* ===============================================
  アニメーション
=============================================== */
	//#Footerのpagetop (全ページ共通)
	function PageTopAnimate(){
		$('#Footer p.pagetop a').each(function(){
			$(this).mouseover(function(){
				//over
				$('#Footer p.pagetop img.piglet')
					.animate({right:0},{duration:75})
					.animate({right:10},{duration:75})
					.animate({right:0},{duration:75})
					.animate({right:10},{duration:75})
					.animate({right:5},{duration:75});
				$('#Footer p.pagetop').animate(
					{height:'83px',top:'-58px'},
					{queue:false,duration:100}
				);
			});
			$(this).mouseout(function(){
				//out
				$('#Footer p.pagetop').animate(
					{height:'45px',top:'-15px'},
					{queue:false,duration:100});
			});
		});
	}
	
	//#MainTopのサイド(商品ラインナップ)  (TOPページのみ)
	function MainTopAnimate(){
		$('#MainTop div.lineup div.lineup-inline dl dd a').each(function(){
			$(this).hover(function(){
				$(this).find('img').animate({marginRight:'16px'},{queue:false,duration:200});
			},function(){
				$(this).find('img').animate({marginRight:'6px'},{queue:false,duration:200});
			});
		});
	}
	
	//下層ページのサイド (下層ページのみ)
	function SideNaviAnimate(){
		$('#Side div.localnavi dl.item dd a').each(function(){
			if(!$(this).hasClass("active")){
				$(this).hover(function(){
					$(this).animate({paddingLeft:'30px'},{queue:false,duration:200});
				},function(){
					$(this).animate({paddingLeft:'20px'},{queue:false,duration:200});
				});
			}
		});
		
		$('#Side div.localnavi dl.brand dd a').each(function(){
			$(this).hover(function(){
				$(this).find('img').animate({marginRight:'16px'},{queue:false,duration:200});
			},function(){
				$(this).find('img').animate({marginRight:'6px'},{queue:false,duration:200});
			});
		});
	}


/* ===============================================
	検索窓 (商品ラインナップページ用)
=============================================== */
	function SearchWindow(){
		$(".search-window").hide();
		$(".section.search div.title p.btn a img.cloes").hide();
		$('.section.search div.title p.btn a').toggle(function(){
			$(".search-window").slideDown('normal');
			$(this).find('img.open').hide();
			$(this).find('img.cloes').show();
			return false;
		},function(){
			$(".search-window").slideUp('normal');
			$(this).find('img.open').show();
			$(this).find('img.cloes').hide();
			return false;
		});
	}

/* ===============================================
  検索結果の商品 (商品ラインアップ用)
=============================================== */
	function SearchItem(TAG){
		//奇数にclass="odd"を追加する
		$(TAG+':odd').addClass('odd');
		
		//高さを揃える (2カラム)
		var sets = [], temp = [];
		$(TAG).find('div').each(function(i) {
			temp.push(this);
			if(i % 2 == 1){
				sets.push(temp);
				temp = [];
			}
		});
		if (temp.length) sets.push(temp);
		$.each(sets, function() {
			$(this).flatHeights();
		});
		
		//divにリンクを張る
		$(TAG).each(function(){
			$(this).removeClass('hover');
			if($(this).find('a').hasClass('location')){
				var Url = $(this).find('a.location').attr('href');
				$(this).hover(function(){
					$(this).addClass('hover');
				},function(){
					$(this).removeClass('hover');
				});
				$(this).click(function(){
					if($(this).find('a.location').attr('target')){
						window.open(Url,'_blank');
					}else{
						location.href = Url;
					}
					return false;
				});
			}
		});
	}

/* ===============================================
  検索結果の商品 (キャンペーン用)
=============================================== */
	function SearchCmp(TAG){
		//奇数にclass="odd"を追加する
		$(TAG+':odd').addClass('odd');
		
		//高さを揃えない (1カラム)
		var sets = [], temp = [];
/*		$(TAG).find('div').each(function(i) {
			temp.push(this);
			if(i % 3 == 1){
				sets.push(temp);
				temp = [];
			}
		});
*/		if (temp.length) sets.push(temp);
		$.each(sets, function() {
			$(this).flatHeights();
		});
		
		//divにリンクを張る
		$(TAG).each(function(){
			$(this).removeClass('hover');
			if($(this).find('a').hasClass('location')){
				var Url = $(this).find('a.location').attr('href');
				$(this).hover(function(){
					$(this).addClass('hover');
				},function(){
					$(this).removeClass('hover');
				});
				$(this).click(function(){
					if($(this).find('a.location').attr('target')){
						window.open(Url,'_blank');
					}else{
						location.href = Url;
					}
					return false;
				});
			}
		});
	}

/* ===============================================
  検索ボックスの背景表示・非表示
=============================================== */
	function SearchBoxBGShowHide(){
		$('form.search-box input.search-box-input').focus(function(){
			var $this = $(this);
			
			if (!$this.data('backgroundImage')) {
				$this.data('backgroundImage', $this.css('backgroundImage'));
			}
			
			$this.css('backgroundImage', 'none');
		}).blur(function(){
			var $this = $(this);
			
			if (!$this.val()) {
				$this.css('backgroundImage', $this.data('backgroundImage'));
			}
		}).each(function(){
			var $this = $(this);
			
			if ($this.val()) {
				$this.focus();
			}
		});
	}
/* ===============================================
  各関数を実行する。
=============================================== */
$(document).ready(function(){
	//マウスオーバー
	initRollOverImages();
	//スムーススクロール
	//SmoothnessScroll();
	//inputフォーカス
	InputFocus();
	//pagetop
	PageTopAnimate();
	SearchBoxBGShowHide();
});

/* ===============================================
  別窓表示対応
=============================================== */
//スーパーカップスペシャルサイト
function owlssp(){
	win1 = window.open('/brand/super/special/index.html','lspecial','resizable=yes, scrollbars=yes,width=' + window.screen.width + ',height=' + window.screen.height);
	win1.focus();
}
//ベジさめファーム
function owvegef(){
	win2 = window.open('http://harusame.info/vegefarm/','vsf','resizable=yes,menubar=no,status=no,toolbar=no,location=yes,scrollbars=yes,left='+(screen.width/2-475)+',top='+(screen.height/2-310)+',width=950,height=620');vsf.focus();return false;
	win2.focus();
}
//からだ食堂CM
function krdcm(){
	win4 = window.open('/brand/karada/cm.html','krd','resizable=yes,menubar=no,status=no,toolbar=no,location=yes,scrollbars=yes,left='+(screen.width/2-360)+',top='+(screen.height/2-200)+',width=780,height=400');vsf.focus();return false;
	win4.focus();
}

//こぶた50周年
function ow50th(){
	win3 = window.open('/campaign/happyboo/','happyboo','resizable=yes, scrollbars=yes');
	win3.focus();
}
