/* =========================
SoftBankMobile Web Site
Croqui Box JS var.1.05
Last Update 2011/07/29 14:30
SINCE 2009/10/30 MSKCH
========================= */

var croquiTop,browserType,cbURL,groupLength,htmlPR;
var cqoCallback=cqcCallback=function(){};
var croquiConf = {
	width:0,
	height:0
}

$(function(){
	
	browserType = (typeof document.body.style.maxHeight!='undefined')?'new':'old';
	if(navigator.userAgent.match('iP'))browserType = 'iP';
	// Style
	var cbPosition = (browserType == 'new')?'fixed':'absolute';
	var firefoxBody = (navigator.userAgent.match('Firefox')||navigator.userAgent.match('Chrome'))?'':'overflow:hidden';

	$('head').append('<style>.croqui-box-on,.croqui-box-on body{'+firefoxBody+'}#croqui-bg{position:absolute;left:0px;top:0px;background:#000000;z-index:1050;margin:0;}#croqui-box{position:'+cbPosition+';left:0px;background:white;z-index:1100;width:100px;height:100px;margin:0;}#croqui-close{position:absolute;cursor:pointer;padding:10px;margin:0;}#croqui-next,#croqui-prev{position:absolute;cursor:pointer;margin:0;}#croqui-iframe{border:none;}#scroll-bar-width{position:absolute;top:-1000px;left:0px;margin:0px;border:0px;padding:0px;width:100px;overflow-y:scroll;}</style>');

	// Scroll Bar Width
	$('<div id="scroll-bar-width"><p></p></div>').prependTo('body');
	scrollBarWidth = $('#scroll-bar-width').width() - $('#scroll-bar-width p').width();
	$('#scroll-bar-width').remove;
	

	$('.croquibox').click(function(){
		$('object,embed').css({visibility:'hidden'});
		runCroqui(this);
		return false;
	});
	
	
		$(window).resize(setPosition);
	
	
	
	
});


	// Main
	function runCroqui(target){
		cbURL = $(target).attr('href');
		eval($(target).attr('rel'));
		htmlPR = ($(window).height()>$('body').height())?0:scrollBarWidth;
		
		
		if(navigator.userAgent.match('Firefox')||navigator.userAgent.match('Chrome')) {
			htmlPR = 0;
		} else {
			$('html').addClass('croqui-box-on').css({'paddingRight':htmlPR});
		}

		if($(window).height()>$('body').height()){
			$('<div id="croqui-bg"></div>').css({'opacity':0,'height':$(window).height()+'px'}).appendTo('body');
		}else{
			$('<div id="croqui-bg"></div>').css({'opacity':0,'height':$('body').height()+'px'}).appendTo('body');
		}
		$('#croqui-bg').animate({opacity:.8},500);
		
		$('<div id="croqui-box" ontouchmove="event.preventDefault()"></div>').css({'width':croquiConf.width,'height':croquiConf.height}).appendTo('body');
		if(croquiConf.cbClass)$('#croqui-box').addClass(croquiConf.cbClass);


		if(croquiConf.iframe==1){
			$('#croqui-box').append('<div style="width:'+croquiConf.width+'px;height:'+croquiConf.height+'px;overflow:hidden;"><iframe id="croqui-iframe" src="'+target.href+'" frameborder=0 width="'+croquiConf.width+'" height="'+croquiConf.height+'"></iframe></div>');
		} else if(cbURL.match(/^#/)) {
			groupLength= $('.croquibox[rel*="'+croquiConf.group+'"]').length-1;
			nowNum = $('.croquibox[rel*="'+croquiConf.group+'"]').index(target);
			
			$('<div>').addClass(croquiConf.group).append($(cbURL).html()).appendTo('#croqui-box');
			setPageSender();
			pageSenderCont();			

			$('#croqui-next,#croqui-prev').click(function(){
				($(this).is('#croqui-next'))?nowNum++:nowNum--;
				if(nowNum<0){nowNum=groupLength}
				else if(nowNum>groupLength){nowNum=0}
				pageSenderCont();
				$('.'+croquiConf.group).html(($($('.croquibox[rel*="'+croquiConf.group+'"]:eq('+nowNum+')').attr('href')).html()));
				return false;
			});
		}

		cqoCallback();
		setClose();
		setPosition();
	
		$('#croqui-bg,#croqui-close').one('click',function(){
			
			$('#croqui-bg,#croqui-box').animate({'opacity':0},{duration:500,complete:function(){
				$(this).remove();
				if(/a/[-1]!='a')$('html').removeClass('croqui-box-on').css({'paddingRight':'0px'});
				$('object,embed').css({visibility:'visible'});
			}});
			cqcCallback();
		});
		
		return false;
	}
	
	// Set Box Position
	function setPosition(){
		htmlPR = ($(window).height()>$('body').height())?0:scrollBarWidth;
		switch(browserType){
			case 'new':	croquiTop = '50%';
						break;
			case 'old':	croquiTop = $(window).scrollTop()+$(window).height()/2;
						break;
			case 'iP': croquiTop = window.pageYOffset*1+window.innerHeight/2;
						break;
		//{alert(window.innerHeight+','+window.pageYOffset);iPstyle=' style="top:'+window.pageYOffset+'px!important;margin:0!imortant;"';}
		}
		
		$('#croqui-box').css({
			'top':croquiTop,
			'left':'50%',
			'marginLeft':(croquiConf.width+htmlPR)/-2,
			'marginTop':croquiConf.height/-2
		})
		
		$('#croqui-bg').css({'width':$('body').width()+htmlPR});
	}
	
	// Set Close Button
	function setClose(){
		$('<img>').attr({'id':'croqui-close'}).appendTo('#croqui-box');

		if(croquiConf.cbButton){
			$('#croqui-close').attr({'src':croquiConf.cbButton});
		} else {
		switch(croquiConf.cbType){
			case 0:		$('#croqui-close').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_close_txt_white.gif'});break;
			case 1:		$('#croqui-close').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_close_jp_blk.gif'});break;
			case 2:		$('#croqui-close').replaceWith('<div id="croqui-close" class="roll-over"><a href="#"><img src="/mb/shared/img/v09b/croqui_box/btn_close_box_jp.gif" alt="このウインドウを閉じる" /></a></div>');break;
			case 3:		$('#croqui-close').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_close_simple_jp.gif'});break;
			case 4:		$('#croqui-close').replaceWith('<div id="croqui-close" class="roll-over"><a href="javascript:void(0);"><img src="/mb/shared/img/v09b/croqui_box/btn_close_box_pt.gif" alt="閉じる" /></a></div>');break;
			default:	$('#croqui-close').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_close_txt_white.gif'});break;
		}

		switch(croquiConf.cbPosition){
			case 0:		$('#croqui-close').css({'top':croquiConf.height+'px','right':'100px'});break;
			case 1:		$('#croqui-close').css({'bottom':'0px','right':'0px'});break;
			case 2:		$('#croqui-close').css({'bottom':'0px','right':'0px','background':'url(/mb/shared/img/v09b/croqui_box/btn_close_box_jp_on.gif) 10px 10px no-repeat'});break;
			case 3:		$('#croqui-close').css({'top':'430px','right':'0px'});break;
			case 4:		$('#croqui-close').css({'top':'0px','right':'0px','background':'url(/mb/shared/img/v09b/croqui_box/btn_close_box_pt_on.gif) 10px 10px no-repeat'});break;
			default:	$('#croqui-close').css({'top':croquiConf.height+'px','right':'0px'});break;
		}
		}
	}
	
	// Set Page button
	function setPageSender(){
		$('<img>').attr({'id':'croqui-next'}).appendTo('#croqui-box');
		$('<img>').attr({'id':'croqui-prev'}).appendTo('#croqui-box');

		switch(croquiConf.pbType){
			case 0:		$('#croqui-next').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_gray_disc_next.gif'})
						.css({'top':0,'right':-65,'paddingLeft':30,'paddingRight':30});
						$('#croqui-prev').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_gray_disc_prev.gif'})
						.css({'top':0,'left':-65,'paddingLeft':30,'paddingRight':30});
						$('#croqui-next,#croqui-prev').css({'paddingTop':croquiConf.height/2-26,'paddingBottom':croquiConf.height/2-26});
						break;
			case 1:		$('#croqui-next').replaceWith('<div id="croqui-next" class="roll-over" style="top:0;padding:0 30px;right:-82px;background:url(/mb/shared/img/v09b/croqui_box/btn_gray_square_next_on.gif) center center no-repeat;"><a href="#"><img src="/mb/shared/img/v09b/croqui_box/btn_gray_square_next.gif" alt="next" /></a></div>');
						$('#croqui-prev').replaceWith('<div id="croqui-prev" class="roll-over" style="top:0;padding:0 30px;left:-82px;background:url(/mb/shared/img/v09b/croqui_box/btn_gray_square_prev_on.gif) center center no-repeat;"><a href="#"><img src="/mb/shared/img/v09b/croqui_box/btn_gray_square_prev.gif" alt="prev" /></a></div>');
						$('#croqui-next,#croqui-prev').css({'paddingTop':croquiConf.height/2-26,'paddingBottom':croquiConf.height/2-26});
						break;
			default:	$('#croqui-next').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_gray_disc_next.gif'})
						.css({'top':0,'right':-65,'paddingLeft':croquiConf.width/2,'paddingRight':30});
						$('#croqui-prev').attr({'src':'/mb/shared/img/v09b/croqui_box/btn_gray_disc_prev.gif'})
						.css({'top':0,'left':-65,'paddingLeft':30,'paddingRight':croquiConf.width/2});
						$('#croqui-next,#croqui-prev').css({'paddingTop':croquiConf.height/2-26,'paddingBottom':croquiConf.height/2-26});
						break;
		}
	}

	// Page Button Control
	function pageSenderCont(){
		if(croquiConf.loop!=1){
			(nowNum<1)?$('#croqui-prev').hide():$('#croqui-prev').show();
			(nowNum<0||nowNum>=groupLength)?$('#croqui-next').hide():$('#croqui-next').show();
		}
	}
	

