$(function(){
/*	ルートへのパスを相対にして格納
------------------------------------------- */					 
	var _ROOT = $($('script').get(0)).attr('src').split('common/')[0];

/*	ラベル処理
------------------------------------------- */
	if(jQuery.browser.msie){	//　IEのlabel内の画像対応
		$('label img').click(function(){
			var target = $(this).parents('label');
			var jumpEle = target.attr('for');
			$('#'+jumpEle).focus();
		});
		
	}else if(jQuery.browser.safari){	//	Safariのlabelタグ対応
		$('label').click(function(){
			var target = $(this);
			var jumpEle = target.attr('for');
			$('#'+jumpEle).focus();
		}).hover(function(){
			$(this).css('cursor','pointer');
		},function(){
			$(this).css('cursor','default');
		});
	}
	
/*	サイト内検索ボックスのfocus処理 for IE
------------------------------------------- */
	var searchText = $('#searchText');
	var bgColor = searchText.css('background-color');
	var boderColor = searchText.css('border-color');
	searchText.css('background-image','url(http://img.yahoo-search.jp/img/logo_wm.gif)');
	searchText.css('background-position','50% 50%');
	searchText.css('background-repeat','no-repeat');	

	$('#searchText').focus(function(){
		searchText.css('background-color','#ffefcd');
		searchText.css('background-image','none');
	}).hover(function(){
		searchText.css('border-color','#ff9934');//ffb9f3		
	},function(){
		searchText.css('border-color','#cccccc');
	}).blur(function(){
		searchText.css('background-color','#f1f1f1');
		if(this.value==""){
		searchText.css('background-image','url(http://img.yahoo-search.jp/img/logo_wm.gif)');
		}
	});
	
	var searchText2 = $('#searchText2');
	var bgColor = searchText2.css('background-color');
	var boderColor = searchText2.css('border-color');
	searchText2.css('background-image','url(http://img.yahoo-search.jp/img/logo_wm.gif)');
	searchText2.css('background-position','50% 50%');
	searchText2.css('background-repeat','no-repeat');	

	$('#searchText2').focus(function(){
		searchText2.css('background-color','#ffefcd');
		searchText2.css('background-image','none');
	}).blur(function(){
		searchText2.css('background-color','#f1f1f1');
		if(this.value==""){
		searchText2.css('background-image','url(http://img.yahoo-search.jp/img/logo_wm.gif)');
		}		
	}).hover(function(){
		searchText2.css('border-color','#ff9934');//ffb9f3	
	},function(){
		searchText2.css('border-color','#cccccc');
	});
	

/*	:first-child処理
------------------------------------------- */
	$(".section").each(function(){
		// console.log($( $(this).children()[0]));
		$( $(this).children()[0]).css('margin-top','0');
	});


/*	Copywriteの年吐き出し
------------------------------------------- */
	$('.copyright ins').each(function(){
		var myDate = new Date();
		$(this).html(myDate.getFullYear()).css('text-decoration','none');
	});

/*	左ナビバナー生成　div#aside > div.gainInfo 
------------------------------------------- */
/*
//	生成されるサンプルHTML
<p><a href="../topics/mokuyou/index.html"><img src="../common/images/aside_bn05.gif" alt="木曜日のお買い得 木曜の市" /></a></p>

//　JSONサンプル
 BANNER_DATA = {
 "info":
	 [
		 {
		 "img": "aside_bn05.gif",
		 "alt": "木曜日のお買い得 木曜の市",
		 "url": "/topics/mokuyou/index.html",
		 "target": "0"	//0：同窓,1：別窓
		 },
		 {
		 "img": "aside_bn05.gif",
		 "alt": "木曜日のお買い得 木曜の市",
		 "url": "topics/mokuyou/index.html",
		 "target": "0"
		 }
	 ]
 };
*/
	$('div#aside div.storeInfo').each(function(){	// 店舗情報
		var display = true;	// 店舗情報　表示or非表示
		var _target = $(this);
		var _bannerPath = _ROOT+'common/images/';
		var _defaultBanner = '<p class="title"><img src="'+_bannerPath+'aside_tx02.gif" alt="店舗情報" width="170" height="19" /></p><dl><dt><a href="/shop/index.html"><img src="'+_bannerPath+'aside_bn03.gif" alt="店舗情報" width="170" height="64" /></a></dt><dd><a href="/shop/index.html">店舗検索、各店舗からのお知らせはこちらから</a></dd></dl>'
		if(display){
			_target.append(_defaultBanner);		// 見出し
//			console.log(BANNER_DATA.shop.length);
			if(BANNER_DATA.shop.length != 0){
				for(var i=0; i<BANNER_DATA.shop.length; i++){
					var _blank;
					if(BANNER_DATA.shop[i].target == '1'){
						_blank = 'target = "_blank"';
					}else {
						_blank = '';
					}
					_target.append('<p><a href="'+BANNER_DATA.shop[i].url+'" '+_blank+'><img src="'+_bannerPath+BANNER_DATA.shop[i].img+'" alt="'+BANNER_DATA.shop[i].alt+'" /></a></p>');
				}
			}
		}else {
			$(this).remove();
		}
	});
	
	$('div#aside div.gainInfo').each(function(){	// お得情報
		var _target = $(this);
		var _bannerPath = _ROOT+'common/images/';
		if(BANNER_DATA.info.length == 0){
			$(this).remove();
		}else{
			_target.append('<p class="title"><img src="'+ _bannerPath+ 'aside_tx01.gif" alt="お得情報" width="170" height="19" /></p>');		// 見出し
			for(var i=0; i<BANNER_DATA.info.length; i++){
				var _blank;
				if(BANNER_DATA.info[i].target == '1'){
					_blank = 'target = "_blank"';
				}else {
					_blank = '';
				}
				_target.append('<p><a href="'+BANNER_DATA.info[i].url+'" '+_blank+'><img src="'+_bannerPath+BANNER_DATA.info[i].img+'" alt="'+BANNER_DATA.info[i].alt+'" /></a></p>');
			}
		}
	});

// 食べるカテゴリ特集バナー
	$('div#aside div.featureArticles').each(function(){	
		var _target = $(this);
		var _bannerPath = _ROOT+'food/images/';
		if(BANNER_DATA.feature.length == 0){
			$(this).remove();
		}else{
			_target.append('<p class="title"><img src="'+ _bannerPath+ 'aside_h01.gif" alt="特集" width="170" height="20" /></p>');		// 見出し
			for(var i=0; i<BANNER_DATA.feature.length; i++){
				var _blank;
				if(BANNER_DATA.feature[i].target == '1'){
					_blank = 'target = "_blank"';
				}else {
					_blank = '';
				}
				_target.append('<p><a href="'+BANNER_DATA.feature[i].url+'" '+_blank+'><img src="'+_bannerPath+BANNER_DATA.feature[i].img+'" alt="'+BANNER_DATA.feature[i].alt+'" /></a></p>');
			}
		}
	});



/*	段組の見出しの高さを揃える
------------------------------------------- */
// DOM構築時実行
ajustHeight01('.wrap2','.title02');
ajustHeight01('.wrap2','.title03');
ajustHeight01('.wrap3','.title02');
ajustHeight01('.wrap3','.title03');
// 文字サイズ変更時実行
function init()  {
  var iBase = TextResizeDetector.addEventListener(function(){
								ajustHeight01('.wrap2','.title02');
								ajustHeight01('.wrap2','.title03');
								ajustHeight01('.wrap3','.title02');
								ajustHeight01('.wrap3','.title03');
							},null);
			 
}
		/*function onFontResize(e,args) {
			var msg = "\nThe base font size in pixels: " + args[0].iBase;
			msg +="\nThe current font size in pixels: " + args[0].iSize;
			msg += "\nThe change in pixels from the last size:" + args[0].iDelta;
			//alert(msg);
		}*/
		
function ajustHeight01(parentClass,titleClass){
	$(parentClass).each(function(){
											 //console.log(this);
		var titleHeight = new Array();
		var target = $(this);
		var colClass= '> .col';
		var colNum = target.children(colClass).length;
		var temp = 0;
		for(var i=0; i<colNum ; i++){
				if(jQuery.browser.msie && $.browser.version == 6.0){	//　IE6のBOXモデルの差分処理
					titleHeight[i] = $(target.children(colClass).children(titleClass)[i]).height();
				}else {
					titleHeight[i] = $(target.children(colClass).children(titleClass)[i]).children().height();
				}
				if(temp < titleHeight[i]){
						temp = titleHeight[i];
						//console.log(i+' : '+titleHeight[i]);
				}
		}
		if(colNum >= 2){
			for(var i=0; i<colNum ; i++){
				if(jQuery.browser.msie && $.browser.version == 6.0){	//　IE6のBOXモデルの差分処理
					$(target.children(colClass).children(titleClass)[i]).children().css('height',temp);
				}else {
					$(target.children(colClass).children(titleClass)[i]).children().css('min-height',temp);
				}
			}
		}
/*		if(colNum < 2){
		}else if(colNum == 2){
			if(titleHeight[0] > titleHeight[1]){
				$(target.children(colClass).children(titleClass)[1]).children().css('height',titleHeight[0]);
			}else if(titleHeight[0] < titleHeight[1]){
				$(target.children(colClass).children(titleClass)[0]).children().css('height',titleHeight[1]);
			}
		}else {
			
		}*/
	});
}

/*	フォントサイズ変更を監視
------------------------------------------- */
/** 
 *  @fileoverview TextResizeDetector
 * 
 *  Detects changes to font sizes when user changes browser settings
 *  <br>Fires a custom event with the following data:<br><br>
 * 	iBase  : base font size  	
 *	iDelta : difference in pixels from previous setting<br>
 *  	iSize  : size in pixel of text<br>
 *  
 * @version 1.0
 */

/**
 * @constructor
 */
TextResizeDetector = function() { 
    var el  = null;
	var iIntervalDelay  = 10;
	var iInterval = null;
	var iCurrSize = -1;
	var iBase = -1;
 	var aListeners = [];
 	var createControlElement = function() {
	 	el = document.createElement('ins');
		el.id='textResizeControl';
		el.innerHTML='&nbsp;';
		el.style.position="absolute";
		el.style.left="-9999px";
		var elC = document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID);
		// insert before firstChild
		if (elC)
			elC.insertBefore(el,elC.firstChild);
		iBase = iCurrSize = TextResizeDetector.getSize();
 	};

 	function _stopDetector() {
		window.clearInterval(iInterval);
		iInterval=null;
	};
	function _startDetector() {
		if (!iInterval) {
			iInterval = window.setInterval('TextResizeDetector.detect()',iIntervalDelay);
		}
	};
 	
 	 function _detect() {
 		var iNewSize = TextResizeDetector.getSize();
		
 		if(iNewSize!== iCurrSize) {
			for (var 	i=0;i <aListeners.length;i++) {
				aListnr = aListeners[i];
				var oArgs = {  iBase: iBase,iDelta:((iCurrSize!=-1) ? iNewSize - iCurrSize + 'px' : "0px"),iSize:iCurrSize = iNewSize};
				if (!aListnr.obj) {
					aListnr.fn('textSizeChanged',[oArgs]);
				}
				else  {
					aListnr.fn.apply(aListnr.obj,['textSizeChanged',[oArgs]]);
				}
			}

 		}
 		return iCurrSize;
 	};
	var onAvailable = function() {
		
		if (!TextResizeDetector.onAvailableCount_i ) {
			TextResizeDetector.onAvailableCount_i =0;
		}

		if (document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID)) {
			TextResizeDetector.init();
			if (TextResizeDetector.USER_INIT_FUNC){
				TextResizeDetector.USER_INIT_FUNC();
			}
			TextResizeDetector.onAvailableCount_i = null;
		}
		else {
			if (TextResizeDetector.onAvailableCount_i<600) {
	  	 	    TextResizeDetector.onAvailableCount_i++;
				setTimeout(onAvailable,200)
			}
		}
	};
	setTimeout(onAvailable,500);

 	return {
		 	/*
		 	 * Initializes the detector
		 	 * 
		 	 * @param {String} sId The id of the element in which to create the control element
		 	 */
		 	init: function() {
		 		
		 		createControlElement();		
				_startDetector();
 			},
			/**
			 * Adds listeners to the ontextsizechange event. 
			 * Returns the base font size
			 * 
			 */
 			addEventListener:function(fn,obj,bScope) {
				aListeners[aListeners.length] = {
					fn: fn,
					obj: obj
				}
				return iBase;
			},
			/**
			 * performs the detection and fires textSizeChanged event
			 * @return the current font size
			 * @type {integer}
			 */
 			detect:function() {
 				return _detect();
 			},
 			/**
 			 * Returns the height of the control element
 			 * 
			 * @return the current height of control element
			 * @type {integer}
 			 */
 			getSize:function() {
	 				var iSize;
			 		return el.offsetHeight;
		 		
		 		
 			},
 			/**
 			 * Stops the detector
 			 */
 			stopDetector:function() {
				return _stopDetector();
			},
			/*
			 * Starts the detector
			 */
 			startDetector:function() {
				return _startDetector();
			}
 	}
 }();


		//id of element to check for and insert control
		TextResizeDetector.TARGET_ELEMENT_ID = 'header';
		//function to call once TextResizeDetector has init'd
		TextResizeDetector.USER_INIT_FUNC = init;
});

