﻿//============================================
//
// resident.js
// 物件サイト用
// Last Update 2011-11-24
//
//============================================

$(function(){
	
	// iframe hight
	//---------------
	$('iframe').load(function(){
		var $t = $(this);
		if (typeof $t.attr('height') == 'undefined') {
			if($t.attr('src').match(document.domain)){
				$t.height(this.contentWindow.document.documentElement.scrollHeight);
			}
		}
	});
	$('iframe').triggerHandler('load');
	
	// sumai
	//---------------
	$('#mailmag').click(function(){
		window.open('/mailreg/','blank');
	});
	$('#favorite').click(function(){
		window.open('/favorite/','blank');
	});
	
	// for juko table
	//---------------
	if(document.getElementById('articleListLead')){
		$('table.summaryTable').addClass('dwellingTable');
		$('table.summaryTable').removeClass('summaryTable');
		$('.dwellingTable td').each(function(){
			var $t = $(this);
			if($t.prop('colSpan')>1){
				$t.addClass('txtL');
			}
			else if($t.text().indexOf('円')!= -1 || $t.text().indexOf('m2')!= -1){
				$t.addClass('txtR');
			}

		});
	}
	
	
	// footerNav
	//---------------
	$('#footer .footerNav li:last').css('background-image','none');
	
	
	// Read XML
	//---------------
	
	// History
	(function(){
		var articleID = location.pathname.split('/', 3)[2];
		if(document.getElementById('readHistory')){
			$.ajax({
				url:'/form/history',
				dataType:'xml',
				type: 'GET',
				data:{'disUidcd':articleID, 'rand':Math.random()},
				cache:false,
				success:function(e){
					if($('data',e).length != 0){
						$('#readHistory').fadeIn();
						$('data',e).each(function(t){
							var $thumb = $("thumbnailUrl",this).text(),
									$name = $("name",this).text(),
									$url = $("url",this).text(),
									$bukkenID = $("id",this).text();
							$('ul#mycarousel').append('<li><a href="'+$url+'?b_history='+$bukkenID+':click" target="_blank"><img src="'+$thumb+'" /></a><span><a href="'+$url+'?b_history='+$bukkenID+':click" target="_blank">'+$name+'</a></span></li>');
							//$('ul#mycarousel').append('<li><a href="'+$url+'" target="_blank" onClick="click_History(\''+$bukkenID+'\');"><img src="'+$thumb+'" onload="view_History(\''+$bukkenID+'\');" /></a><span><a href="'+$url+'" target="_blank" onClick="click_History(\''+$bukkenID+'\');">'+$name+'</a></span></li>');
						});
						$('#mycarousel').jcarousel({scroll: 1, visible: 4});
					}
				},
				error: function(){
					$('#readHistory').hide();
				}
			});
		};
		
		// Reccomend
		if(document.getElementById('interest')){
			var interest = [];
			$.ajax({
				url:'/form/recommend',
				dataType:'xml',
				type: 'GET',
				data:{'UIDCD':articleID, 'rand':Math.random()},
				cache:false,
				success:function(e){
					if($('data',e).length != 0){
						$('#interest').fadeIn();
						$('data',e).each(function(t){
							var $name = $("name",this).text(),
									$url = $("url",this).text(),
									$bukkenID = $("id",this).text();
							interest.push('<li><a href="'+$url+'?d_recommend='+$bukkenID+':click" target="_blank">'+$name+'</a></li>');
							//interest.push('<li><a href="'+$url+'" target="_blank" onClick="click_Recomend(\''+$bukkenID+'\');">'+$name+'</a><img src="/common/resident/images/s_code_beacon.gif" onload="view_Recomend(\''+$bukkenID+'\');" /></li>');
						});
						for(var i=0; i<5; i++){
							$('#docRequest').append(interest[i]);
						}
						$('.recomInner').autoHeight({column:2});
					}
				},
				error: function(){
					$('#interest').hide();
				}
			});
		}
	})();
	
	
	// Execution Custom Method and Plugins
	//-------------------------------------------------------
	$('.column2 > .columnBox > .inner').autoHeight({column:2});
	$('.recomInner').autoHeight({column:2});
	$('.conversionSection .subSection .inner').autoHeight({column:3});
	$("a[href*='#']").slideScroll();
	$("img.rollover").rollover();
	
});


