var minW = 940;
var minH = 600;

var closeTxt = new Object();
closeTxt['en'] = 'Close';
closeTxt['tc'] = '關閉';
closeTxt['sc'] = '关闭';

var lightbox={};
lightbox.status=0;
lightbox.contentId=false;
lightbox.selector='a[rel=lightbox]';
lightbox.width=706;
lightbox.height=560+30;

lightbox.ini=function(){

	$(lightbox.selector).unbind().click(function(e){

		e.preventDefault();
		
		lightbox.status=1;
		lightbox.overlay();

		$('<div id="lightbox" />').appendTo('body').css({
			'width':lightbox.width,
			'height':lightbox.height,
			'z-index':999
		}).append('<div class="heading"><div id="lightboxClose"><a class="close" href="javascript:;">' + closeTxt[lang] + '</a></div></div>').center();
		
		$('#lightbox .close').click(lightbox.close);
		
		if(this.title)
			$('#lightbox .title').html(this.title);
		
		// launch flv player
		if(this.href.toLowerCase().indexOf('.mp4')>-1 || this.href.toLowerCase().indexOf('.flv')>-1){
			
			var srcPath=this.href.substring(this.href.indexOf('#')+1);
			
			$('#lightbox').append('<div id="lightbox_flash" ><p>You need to upgrade your <strong>FLASHPLAYER</strong><br>This site requires ADOBE FLASH PLAYER VERSION 9 or GREATER.<br>Please <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">CLICK HERE</a> to download.</p></div>');
			
			if(iPx()){
				
				window.location=srcPath;
				lightbox.close();
				
			}else{

				var video_width=443;
				var video_height=267;
				
				$('#lightbox').css({
					'width':video_width,
					'height':video_height+50
				}).find('.heading').css('width',video_width-20);

				var flashvars = {
					srcPath:srcPath,
					downloadPath:"",
					imgPath:"",
					videoW:video_width,
					videoH:video_height,
					autoLoop:"false",
					controlBarAutoHide:"false",
					autoPlay:"true",
					file:srcPath,
					autostart:'false',
					'controlbar.position':'over',
					'controlbar.idlehide':true
				};
				var flash_params = {
					menu: "false",
					scale: "noScale",
					allowFullscreen: "true",
					allowScriptAccess: "always",
					bgcolor: "#000000"
				};
				var file = '/' + lang + '/swf/player.swf';
				var attributes = { id:'videoSWFa', name:'videoSWFa'};
				swfobject.embedSWF(file,'lightbox_flash',video_width,video_height,'9.0.115','false',	flashvars, flash_params, attributes);
				//swfobject.embedSWF('/' + lang + '/swf/flvPlayer.swf', 'lightbox_flash', video_width, video_height, '9.0.0', "expressInstall.swf", flashvars, flash_params);
				
			}
			
			lightbox.relocate();
			$(window).resize(lightbox.relocate);

		}else if(/(jpg|gif|png)$/i.test(this.href)){

			//if(this.href.indexOf('#')>-1){
				
				var src=this.href.substring(this.href.indexOf('#')+1);
				
				var img = d.createElement('img');
				img.src = src+'?'+Math.random();
				
				var loadingDiv = createDiv('', 'loading');
				$('#lightbox').append(loadingDiv);
				//$(loadingDiv).center();
				
				img.onload = function()
				{
					var w = img.width;
					var h = img.height;
					var screenW = d.documentElement.clientWidth - 60;
					var screenH = d.documentElement.clientHeight - 110;
					
					if (w >= h)
					{
						if (w > screenW)
						{
							w = screenW;
							if (w < minW)	w = minW;
							img.style.width = w + 'px';
						}
					}
					if (h > w)
					{
						if (h > screenH)
						{
							h = screenH;
							if (h < minH)	h = minH;
							img.style.width = 'auto';
							img.style.height = h + 'px';
						}
					}
					
					var imgDiv=document.createElement('div');
					imgDiv.id='lightboxImg';
					imgDiv.appendChild(this);
					
					$(loadingDiv).remove();
					$('#lightbox').append(imgDiv).width($(this).width()).height($(this).height()+50).center().show();
					$(window).unbind('resize').resize(lightbox.relocate);
				}
					
			//}
			
		}else{

			$('#lightbox').append('<div id="lightbox_content" />');

			if(this.href.indexOf('#')>-1){
				
				var id=this.href.substring(this.href.indexOf('#')+1);
				lightbox.contentId=id;
				var $content=$('#'+id);
				$('#lightbox').width($content.width()).height($content.height()+50).center();
				$content.appendTo('#lightbox_content').show();
				$(window).unbind('resize').resize(lightbox.relocate).resize();
				
			}

		}

	});
	
};


