/*
	Leonardo Rossetti (motw.leo@gmail.com)

	* jQuery Floatbox Plugin 1.0.4
	* Copyright (c) 2008 Leonardo Rossetti (motw.leo@gmail.com)
	* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
	* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	* THE SOFTWARE.
/*
----------------------------------------------------------------------------------------
XMLサンプル 書式
----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<album><id></id>
<pos>w</pos>//縦位置横位置設定 w:横、h：縦
<date>2009.6.10</date>
<cat>四季のうつろい</cat>
<title>楽水園の紅葉</title>
<entry>当社の歴史は、およそ1,800年以上前に遡り、全国に2,129社ある住吉神社の中でも、その最初の神社と云われております。当社では、古より25年ごとに式年遷宮を行っており、1119年、御遷宮についての記述が「中右記」に見え、 近年においても、天保、明治の御造替に続き、昭和10年・35年・60年と厳粛盛大に御遷宮の儀が斎行されました。</entry>
<foto><p><img alt="楽水園の紅葉" src="images/foto_w.jpg" /></p></foto>
<previous><![CDATA[<a href="p01.xml">前の写真</a>]]></previous>
<next><![CDATA[<a href="p01.xml">次の写真</a>]]></next>
</album>
----------------------------------------------------------------------------------------
HTMLサンプル
----------------------------------------------------------------------------------------
<div id="fPos" class="">
<div id='fotoHead'>
<p class="fDate">2009.6.10</p>
<p class="fCat">四季のうつろい</p>
<h2 id="xTitle">楽水園の紅葉</h2>
<p id="xEntry">当社の歴史は、およそ1,800年以上前に遡り、全国に2,129社ある住吉神社の中でも、その最初の神社と云われております。当社では、古より25年ごとに式年遷宮を行っており、1119年、御遷宮についての記述が「中右記」に見え、 近年においても、天保、明治の御造替に続き、昭和10年・35年・60年と厳粛盛大に御遷宮の儀が斎行されました。</p>
<!-- end [id="fotoHead"]--></div>
<div id="Foto">
<div id="FotoIMG">
<p><img src="images/foto_w.jpg" alt="楽水園の紅葉" /></p>
</div>
<div id="blockFotoNavi">
<ul class="arrow">
<li class="bPrev" id="xPrev"><a href="prev.html">前の写真</a></li>
<li class="bNext" id="xNext"><a href="next.html">次の写真</a></li>
</ul>
<p id="returnTop"><a href="#">Close</a></p>
<!-- end blockFotoNavi--></div>
<!-- end [id="Foto"]--></div>
<!-- end ["w/h"]--></div>
*/