jQuery.event.add(window, "load", function(){
// QR Code [Article Outline Page]
	//---------------
	if(document.getElementById('qrImg')){
		var $t = $('#qrCode img#qrImg'),
				$p = $('#qrCode'),
				$qrRemove = function(){
					$p.remove();
					$('#outlineIntro #articleSummary h1').css('width','auto');
					$('#outlineIntro #articleSummary h1 span').css('width','520px');
					$('#outlineIntro #articleSummary p').css('width','auto');
					return true;
				};
		if($.browser.msie && parseInt($.browser.version)==8){
			var tt = document.getElementById('qrImg');
			if(tt.readyState != 'complete'){
				$qrRemove();
			}
		}else if($.browser.msie && parseInt($.browser.version)==9){
			var tt = document.getElementById('qrImg');
			if(tt.fileSize == '-1'){
				$qrRemove();
			}
		}else if($._isIE(6) || $._isIE(7)){
			if(!$t.attr('complete')){
				$qrRemove();
			}
		}else{
			$t.error(function(){
				$qrRemove();
			});
			if($t.height()<25){// Firefox
				$qrRemove();
			}
		}
	}
	
	if($('#readHistory').css('display')=='none' && $('#subRecommend').css('display')=='none'){
		$('#recommend').hide();
	}
	
});

(function($){
	
	//**********************************
	// Custom jQuery Function
	//**********************************
	
	//----------------------------------
	// IE Version Distinction
	//
	// IE6 : $._isIE(6)
	// IE7 : $._isIE(7)
	// IE8 : $._isIE(8)
	// IE9 : $._isIE(9)
	// IE  : $._isIE()
	//----------------------------------
	$._isIE = function(){
		var version = arguments.length >= 1 ? parseInt(arguments[0]) : '';
		switch (version){
			case 6:
				return !$.support.opacity && 
					!$.support.style && 
						typeof document.documentElement.style.maxHeight == "undefined" ;
				break;
			case 7:
				return !$.support.opacity &&
					!$.support.style &&
						typeof document.documentElement.style.maxHeight != "undefined" ;
				break;
			case 8:
				//return !$.support.opacity && $.support.style;
				return !$.support.opacity && $.support.hrefNormalized;
				break;
			case 9:
				return !$.support.noCloneEvent && $.support.opacity;
			default :
				return !$.support.opacity;
		}
		return false;
	};
	
	$._getMaxWidth = function(options){
		var op = $.extend({
			act : $('table')
		},options || {});
		
		var maxValue = 0;
		if(op.act.length >= 1){
			op.act.each(function(){
				if(maxValue < $(this).outerWidth()){
					maxValue = $(this).outerWidth();
				}
			});
			return maxValue;
		}else{
			return maxValue;
		}
	};
	
	
	//**********************************
	// Original Methods
	//**********************************
	$.fn.extend({
		
		rollover : function(){
			$(this).mouseover(function(){
				if(!$(this).attr("src").match(/^(.+)_on(\.[a-z]+)$/)){
					$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"));
				}
			}).mouseout(function(){
				$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/, "$1$2"));
			}).each(function(){
				$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))
			});
		}
	
	});//END $.fn.extend
	
	
//**********************************
// Plugins
//**********************************
/*---------------------------------------------------------------
jquery-auto-height.js

Copyright (c) 2010 Tomohiro Okuwaki (http://www.tinybeans.net/blog/)
Licensed under MIT Lisence:
http://www.opensource.org/licenses/mit-license.php
http://sourceforge.jp/projects/opensource/wiki/licenses%2FMIT_license

Since:   2010-04-19
Update:  2010-07-02
version: 0.03
Comment: 

jQuery 1.2 later
 ----------------------------------------------------------------*/