lightbox.open=function(id,title){

		lightbox.status=1;
		lightbox.overlay();

		$('<div id="lightbox" />').appendTo('body').css({
			'width':lightbox.width,
			'height':lightbox.height,
			'z-index':99
		}).append('<div class="heading"><div class="title"></div><div class="close">Close X</div></div>').center();

		$('#lightbox .close').click(lightbox.close);
		
		$('#lightbox .title').html(title);
		if(title=='')
			$('#lightbox .heading').remove();

		$lightbox=$('#lightbox').append('<div id="lightbox_content" />');

		lightbox.contentId=id;

		var $content=$('#'+id);

		$lightbox.width($content.width()).height($content.height()+50).center();
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');

		$content.appendTo('#lightbox_content').show();
		$(window).unbind('resize').resize(lightbox.relocate);
		
		return $lightbox;

}


lightbox.relocate=function(){
	if(lightbox.status==1){
		lightbox.overlay();
		var $lightbox=$('#lightbox').center();
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');
	};
}


lightbox.relocateShift=function(){
	if(lightbox.status==1){
		lightbox.overlay();
		if(iPx())
			var top=$(window).scrollTop()+30;
		else
			var top=$(window).scrollTop()+100;
		var $lightbox=$('#lightbox').center().css('top',top);
		if(parseInt($lightbox.css('top'))<0)
			$lightbox.css('top','0px');
	};
}


lightbox.overlay=function(){
			
	$('#overlay').remove();

	var w_width=$(document).width();
	var w_height=$(document).height();
	
	if($.browser.msie){
		if(parseInt($.browser.version)==6)
			w_width=w_width-scrollbarWidth()-8;
		else if(parseInt($.browser.version)==8)
			w_width=w_width-scrollbarWidth()-8;
	}
	
	$('<div id="overlay" />').appendTo('body').css({
		'width':w_width,
		'height':w_height
	}).click(lightbox.close);
			
	// IE6 select box fix
	if ($.browser.msie && parseInt($.browser.version)<=6){
		$('select').css('visibility','hidden');
		$('.popup select').css('visibility','visible');
	};

}


lightbox.close=function(){
	
	if(lightbox.contentId!=false){
		$('#'+lightbox.contentId).hide().appendTo('body');
		lightbox.contentId=false;
	};
	
	
	$('#overlay, #lightbox').remove();
	
	// IE6 select box fix
	if ($.browser.msie && parseInt($.browser.version)<=6)
		$('select').css('visibility','visible');
		
	lightbox.status=0;
	
}


/* center positioning */
jQuery.fn.center = function () {
	
	var top=( $(window).height() - this.height() ) / 2+$(window).scrollTop();
	var left=( $(window).width() - this.width() ) / 2+$(window).scrollLeft();
	
    this.css("position","absolute");
    this.css("top", top + "px");
    this.css("left", left + "px");
    return this;
};

/* detect iDevice */
function iPx(){
	if((navigator.userAgent.match(/iPhone/i)) ||  (navigator.userAgent.match(/iPod/i)) ||  (navigator.userAgent.match(/iPad/i)))
		return true;
	return false;
}


function replaceSwfWithEmptyDiv(targetID){
	var el = document.getElementById(targetID);
	if(el){
		var div = document.createElement("div");
		el.parentNode.insertBefore(div, el);
		swfobject.removeSWF(targetID);
		div.setAttribute("id", targetID);
	};
}

function scrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"/></div>');
    /* Append our div, do our calculation and then remove it */
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

$(function(){
	lightbox.ini();
});
