/*
	DivBox gallery v1.1
	@copyright: http://wwww.phpbasic.com
	@Download: http://code.google.com/p/divbox
	@Contact: w2ajax@gmail.com for lastest version.
*/
(function($){
	$.fn.divbox = function(opt){
		var _cfg = {
			width: null, 
			height: null,
			left: null,
			top: null,
			type: null,
			src: 'href',
			scrollbar: 'auto',
			closed: '#divbox_frame .closed',
			prev: '#divbox_frame .prev',
			next: '#divbox_frame .next',
			path: 'players/',
			full_drag_handle: false,
			resize_large_image: true,
			overlay: true,
			caption: true,
			event: 'click',
			download_able:['pdf','zip','gz','rar','doc','docx','xls','xslx','ppt','pptx','csv']
			
		}
		if(opt) $.extend(_cfg,opt);

		var oMatch = this;
		var objArr = [];
		$(oMatch).each(function(i,o){
			objArr[i] = o;
		});
		function _run(index,init){
			var fn = {}
			fn.closed = function(){
				$('#divbox_frame').animate({
					'top': _click.top,
					'left': _click.left,
					width: '0px',
					height: '0px'
				},500,function(){
					$(this).remove();
					$('#divbox').remove();
					$('object,embed,select').css({'visibility':'visible'});
				});
			}
			fn.init = function(){
				var requires  = '#divbox,#divbox_frame,#divbox_content,#divbox_ajax';
				$(requires).remove();
				$('body').prepend('<div id="divbox"></div><div id="divbox_ajax"></div><div id="divbox_frame"><div class="closed"></div><div id="divbox_data"><div id="divbox_content"></div><div class="prev"></div><div class="caption"></div><div class="next"></div></div></div>');
				$(_cfg.closed+','+_cfg.next+','+_cfg.prev).hide();
				
				if(_cfg.overlay){
					$('#divbox').css({
						'width': sizesystem[0]+'px',
						'height': sizesystem[1]+'px',
						'position':'absolute',
						'zIndex':'10001',
						'left':'0',
						'top':'0'
					}).click(function(){
						fn.closed();
					});
				}
				$('#divbox_frame').css({
					'position':'absolute',
					'top': _click.top,
					'left': _click.left,
					'zIndex':'10002',
					'width': 0,
					'height': 0
				}).animate({
					width: 50, 
					height: 50,
					top: _cfg.top?sizesystem[3] +  _cfg.top: sizesystem[3] + Math.round(sizesystem[5]/2),
					left: _cfg.left?sizesystem[2] +_cfg.left:Math.round(sizesystem[4]/2) 
				});
				
				$(window).resize(function(){
					var sizesystem = pageSize(ie);
					$('#divbox').css({
						'width': sizesystem[0]+'px',
						'height': sizesystem[1]+'px'
					});
					var w = $(oFrame).outerWidth();
					var h = $(oFrame).outerHeight();
					$(oFrame).css({
						top: _cfg.top?sizesystem[3] +  _cfg.top: sizesystem[3] + Math.round((sizesystem[5] - w)/2),
						left:  _cfg.left?sizesystem[2] +_cfg.left:Math.round((sizesystem[4] - h)/2) 
					});
				});
			}
			fn.animate = function(t,l,w,h,fncallback,fnclosed, caption){
				$('#divbox').unbind('click');
				$(_cfg.closed+','+_cfg.next+','+_cfg.prev).hide();
				var border = ie?parseInt($(oFrame).css('border-left-width'))+parseInt($(oFrame).css('border-right-width')):0;
				$(oFrame).removeClass('white').animate({
					left:_cfg.left?_cfg.left:l,
					width:_cfg.width?_cfg.width+border:w+border
				},500).animate({
					top: _cfg.top?sizesystem[3] + _cfg.top:t, 
					height: _cfg.height?_cfg.height:h
				},500,function(){																																															 					if(typeof(fncallback) == 'function') fncallback($('#divbox_content'));
				
					$(_cfg.closed).show().click(function(){
						if(typeof(fnclosed) == 'function') fnclosed($('#divbox_content'));
						fn.closed();
					});
					
					$('#divbox').bind('click',function(){
						if(typeof(fnclosed) == 'function') fnclosed($('#divbox_content'));
						fn.closed();
					});
					
					$(this).addClass('white');
					if(_cfg.caption!=false){
						var c = $(oFrame).find('.caption');
						var cH = c.outerHeight(true);
						$(oFrame).animate({height: h + cH}).find('.caption').show();
						var btn_top = h + Math.round(cH/2)-12; // 12 = 1/2 height of button prev/next icon
						$(_cfg.prev+','+_cfg.next).css({top: btn_top}).show();
						if(index*1>0){
							$(_cfg.prev).removeClass('prevDisabled').bind('click',function(){
								fn.prevItem(index);
							});
						}else{
							$(_cfg.prev).addClass('prevDisabled').unbind('click');		
						}
						$(_cfg.next).html((index*1+1)+'/'+total)
						if(index*1<total - 1){
							$(_cfg.next).removeClass('nextDisabled').bind('click',function(){
								fn.nextItem(index);
							});
						}else{
							$(_cfg.next).addClass('nextDisabled').unbind('click');	
						}
					}
					$(document).keydown(function(e) {
						var k = e?e.keyCode:event.keyCode;
						if(k==27){
							if(typeof(fnclosed) == 'function') fnclosed($('#divbox_content'));
							fn.closed();
						}
					});
					try {
						$("#divbox_frame").draggable({ handle: $("#divbox_frame") }).css({ cursor: 'move' });
						if(!_cfg.full_drag_handle) $('#divbox_content').css({ cursor: 'pointer' });
					} catch(e) { /* requires jQuery UI draggables */ }
				});
			}
			
			fn.prevItem = function(index){
				if(index*1>0) _run(index*1 - 1);
			}
			
			fn.nextItem = function(index){
				if(index*1<total - 1) _run(index*1 + 1);
			}
			fn.parseType = function(src){
				if(_cfg.type) return _cfg.type;
				var aExt = src.split('.');
				var ext = aExt[aExt.length-1];
				return ext.toLowerCase();
			}
			fn.viewImage = function(src,caption){
				$('#divbox_content').html('<img src="'+src+'" />').find('img').hide();
				var Img = new Image();
				Img.src = src;
				Img.onload = function(){
					$('#divbox_content img').attr('src',src);
					var imgW = Img.width;
					var imgH = Img.height;
					var zoom = 0;
					if(_cfg.resize_large_image){
						if(imgW >= sizesystem[4] - 100 || imgH >= sizesystem[5] - 100){
							if(imgW >= sizesystem[4] - 100){
								imgW = sizesystem[4] - 100;
								imgH = Math.round(imgW*Img.height/Img.width);
							}
							if(imgH >= sizesystem[5] - 100){
								tH = sizesystem[5] - 100;
								imgW = Math.round(imgW*tH/imgH);
								imgH = tH;
							}
							zoom = 1;
						}
					}
					var top = sizesystem[3] + Math.round((sizesystem[5] - imgH)/2);
					var left = Math.round((sizesystem[4] - imgW)/2);
					fn.animate(top,left,imgW,imgH,function(o){
						$(o).find('img').css({'width': imgW,'height': imgH}).fadeIn();
					},false,caption)
					//IE 
					Img.onload=function(){};
				}
				
			}
			fn.flashEmbedString = function(file,w,h,type){ // default type is FLV
				var flashvar = '';
				if(type=='mp3') flashvar = '&provider=sound';
				var str = '<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="'+w+'" height="'+h+'">';
				str += '<param name="movie" value="'+_cfg.path+'player-viral.swf" />';
				str += '<param name="allowfullscreen" value="true" />';
				str += '<param name="flashvars" value="file='+file+'&autostart=true'+flashvar+'" />';
				str += '<embed';
					str += ' type="application/x-shockwave-flash"';
					str += ' id="player2"';
					str += ' name="player2"';
					str += ' src="'+_cfg.path+'player-viral.swf" ';
					str += ' width="'+w+'" ';
					str += ' height="'+h+'"';
					str += ' allowfullscreen="true"';
					str += ' flashvars="file='+file+'&start=true&autostart=true'+flashvar+'" ';
				str += ' />';
				str += '</object>';
				return str;
				
			}
			fn.viewFLV = function(src,caption){
				var winW = 400;
				var winH = 300;
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = Math.round((sizesystem[4] - winW)/2);
				var str = fn.flashEmbedString(obj.href,winW,winH,'flv');
				fn.animate(top,left,winW,winH,function(o){
					$(o).html(str);
				},false,caption);
			}
			fn.viewMP3 = function(src,caption){
				var winW = 320;
				var winH = 80;
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = Math.round((sizesystem[4] - winW)/2);
				var str = fn.flashEmbedString(src,winW,winH,'mp3');
				fn.animate(top,left,winW,winH,function(o){
					$(o).html(str);
				},false,caption);
			}
			fn.viewWMV = function(src,caption){
				var winW = 400;
				var winH = 300;
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = Math.round((sizesystem[4] - winW)/2);
				var str = '<object  type="application/x-oleobject" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"';
				str += ' width="'+winW+'" height="'+winH+'">';
				str += '<param name="filename" value="'+src+'" />';
				str += '<param name="Showcontrols" value="true" />';
				str += '<param name="autoStart" value="true" />';
				str += '<embed type="application/x-mplayer2" src="'+src+'" Showcontrols="true" autoStart="true" width="'+winW+'" height="'+winH+'"></embed>';
				str += '<object/>';
				fn.animate(top,left,winW,winH,function(o){
					$(o).html(str);
				},false,caption);
			}
			fn.viewSWF = function(src,caption){
				var winW = 400;
				var winH = 300;
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = Math.round((sizesystem[4] - winW)/2);
				var str = fn.flashEmbedString(src,winW,winH);
				fn.animate(top,left,winW,winH,function(o){
					$(o).html(str);
				},false,caption);
			}
			fn.viewElement = function(caption){
				var e = '#'+$(obj).attr('rel');
				var winW = $(e).outerWidth();
				var winH = $(e).outerHeight();
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = Math.round((sizesystem[4] - winW)/2);
				fn.animate(top,left,winW,winH,function(o){
					$(o).html($(e).html());
					$(o).find('object,embed,select').css({'visibility':'visible'});
					$(e).html('');
				},function(o){
					$(e).html($(o).html());
				},caption);
			}
			fn.viewAjax = function(src,caption){
				$.post(src,{},function(data){
					$('#divbox_ajax').html(data);
					var winW = $('#divbox_ajax').outerWidth();
					var winH = $('#divbox_ajax').outerHeight();
					var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
					var left = Math.round((sizesystem[4] - winW)/2);
					fn.animate(top,left,winW,winH,function(o){
						$(o).html(data);
						$('#divbox_ajax').remove();
					},false,caption);
					
				});
			}
			fn.viewDefault = function(src,caption){
				var winW = sizesystem[4]-100;
				var winH = sizesystem[5]-100;
				var top = sizesystem[3] + Math.round((sizesystem[5] - winH)/2);
				var left = sizesystem[2] + Math.round((sizesystem[4] - winW)/2);
				fn.animate(top,left,winW,winH,function(o){
					$(o).html('<iframe src="'+src+'" width="'+(winW-12)+'" frameborder="0" scrolling="'+_cfg.scrollbar+'" height="'+winH+'"></iframe>');	
				},false,caption);
			}
			var obj = objArr[index];
			var ie = $.browser.msie;
			var sizesystem = pageSize(ie);
			var total = $(oMatch).length;
			var _click = $(obj).offset();
			var src = $(obj).attr(_cfg.src).toString();
			var ext = fn.parseType(src);
			
			// Download able
			for(var i in _cfg.download_able) if(ext==_cfg.download_able[i]){
					return true;
			}
			
			//
			var caption = '';
			if(typeof(_cfg.caption)=='function'){
				caption = _cfg.caption(obj);
			}else if(_cfg.caption === true){
				caption = $(obj).attr('title');	
			}
			
			if(init) fn.init();
			$(_cfg.prev).unbind('click');
			$(_cfg.next).unbind('click');
			var oFrame = $('#divbox_frame');
			$(oFrame).find('.caption').hide().html(caption);
			
			switch(ext){
				case 'jpg':
				case 'jpeg':
				case 'gif':
				case 'png': fn.viewImage(src,caption);break;
				case 'flv':fn.viewFLV(src,caption);break;
				case 'wmv': fn.viewWMV(src,caption);break;
				case 'mp3': fn.viewMP3(src,caption);break;
				case 'swf': fn.viewSWF(src,caption);break;
				case 'element': fn.viewElement(caption); break;
				case 'ajax': fn.viewAjax(src,caption); break;
				default: fn.viewDefault(src,caption);break;
			}			
			return false;
		}
		$(oMatch).bind(_cfg.event,function(){
			var index = 0;
			for(var i in objArr) if(objArr[i] === this) index = i;
			return _run(index,true);
		});
	}
	function pageSize(ie){
		var de = document.documentElement;
		var winW = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		winW -= 18;
		var winH = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		var x = window.pageXOffset || self.pageXOffset || (de&&de.scrollLeft) || document.body.scrollLeft;
		var y = window.pageYOffset || self.pageYOffset || (de&&de.scrollTop) || document.body.scrollTop;
		var pW = window.innerWidth || document.body.scrollWidth || document.body.offsetWidth;
		var pH = window.innerHeight+window.scrollMaxY || document.body.scrollHeight || document.body.offsetHeight;
		var w = pW<winW?winW:pW; 
		w -= 18;
		var h = pH<winH?winH:pH;
		arrayPageSize = [w,h,x,y,winW,winH];
		return arrayPageSize;
	}
})(jQuery)

