$(function() {
	//ROLLOVER
	$('.btn').rollover();
	$('#navigationBox ul li img').rollover();
	$('#purchaseInfo ul#pageInnerLink li img').rollover();
	$('#header ul li img').rollover();
	$('ul#qNavi li img').rollover();
	$('ul.topBtnArea li img').rollover();
	$('#onlineBuySec li img,#onlineSellSec p img').rollover();
	$('#searchBox form p input').rollover();
	$('ul#sideFixNavi li img').rollover();
	
	//TOOLTIPS
	$(function($){
		$('.tip').toolTip();
	});
	
	//LINK
	$('.linkBox').each(function() {
		$(this).hover(function() {
			$(this).stop(true,true).animate({
				opacity:0.8
			}, 'fast');
		},function() {
			$(this).stop(true,true).animate({
				opacity:1
			}, 'fast');
		});
	});
	$('.linkBox').click(function() {
		//リンクが外部のとき
		if($(this).find('a').attr('target') == '_blank') {
			window.open($(this).find('a').attr('href'),'_blank');
			return false;
		} else {
			window.location = $(this).find('a').attr('href');
			return false;
		};
	});
	
	//CORNER
	$('#questionMainArea dt span').corner("round 2px").parent().css('padding', '1px').corner("round 3px");
	$('.corner').corner("round 2px").parent().css('padding', '1px').corner("round 3px");
	$('h4.ssttl-pick').corner("3px");
	
	//PRETTYPHOTO
	$(window).load(function() {
		$('a[rel^="prettyPhoto"]').prettyPhoto({
			allow_resize:false
		});
	});
	
	//SEARCH
	$('dd.searchBox input',this).focus(function() {
		if($(this).val() == $(this).attr('defaultValue')) {
			$(this).val('');
		};
	}).blur(function() {
		if(jQuery.trim($(this).val()) == '') {
			$(this).val($(this).attr('defaultValue'));
		};
	});
	
	//SEARCH TOGGLE
	$('dt.toggleTrigger').each(function() {
		//フラグ管理
		var flag = true;
		var img = $(this).children();
		var src = img.attr('src');
		var _on = src.replace(/^(.+)(\.[a-z]+)$/, '$1-on$2');
		$(this).click(function () {
			if(flag == true) {
				img.attr('src', _on);
				flag = false;
			} else {
				img.attr('src', src);
				flag = true;
			};
			$(this).next().stop(true,true).slideToggle();
		});
	});

	if($('ul#yearly li').hasClass('select')) {
		$('ul#yearly li a').click(function() {
			$('ul#yearly li').removeClass('select');
		});
	};
	
	$('.monthlyInner p.btn-close a').click(function() {
		$('ul#yearly li').removeClass('ui-tabs-selected');
		$('ul#yearly li').removeClass('ui-state-active');
		$($(this).parent().parent().parent()).addClass('ui-tabs-hide');
	});

	//GRID
	if($('#gridAreaInner').size() > 0) {
		$(window).load(function() {
			var leftInnerHeight = $('.leftColumn-details').innerHeight();
			$('#mainContents').css('minHeight', leftInnerHeight);
		});
		var gridHandler = $('#gridAreaInner').vgrid({
			easeing: "easeOutQuint",
			useLoadImageEvent: true,
			useFontSizeListener: true,
			time: 400,
			delay: 20
		});

		//SEARCH TOGGLE
		$('dt.toggleTrigger').click(function () {
			setTimeout(function () {
				gridHandler.vgrefresh();
			},850);
		});
	};
	
	//WELCOMEBANNER
	var siteURL = location.hostname;
	var welcomeBannerContainer = $('<div id="welcomeBannerContainer"> \
	<div id="welcomeBanner"> \
	<div id="welcomeBannerInner"> \
	<h2><img src="/shared/images/welcome/sttl-welcome.png" width="381" height="57" alt="WELCOME TO JUMBLE STORE!!" /></h2> \
	<h3><img src="/shared/images/welcome/ssttl-helpyou.png" width="135" height="21" alt="MAY I HELP YOU?" /></h3> \
	<h4><img src="/shared/images/welcome/sssttl-list.png" width="570" height="13" alt="ジャンブルストアへようこそ！お探しのものはおありですか？" /></h4> \
	<ul id="welcomePageList" class="sectionBase"> \
	<li id="welcome01"><img src="/shared/images/welcome/btn-purchase.png" width="140" height="119" alt="買取をしてほしい！" /></li> \
	<li id="welcome02"><img src="/shared/images/welcome/btn-shop.png" width="140" height="119" alt="お店に行きたい！" /></li> \
	<li id="welcome03"><img src="/shared/images/welcome/btn-online.png" width="140" height="119" alt="今すぐ買いたい！" /></li> \
	<li id="welcome04"><img src="/shared/images/welcome/btn-home.png" width="140" height="119" alt="楽しみたい！" /></li> \
	</ul> \
	<p id="welcomeClose"><img src="/shared/images/welcome/btn-close.gif" width="18" height="18" alt="閉じる" /></p> \
	</div> \
	</div> \
	</div>');

	var welcomeBannerAfter = $('<div id="welcomeAfterText"> \
	<p><img src="/shared/images/welcome/thankyou.png" width="286" height="286" alt="WELCOME TO JUMBLE STORE!!THANK YOU LET&acute;S ENJOY!" /></p> \
	</div>');

	var welcomeBannerOverlay = $('<div id="welcomeOverlay"></div>').css({
		width:$(window).width(),
		height:$(document).height(),
		opacity:0.1
	});
	
	$(window).resize(function() {
		welcomeBannerOverlay.css({
			width:$(window).width()
		});
	});
	
	function helpBtnClickHandler() {
		$('li#btn-help').click(function() {
			if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
				$('dd.searchBox select').css({visibility:"hidden"});
			};
			$('body').append(welcomeBannerContainer).append(welcomeBannerOverlay);
			helpHandler();
			mouseMoveHandler();
		});
	};
	helpBtnClickHandler();

	function overlayCloseHandler() {
		welcomeBannerOverlay.click(function() {
			welcomeBannerContainer.remove();
			welcomeBannerOverlay.remove();
			welcomeBannerAfter.stop(true,true).fadeOut("fast", function(){
				$(this).css('filter','');
				$(this).remove();
			});
			if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
				$('dd.searchBox select').css({visibility:"inherit"});
			};
			$(window).trigger('resize');
		});
	};
	function welcomeCloseHandler() {
		$('p#welcomeClose').bind('click',(function() {
			welcomeBannerContainer.remove();
			welcomeBannerOverlay.remove();
			welcomeBannerAfter.stop(true,true).fadeOut("fast", function(){
				$(this).css('filter','');
				$(this).remove();
			});
			$(window).trigger('resize');
		}));
	};
	
	function helpHandler() {
		var timerFlug = false;
		$('ul#welcomePageList li').click(function() {
			welcomeBannerContainer.remove();
			var index = $(this).index();
			if(index == 0) {
				window.location = '/purchase/?welcome';
			} else if(index == 1) {
				window.location = '/shop/?welcome';
			} else if(index == 2) {
				window.location = '/shopping/?welcome';
			} else if(index == 3) {
				if(location.href == 'http://'+ siteURL +'/?welcome' || location.href == 'http://'+ siteURL +'/') {
					$('body').append(welcomeBannerAfter);
					welcomeBannerAfter.stop(true,true).fadeIn("fast", function(){
						$(this).css('filter','');
					});
					afterTimerAction = setTimeout(function () {
						welcomeBannerAfter.stop(true,true).fadeOut("fast", function(){
							$(this).css('filter','');
							$(this).remove();
						});
						welcomeBannerOverlay.remove();
					},3000);
				} else {
					window.location = '/?welcome';
				};
				welcomeBannerOverlay.click(function() {
					clearTimeout(afterTimerAction);
				});
			} else {
				return false;
			};
		});
		overlayCloseHandler();
		welcomeCloseHandler();
	};

	function mouseMoveHandler() {
		welcomeBannerContainer.mousedown(function(e) {
			welcomeBannerContainer.data('clickPointX' ,e.pageX - welcomeBannerContainer.position().left)
			.data('clickPointY' ,e.pageY - welcomeBannerContainer.position().top);
			$(document).mousemove(function(e) {
				welcomeBannerContainer.css({
					top:e.pageY - welcomeBannerContainer.data('clickPointY') + 'px',
					left:e.pageX - welcomeBannerContainer.data('clickPointX') + 'px'
				});
			});
		}).mouseup(function() {
			$(document).unbind('mousemove');
		});
	};
	
	mouseMoveHandler();
	
	$(window).resize(function(){
		var scrollTop = parseInt($(this).scrollTop());
		var _width = $(window).width()/2 - 316;
		var _width02 = $(window).width()/2 - 143;
		var _height = $(window).height()/2 - 168;
		var _height02 = $(window).height()/2 - 143;
		welcomeBannerContainer.stop(true,true).animate({
			top: scrollTop + _height,
			left: _width
		},200);
		welcomeBannerAfter.stop(true,true).animate({
			top: scrollTop + _height02,
			left: _width02
		},200);
		welcomeBannerOverlay.css({
			height:$(document).height()
		});
	});
	$(window).scroll(function() {
		$(window).trigger('resize');
		var scrollTop = parseInt($(this).scrollTop());
		var _height = $(window).height()/2 - 168;
		var _height02 = $(window).height()/2 - 143;
		welcomeBannerContainer.stop(true,true).animate({
			top: scrollTop + _height
		},200);
		welcomeBannerAfter.stop(true,true).animate({
			top: scrollTop + _height02
		},200);
	});

	$(window).trigger('resize');

	$(function(){
		$(window).trigger('resize');
	});

	if(($('body#page-home').size() > 0 || $('body#page-purchase').size() > 0 || $('body#page-shop').size() > 0 ||$('body#page-shopping').size() > 0) && location.search.match(/[?&]welcome(&|$)/)) {
		$('body').append(welcomeBannerAfter);
		$('ul#sideFixNavi').css({
			visibility:'hidden'
		});
		setTimeout(function () {
			$('body').append(welcomeBannerOverlay);
			$('ul#sideFixNavi').css({
				visibility:'inherit'
			});
			welcomeBannerAfter.stop(true,true).fadeIn("fast", function(){
				$(this).css('filter','');
			});
			timerAction = setTimeout(function () {
				welcomeBannerAfter.stop(true,true).fadeOut("fast", function(){
					$(this).css('filter','');
					$(this).remove();
				});
				welcomeBannerOverlay.remove();
			},2000);
		},2000);
		welcomeBannerOverlay.click(function() {
			if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
				$('dd.searchBox select').css({visibility:"inherit"});
			};
			welcomeBannerOverlay.remove();
			welcomeBannerAfter.stop(true,true).fadeOut("fast", function(){
				$(this).css('filter','');
				$(this).remove();
			});
			if('ul#sideFixNavi:hidden') {
				$('ul#sideFixNavi').css({
					visibility:'inherit'
				});
			};
			clearTimeout(timerAction);
			$(window).trigger('resize');
		});
	};

	//BLOG
	if($('body#page-blog').size() > 0) {
		//BLOG RECENT
		$('.recentBox:odd').addClass('odd');
		$('.recentBox:even').addClass('even');

		//BLOG EDITOR
		$('.editorBox:odd').addClass('odd');
		$('.editorBox:even').addClass('even');
	};
	
	//PURCHASE
	if($('body#page-purchase').size() > 0) {
		//ボックスの高さ
		var row = 3;
		var listHeight = 0;
		$('#itemsBrandArea #brandList .sectionBase ul').each(function(i) {
			if(listHeight < $(this).innerHeight()) {
				listHeight = $(this).innerHeight();
			};
			$(this).addClass('row');
			if(i% row == row-1) {
				$('ul.row').height(listHeight).removeClass('row');
				listHeight = 0;
			};
		});
		$('ul.row').innerHeight(listHeight).removeClass('row');
	};

	//CONTACT
	if($('body#page-contact').size() > 0) {
		$('body#page-contact table#personInfo tr:even').addClass('odd');
		$('body#page-contact table#personInfo tr:odd').addClass('even');
	};

	//SHOP
	if($('body#page-shop').size() > 0) {
		$('#shopInfoBox .sectionBase:odd,#shopListArea table:odd,#shopDetailArea table tr:odd,#searchNearShop dl:odd').addClass('odd');
		$('#shopInfoBox .sectionBase:even,#shopListArea table:even,#shopDetailArea table tr:even,#searchNearShop dl:even').addClass('even');

		if($('#shopImage ul').size() > 0) {
			$('#shopImage ul').innerfade({ 
				speed: 2000,
				timeout: 4000,
				type: 'sequence',
				containerheight: '348px'
			});
		};
	};

	//COORDINATE
	if($('body#page-coordinate').size() > 0) {
		//カテゴリーのボタン
		$('#squeezeCategory li input[type="checkbox"]').ezMark();
		//ROLLOVER
		$('#btn-squeeze input').rollover();
		
		//フェード切り替え
		$('#coordinateThumb p.thumb img').each(function() {
			$(this).click(function() {
				$('#coordinateThumb p.thumb img').removeClass('selected');
				$(this).addClass('selected');
				$('#bigImgArea #changeImg').css({filter:''});
				$('#bigImgArea #changeImg').fadeOut("fast", function(){
					$(this).css('filter','');
				}).delay(800).fadeIn("fast", function(){
					$(this).css('filter','');
				});
				if($('#coordinateMainBoxInner #bigImgArea span').size() > 0) {
					$('#coordinateMainBoxInner #bigImgArea span').css({filter:''});
					$('#coordinateMainBoxInner #bigImgArea span').fadeOut("fast", function(){
						$(this).css('filter','');
					}).delay(800).fadeIn("fast", function(){
						$(this).css('filter','');
					});
				};
			});
		});

		//フローティング
		var coordinateTip=$('<div class="coordinateTip pngBack"></div>').hide();
		$('body').append(coordinateTip);
		$('.coordinateItem dl').css({
			display:'none'
		});

		$('.coordinateItem p.thumb img').each(function() {
			$(this).hover(function(e) {
				coordinateTip.html($(this).parent().next().html());
				coordinateTip.stop(true,true).fadeIn("fast",function(){
					$(this).css('filter','');
				}).css({
					position:'absolute',
					top:e.pageY+20,
					left:e.pageX-30
				}).wrapInner('<div class="coordinateTipInner pngBack"><dl></dl></div>');
			},function() {
				coordinateTip.fadeOut("fast",function(){
					$(this).css('filter','');
				});
			}).mousemove(function(e) {
				coordinateTip.css({
					top:e.pageY+20,
					left:e.pageX-30
				});
			});
			
			$(this).click(function() {
				$('.floatWindow').fadeOut("fast", function(){
					$(this).css('filter','');
				});
				$('.floatTxt dl').html($(this).parent().next().html());
				setTimeout(function () {
					$('.floatWindow').fadeIn("fast", function(){
						$(this).css('filter','');
					});
				},1000);
			});
		});
		$('.coordinateTotal p.thumb img').click(function() {
			$('.floatWindow').fadeOut("fast", function(){
				$(this).css('filter','');
			});
		});

		$('.floatWindow').mousedown(function(e) {
			$('.floatWindow').data('clickPointX' ,e.pageX - $('.floatWindow').position().left)
			.data('clickPointY' ,e.pageY - $('.floatWindow').position().top);
			$(document).mousemove(function(e) {
				$('.floatWindow').css({
					top:e.pageY - $('.floatWindow').data('clickPointY') + 'px',
					left:e.pageX - $('.floatWindow').data('clickPointX') + 'px'
				});
			});
		}).mouseup(function() {
			$(document).unbind('mousemove');
		});
	};
	
	//PRIVATE BRAND
	if($('body#page-antiqulothes').size() > 0) {
		$('ul#pbModels li span').each(function() {
			$(this).hover(function() {
				$(this).stop(true,true).animate({
					opacity:0.8
				}, 'fast');
			},function() {
				$(this).stop(true,true).animate({
					opacity:1
				}, 'fast');
			});
		});
	};
});