$.fn.autoHeight = function(options){
	var op = $.extend({
		column  : 0,
		clear   : 1,
		height  : 'min-height', // or [height]
		reset   : '', // height reset
		descend : function descend (a,b){ return b-a; }
	},options || {});
	
	var self = $(this);
	var n = 0,
		hMax,
		hList = new Array(),
		hListLine = new Array();
		hListLine[n] = 0;
		
	// get elements height
	self.each(function(i){
		if (op.reset == 'reset'){
			//$(this).removeAttr('style');
			$(this).css(op.height,'0');
			if($._isIE(6)){$(this).css('height','0');}
		}
		var h = $(this).outerHeight();
		hList[i] = h;
		if (op.column > 1){
			if (h > hListLine[n]){
				hListLine[n] = h;
			}
			if ((i > 0) && (((i+1) % op.column) == 0)){
				n++;
				hListLine[n] = 0;
			};
		}
	});
	

	hList = hList.sort(op.descend);
	hMax = hList[0];
	
	var browser = $.browser.version;
	if (op.column > 1){
		for (var j=0; j<hListLine.length; j++){
			for (var k=0; k<op.column; k++){
				if (browser == '6.0'){
					self.eq(j*op.column+k).height(hListLine[j]);
					if (k == 0 && op.clear != 0) self.eq(j*op.column+k).css('clear','both');
				} else {
					self.eq(j*op.column+k).css(op.height,hListLine[j]);
					if (k == 0 && op.clear != 0) self.eq(j*op.column+k).css('clear','both');
				}
			}
		}
	} else {
		if (browser == '6.0'){
			self.height(hMax);
		} else {
			self.css(op.height,hMax);
		}
	}
	
};



/*---------------------------------------------------------------
jQuery.slideScroll.js

jQuery required (tested on version 1.2.6)
encoding UTF-8

Copyright (c) 2008 nori (norimania@gmail.com)
http://moto-mono.net
Licensed under the MIT

$Update: 2008-12-24 20:00
$Date: 2008-12-22 23:30
 ----------------------------------------------------------------*/
$.fn.slideScroll = function(options){
	
	var c = $.extend({
		interval: 20,
		easing: 0.6, // 0.4 ~ 2.0 
		comeLink: false
	},options);
	var d = document;
	
	var timer;
	var pos;
	
	function currentPoint(){
		var current = {
			x: d.body.scrollLeft || d.documentElement.scrollLeft,
			y: d.body.scrollTop || d.documentElement.scrollTop
		}
		return current;
	}
	
	function setPoint(){
		var h = d.documentElement.clientHeight;
		var w = d.documentElement.clientWidth;
		var maxH = d.documentElement.scrollHeight;
		var maxW = d.documentElement.scrollWidth;
		
		pos.top = ((maxH-h)<pos.top && pos.top<maxH) ? maxH-h : pos.top;
		pos.left = ((maxW-w)<pos.left && pos.left<maxW) ? maxW-w : pos.left;
	}
	
	function nextPoint(){
		var x = currentPoint().x;
		var y = currentPoint().y;
		var sx = Math.ceil((x - pos.left)/(5*c.easing));
		var sy = Math.ceil((y - pos.top)/(5*c.easing));
		var next = {
			x: x - sx,
			y: y - sy,
			ax: sx,
			ay: sy
		}
		return next;
	}
	
	function scroll(){
		timer = setInterval(function(){
			nextPoint();
			
			if(Math.abs(nextPoint().ax)<1 && Math.abs(nextPoint().ay)<1){
				clearInterval(timer);
				window.scroll(pos.left,pos.top);
			}
			window.scroll(nextPoint().x,nextPoint().y);
		},c.interval);
	}
	
	function comeLink(){
		if(location.hash){
			if($(location.hash) && $(location.hash).length>0){
				pos = $(location.hash).offset();
				setPoint();
				window.scroll(0,0);
				if($.browser.msie){
					setTimeout(function(){
						scroll();
					},50);
				}else{
					scroll();
				}
			}
		}
	}
	if(c.comeLink) comeLink();
	
	$(this).each(function(){
		if(this.hash && $(this.hash).length>0 
			&& this.href.match(new RegExp(location.href.split("#")[0]))){
			var hash = this.hash;
			$(this).click(function(){
				pos = $(hash).offset();
				clearInterval(timer);
				setPoint();
				scroll();
				return false;
			});
		}
	});
};



})(jQuery);



// for Debug
//============================
if (!('console' in window)){
		window.console = {};
		window.console.log = function(str){
				return str;
		};
}
//============================
