var IE6 = /msie|MSIE 6/.test(navigator.userAgent);
function load(){
	Lightbox.init();
}
var Lightbox = {
	init:function(){
		this.fx_length=400;
		if($('fboverlay') == null){
			var fboverlay = new Element('div',{id:'fboverlay'});
			fboverlay.inject(document.body);
		}
		if($('formbox_container') == null){
			var formboxcontainer = new Element('div',{id:'formbox_container'});
			formboxcontainer.inject(document.body);
		}
		$('fboverlay').setStyle('opacity', 0);
		$('fboverlay').addEvent('click', this.hide.bind(this))
		this.showfx = new Fx.Tween($('fboverlay'), {
			duration:this.fx_length,
			wait:false,
			onComplete:function(){
				$('formbox_container').setStyle('display','block');
			}
		});
		this.hidefx = new Fx.Tween($('fboverlay'),{
			duration:this.fx_length,
			wait:false,
			onComplete:function(){
				if($('flash') != null){
					$('flash').setStyle('visibility','visible');
				}
				$('fboverlay').setStyle('display','none');
			}
		});
		this.scan();
	},
	open:function(url, width, height){
		this.show(width, height);
		var req = new Request.HTML({
			method: 'get',
			url: url,  
			update: $('formbox_container'),
			evalScripts:true
		}).send();
	},
	submit:function(form, width, height, callback){
		this.show(width, height);
		if(callback != null){
			this.form = form;
			callback();
		}
		var req = new Request.HTML({
			url: form.action,  
			update: $('formbox_container'),
			evalScripts:true
		}).post(form);
	},
	show:function(width, height){
		$('formbox_container').setStyle('background','#FFFFFF');
		$('formbox_container').innerHTML='<div id="loading"><img src="/images/indicator.gif" alt="loading..."/></div>"'
		if($('fboverlay').getStyle('display') != 'block'){
			if(IE6){
				var win_width=document.body.clientWidth;
				var flash_height=$('fake_flash').getSize().y+20;
				var win_height=document.body.clientHeight>flash_height?document.body.clientWidth:flash_height;
				var top=document.body.clientHeight;
				var left=document.body.clientWidth;
			}else{
				var win_width=Window.getScrollSize().x;
				var win_height=Window.getScrollSize().y;
				var top=Window.getSize().y;
				var left=Window.getSize().x;
			}
			$('fboverlay').setStyles({
				display:'block',
				width:win_width,
				height:win_height
			});
			top = (top - parseInt(height))/2 + Window.getScroll().y;
			top=top<=0?10:top;
			left = (left - parseInt(width))/2 + Window.getScroll().x;
			left=left<=0?10:left;
			$('formbox_container').setStyles({
				display:'block',
				width:width+'px',
				height:height+'px',
				top:top+'px',
				left:left+'px'
			});
			if($('flash') != null){
				$('flash').setStyle('visibility','hidden');
			}
			this.showfx.start('opacity', .8);
		}
	},
	hide:function(){
		$('formbox_container').setStyles({
			display:'none',
			background:'transparent'
		});
		$('formbox_container').getChildren()[0].dispose();
		$('formbox_container').innerHTML='<img src="/images/indicator.gif"/>';
		this.hidefx.start('opacity', 0);
	},
	scan:function(){
		$$('.lightbox_link').each(function(el){
			var width=500;
			var height=400;
			if(el.hasClass('lightbox_large'))
				height=width=600;
			el.addEvent('click', this.open.bind(this, [el.href, width, height]));
			el.href='#';
			el.removeClass('lightbox_link');
		}.bind(this));
		$$('.lightbox_hide').each(function(el){
			el.addEvent('click', this.hide.bind(this));
			el.removeClass('lightbox_hide');
		}.bind(this));
		$$('.lightbox_form').each(function(el){
			var button = el.getElement('.lightbox_button');
			if(button != null){
				var callback;
				if(button.onclick != null){
					callback = button.onclick;
					button.onclick=null;
				}
				var width=500;
				var height=400;
				if(button.hasClass('lightbox_large'))
					height=width=600;
				button.addEvent('click', this.submit.bind(this, [el,width,height,callback]));
				el.removeClass('lightbox_form');
				button.removeClass('lightbox_button');
			}
			var cancel = el.getElement('.lightbox_cancel');
			if(cancel != null){
				var width=500;
				var height=400;
				if(cancel.hasClass('lightbox_large'))
					height=width=600;
				cancel.addEvent('click', this.open.bind(this, [cancel.id, width, height]));
				cancel.removeClass('lightbox_cancel');
			}
		}.bind(this));
	}
}
function buildFancyUpload(el, fileLimit, returnTo){
	var data = new Element('form');
	el.getElements('input.required').each(function(el){
		el.inject(data);
	});
	var swiffy = new FancyUpload2(el.getElement('.status'), el.getElement('.list'), {
		url: el.getElement('form').action,
		fieldName: 'Filedata',
		limitFiles: fileLimit,
		onLoad: function() {
			el.getElement('.status').removeClass('hide');
			el.getElement('.fallback').destroy();
		},
		onComplete:function(file, response){
			file = swiffy.getFile(file);
			var line = new Element('div', {
				'html':response,
				'class':'image_title'
			});
			file.element.empty();
			line.inject(file.element);
			file.element.removeClass('file-uploading');
			file.element.removeClass('file');
			file.element.addClass('uploaded-file');
			Lightbox.open(returnTo, 600, 600);
		},
		data:data
	});
 	el.getElement('.browse').addEvent('click', function() {
		swiffy.browse({'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'});
		return false;
	});
 	el.getElement('.clear').addEvent('click', function() {
		swiffy.removeFile();
		return false;
	});
    el.getElement('.go').addEvent('click', function() {
		swiffy.upload();
		return false;
	});
}

