if (typeof Ynn == 'undefined') Ynn = {};

$(function() {
		if (Ynn.setup) Ynn.setup();
		if ($("#introductionMoreLink")) {
			$("#introductionMoreLink").click(Ynn.displayIntroduction);
		}
		
		if ($("#introductionBackLink")) {
			$("#introductionBackLink").click(Ynn.backIntroduction);
		}
		
	}
);

Ynn.inputTextKeyword = function (id, defaultText) {
    var obj = $(id)[0];
    if (obj == undefined) {
    	return;
    }
    
    if (obj.value == defaultText) {
        obj.style.color = "#aaa";
    } else {
        obj.style.color = '#000';
    }
    
    $(window).load(function() {
        var val = obj.value;
        if (val == null || val == '') {
            obj.style.color = '#aaa';
            obj.value = defaultText;
        }
    });

    $(id).focus(function() {
        if (obj.value == defaultText) {
            obj.style.color = '#000';
            obj.value = '';
        }
    });
    
    $(id).blur(function() {
        if (obj.value == '') {
            obj.style.color = '#aaa';
            obj.value = defaultText;
        }
    });

    var form = obj;
    while (true) {
        form = form.parentNode;
        if (form.tagName.toLowerCase() == 'form') {
            break;
        }
    }
    
    $(".search_form").each(function() {
    		$(this).submit(function() {
    	        if (obj.value == defaultText) {
    	            obj.value = '';
    	        }
    	        if (obj.value == '') {
    	        	return false;
    	        }
    		});
    	}
    );
}

Ynn.displayIntroduction = function() {
	$("#introduction").hide();
	$("#introductionLong").show();
	return false;
}

Ynn.backIntroduction = function() {
	$("#introductionLong").hide();
	$("#introduction").show();
	return false;
}


if (typeof Ynn.Property == 'undefined') Ynn.Property = {};

Ynn.Property.Map = (function () {
	var map = null;
	var pin = null;
	var currentLatLng = null;
	var currentYaw = null;
	var manicon = null;
	var fovMarker = null;
	
	return {

		setup: function(lat, lng, fromAddress) {
			if (!(lat > 0 && lng > 0) && fromAddress == undefined) {

				var geocoder = new GClientGeocoder();
				geocoder.getLatLng(address,
						function (point) {
							if (!point) {
								$("googleMap").html("ごめんなさい。地図は表示できません。");
								return;
							} else {
								Ynn.Property.Map.setup(point.lat(), point.lng(), true);
								return;
							}
						}
				);
				
				return;
			}
			
			if (!(lat > 0 && lng > 0)) {
				$("googleMap").html("ごめんなさい。地図は表示できません。");
				return;
			}
			
			var start = {
				latlng: new GLatLng(lat, lng)
			}
			currentLatLng = start.latlng;
			
			map = new GMap2($('#googleMap')[0]);
			map.disableInfoWindow();
			map.enableContinuousZoom();
			map.setCenter(start.latlng, 14);
			map.addControl(new GSmallMapControl);
			$(window).unload(GUnload);
												
			if (fromAddress == undefined) {
				var icon = new GIcon();
				if (certified == undefined || certified != 1) {
					icon.image = '/img/pin.png';
					icon.iconSize = new GSize(27, 36);
					icon.shadow = '/img/shadow-pin.png';
					icon.shadowSize = new GSize(46, 36);
					icon.iconAnchor = new GPoint(13, 18);
				} else {
					icon.image = '/img/pin47.png';
					icon.iconSize = new GSize(27, 44);
					icon.shadow = '/img/shadow-pin47.png';
					icon.shadowSize = new GSize(50, 44);					
					icon.iconAnchor = new GPoint(13, 22);					
				}
				
				pin = new GMarker(start.latlng, icon);
				map.addOverlay(pin);
			}
		},
		setStreetViewPin: function(latlng) {
			currentLatLng = latlng;
			manicon = new GIcon(G_DEFAULT_ICON);
			manicon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
			manicon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
			manicon.iconSize = new GSize(49, 52);
			manicon.iconAnchor = new GPoint(25, 35);
			manicon.infoWindowAnchor = new GPoint(25, 25);
			manicon.imageMap = [
			26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
			16,20, 16,14, 19,13, 22,8
			];
			
			Ynn.Property.Map.placeFovMarker();			

			GEvent.addListener(map, "click", function(overlay, point) {
					Ynn.Property.StreetView.move(point);
					map.setCenter(point);
					return;
				}
			);
//			svOverlay = new GStreetviewOverlay();
//			map.addOverlay(svOverlay);
			
			return
		},
		handleInitialized: function(location) {
			map.setCenter(location.latlng);
			currentLatLng = location.latlng;
			Ynn.Property.Map.placeFovMarker();
			return;
		},
		handleYawChange: function(yaw) {
			Ynn.Property.Map.placeFovMarker();
			return;
		},
		placeFovMarker: function() {
			if (fovMarker != undefined) {
				map.removeOverlay(fovMarker);
			}
			fovMarker = new GMarker(currentLatLng, {icon:manicon, clickable: false});
			map.addOverlay(fovMarker);
			if ($("#panoflash2")) {
				$("#panoflash1").hide();
			}

			return;
		}
	}
})();

