var isMSIE6 = $.browser.msie && $.browser.version == "6.0";
var isMSIE7 = $.browser.msie && $.browser.version == "7.0";
var isIPad = navigator.userAgent.match(/iPad/i);
var isIPhone = navigator.userAgent.match(/iPhone/i);
	
$(function() {
	//site search
	var action = "http://www.raito.co.jp/search/";
	$('form#mFFmQS').submit(function() {
		window.location = action + '?q='
			+ encodeURIComponent($(this).find('.mFFmSearch').val());
		return false;
	});
	
	// rollover
	$('#pageTop img, .pageTop img, #content .more img, .imgover').each(function() {
		var osrc = $(this).attr('src');
		var hsrc = osrc.replace(/(\.gif|\.jpg|\.png)/, '_o$1');
		$.data(this, 'osrc', osrc);
		$.data(this, 'hsrc', hsrc);
		$('<img>').attr('src', hsrc);
	}).hover(function() {
		$(this).attr('src', $.data(this, 'hsrc'));
	},function() {
		$(this).attr('src', $.data(this, 'osrc'));
	});

	// scroll
	$('a[href^=#], area[href^=#]').click(function() {
		var $t = $(this.hash);
		if (this.hash.length > 1 && $t.length) {
			$.scrollTo($t, 400);
			return false;
		}
	});

	//placeholder
	$('input.placeholder, textarea.placeholder').each(function() {
		if (!$(this).val() || $(this).val() == $(this).attr('title')) {
			$(this).val($(this).attr('title')).addClass('empty');
		}
	}).live('focus', function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('').removeClass('empty');
		}
	}).live('blur', function() {
		if (!$(this).val().length) {
			$(this).val($(this).attr('title')).addClass('empty');
		}
	}).parents('form').submit(function() {
		$(this).find('.placeholder').each(function() {
				if ($(this).val() == $(this).attr('title')) {
					$(this).val('');
				}
		});
	});

	//change txt size
	if(isMSIE6 || isMSIE7) {
		$('#mFFontManager').css({'display' : 'inline', 'zoom': 1});
	} else {
		$('#mFFontManager').css('display', 'inline-block');
	}
	
	var $alt_css = $('link[rel="alternate stylesheet"]');
	var setCss = function(title) {
		var $css = $alt_css.filter('[title=' + title + ']');
		if ($css.length) {
			$alt_css.prop('disabled', true);
			$css.prop('disabled', false);
			var date = new Date();
			date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
			$.cookie('css', title, {path: '/', expires: date});
		}
	};
	// default css
	setCss($.cookie('css'));
	$('#mFFontManager ul li a').unbind('click').click(function() {
		var fontsize = $(this).attr('href').replace(/^#/, '');
		setCss(fontsize);
		return false;
	});
	
	// global navigation
	$('div#gNav img').each(function() {
		if($(this).css('visibility') == 'hidden') {
			$(this).parents('li').css('zIndex', 99);
		}
	});
	
	$('div#gNav ul > li').each(function() {
		var osrc = $(this).css('zIndex');
		$.data(this, 'osrc', osrc);
	}).hover(function(){
		$(this).css('zIndex', 99);
	},function(){
		$(this).css('zIndex', $.data(this, 'osrc'));
	});
	
});


// makeGMap
function makeGMap(map, myLatlng, mapOptions, markers) {
		var _mapOptions = $.extend({
			zoom: 15,
			scrollwheel: false,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scaleControl: true
		}, mapOptions);
		
		var _mapObj = new google.maps.Map(map.get(0), _mapOptions);
		
		if(!markers.length) {
			var _marker = new google.maps.Marker({map: _mapObj, position: myLatlng, title: ""});
		}

		google.maps.event.addListener(_mapObj,'tilesloaded',function() {
			//$.data(this, 'mapWrap').css({'visibility': 'hidden', 'position': 'absolute', 'left' : '0', 'top' : '0'});
			/*
			var $sc = $('.shisyaMapCanvas');
			$sc.find('> div').css('zIndex', 1);
			$sc.find('> div > div:first-child').css('zIndex', 2);*/
		});

		for(var i = 0; i < markers.length; i++) {
			var _marker = new google.maps.Marker(
		        					$.extend({
										map: _mapObj,
										position: myLatlng,
										title: ""
									}, markers[i].settings));
			
			setMarkerListener(_mapObj,_marker,markers[i].infoWindow);
			//google.maps.event.trigger(_marker, 'click');
		}
		
		return _mapObj;
}

