var img;
var preLoadImgs = new Array();
preLoadImgs.push("/resource/images/menu_feature_on.gif");
preLoadImgs.push("/resource/images/menu_topics_on.gif");
preLoadImgs.push("/resource/images/menu_inspire_on.gif");
preLoadImgs.push("/resource/images/menu_recommend_on.gif");
preLoadImgs.push("/resource/images/menu_about_on.gif");
preLoadImgs.push("/resource/images/menu_howto_on.gif");
preLoadImgs.push("/resource/images/menu_search_on.gif");
preLoadImgs.push("/resource/images/menu_entry_on.gif");
preLoadImgs.push("/resource/images/topics_entry_on.gif");
preLoadImgs.push("/resource/images/menu_inquery_on.gif");

preLoadImgs.push("/resource/images/block-icon-topics_week_hover.gif");
preLoadImgs.push("/resource/images/block-icon-topics_month_hover.gif");

preLoadImgs.push("/resource/images/howto-navi_a_on.gif");
preLoadImgs.push("/resource/images/howto-navi_b_on.gif");
preLoadImgs.push("/resource/images/howto-navi_c_on.gif");
preLoadImgs.push("/resource/images/howto-navi_d_on.gif");

for(var i in preLoadImgs){
	img = "preLoadImg" + i;
	document.img = new Image();
	document.img.src = preLoadImgs[i];
}

function spamMeasures(){
	var obj = getObj("link");
	obj.innerHTML = obj.innerHTML.replace(/([a-z0-9\w.-]+)＠([a-z0-9\w.-]+)/g, '<a href="mailto:$1@$2"><img src="/resource/images/main_contents-icon_mail.gif" width="123" height="20" alt="メールを送る" /></a>');
}
function getObj(id){
	return document.all && document.all(id) || document.getElementById && document.getElementById(id);
}

