/* ================================================================================
* Title: Jquery Function
*
* Copyright (c) 2009 Astra The Studio Inc.
* http://www.studio.co.jp/
* Date: Feb.13, 2009
* @author: Chihiro Mori
* @version: 1.0.3
*
* jQuery 1.3.2
* jQuery.ScrollTo 1.4.2
*
* 確認済ブラウザ: [Mac] FireFox3.0, Safari3.1.2 [Win] IE6.0, 7.0
* Macで作業するときの注意：バックスラッシュ\を入力するときはoptionを押しながらエンマーク
================================================================================ */
/* 目次
selfLink()     : 自ページリンクにクラス付与
menuRollover() : 透明効果を使ったナビゲーションのマウスオーバー
btnRollover()  : 透明効果を使ったナビゲーションのマウスオーバー(クラス付与)
selfScroll()   : ページ内スムーススクロール。jQuery.ScrollTo を使用
*/
(function($)
{
//---------------------------------------------------------------------
	$(function()
	{
		$.ASconf.gNavOnOff();
		$.ASconf.selfLink();
	  /*$.ASconf.osawaSnavFix();*/
		$.ASconf.menuRollover();
		$.ASconf.menuCurrent();
		$.ASconf.sideRollover();
		$.ASconf.btnRollover();
		$.ASconf.selfScroll();
		$.ASconf.menuScroll();
		$.ASconf.imgResize();
		$.ASconf.googleMap();
		$.ASconf.swapImg();
		$.ASconf.addClassLast();
		$.ASconf.addClassEven();
		$.ASconf.osawaInq();
		$.ASconf.blog();
		$.ASconf.noFair();
		$.ASconf.ddpng();
		$.ASconf.photo();
	//	$.ASconf.inq();
	});
//---------------------------------------------------------------------
	$.ASconf =
	{
	//---------------------------------------------------------------------
	exetend:function(){
               $.fn.extend({
                    //.log()をつけてチェーン途中でもログ出力
                    log: function() {
                           console.log($(this));
                           return this;
                    },
                    //画像の本来の大きさ取得
                    getImgSize:function(){
                           var image = $(this).get()[0];                       
                           var run, mem, w, h, key = "actual";
                           if('naturalWidth' in image){//for Firefox Webkit
                                        return {height:image.naturalHeight, width:image.naturalWidth}
                           }
                           if('src' in image){//for IE
                                 if (image[key] && image[key].src === image.src) {return  image[key];}
                                        if (document.uniqueID) { // for IE
                                             run = image.runtimeStyle;//runtimeStyleを保存
                                             mem = {w:run.width,h:run.height};//幅と高さを保存
                                             run.width = "auto";//runtimeStyleを一旦autoに上書き→runtimeStyleは!importantよりも最優先される
                                             run.height = "auto";
                                             w = image.width;//改めてwidthとかを取る
                                             h = image.height;
                                             run.width = mem.w;//元に戻してあげる
                                             run.height = mem.h;
                                        } else {// for Opera and Other
                                             mem = {w: image.width, h: image.height}; //幅と高さを保存
                                             $(this)
                                                    .removeAttr("width")
                                                    .removeAttr("height")
                                                    .css({width:"",  height:""});//attrとかcss上の幅と高さを削除
                                             w = image.width;//改めてwidthとかを取る
                                             h = image.height;
                                             image.width  = mem.w;//かわいそうなので、元に戻してあげる
                                             image.height = mem.h;
                                        }
                                        return image[key] = {width: w, height: h, src: image.src};//imageにwidthとheightを入れる。
                           }
                           return {height:image.height,width:image.width}//幅と高さを返す
                                     
                    },
                    //ブラウザーごとに分岐
                    browserIf:function(obj){
                           //http://w3g.jp/blog/tools/jquery_browser_sniffingを参照
                           if(!$.support.checkOn && $.support.checkClone){
                                 var _browser = "webkit";
                           }else if($.support.checkOn && $.support.htmlSerialize && window.globalStorage){
                                 var _browser = "firefox";
                           }else if($.support.checkOn && $.support.htmlSerialize && !window.globalStorage){
                                 var _browser = "opera";
                           }else if(!$.support.noCloneEvent && $.support.opacity){
                                 var _browser = "ie9";
                           }else if(!$.support.opacity){
                                 if(!$.support.style){
                                        if (typeof document.documentElement.style.maxHeight != "undefined") {
                                          var _browser = "ie7";
                                        } else {
                                          var _browser = "ie6";
                                        }
                                 }else{
                                   var _browser = "ie8";
                                 }
                           }else{
                                 var _browser = "unknown";
                           }
                           //ブラウザの種類を元に関数を実行
                           if(obj !== undefined){//objが無いときは実行しない
                                 if($.isFunction(eval('obj.'+_browser))){//該当のブラウザ用の関数が設定されてないときは実行しない
                                        obj[_browser](this);//引数にthis
                                 }
                                 if($.isFunction(obj.elseBrowser)){//elseBrowserが設定されてないときは実行しない。
                                        obj.elseBrowser(this);//引数にthis
                                 }
                           }
                           return this;
                    },
                    //ブラウザーごとのクラスを付与
                    addBrowserClass:function(){
                           //http://w3g.jp/blog/tools/jquery_browser_sniffingを参照
                           if(!$.support.checkOn && $.support.checkClone){
                                var _browser = "webkit";
                           }else if($.support.checkOn && $.support.noCloneEvent && window.globalStorage){
                              var _browser = "firefox";
                           }else if($.support.checkOn && $.support.noCloneEvent && !window.globalStorage){
                                var _browser = "opera";
                           }else if(!$.support.noCloneEvent && $.support.opacity){
                                var _browser = "ie9";
                           }else if(!$.support.opacity){
                                 if(!$.support.style){
                                        if (typeof document.documentElement.style.maxHeight != "undefined") {
                                          var _browser = "ie7";
                                        } else {
                                          var _browser = "ie6";
                                        }
                                 }else{
                                     var _browser = "ie8";
                                 }
                           }else{
                                 var _browser = "unknown";
                           }
                           $(this).addClass(_browser)
                           return this;
                    }
               });
		},
		selfLink: function ()
		{
			var c = $.extend(
			{
				selfLinkClass:'current'
			});
			
			$.ASconf.selfLinkClass = '.' + c.selfLinkClass;
			$.ASconf.theUrl = location.href.replace(location.hash, '');
			$.ASconf.shrUrl = document.getElementById('import').href.replace('css/import.css', '');
			var theUrl = $.ASconf.theUrl.replace(/(\/|\#)$/, '/index.html');
			
			$('a[href]').each(function()
			{
				var i = document.createElement('span');
				i.innerHTML = '<a href="' + $(this).attr('href') + '" />';
				var absolutePath = i.firstChild.href;				
				if (absolutePath == theUrl)
				{
					$(this).addClass(c.selfLinkClass).removeAttr('href');
				}
			});
		}
		,
		//-----------------------------------------------------------------
		menuRollover: function()
		{
			var c = $.extend(
			{
				selector:'#cNav img, #sideContactBtn img, #sideInquiryBtn img,#cNav_top img,#cNav_contents img'
			});
			$(c.selector).each(function()
			{
				if($(this).parent().attr('class'))
				{
					$(this).fadeTo(0, 0.7);
				}
				else
				{
					$(this).hover(function()
					{
						$(this).not(':animated').fadeTo('fast', 0.0);
					}
					,function()
					{
						$(this).fadeTo('fast', 1);
					});
				};
			});
		}
		,
		//-----------------------------------------------------------------
		menuCurrent:function(){
if( $('body').hasClass('partyreport')){
			var t = $('#cNav li:eq(2) a','#cNav_top li:eq(2) a','#cNav_contents li:eq(2) a');
			t.addClass('current').removeAttr('href');
				t.find('img').attr("src",'/shr/img/base/header_bt3_cr.gif').fadeTo(0, 1);
	
	
	} else {

			var t = $('#cNav a.current img','#cNav_top a.current img','#cNav_contents a.current img');
			var crTxt = "_cr"
			if(t.attr("src") != undefined){
				var str = t.attr("src");
				var str1 = str.slice(0,str.lastIndexOf('.'));
				var str2 = str.slice(str.lastIndexOf('.'));
	/*			var strSplit = str.split(".");
				if(str.indexOf("../") == 0){
					var parent = "..	";
				} else {
					var parent = "";
				}*/
			//	var crUrl = [parent,strSplit[strSplit.length - 2], crTxt,".",strSplit[strSplit.length - 1]];
			var crUrl = str1 + crTxt + str2;
				t.attr("src",crUrl).fadeTo(0, 1);
			}
	}
		},

		//-----------------------------------------------------------------
		sideRollover: function()
		{
			var c = $.extend(
			{
				selector:'#sNav img,#sNavOption img'
			});
			$(c.selector).each(function()
			{
				if($(this).parent().attr('class'))
				{
					$(this).fadeTo(0, 0);
				}
				else
				{
					$(this).hover(function()
					{
						$(this).not(':animated').fadeTo(0, 0);
					}
					,function()
					{
						$(this).fadeTo('fast', 1);
					});
				};
			});
		}
		,
		//-----------------------------------------------------------------
		btnRollover: function()
		{
			var c = $.extend(
			{
				selector:'#header a[href] img,#main a[href] > img,img.btn'
			});

			$(c.selector).each(function()
			{
				if($(this).parent().hasClass('current'))
				{
					$(this).fadeTo(0, 0.7).bind('onMouseOut',function(){return false;});
				}
				else
				{
					$(this).hover(function()
					{
						$(this).stop().fadeTo('normal', 0.7);
					}
					,function()
					{
						$(this).stop().fadeTo('fast', 1);
					});
				};
			});
		}
		,
		//-----------------------------------------------------------------
		selfScroll: function()
		{
			var c = $.extend(
			{
				selector:'#pagetop'
			});
			$('a[href*="#"]').each(function()
			{
				if(this.href.split('#')[0] == $.ASconf.theUrl)
				{//ページ内リンクだったら
					$(this).click(function()
					{
						var hash= this.hash;
						var target = $(hash).offset().top;
						$($.browser.safari ? 'body' : 'html').animate({scrollTop: target}, 500);			
						return false;
					});
				}
			});
		}
		,
		//-----------------------------------------------------------------
		menuScroll: function()
		{
			//if(!$.browser.msie && !$.browser.version <= 6){
			$('body.places #side').each(function()
			{
				var target = $(this);
				var offsetTop = target.offset().top;
				var defaultOffset = offsetTop;
				var targetHeight = target.attr('offsetHeight') + $('#sNavOption').attr('offsetHeight') +$('#sNavOption').height()+20;
				var footerOffset = $('#descriptionBox').offset().top;
				var maxScroll = Math.abs(footerOffset - (targetHeight + defaultOffset));
				$('#contents').css('min-height',$('#side').height() + $('#descriptionBox').height() + 60);
				function winScroll(e)
				{
					var scrollTop = $(document).scrollTop();
					if(scrollTop - defaultOffset < maxScroll)
					{
						if(offsetTop < scrollTop)
						{
							var offset = (scrollTop - defaultOffset);
							$(target).stop().animate({top:offset+'px'},{ duration:350, queue: false });
						}
						else
						{
							$(target).stop().animate({top:0},{ duration:350, queue: false });
						}						
					}
					else
					{
						$(target).stop().animate({top:maxScroll},{ duration:350, queue: false });
					}
				}
				winScroll();//Fire Fox で Google Analyticsを埋め込んだページでは、ページ読み込み時のscrollイベントが発生しない？？ので、その対策
				$(window).bind('scroll', winScroll);
				$(window).bind('resize', winScroll);
			});
		}
		//}
		,
		//-----------------------------------------------------------------
		imgResize: function()
		{
			if(!$('body').hasClass('home')&&!$('body').hasClass('inquiry')){
			var target = $('#visual img,');
			var tml = $('#visual img').offset().left;
			//console.log(tml);
			var maxWidth = 1200;
			
			function resizer(e)
			{
				var winWidth = $(document).width();
				if(winWidth < maxWidth)
				{
					var left = (maxWidth -winWidth) / 2 + tml;
					target.stop().css('margin-left', -left + 'px');
				}
				else if ( winWidth >= maxWidth)
				{
					target.stop().css('margin-left', 'auto');
				}
			}
			
			resizer();
			$(window).bind('resize', resizer);
			}
		},
		//-----------------------------------------------------------------
		googleMap: function()
		{
			if($('body').hasClass('places')&&$('body').hasClass('access')){
				$('body.access #map').googleMaps({
					latitude:north,
					longitude:east,
					depth:zoom,
					markers: {
						latitude:north,
						longitude:east,
						info: {
							layer: '#side div.placeLogo a',
							popup: true
						}	
					}
				});
			}
		},
		//-----------------------------------------------------------------
		gNavOnOff: function()
		{
			var cookieName = 'wgportal';
			if($('body').hasClass('portal')||$('body').hasClass('home')){
				$.cookie(cookieName,1);
			} else if($('body').hasClass('places')){
				if($.cookie(cookieName) == 1){
					$('#cNav,#cNav_top,#cNav_contents').css("display","block");
					//$('#sNavOptionWrap').css("display","none");
					$('#sNav li:last-child').css("padding-bottom","0px");
				} else {
					$('#cNav,#logo,#gNav,#cNav_top,#cNav_contents').css("display","none");
					//$('#sNavOptionWrap').css("display","block");
					$('#minamiaoyama,#yokohama').css("display","none");
					$('#miniTel').css("display","block");
					$('#headerWrap').addClass('noPortal');
				}
			}
		},
		//-----------------------------------------------------------------
		swapImg: function()
		{
			var t = $('.mod-swapImage');
			var h = t.find('.thum > .imgBox').height();
			var w = t.find('.thum > .imgBox').width();
			t.find('.thum > .imgBox > img').hover(
				function(){
					t.find('.imgBox > img.bigImg').attr('src',$(this).attr('src'));
				},
				function(){
				}
			).css({cursor:'pointer',opacity:'1.0'});
			
		},
		//-----------------------------------------------------------------
		ddpng: function()
		{
			if(typeof document.documentElement.style.maxHeight == "undefined"){
				if(!$('body').hasClass('home')){
/*					$.getScript("http://www.wedding-garden.jp/shr/js/DD_belatedPNG_0.0.8a-min.js", function(){
						DD_belatedPNG.fix('#headerWrap, #footer, #visualWrap, #side .placeLogo a');	
					});*/
					$.ajax({
						type: "GET",
						url: "/shr/js/DD_belatedPNG_0.0.8a-min.js",
						dataType: "script",
						complete: function(){
							DD_belatedPNG.fix('#headerWrap, #footer, #visualWrap, #side .placeLogo a');	
						}
					});
				} else {
				//	DD_belatedPNG.fix('#headerWrap, #footer');	
				}
			}
		},
		
		//-----------------------------------------------------------------
		addClassLast: function()
		{
			$('#mod-recContents > div.cntBox:last, div.mod-topicList dl dd:last-child,div.mod-topicList dl dt:nth-child(5),div.mod-topicList dl dt:nth-child(3),#main div.mod-swapImage > div.thum > div.imgBox:last')
			.addClass('last');
		},
		//-----------------------------------------------------------------
		addClassEven: function()
		{
			$('#mod-planlist>div.mod-topicList:nth-child(odd),div#mod-partyreport > .mod-partyreportBox:odd')
			.addClass('even');
		},
		osawaInq: function()
		{
			if($('body').hasClass('osawa')){
				var allA = $('a');
				allA.each(function(n){
					if($(this).attr('href')){
						if($(this).attr('href').match(/^(.*?inquiry\/)index\.html$/i)){
						$(this).attr('href',RegExp.$1+'osawa.html');
						} else if($(this).attr('href').match(/^(.*?inquiry\/)observation\.html$/i)){
						$(this).attr('href',RegExp.$1+'osawa_observation.html');
							
						}
					}
				})
			};
		},
		inq: function()
		{
			var cookieName = 'wgplace';
			var placeNameList = ['berrick','chaya','fukudaya','hosokawa','labrasserie','laveronica','osawa','ramages','sankeien'];
			var className = $('body').attr('class').split(' ');
			var layer2 =className[1];
			if(placeNameList.indexOf(layer2) != -1){
				//$.cookie(cookieName, layer2)
				//$.cookie(cookieName,layer2);本番用
			} else if($('body').hasClass('inquiry')){
				if(layer2 == 'form'){
					if($.cookie(cookieName)){
						$('#' + $.cookie(cookieName)).attr('checked','checked');
					}
				} else if(layer2 == 'thanks'){
					$.cookie(cookieName,null);
				}
			} else {
				$.cookie(cookieName,null);
				$.cookie('the_cookie', null);
			}
		},
		blog: function()
		{	
			if($('body').hasClass('blog') && !$('body').hasClass('single')){
				var maxWidth = $('#mainColum').width();
				$('#mainColum > .post').each(function(){
					var p = $(this);
					var c = p.find('.commentBox');
					var t = p.find('.trackbackURL');
					c.hide();
					t.hide();
					p.find('div.writeComment').click(function(){
						c.slideToggle();
					});
					p.find('div.trackback').click(function(){
						t.slideToggle();
					});
					var img = p.find('img');
					var memW = img.width();
					var memH= img.height();
					if(memW > maxWidth){
						var w = maxWidth - 20;
						var h = memH * (w / memW);
						img.attr('height',h);
						img.attr('width',w);
					}
				});
				$('#mainColum > div.post > div.postBody img[src]').each(function(){
					var imgURL = $(this).attr('src');
					imgURL = imgURL.replace(/http:\/\/www\.wedding-garden\.jp\/weblog_planner\/([0-9]+\/)([0-9]+\/)(.*\.(jpg|JPG))/,"http://www.wedding-garden.jp/weblog_planner/_$1$2$3");
					$(this).attr('src',imgURL);
				});	
			}
		},
	//-----------------------------------------------------------------
	osawaSnavFix: function()
		{
			if($('body').hasClass('osawa')){
				var restLogo = $('<a id="osawaRestLogo" target="_blank" href="http://www.ristoranteosawa.com/"><img class="btn" width="250" height="76" alt="大澤でお食事の方はこちら" src="http://9axis.com/test/wg/shr/img/osawa/side_bt_osawabanner.jpg"></a>');
				$('#sNavWrap').append(restLogo).find('#sNavWGlogo').remove();
			}
		},
	//-----------------------------------------------------------------
	noFair: function()
		{
			if($('#sNavBtn1').length == 0){
				$('div.mod-leadContents > div.textBox > div.btnBox').remove();					
			}
		},
	//-----------------------------------------------------------------
	photo: function()
		{
			if($('body').hasClass('photogallery')){
				
				$.getScript("/shr/js/jquery.okphotogallary.js", function(){
				  
				  $.fn.okphotogallary();
				});
								
			}
		}
	//-----------------------------------------------------------------
	
	};
//---------------------------------------------------------------------
})(jQuery);