function setMarkerListener(mapObj, marker, infoWindowSettings) {
		var _infoWindow = new google.maps.InfoWindow($.extend({
								disableAutoPan: false,
								position: marker.getPosition()
							}, infoWindowSettings));

		google.maps.event.addListener(marker, 'click', function() {
			_infoWindow.open(mapObj, this);
			//_mapObj.panTo(myLatlng);
		});
}

function makeTab() {
	var $cTabArea = $('.cTabArea');
	
	$cTabArea.each(function() {	
		var $_cTabPhotoPanels = $(this).find('> .cTabPhotoPanels');
		var $_cTabPhotoPanelsLi = $_cTabPhotoPanels.find('> li');
		var $_cTabPhotoLen = $_cTabPhotoPanelsLi.length;
		
		var $_cTabPNNav = $(this).find('> .cTabPNNav');
		var $_cTabPNNavPrev = $_cTabPNNav.find('> .cTabPNNavPrev');
		var $_cTabPNNavNext = $_cTabPNNav.find('> .cTabPNNavNext');
		var $_cTabPNNavAnchor = $_cTabPNNav.find('> li a');
		
		var $_cTabNav = $(this).find('> .cTabNav');
		var $_cTabNavAnchor = $_cTabNav.find('> li a');
		var $_currentNum = 1;
		
		if($_cTabPhotoLen === 1) {
			$_cTabNav.hide();
			$_cTabPNNav.hide();
		}
		
		$_cTabPhotoPanelsLi.filter(':not(' + $_cTabNav.find('> li a.selected').attr('href') + ")").hide();
		_updateNav();
		
		/* tabNav */
		$_cTabNavAnchor.unbind('click').click(function() {
			$_currentNum = parseInt($(this).attr('href').replace('#' + $_cTabPhotoPanels.attr('id'), ''));
			_updateNav();
			
			return false;
		});
		
		/* prev next */
		$_cTabPNNavAnchor.unbind('click').click(function() {
			_countNum($(this).attr('href').replace('#', ''));
			return false;
		});
		
		function _updateNav() {
			_updatePN();
			$_cTabNavAnchor.removeClass('selected');
			$_cTabNav.find('> li:nth-child(' + $_currentNum +') a').addClass("selected");
			$_cTabPhotoPanelsLi.hide();
			$_cTabPhotoPanelsLi.filter(':nth-child(' + $_currentNum +')').show();
		}

		function _updatePN() {
			
			if($_cTabPhotoLen == 1) {
				$_cTabPNNavPrev.hide();
				$_cTabPNNavNext.hide();
				return;
			}
			
			if($_currentNum == 1) {
				$_cTabPNNavPrev.hide();
				$_cTabPNNavNext.show();
			} else if ($_currentNum == $_cTabPhotoLen) {
				$_cTabPNNavPrev.show();
				$_cTabPNNavNext.hide();
			} else {
				$_cTabPNNavPrev.show();
				$_cTabPNNavNext.show();
			}
		}
		
		function _countNum(dir) {
			if(dir === 'prev') {
				if($_currentNum <= 1) {
					$_currentNum = 1;
				} else {
					$_currentNum -= 1;
				}
			} else if (dir === 'next') {
				if($_currentNum >= $_cTabPhotoLen) {
					$_currentNum = $_cTabPhotoLen;
				} else {
					$_currentNum += 1;
				}
			}
			
			_updateNav();
		}
	});
}

