/*
	@interface.js
	@author k
	@dateCreated 2009/08/28 
	@dateModified 2010/10/28 
	@description インターフェース、レイアウト、ビヘイビア
	include jQuery
*/

$(function(){

/**
* ドラッグ+ソート
* with:jquery-ui-1.8.2.custom.min.js
***/
	//$(".main_frame").sortable({
	$("#sub1,#under_main").sortable({
		items: '.sortable',
		handle: '.dheader',
		revert: false, // animation
		placeholder: 'sortable_placeholder',
		forcePlaceholderSize: true,
		distance: 3,
		update: function(event, ui) {
			var result = $(event.target).sortable('toArray');
			var id = event.target.id;
			$.post("_changelayout",{area: id, parts: result, url: location.href });
		}
	});
	//opera 機能off
	if(window.opera){
		$('.dheader').removeClass('dheader')
	}

/**
* showHideインターフェース
***/	
	//showHide ターゲット+トリガー セット
	$('.sortable > .sheader').next('div').addClass('showHide-Target');
	$('.sortable > .sheader').after('<span class="showHide-Trigger">+/-</span>');
	
	//showHide toggleアイコン
	var tglIcon={
		hide:"/img/common/leftBtn.gif",
		show:"/img/common/downBtn.gif"
	};
	
	//hide要素クッキー保持
	var closeElements;
	var cookie = $.cookie("closeElm");
	if(cookie){
		closeElements = cookie.split(':');
	}else{
		closeElements =[];
	}

	//hide要素チェック、状態表示
	$('div.showHide-Target').each(function(){
		var i;
		for(i = closeElements.length - 1; i >= 0; i--){
			if(closeElements[i] == $(this).parent().get(0).id){
				$(this).hide();
				$(this).prev('span.showHide-Trigger').css("background-image","url("+tglIcon.hide+")");
				break;
			}
		}
	});
	
	//showHide切り替え click event
	$('span.showHide-Trigger').each(function(){
		$(this).click(function(){
			var i;
			for(i = closeElements.length - 1; i >= 0; i--){
				if(closeElements[i] == $(this).parent().get(0).id){
					break;
				}
			}
			if(i >= 0){
				// 表示
				closeElements.splice(i,1);
				$(this).next('div.showHide-Target').slideToggle('fast');
			    $(this).css("background-image","url("+tglIcon.show+")");
			}else{
				// 非表示
				closeElements.push($(this).parent().get(0).id);
				$(this).next('div.showHide-Target').slideToggle('fast',
					function(){
						var id=$(this).parent().get(0).id;
						var result = $(this).parent().parent().sortable('toArray');
						//console.log(result);
						$.post("_changelayout",{area: id, parts: result, url: location.href });
					});
				/*
				closeElements.push($(this).parent().get(0).id);
				$(this).parent().parent().append('<div id="tmp_moveto" style="height: 17px;"></div>');
				$(this).next('div.showHide-Target').toggle('blind',{},400,function(){
					$(this).parent().effect('transfer',{ to: "#tmp_moveto", className: 'transfer_placeholder' },600,
						function(){
							$("#tmp_moveto").remove();
							$(this).parent().append($(this));
							var result = $(this).parent().sortable('toArray');
							var id = $(this).parent().get(0).id;
							$.post("_changelayout",{area: id, parts: result, url: location.href });
						});
				});
				*/
			    $(this).css("background-image","url("+tglIcon.hide+")");
			}
			$.cookie("closeElm",closeElements.join(':'),{path: '/', expires: 1000});
			
		});
	});
	
/**
* fancybox
* with:jquery.fancybox-1.3.1.pack.js
***/
	$('a.fancypopup').fancybox();

	//fancybox発動
	$('.fancypopup').click(function () {
		//右クリック禁止
//		$(document).bind("contextmenu",function(e){
//			return false;
//		});
	}).fancybox();

	//右クリック禁止
//	$('img').bind("contextmenu",function(e){
//		return false;
//	});
/**
* popup window
* usage :<a href="url" rel="popup_600_500" title="page">link</a>
***/
	$('a[rel^=popup]').bind('click',function(){
		var elm=this;
		var w=elm.rel.split("_")[1] || 500,
			h=elm.rel.split("_")[2] || 500,
			url=elm.href,
			name=elm.title;
		var childWin = window.open(url, name,"width="+w+",height="+h+"toolbar=no,menubar=yes,status=yes,scrollbars=yes,resizable=yes");
			childWin.focus();
		return false;
	});

	//サブページホームへ戻る表示処理
	if(window.location.pathname.indexOf("special")){
		var checkpram = window.location.search.indexOf("inside=true",0);
		if(checkpram == -1){
			$(".closesubcontents").html('<a href="/" id="subpageback">おんたけ王滝ホームへ</a>');
		}
	}
});