jQuery(window).ready(function(){
	jQuery('.menuBtn img').hover(function(){
		jQuery(this).attr('src',jQuery(this).attr('src').replace(/^(.+)_off(\.[a-z]+)$/, "$1_on$2"));
	}, function(){
		jQuery(this).attr('src',jQuery(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, "$1_off$2"));
	});

	var initialHeight;	/*--- for 'browsingHistory' & 'collaborativFiltering' ---*/
	if (jQuery.browser.msie && jQuery.browser.version <= 7) {	// for IE7 or older only
		initialHeight = '35px';
	} else {
		initialHeight = '15px';
	}

	jQuery('#browsingHistoryBtn').toggle(function(){
		jQuery('#browsingHistory-block').animate({
			width: '390px'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#browsingHistory-block div.inner').animate({
			height: '100%'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#browsingHistory-block').addClass('doublesize');
	},function(){
		jQuery('#browsingHistory-block').animate({
			width: '190px'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#browsingHistory-block div.inner').animate({
			height: initialHeight
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#browsingHistory-block').removeClass('doublesize');
	});

	jQuery('#collaborativFilteringBtn').toggle(function(){
		jQuery('#collaborativFiltering-block').animate({
			width: '390px'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#collaborativFiltering-block div.inner').animate({
			height: '100%'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#collaborativFiltering-block').addClass('doublesize');
	},function(){
		jQuery('#collaborativFiltering-block').animate({
			width: '190px'
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#collaborativFiltering-block div.inner').animate({
			height: initialHeight
			},
			500,
			'easeInOutCubic'
		);
		jQuery('#collaborativFiltering-block').removeClass('doublesize');
	});

	var domain = location.href.match(/^https?(:\/\/[a-zA-Z0-9.]+)\//i)[1];
	jQuery('a[href^=http]').not('[href*="'+domain+'"]').attr('target' ,'_blank');
	jQuery('a.targetBlank').attr('target' ,'_blank');

	jQuery('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = jQuery(this.hash);
			$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({ scrollTop: targetOffset }, 500, 'easeInOutCubic');
				return false;
			}
		}
	});

	jQuery('#closeButton').click(function () {
		window.close();
	});

/*
	jQuery('.jump').click(function () {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {  
            var $target = jQuery(this.hash);  
            $target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                jQuery('html,body').animate({ scrollTop: targetOffset }, 'slow', 'swing');
                return false;
            }
        }
	});
*/
	function replacePage() {
		jQuery('.pageNaviLink').click(function(){
			jQuery('#feature-content').append('<div id="feature-content-mask"><img src="/resource/images/loading.gif" width="32" height="32" alt="Loading..." /></div>');
			var targetURL = jQuery(this).attr('rel');
			jQuery('#feature-content').load(targetURL+' div#feature-content div.inner', function(){
				jQuery('a[href*=#]').click(function() {
					// ページトップへのスムーズスクロールを再定義
					if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
						var $target = jQuery(this.hash);
						$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
						if ($target.length) {
							var targetOffset = $target.offset().top;
							jQuery('html,body').animate({ scrollTop: targetOffset }, 500, 'easeInOutCubic');
							return false;
						}
					}
				});
				replacePage();
				jQuery('#feature-content-mask').remove();
			});
		});
	}
	replacePage();

	jQuery('.howto-naviBtn img').hover(function(){
		jQuery(this).attr('src',jQuery(this).attr('src').replace(/^(.+)_off(\.[a-z]+)$/, "$1_on$2"));
	}, function(){
		jQuery(this).attr('src',jQuery(this).attr('src').replace(/^(.+)_on(\.[a-z]+)$/, "$1_off$2"));
	});

	jQuery('#menuInSiteSearch').hover(function(){
		jQuery('#q').focus();

		jQuery.ajax({
			url: '/xml/searchQueryStrings.php?type=1',
			dataType: 'xml',
			success: function(xmlResponse) {
				var data = jQuery('query', xmlResponse).map(function() {
					return {
						value: jQuery('string', this).text()
					};
				}).get();
				jQuery('#q').autocomplete({
					source: data
				});
			}
		});
	});
	jQuery('#sa').click(function(){
		if (jQuery('#q').val()!='') {
			jQuery('#menuInSiteSearch form').submit();
		} else {
			jQuery('#q').focus();
		}
	});

	if (jQuery('#inquiriesTo').length) {
		jQuery('#inquiriesTo').html(jQuery('#inquiriesTo').html().replace(/([a-z0-9\w.-]+)＠([a-z0-9\w.-]+)/g, '<a href="mailto:$1@$2">$1＠$2</a>'));
	}

	jQuery('a[rel*=facebox]').facebox();
	jQuery('[rel^=lightbox]').lightBox();
	jQuery('#gallery a').lightBox();

	jQuery('.with-tooltip').simpletooltip();

	var currentUpdate;
	var prevUpdate;
	var nextUpdate;
	var maxRecentUpdateNum = jQuery('#carouselPrev').attr('rel');
	jQuery('#carouselPrev').click(function(){
		jQuery('#recentUpdatesLoading').css('display', 'block');
		currentUpdate = parseInt(jQuery('#carouselPrev').attr('rel'));
		prevUpdate = currentUpdate-1;
		nextUpdate = currentUpdate+1;
		if(prevUpdate==0) {
			prevUpdate = maxRecentUpdateNum;
		} else if(prevUpdate>maxRecentUpdateNum) {
			prevUpdate = 1;
		}
		if(nextUpdate==0) {
			nextUpdate = maxRecentUpdateNum;
		} else if(nextUpdate>maxRecentUpdateNum) {
			nextUpdate = 1;
		}
		jQuery.ajax({
			type: 'get',
			url: '/xml/recentUpdates.xml',
			cache: false,
			success: function(xml) {
				jQuery(xml).find('update').each(function() {
					if(jQuery(this).attr('id')==currentUpdate) {
					//
						jQuery('#recent-updates').prepend(jQuery(this).text());
						jQuery('#recent-updates ul:first').css('margin-top','-386px').animate({
							marginTop: "0"
						}, 'slow', 'swing', function() {
							jQuery('#recent-updates ul:last').remove();
						});
						return false;
					}
				});
				jQuery('#recentUpdatesLoading').css('display', 'none');
			}
		});
		jQuery('#carouselPrev').attr('rel', prevUpdate);
		jQuery('#carouselNext').attr('rel', nextUpdate);
	});
	jQuery('#carouselNext').click(function(){
		jQuery('#recentUpdatesLoading').css('display', 'block');
		currentUpdate = parseInt(jQuery('#carouselNext').attr('rel'));
		prevUpdate = currentUpdate-1;
		nextUpdate = currentUpdate+1;
		if(prevUpdate==0) {
			prevUpdate = maxRecentUpdateNum;
		} else if(prevUpdate>maxRecentUpdateNum) {
			prevUpdate = 1;
		}
		if(nextUpdate==0) {
			nextUpdate = maxRecentUpdateNum;
		} else if(nextUpdate>maxRecentUpdateNum) {
			nextUpdate = 1;
		}
		jQuery.ajax({
			type: 'get',
			url: '/xml/recentUpdates.xml',
			cache: false,
			success: function(xml) {
				jQuery(xml).find('update').each(function() {
					if(jQuery(this).attr('id')==currentUpdate) {
						jQuery('#recent-updates').append(jQuery(this).text());
						jQuery('#recent-updates ul:first').animate({
							marginTop: "-386px"
						}, 'slow', 'swing', function() {
							jQuery(this).remove();
						});
						return false;
					}
				});
				jQuery('#recentUpdatesLoading').css('display', 'none');
			}
		});
		jQuery('#carouselPrev').attr('rel', prevUpdate);
		jQuery('#carouselNext').attr('rel', nextUpdate);
	});

	var currentTopPageTopicsOffset = 11;
	jQuery('#moreTopicsBtn').click(function(){
//		if(jQuery(this).data('sticky-post-id')) var stickyPostId = jQuery(this).data('sticky-post-id');
//if(jQuery(this).data('sticky-post-id')) alert('exist');
//alert(jQuery(this).data('stickyPostId'));
var stickyPostId = '6613';

		jQuery('#moreTopicsLoading').css('display', 'inline');
		jQuery.ajax({
			type: 'get',
			url: '/xml/topPageMoreTopics.php',
			data: 'offset='+currentTopPageTopicsOffset+'&sticky_post_id='+stickyPostId,
			cache: false,
			success: function(xml) {
				var posLeft = jQuery('#moreTopicsBtn').css('left');
				var posTop = jQuery('#moreTopicsBtn').css('top');
				var moreTopics;
				if(jQuery(xml).find('count').text()>0) {
					jQuery(xml).find('entry').each(function() {

						jQuery('#moreTopics').before(jQuery(this).text());
					});
					currentTopPageTopicsOffset += 7;
				} else {
					jQuery('#moreTopicsBtn').text('もう無いっス').fadeOut('slow', function() {
						jQuery('#moreTopics').remove();
					});
				}
				jQuery('#simpleTooltip').remove();
				jQuery('#moreTopicsLoading').css('display', 'none');
			}
		});
	});

	var moreSameGenrecurrentOffset = 10;
	jQuery('#moreSameGenreBtn').click(function(){
		var relationXml = jQuery(this).attr('rel');
		var posX = jQuery(this).parent().css('left');
		var posY = jQuery(this).parent().css('top');
		jQuery('#moreSameGenreLoading').css('display', 'inline');
		jQuery.ajax({
			type: 'get',
			url: relationXml,
			cache: false,
			success: function(xml) {
				if(jQuery(xml).find('conut').text()>0) {
					var ctr = 1;
					jQuery(xml).find('entry').each(function() {
						if(ctr>moreSameGenrecurrentOffset) {
							var addBlock = '<div class="block entry preview" style="left:'+posX+';top:'+posY+';">'+jQuery(this).text()+'</div>';
							jQuery('#moreSameGenre').before(jQuery(addBlock));
						}
						ctr += 1;
						if(ctr>jQuery(xml).find('conut').text()) {
							jQuery('#moreSameGenre').fadeOut('slow', function() {
								jQuery('#moreSameGenre').remove();
							});
							return false;
						}
						if(ctr>moreSameGenrecurrentOffset+10) return false;
					});
					moreSameGenrecurrentOffset += 10;
				}
				jQuery('#simpleTooltip').remove();
				jQuery('#moreSameGenreLoading').css('display', 'none');
			}
		});
	});
})