//removes dotted outlines from links
function remove_dotted_outlines(){
	$$('a').each(function(el,i){
		el.addEvent('mousedown',function(e){
			this.blur();
		    this.hideFocus = true;
		});
		el.addEvent('mouseout',function(e){
			this.blur();
		    this.hideFocus = false;
		});
		el.addEvent('mouseup',function(e){
			this.blur();
		    this.hideFocus = false;
		});
	});
}

function compileData(){
	var data = '';
	var form = Lightbox.form;
	form.getElements('.data').each(function(el){
		var local_data = ''
		var add=true;
		el.getElements('.data_cell').each(function(txt){
			var value = txt.getElements('input,select')[0];
			if(value.hasClass('required') && value.value == '')
				add=false;
			var stuff = value.value;
			var pre = txt.getElements('.pre').length==0?'':txt.getElements('.pre')[0].innerHTML;
			var post = txt.getElements('.post').length==0?'':txt.getElements('.post')[0].innerHTML;
			if(pre!='' && stuff.indexOf(pre)!=0 && stuff != ' ')
				stuff = pre + stuff;
			if(post!='' && (stuff.lastIndexOf(post)==-1 || stuff.lastIndexOf(post)!=stuff.length-post.length) && stuff != ' ')
				stuff += post
			local_data+=stuff+'~';
		});
		if(add && local_data != '')
			data += local_data.substring(0,local_data.length-1)+'`';
		el.dispose();
	});
	data = data.substring(0,data.length-1);
	var input = new Element('input');
	input.setProperties({
		type:'hidden',
		value:data,
		name:'resource[data]',
		id:'resource_data'
	});
	input.inject(form);
}
function popup(Url){
	Url=Url.split(",");
	if(Url[3] != null && Url[3] == '1'){
		newwindow = window.open('','_blank');
		newwindow.location=Url[0];
	}else{
		Lightbox.open(Url[0], Url[1], Url[2]);
	}
}