(function ($) {
var pagerFlag= false;
  $.floatbox = function (options) {
		var settings = $.extend({
			bg:"floatBg",
			box:"floatBox",
			ajax:null,
			content : ""
		}, options);
		
		var showBox = function(){
			if (typeof document.body.style.maxHeight != "undefined") {
			} else {
				if (navigator.userAgent.match(/AppleWebKit\/\d.+Safari\/\d.+/)) {
					var scrollTop = document.body.scrollTop;
				} else {
					var scrollTop = document.documentElement.scrollTop;
				}
				var topIE6 = (document.documentElement.clientHeight - $('#floatBox').height()) / 2 + scrollTop;
			} 
			
			var content = typeof settings.content === "string" ? settings.content : settings.content.clone();
			if(!pagerFlag){
				$("<div id='floatBg'></div>")
					.bind("click", function(){closeBox();})
					.css({opacity:"0.9"})
					.height($('#wrap').height())
					.width($('#wrap').width())
					.appendTo("body");
			}
			$("<div id='floatBox'></div>")
				.appendTo("body")
				.append('<div id="fPos"><div id="fotoHead"><p class="fDate"></p><p class="fCat"></p><h2 id="xTitle"></h2><p id="xEntry"></p></div><div id="Foto"><div id="FotoIMG"></div><div id="blockFotoNavi"><ul class="arrow"><li class="bPrev" id="xPrev"></li><li class="bNext" id="xNext"></li></ul><p id="returnTop"><a href="#">Close</a></p></div></div></div>')
				.append('<img id="loading" src="images/ajax-loader.gif" alt="loding" />')
				//クローズボタン
				.find("#returnTop > a").bind("click", function () {closeBox();return false})
				.hide()
				.end();
			$("#" + settings.bg).fadeIn(200, function(){
				$("#" + settings.box).css({top:topIE6}).fadeIn(200);
				if(!pagerFlag){
					closeOverLayer();
				}
			});
		}
//XMLロード
			$.ajax({
                    url: settings.ajax.url,
                    cache: false,
                    success: function(xml){
						
						$('img#loading').fadeOut(200, function(){$(this).remove();});
						$('#returnTop > a').fadeIn(200);
						 //ポジション設定
						$(xml).find('pos').each(function(){
							var entry_pos = $(this).text();
							$("#floatBox").addClass(entry_pos);//縦、横位置クラス設定
							$("#fPos").addClass(entry_pos);
							
						});
						 //日付設定
						$(xml).find('date').each(function(){
							var entry_date = $(this).text();
							$(".fDate").html(entry_date).hide().fadeIn(200);
						});
						 //カテゴリー設定
						$(xml).find('cat').each(function(){
							var entry_cat = $(this).text();
							$(".fCat").html(entry_cat).hide().fadeIn(200);
						});
                        //タイトル設定
						$(xml).find('ftitle').each(function(){
							var title_text = $(this).text();
							document.title =  title_text+" ：　【公式】筑前一之宮 住吉神社 | 住吉アルバム";
							$("#xTitle").html(title_text).hide().fadeIn(200);
					   });
						 //本文設定
						$(xml).find('entry').each(function(){
							var entry_text = $(this).text();
							$("#xEntry").html(entry_text).hide().fadeIn(200);
						});
						 //写真設定
						$(xml).find('p').children('img').each(function(){
						var getImg = $(this).attr('src');
						$("#FotoIMG").append('<p><img src="'+getImg+'" /></p>').hide().fadeIn(200);
						});
						//ナビゲーション
						$(xml).find('previous').each(function(){
								var prev_text = $(this).text();
								if(prev_text == ""){$("#xPrev").hide();}
								else{$("#xPrev").html(prev_text).hide().fadeIn(200);pagerFunc("#xPrev > a");}
							});
						$(xml).find('next').each(function(){
								var next_text = $(this).text();
								if(next_text == ""){$("#xNext").hide();}
								else{$("#xNext").html(next_text).hide().fadeIn(200);pagerFunc("#xNext > a");}
							});
                    },
					complete: function(){
						//alert('complete!')
						}
					
                });
		
		var closeBox = function(){
			pagerFlag = false;
			$("#" + settings.box).fadeOut(200, function () {
				$("#" + settings.bg).fadeOut(200, function () {
					$("#" + settings.box).remove();
					$("#" + settings.bg).remove();
					document.title =  "【公式】筑前一之宮 住吉神社 | 住吉アルバム";
				});
			});
		}
		
		var closeOverLayer = function(){
			$("#floatBg").hover(function(){tooltipFlag = true;},function(){tooltipFlag = false;})
		}
		
		
//ページリンク系
		var pagerFunc = function(target){
			$(target).click(function(){
				var getURL = $(target).attr("href");
				//alert(getURL);
				$('#floatBox').fadeOut(200, function(){
					$('#floatBox').remove();
					pagerFlag = true;
					$.floatbox({ajax:{url:getURL}});
				});			
				return false;
			});
		}
		//inits the floatbox
		var init = function () {
			//shows box
			showBox();
			//adds cross browser event to esc key to hide floating box
			$(document).one("keypress", function (e) {
				var escKey = $.browser.mozilla ? 0 : 27;
				if(e.which === escKey){closeBox();}
			})
			.one("keydown", function (e) {
				var escKey = $.browser.mozilla ? 0 : 27;
				if (e.which === escKey){closeBox();}
			});
			//if msie6, adds event to browser scroll to keep floatbox ina fixed position and uses css hack for full background size
			if($.browser.msie){
				$("body, html").css({height: "100%", width: "100%"});
				$(window).bind("scroll", function () {
					$("#" + settings.bg).css("top", "0px");
				});
			}
			$(window).resize(function(){
   			$("#" + settings.bg).height($("#wrap").height());
				$("#" + settings.bg).width($("#wrap").width());
			});
		};
			//starts the plugin
		init();
  };
})(jQuery);




