/**
 * jQuery lightpop plugin
 * This jQuery plugin was inspired on jQuery lightBox plugin by Leandro Vieira Pinho (http://leandrovieira.com/projects/jquery/lightpop/)
 * @name jquery.lightpop-0.3.5.js
 * @author wokamoto - http://dogmap.jp
 * @version 0.3.5
 * @date January 9, 2008
 * @category jQuery plugin
 * @copyright (c) 2007-2008 wokamoto (dogmap.jp)
 * @license	Released under the GPL license (http://www.gnu.org/copyleft/gpl.html)
 * @example http://dogmap.jp/lightpop_sample/sample.html
 */
(function(jQuery){
jQuery.fn.lightpop = function(settings){
 settings = jQuery.extend({
	overlayBgColor:	'#000'
	,overlayOpacity:	0.7
	,contentFrameType:	'border'
	,contentBorder:	'none'
	,boxBorderSize:	6
	,containerBorderSize:	10
	,containerResizeSpeed:'normal'
	,contentBgColor:	'#FFF'
	,imageBox:		'/img/lightpop_images/lightpop-box.gif'
	,imageBorderL:	'/img/lightpop_images/lightpop-border-l.gif'
	,imageBorderR:	'/img/lightpop_images/lightpop-border-r.gif'
	,imageLoading:	'/img/lightpop_images/lightpop-ico-loading.gif'
	,imageBtnPrev:	'/img/lightpop_images/lightpop-btn-prev.gif'
	,imageBtnNext:	'/img/lightpop_images/lightpop-btn-next.gif'
	,imageBtnClose:	'/img/lightpop_images/lightpop-btn-close.gif'
	,imageBlank:	'/img/lightpop_images/lightpop-blank.gif'
	,imageBtnPrevWidth:	63
	,imageBtnNextWidth:	63
	,imageBtnCloseWidth:	66
	,txtImage:		'Image'
	,txtOf:		'of'
	,keyToClose:		'c'
	,keyToPrev:		'p'
	,keyToNext:		'n'
	,flvplayer:		'flvplayer.swf'
	,iconAdd:		true
//	,iconImage:		'/img/lightpop_images/icon-image.png'
//	,iconVideo:		'/img/lightpop_images/icon-video.png'
//	,iconContents:	'/img/lightpop_images/icon-contents.png'
//	,iconYouTube:	'/img/lightpop_images/icon-youtube.png'
//	,iconMetacafe:	'/img/lightpop_images/icon-metacafe.png'
//	,iconLiveLeak:	'/img/lightpop_images/icon-liveleak.png'
//	,iconGoogleVideo:	'/img/lightpop_images/icon-googlevideo.png'
//	,iconifilm:		'/img/lightpop_images/icon-ifilm.png'
//	,iconDailymotion:	'/img/lightpop_images/icon-dailymotion.png'
 }, settings);
 var frameBorder = (settings.contentFrameType.toLowerCase() == 'border');
 var contentInfo = new Array();

 var fileTypes = new Array(
	{
	type: 'Image'
	,match: function(strUrl){return (strUrl.match(/\.(jpe?g|gif|png|bmp)[\?.*]?$/i));}
	//,match: function(strUrl){return (strUrl.match(/\.(jpe?g|gif|png|bmp)[\?]?.*$/i));}
	,set: function(contentNo){
		image_load(contentInfo[contentNo].href, (function(n){return function(){
			var arrSizes = get_sizes_from_str(this.src, new Array(this.width, this.height));
			contentInfo[n] = jQuery.extend(contentInfo[n], {content:'<img src="' + this.src.replace(/\?.*$/, '') + '" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" />', width:arrSizes[0], height:arrSizes[1], later:(jQuery.browser.msie ? 250 : 100)});
			set_content_to_view(n);
			this.onload = function(){};
		}})(contentNo));
	 }
	,preload: function(contentNo){
		image_load(contentInfo[contentNo].href, (function(n){return function(){
			var arrSizes = get_sizes_from_str(this.src, new Array(this.width, this.height));
			contentInfo[n] = jQuery.extend(contentInfo[n], {content:'<img src="' + this.src.replace(/\?.*$/, '') + '" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" />', width:arrSizes[0], height:arrSizes[1], later:(jQuery.browser.msie ? 250 : 100)});
			this.onload = function(){};
		}})(contentNo));
	 }
	}
 ,{
	type: 'Video'
	,match: function(strUrl){return (strUrl.match(/\.(flv|swf|rm|mov|3gp|mp4|asf|avi|mpg|wmv)[\?]?.*$/i));}
	}
 ,{
	type: 'YouTube'
	,match: function(strUrl){return (strUrl.match(/\.youtube\.com\/watch/i));}
	}
 ,{
	type: 'msnVideo'
	,match: function(strUrl){return (strUrl.match(/video\.msn\.com/i));}
	}
 ,{
	type: 'yahooJapanVideo'
	,match: function(strUrl){return (strUrl.match(/videocast\.yahoo\.co\.jp/i));}
	}
 ,{
	type: 'metacafe'
	,match: function(strUrl){return (strUrl.match(/\.metacafe\.com\/watch/i));}
	}
 ,{
	type: 'LiveLeak'
	,match: function(strUrl){return (strUrl.match(/\.liveleak\.com\/view/i));}
	}
 ,{
	type: 'GoogleVideo'
	,match: function(strUrl){return (strUrl.match(/video\.google\.com\/videoplay/i));}
	}
 ,{
	type: 'IFILM'
	,match: function(strUrl){return (strUrl.match(/\.ifilm\.com\/video/i));}
	}
 ,{
	type: 'Dailymotion'
	,match: function(strUrl){return (strUrl.match(/\.dailymotion\.com\/video/i));}
	}
 ,{
	type: 'nicovideo'
	,match: function(strUrl){return (strUrl.match(/\.nicovideo\.jp\/watch/i));}
	}
 ,{
	type: 'Contents'
	,match: function(strUrl){return (strUrl.match(window.location.host));}
	,set: function(contentNo){
		jQuery.get(contentInfo[contentNo].href, function(responseText, status){
			var arrSizes = get_sizes_from_str(contentInfo[contentNo].href);
			var strSrc = '<div width="' + arrSizes[0] + '" height="' + arrSizes[1] + '">' + responseText.replace(/[\r\n]/g, '').replace(/.*<body.*?>(.*?)<\/body>.*$/, '$1').replace(/<script.*?>.*<\/script>/g, '') + '</div>';
			contentInfo[contentNo] = jQuery.extend(contentInfo[contentNo], {content:strSrc, width:arrSizes[0], height:arrSizes[1], later:500});
			set_content_to_view(contentNo);
		});
	 }
	,preload: function(contentNo){}
	,content_css: {'text-align':'left', 'line-height':'1.2em'}
	}
 );

 // initialize
 var initialize = function(jQueryObj){
	// image preload
	image_load(settings.imageBtnPrev,	function(){settings.imageBtnPrevWidth	= (this.width > 0 ? this.width : 63); this.onload = function(){};});
	image_load(settings.imageBtnNext,	function(){settings.imageBtnNextWidth	= (this.width > 0 ? this.width : 63); this.onload = function(){};});
	image_load(settings.imageBtnClose, function(){settings.imageBtnCloseWidth = (this.width > 0 ? this.width : 66); this.onload = function(){};});
	if (!frameBorder) {jQuery.each(new Array(settings.imageBox, settings.imageBorderL, settings.imageBorderR), function(){image_load(this);});}

	// get matched object
	var domMatchedObj = new Array();
	var intImage = 0;
	var location = window.location;
	contentInfo.length = 0;
	jQueryObj.filter('a').each(function() {
		var jQuery_this = jQuery(this);
		if (!jQuery_this.attr('href').match(/^https?:/i) && location.protocol.match(/^https?:/i)) {
		if (jQuery_this.attr('href').match(/^\//i)) {
			jQuery_this.attr('href', location.protocol + '//' + location.hostname + '/' + jQuery_this.attr('href'));
		} else {
			jQuery_this.attr('href', location.href.replace(/^(.*\/).*$/i, '$1') + jQuery_this.attr('href').replace(/^\/?(.*)$/i, '$1'));
		}
	 }

	 for (var filetype = fileTypes[0], optindex = 0; optindex < fileTypes.length; optindex++, filetype = fileTypes[optindex]) {
		if (filetype.match(jQuery_this.attr('href'))) {
			// set icons to link
			if (settings.iconAdd && jQuery_this.children('img').length == 0) {
			jQuery_this.css({background:'transparent url(' + (settings['icon' + filetype.type] ? settings['icon' + filetype.type] :'images/icon-' + filetype.type.toLowerCase() + '.png') + ') no-repeat scroll 1px 0pt', 'padding-left':'20px'});
			}

		 // content info
		 contentInfo.push({
			index: optindex,
			type:	(filetype.type ? filetype.type : settings.txtImage),
			href:	jQuery_this.attr('href'),
			title: (jQuery_this.attr('title') ? jQuery_this.attr('title') : jQuery_this.html().replace(/<[^>]*>/ig, ''))
		 });

		 // bind click event
		 jQuery_this.unbind('click').click((function(i){return function(){start_lightpop(i); return false;}})(intImage));

		 // push array
		 domMatchedObj.push(this);
		 intImage++;
		 break;
		}
	 }
	});
	return new jQuery(domMatchedObj);
 };

 // start!
 var start_lightpop = function(intClickedContent){
	// hide embed, object, select element
	set_visibility(jQuery('embed, object, select'), false);

	// set interface
	if (frameBorder) {
	 jQuery('body').append(jQuery('<div id="overlay" /><div id="lightpop"><div id="lightpop-box"><div id="lightpop-box-content"><div id="lightpop-content-box"><div id="lightpop-content" /></div></div><div id="lightpop-data-box"><div id="lightpop-data"><div id="lightpop-details"><div id="lightpop-details-caption" /><div id="lightpop-details-number" /></div><div id="lightpop-data-nav"><a href="#" id="lightpop-data-nav-close"><img src="'+settings.imageBtnClose+'" /></a></div></div></div><div id="lightpop-nav"><a href="#" id="lightpop-nav-prev" /><a href="#" id="lightpop-nav-next" /></div><div id="lightpop-loading"><a href="#" id="lightpop-loading-link"><img src="'+settings.imageLoading+'" /></a></div></div></div>').hide());
	} else {
	 jQuery('body').append(jQuery('<div id="overlay" /><div id="lightpop"><div id="lightpop-box"><div id="lightpop-box-content"><div id="lightpop-box-hd"><div id="lightpop-box-hdc" /></div><div id="lightpop-box-bd"><div id="lightpop-box-bdc"><div id="lightpop-content-box"><div id="lightpop-content" /></div><div id="lightpop-data-box"><div id="lightpop-data"><div id="lightpop-details"><div id="lightpop-details-caption" /><div id="lightpop-details-number" /></div><div id="lightpop-data-nav"><a href="#" id="lightpop-data-nav-close"><img src="'+settings.imageBtnClose+'" /></a></div></div></div></div></div><div id="lightpop-box-ft"><div id="lightpop-box-ftc" /></div></div><div id="lightpop-nav"><a href="#" id="lightpop-nav-prev" /><a href="#" id="lightpop-nav-next" /></div><div id="lightpop-loading"><a href="#" id="lightpop-loading-link"><img src="'+settings.imageLoading+'" /></a></div></div></div>').hide());
		settings.contentBorder = 'none';
	}

	// set interface CSS
	var arrPageSizes	= get_page_sizes();
	var arrPageScroll = get_page_scroll();
	var initSize = 250;

	// overlay
	jQuery('#overlay').css({position:'absolute', top:0, left:0, 'background-color':settings.overlayBgColor, opacity:settings.overlayOpacity, width:arrPageSizes[0], height:arrPageSizes[1], 'z-index':90});
	jQuery('#lightpop').css({position:'absolute', top:arrPageScroll[1] + Math.round(arrPageSizes[3] / 10), left:arrPageScroll[0], width:'100%', 'text-align':'center', 'line-height':0, border:'none', 'z-index':100});
	jQuery('#lightpop a img').css({border:'none'});

	// container
	jQuery('#lightpop-box').css({position:'relative', width:initSize, height:initSize, top:0, margin:'0 auto', padding:0, 'background-color':settings.contentBgColor, border:settings.contentBorder, overflow:(frameBorder ? 'hidden' : 'visible')});
	jQuery('#lightpop-content-box').css({'background-color':'transparent'});
	jQuery('#lightpop-content').css({margin:(frameBorder ? 0 : '0 auto'), padding:(frameBorder ? 0 : settings.containerBorderSize + 'px 0')});
	jQuery('#lightpop-box-content').css({position:'relative', width:initSize, height:initSize, 'background-color':'transparent', margin:'0 auto', padding:(frameBorder ? settings.containerBorderSize : 0), overflow:(frameBorder ? 'hidden' : 'visible')});
	if (!frameBorder) {
		set_box_css(false).css({position:'relative'}).hide();
		jQuery('#lightpop-box-hd').css({height:settings.boxBorderSize, top:0, margin:'0 ' + settings.boxBorderSize + 'px 0 0'});
		jQuery('#lightpop-box-hdc').css({height:settings.boxBorderSize, top:0, margin:'0 ' + settings.boxBorderSize*-1 + 'px 0 ' + settings.boxBorderSize + 'px'});
		jQuery('#lightpop-box-ft').css({height:settings.boxBorderSize, bottom:0, margin:'0 ' + settings.boxBorderSize + 'px 0 0'});
		jQuery('#lightpop-box-ftc').css({height:settings.boxBorderSize, bottom:0, margin:'0 ' + settings.boxBorderSize*-1 + 'px 0 ' + settings.boxBorderSize + 'px'});
	}

	// navigation
	jQuery('#lightpop-nav').css({position:'absolute', top:0, left:0, height:'10px', width:'100%', padding:0, margin:(frameBorder ? '0' : settings.boxBorderSize + 'px') + ' auto', 'z-index':10});
	jQuery('#lightpop-nav a').css({display:'block', height:'100%', zoom:1, margin:(frameBorder ? 0 : '0 ' + settings.boxBorderSize + 'px'), outline:'none'});
	jQuery('#lightpop-nav-prev').css({width:settings.imageBtnPrevWidth, left:0, 'float':'left'});
	jQuery('#lightpop-nav-next').css({width:settings.imageBtnNextWidth, right:0, 'float':'right'});

	// loading image
	jQuery('#lightpop-loading').css({position:'absolute', top:'40%', left:0, height:'20%', width:'100%', margin:'0 auto', 'text-align':'center', 'line-height':0});

	// content data
	jQuery('#lightpop-data-box').css({font:'10px Verdana, Helvetica, sans-serif', 'background-color':settings.contentBgColor, 'line-height':'1.4em', width:'100%', margin:'0 auto', padding:'0 ' + settings.containerBorderSize + 'px 0', overflow:'hidden'}).hide();
	jQuery('#lightpop-data').css({position:'relative', padding:'0 ' + settings.containerBorderSize + 'px', color:'#666', left:0, bottom:0});
	jQuery('#lightpop-details').css({width:'70%', 'float':'left', 'text-align':'left'});
	jQuery('#lightpop-details-caption').css({'float':'left', 'font-weight':'bold', width:'100%'});
	jQuery('#lightpop-details-number').css({'float':'left', clear:'left', width:'100%', 'padding-bottom':'1.0em'});
	jQuery('#lightpop-data-nav-close').css({'float':'right', width:settings.imageBtnCloseWidth, 'padding':'0.35em 0'});

	// bind event
	jQuery('#overlay').click(finish_lightpop).fadeIn(settings.containerResizeSpeed, function(){jQuery('#lightpop').show();});
	jQuery('#lightpop-loading-link, #lightpop-data-nav-close').click(function(){finish_lightpop(); return false;});
	window_resize(true);

	// set content
	set_content(intClickedContent);
 };

 // set content
 var set_content = function(contentNo){
	set_visibility(jQuery('#lightpop-data, #lightpop-details, #lightpop-details-caption, #lightpop-details-number, #lightpop-data-nav-close'), false);
	jQuery('#lightpop-nav, #lightpop-nav-prev, #lightpop-nav-next').hide();
	jQuery('#lightpop-content').hide().children().remove();
	jQuery('#lightpop-loading, #lightpop-box, #lightpop-box-content').show();
	set_box_css(false);
	if(!frameBorder && jQuery.browser.safari){jQuery('#lightpop-data-box').height('auto');}
	set_content_to_view(contentNo);
 };

 // set content to view
 var set_content_to_view = function(contentNo){
	(contentInfo[contentNo].content
		? function(n){jQuery('#lightpop-content').append(jQuery(contentInfo[n].content)); setTimeout(function(){show_container(n);}, contentInfo[n].later);}
		: (fileTypes[contentInfo[contentNo].index].set ? fileTypes[contentInfo[contentNo].index].set : (function(contentNo){set_video_info(contentNo); set_content_to_view(contentNo);}))
	)(contentNo);
 };

 // show container
 var show_container = function(contentNo){
	var intWidth	= contentInfo[contentNo].width	+ (settings.containerBorderSize + (frameBorder ? 0 : settings.boxBorderSize)) * 2;
	var intHeight = contentInfo[contentNo].height + settings.containerBorderSize * 2;

	jQuery('#lightpop-box').animate({width:intWidth, height:intHeight}, settings.containerResizeSpeed, function(){
		// resize content and data
		jQuery('#lightpop-box-content').css({width:(frameBorder ? contentInfo[contentNo].width : intWidth), height:contentInfo[contentNo].height});
		jQuery('#lightpop-content').css({width:contentInfo[contentNo].width, height:contentInfo[contentNo].height});
		jQuery('#lightpop-data-box').css({width:contentInfo[contentNo].width});

		// set content css
		if (fileTypes[contentInfo[contentNo].index].content_css) {jQuery('#lightpop-content').children().css(fileTypes[contentInfo[contentNo].index].content_css);}

		// show content
		jQuery('#lightpop-loading').hide();
		if (contentInfo[contentNo].content.match(/<object.*>/i)) {jQuery('#lightpop-content').show();} else {jQuery('#lightpop-content').fadeIn();}
		set_box_css(true);

		// set content data
		jQuery('#lightpop-details-caption').html(contentInfo[contentNo].title);
		jQuery('#lightpop-details-number').html(contentInfo[contentNo].type + (contentInfo.length > 1 ? ' ' + (contentNo + 1) + ' ' + settings.txtOf + ' ' + contentInfo.length : ''));
		var data_box = set_visibility(jQuery('#lightpop-data, #lightpop-details, #lightpop-details-caption, #lightpop-details-number, #lightpop-data-nav-close'), false).show();
		jQuery('#lightpop-data-box').slideDown('fast', function(){
		var intDataboxHeight = (!jQuery.browser.safari ? jQuery(this).height() : this.scrollHeight);
		intDataboxHeight = (intDataboxHeight < 28 ? 28 : intDataboxHeight);
		intHeight += intDataboxHeight;
		if (frameBorder) {
			jQuery('#lightpop-box').animate({width:intWidth, height:intHeight}, 'fast', function(){
			set_visibility(data_box, true);
			});
		} else {
			if (jQuery.browser.safari) {jQuery('#lightpop-data-box').height(intDataboxHeight);}
			set_visibility(data_box, true);
		}
	 });

	 // set navigation
	 jQuery('#lightpop-nav').css({width:intWidth}).show(function(){
		jQuery('#lightpop-nav-prev, #lightpop-nav-next').css({height:Math.round(intHeight / 3), background:'transparent url(' + settings.imageBlank + ') no-repeat'}).unbind();
		if(contentNo != 0){
		 jQuery('#lightpop-nav-prev').hover(
			function(){jQuery(this).css({background:'url(' + settings.imageBtnPrev + ') left 30% no-repeat'});},
			function(){jQuery(this).css({background:'transparent url(' + settings.imageBlank + ') no-repeat'});}
		 ).show().click((function(i){return function(){set_content(i); return false;}})(contentNo - 1));
		}
		if(contentNo != (contentInfo.length - 1)){
		 jQuery('#lightpop-nav-next').hover(
			function(){jQuery(this).css({background:'url(' + settings.imageBtnNext + ') right 30% no-repeat'});},
			function(){jQuery(this).css({background:'transparent url(' + settings.imageBlank + ') no-repeat'});}
		 ).show().click((function(i){return function(){set_content(i); return false;}})(contentNo + 1));
		}
		keyboard_navigation(true, contentNo);
	 });

	 // preload contents
	 if((contentInfo.length - 1) > contentNo){preload(contentNo + 1);}
	 if(contentNo > 0){preload(contentNo - 1);}
	});
 };

 // preload
 var preload = function(contentNo){
	if(!contentInfo[contentNo].content) {
	 (fileTypes[contentInfo[contentNo].index].preload
		 ? fileTypes[contentInfo[contentNo].index].preload
		 : (function(n){set_video_info(n);})
	 )(contentNo);
	}
 };

 // get content information
 var get_sizes_from_str = function(strText, defaultSizes){
	var intWidth = 0, intHeight = 0;
	var arrText = strText.toLowerCase().replace(/[\r\n]/g,'').replace(/["']/g,'').match(/(width|height)=(\d+)/ig);
	if (arrText) {
	 jQuery.each(arrText, function(){
		if (this.match(/^width=\d+$/i))	{intWidth	= Number(this.replace(/^width=(\d+)$/, '$1'));}
		if (this.match(/^height=\d+$/i)) {intHeight = Number(this.replace(/^height=(\d+)$/,'$1'));}
	 });
	}
	if (intWidth == 0 || intHeight == 0) {
	 if (defaultSizes) {
		intWidth	= defaultSizes[0];
		intHeight = defaultSizes[1];
	 } else {
		var arrPageSizes = get_page_sizes();
		intWidth	= Math.round(arrPageSizes[0] / 4)
		intHeight = Math.round(arrPageSizes[1] / 4);
	 }
	}
	return new Array(intWidth, intHeight);
 };

 var set_video_info = function(contentNo) {
	var strUrl = contentInfo[contentNo].href.replace(/&.*$/i,'');
	var arrSizes, vid, strSrc = null, intLater = 1500;
	switch(contentInfo[contentNo].type){
	 case 'YouTube':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(425, 355));
		vid		= strUrl.replace(/^.*\.youtube.*watch.*?v=(.*)$/i, '$1');
		strUrl = 'http://www.youtube.com/v/' + vid;
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1]);
		break;
	 case 'msnVideo':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(432, 364));
		vid = strUrl.split('=');
		vid = vid[1];
		strUrl = "http://images.video.msn.com/flash/soapbox1_1.swf?c=v&ifs=true&fr=msnvideo&mkt=ja-JP&brand=&v=" + vid;
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1]);
		break;
	 case 'yahooJapanVideo':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(425, 350));
		vid = strUrl.split('=');
		vid = vid[1];
		strUrl = "http://videocast.yahoo.co.jp/player/vsuexy.swf?vid=" + vid;
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1]);
		break;
	 case 'metacafe':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(400, 345));
		vid		= strUrl.replace(/\?.*$/, '').replace(/\/$/i,'').replace(/^.*\.metacafe.*watch\/(.*)$/i, '$1');
		strUrl = 'http://www.metacafe.com/fplayer/' + vid + '.swf';
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1]);
		break;
	 case 'LiveLeak':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(450, 370));
		vid		= strUrl.replace(/^.*\.liveleak.*view.*?i=(.*)$/i, '$1');
		strUrl = 'http://www.liveleak.com/player.swf?autostart=false&token=' + vid;
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1], {quality:"high"});
		break;
	 case 'GoogleVideo':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(400, 326));
		vid		= strUrl.replace(/^.*video\.google\.com\/videoplay.*docid=(.*)$/i, '$1');
		strUrl = 'http://video.google.com/googleplayer.swf?docId=' + vid + '&#038;hl=en';
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1], {flashvars:''});
		break;
	 case 'IFILM':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(448, 365));
		vid		= strUrl.replace(/\?.*$/, '').replace(/\/$/i,'').replace(/^.*\.ifilm\.com.*video\/(.*)$/i, '$1');
		strUrl = 'http://www.ifilm.com/efp';
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1], {flashvars:'flvbaseclip=' + vid + '&', quality:"high", bgcolor:'000000'});
		break;
	 case 'Dailymotion':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(420, 330));
		vid		= strUrl.replace(/^.*\.dailymotion\.com\/video\/([^_]*).*$/i, '$1');
		strUrl = 'http://www.dailymotion.com/swf/' + vid;
		strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1], {allowFullScreen:'true', allowScriptAccess:'always'});
		break;
	 case 'nicovideo':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(312, 176));
		vid		= strUrl.replace(/^.*\.nicovideo\.jp\/watch\/(.*)$/i, '$1');
		strSrc = '<iframe width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" src="http://www.nicovideo.jp/thumb/' + vid + '" scrolling="no" style="border:solid 1px #CCC;" frameborder="0"><a href="http://www.nicovideo.jp/watch/' + vid + '">' + contentInfo[contentNo].tytle + '</iframe>';
		intLater = 500;
		break;
	 case 'Video':
		arrSizes = get_sizes_from_str(contentInfo[contentNo].href, new Array(320, 240));
		strUrl = strUrl.replace(/\?.*$/, '');
		switch(strUrl.match(/\.(flv|swf|rm|mov|3gp|mp4|asf|avi|mpg|wmv)$/i)[1]){
		 case 'flv':
			strUrl = settings.flvplayer + '?file=' + strUrl;
		 case 'swf':
			strSrc = get_flash_src(strUrl, arrSizes[0], arrSizes[1], {quality:"high", bgcolor:"#000"});
			break;
		 case 'rm':
			strSrc = '<object classid="clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '"><param name="src" value="' + strUrl + '" /><param name="autostart" value="true" /><param name="controls" value="imagewindow,controlpanel" /><embed src="' + strUrl + '" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" autostart="true" controls="imagewindow,controlpanel" type="audio/x-pn-realaudio-plugin"></embed></object>';
			break;
		 case 'mov':
		 case 'mp4':
		 case '3gp':
			arrSizes[1] += 20;
			strSrc = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '"><param name="src" value="' + strUrl + '" /><param name="href" value="' + strUrl + '" /><param name="controller" value="true" /><param name="pluginspage" value="http://www.apple.com/quicktime/download/" /><param name="autoplay" value="true" /><param name="bgcolor" value="000000" /><embed src="' + strUrl + '" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" bgcolor="000000" autoplay="true" controller="true" src="' + strUrl + '" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>';
			break;
		 default:
			arrSizes[1] += 20;
			strSrc = '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" type="application/x-oleobject"><param name="url" value="' + strUrl + '" /><param name="autostart" value="true" /><param name="uiMode" value="full" /><embed src="' + strUrl + '" width="' + arrSizes[0] + '" height="' + arrSizes[1] + '" autostart="true" uiMode="full" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/mediaplayer/"></embed></object>';
			break;
		}
		intLater = 1000;
		break;
	}
	if (strSrc) {contentInfo[contentNo] = jQuery.extend(contentInfo[contentNo], {content:strSrc, width:arrSizes[0], height:arrSizes[1], later:intLater});}
 }

 var get_flash_src = function(url, width, height, param) {
	param = jQuery.extend({movie: url, wmode: 'transparent'}, param);
	var strSrc = '<object data="' + url + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" wmode="' + param.wmode + '">';
	jQuery.each(param, function(key){strSrc += '<param name="' + key + '" value="' + this + '" />';})
	strSrc += '</object>';
	return strSrc;
 };

 // set box css
 var set_box_css = function(enable) {
	if (!frameBorder) {
	 var jQueryObj = jQuery('#lightpop-box-hd, #lightpop-box-hdc, #lightpop-box-bd, #lightpop-box-bdc, #lightpop-box-ft, #lightpop-box-ftc');
	 var bg_transparent = 'transparent', bg_content = settings.contentBgColor;
	 if (enable) {
		jQuery('#lightpop-box').css({'background-color':bg_transparent});
		jQuery('#lightpop-box-hd').css({background:bg_transparent + ' url(' + settings.imageBox + ') left top no-repeat'});
		jQuery('#lightpop-box-hdc').css({background:bg_transparent + ' url(' + settings.imageBox + ') right top no-repeat'});
		jQuery('#lightpop-box-bd').css({background:bg_content + ' url(' + settings.imageBorderL + ') left top repeat-y'});
		jQuery('#lightpop-box-bdc').css({background:bg_transparent + ' url(' + settings.imageBorderR + ') right top repeat-y'});
		jQuery('#lightpop-box-ft').css({background:bg_transparent + ' url(' + settings.imageBox + ') left bottom no-repeat'});
		jQuery('#lightpop-box-ftc').css({background:bg_transparent + ' url(' + settings.imageBox + ') right bottom no-repeat'});
		jQueryObj.show();
	 } else {
		jQuery('#lightpop-box').css({'background-color':bg_content});
		jQueryObj.css({background:bg_transparent});
	 }
	 return jQueryObj;
	}
 };

 // image loader
 var image_load = function(src, callback){
	var objImageLoader = new Image();
	if (callback) {objImageLoader.onload = callback;}
	objImageLoader.src = src;
	return objImageLoader;
 }

 // set visibility
 var set_visibility = function(jQueryObj, enable){return jQueryObj.css({visibility:(enable ? 'visible' : 'hidden')});}

 // set keydown event
 var keyboard_navigation = function(enable, contentNo){
	jQuery(document).unbind('keydown');
	if (enable) {
	 jQuery(document).keydown(function(objEvent){
		var key,keycode,escapeKey;
		if(!objEvent){
		 keycode = event.keyCode;
		 escapeKey = 27;
		} else {
		 keycode = objEvent.keyCode;
		 escapeKey = objEvent.DOM_VK_ESCAPE;
		}
		key = String.fromCharCode(keycode).toLowerCase();
		if ((key == settings.keyToClose) || (key == 'x') || (keycode == escapeKey)) {
		 finish_lightpop();
		} else if (((key == settings.keyToPrev) || (keycode == 37)) && contentNo != 0) {
		 set_content(contentNo - 1);
		} else if (((key == settings.keyToNext) || (keycode == 39)) && contentNo != (contentInfo.length - 1)) {
		 set_content(contentNo + 1);
		}
	 });
	}
 };

 // set window resize event
 var window_resize = function(enable){
	jQuery(window).unbind('resize');
	if (enable) {
	 jQuery(window).resize(function(){
		var arrPageSizes	= get_page_sizes();
		var arrPageScroll = get_page_scroll();
		jQuery('#overlay').css({width:arrPageSizes[0], height:arrPageSizes[1]});
		jQuery('#lightpop').css({top:arrPageScroll[1] + Math.round(arrPageSizes[3] / 10), left:arrPageScroll[0]});
	 });
	}
 };

 // get page sizes
 var get_page_sizes = function(){
	var xScroll,yScroll, windowWidth, windowHeight, pageHeight, pageWidth;
	if (window.innerHeight && window.scrollMaxY) {
	 xScroll = window.innerWidth	+ window.scrollMaxX;
	 yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
	 xScroll = document.body.scrollWidth;
	 yScroll = document.body.scrollHeight;
	} else {
	 xScroll = document.body.offsetWidth;
	 yScroll = document.body.offsetHeight;
	}
	if (self.innerHeight) {
	 if(document.documentElement.clientWidth){
		windowWidth = document.documentElement.clientWidth;
	 } else {
		windowWidth = self.innerWidth;
	 }
	 windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
	 windowWidth	= document.documentElement.clientWidth;
	 windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
	 windowWidth	= document.body.clientWidth;
	 windowHeight = document.body.clientHeight;
	}
	pageWidth	= (xScroll < windowWidth	? xScroll : windowWidth);
	pageHeight = (yScroll < windowHeight ? windowHeight : yScroll);
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
 };

 // get page scroll
 var get_page_scroll = function(){
	var xScroll, yScroll;
	if(self.pageYOffset){
	 yScroll = self.pageYOffset;
	 xScroll = self.pageXOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
	 yScroll = document.documentElement.scrollTop;
	 xScroll = document.documentElement.scrollLeft;
	}else if(document.body){
	 yScroll = document.body.scrollTop;
	 xScroll = document.body.scrollLeft;
	}
	return new Array(xScroll, yScroll);
 };

 // finish!
 var finish_lightpop = function(){
	set_visibility(jQuery('object',jQuery('#lightpop')), false).remove();
	jQuery('#lightpop').slideUp(function(){
	 jQuery(this).remove();
	 jQuery('#overlay').fadeOut(function(){
		jQuery(this).remove();
		// show embed, object, select element
		set_visibility(jQuery('embed, object, select'), true);
	 });
	});
	keyboard_navigation(false);
	window_resize(false);
 };

 return initialize(this);
};})(jQuery);