Ynn.Property.StreetView = (function() {
	var start = null;
	var streetView = null;
	
	return {
		setup: function(lat, lng) {
			start = {
					latlng: new GLatLng(lat, lng)
			}
			client = new GStreetviewClient();
			client.getNearestPanoramaLatLng(start.latlng, 
					function(latlng){
						if (latlng == null) {
							return Ynn.Property.StreetView.onError(600);
						}
						
						var yaw = Ynn.Property.StreetView.calcYaw(latlng, start.latlng);
						if (yaw < 0) {
							yaw = 0;
						}
						
						var pov = {
							yaw: yaw
						};
						streetView = new GStreetviewPanorama(document.getElementById("streetView"));						
						streetView.setLocationAndPOV(latlng, pov);
						
						Ynn.Property.Map.setStreetViewPin(latlng);
						GEvent.addListener(streetView, "initialized", Ynn.Property.Map.handleInitialized);
						GEvent.addListener(streetView, "yawchanged", Ynn.Property.Map.handleYawChange);
						GEvent.addListener(streetView, "error", Ynn.Property.StreetView.handleNoView);
						return;
			});
		},
		
		calcYaw: function(fromLatLng, toLatLng) {
			var yaw = 90 - Math.atan2(
							toLatLng.lat() - fromLatLng.lat(),
							toLatLng.lng() - fromLatLng.lng()
			) * 180 / Math.PI;
			if (yaw < 0) {
				yaw += 360;
			}
			return yaw;
		},
		
		handleNoView: function(error_code) {
			if (error_code == 600) {
				//$("#streetView").html("Googleストリートビュー未対応地域です。");
				return;
			}
		},
		
		onError: function(error_code) {
			if (error_code == 600 || error_code == 603) {
				//$("#googleMap").height($("#googleService").height());
				$("#googleMap").height(370);
				$("#streetView").hide();
				Ynn.Property.Map.setup(start.latlng.lat(), start.latlng.lng());
				return;
			}
		},
		move: function(latLng) {
			streetView.setLocationAndPOV(latLng);
		}
	}
})();

Ynn.Flavour = function(element) {
	this.element = (element && element.jquery ? element : $(element)) || $('body');
};

Ynn.Flavour.prototype = {
	stripe: function(element) {
		if (element)
			_stripe(element);
		else {
			_stripe(this.element.find('table.striped'));
			_stripe(this.element.find('ul.striped'));
		}
		return this;

		function _stripe(element) {
			if (!element.length) return;
			var tag;
			switch (element[0].tagName.toLowerCase()) {
			case 'table':
				tag = 'tr';
				break;
			case 'ul':
				tag = 'li';
				break;
			}	
			if (!tag) return;
			element
				.find(tag+':even').removeClass('even').addClass('odd').end()
				.find(tag+':odd').removeClass('odd').addClass('even');
		}
	}
};

(function($) {
	$.fn.roll = function(o) {
		o = o || '_o';
		return this.each(function() {
			var src = this.src;
			if (!src) return;
			var self = $(this);
			var out  = src;
			var over = src.replace(/(\.[^.]+$)/, o+'$1');
			$('<img src="'+over+'"/>').load(function() {
				self.hover(
					function() { this.src = over; },
					function() { this.src = out; }
				);
			});
		});
	};

	$.fn.external = function() {
		return this.each(function() { this.target = '_blank'; });
	};

	$.fn.fl = function(o) {
		if (typeof arguments[0] == 'string') {
			var method = arguments[0];
			var args   = $.makeArray(arguments).slice(1);

			var id = this.attr('id');
			var fl = $.browser.msie ? window[id] : document[id];

			if (fl && typeof fl[method] == 'function') {
				var params = [];
				$.each(args, function(i) { params.push('_'+i); });

				var p = params.join();
				Function(p, 'this('+p+')').apply(fl[method], args);
			}
		}
		else {
			o = $.extend({
				version: '9.0.47',
				params : {
					wmode: 'transparent'
				}
			}, o);
			if (swfobject.hasFlashPlayerVersion(o.version)) {
				o.attrs = $.extend(o.attrs || {}, {
					data  : o.src,
					width : o.width,
					height: o.height
				});
				if (o.flashvars)
					o.params.flashvars = $.param(o.flashvars);

				var element = o.id ?
					$('<div id="'+o.id+'"/>').appendTo(this) :
					this;
				swfobject.createSWF(
					o.attrs,
					o.params,
					element.attr('id')
				);
			}
		}
		return this;
	};
})(jQuery);

$(function() {
	new Ynn.Flavour().stripe();
});
