var current_form;
var lightbox;
var index = 0;
var biggestw = 1800;
var biggesth = 1800;
var baseurl;

window.addEvent('domready', function() {
	
	baseurl = window.location;
	$$('.envelope').addEvent('click',function(e) {
		if(e.event.clientX > window.getSize().x * .5)
		galleryForward();
		else
		galleryBack();
	});
	
	var me = new Sortables('.sortable', {
		opacity:0,
		clone: true,
		revert: true,
		constrain: false,
		onSort: function(r) {
			
		},
		initialize: function(){
		
		},
		onComplete: function(r) {
			new Request({
				url: base_url + 'ajax/misc/save_order',
				data: { items: me.serialize(false,function(element, index){
				    return element.getProperty('id');
				}) }
			}).send();
		}
	});
	
	window.addEvent('keydown', function(key) {
		if(key.code==39 && !key.shift && !key.alt && !key.control && !key.shift) {
			galleryForward();
		}
		if(key.code==37 && !key.shift && !key.alt && !key.control && !key.shift) {
			galleryBack();
		}
	});

	$$('.body-padding').each(function(el) {
		el.adopt(new Element('div',{'html':'(click image to move forward)', 'class':'label'}));
	});

	var hash = window.location.hash;
	hash = hash.substring(1);

	if(hash)
	galleryJumpTo(hash,true);
	
	$$('.bodywrapper').setStyle('display','block');
	
	reviveTiny();
	checkSizes();
	getFocus();
});

window.addEvent('resize', function() {
	checkSizes();
});

function checkSizes() {
	
	var tpadding = 60;
	
	biggestw = window.getSize().x - 0;
	
	$$('.envelope').each(function(e) {
		
		e.getParent().setStyle('width', biggestw);
		
		var c = 0;
		var bigh = window.getSize().y;
		
		e.getParent().setStyle('overflow','hidden');
		
		if(e.getChildren().length > 1)
		e.setStyle('cursor','pointer');

		if(e.getChildren()[0].hasClass('vertical')) {
			var ch = (window.getSize().y - 40);
			var cw = (ch * 4) / 6;
			if(window.getSize().x - 40 < cw) {
				var cw = (window.getSize().x - 40);
				var ch = (cw * 6) / 4;
			}
		}else{
			var ch = (window.getSize().y - 40);
			var cw = (ch * 6) / 4;
			if(window.getSize().x - 40 < cw) {
				cw = window.getSize().x - 40;
				ch = (cw * 4) / 6;
			}
		}
		
		e.setStyle('width',cw+'px');
		e.setStyle('height',ch+'px');
		
		e.getChildren().each(function(i) {
			
			biggestw = cw - 50;
			bigh = ch - 40;
			
			var h = i.get('h');
			var oh = h;
			var w = i.get('w');
			var ow = w;
			
			i.setStyle('width', biggestw + 'px');
			i.setStyle('position','absolute');
			
			var allowedh = ch - 60;
			
			if(i.getChildren().length==2) {
				biggestw = ((cw - 40) * .5) - 20;
			}
			
			var t = i.getChildren('.txt');
			t = t[0];
			
			i.getChildren('.txt').each(function(child) {
				if(biggestw < 300) {
					var tw = 300;
					e.getParent().setStyle('width', 600);
				}else{
					var tw = biggestw;
				}
				child.setStyle('width',tw + 'px');				
			});
			
			if(i.hasClass('layoutbelow') || i.hasClass('layoutabove')) {
				i.getChildren('.txt').setStyle('width','100%');
				allowedh = ch - t.getFirst().getSize().y - tpadding + 20;
				i.getChildren('.image').setStyle('margin-top','-' + (t.getFirst().getSize().y * .5) + 'px');
			}else if(i.hasClass('layoutright')){
				i.getChildren('.image').each(function(child) {
					//child.setStyle('text-align','right');
				});
			}
						
			if(allowedh < h) {
				i.getChildren('.image').each(function(child) {
					h = allowedh;
					child.getChildren().setStyle('height', h + 'px');
					w = w * h / oh;
					child.getChildren().setStyle('width', w + 'px');
					//child.setStyle('width', w + 'px');				
				});
			}
			
			if(biggestw < w) {
				i.getChildren('.image').each(function(child) {
					w = biggestw;
					child.getChildren().setStyle('width', w + 'px');
					h = oh * w / ow;
					child.getChildren().setStyle('height', h + 'px');
				});
			}
			
			if(t.getFirst().getSize().y > h)
			h = t.getFirst().getSize().y;
			var top = (bigh * .5) - (h * .5);
			if(top < 20) top = 20;

			if(i.getChildren().length==2) {
				
			}else{
				i.setStyle('left', (cw-biggestw) * .5 + 'px');
			}
			
			i.setStyle('top', (ch-h) * .5 + 'px');
			
			c++;
		});
		e.setStyle('margin-top', (window.getSize().y - ch) * .5 + 'px');
		e.getParent().setStyle('height', window.getSize().y - 0 + 'px');
		show(e);
	});
	
}

function galleryBack() {
	var gal = $$('.envelope')[0];
	index = index == 0 ? gal.getChildren('.wraps').length-1 : index-1;
	galleryJumpTo(index);
}

function galleryForward() {
	$$('.label').tween('opacity',0);
	var gal = $$('.envelope')[0];
	index = index+1 >= gal.getChildren('.wraps').length ? 0 : index+1;
	galleryJumpTo(index);
}

function galleryJumpTo(i,notween) {
	return;
	var gal = $$('.envelope')[0];
	index = i = parseInt(i);
	gal.set('tween', { duration: 500, transition: Fx.Transitions.Quint.easeOut });
	$$('.count').set('html', (i+1) + ' / ' + gal.getChildren('.wraps').length);
	if(notween)
	gal.setStyle('margin-left', (-biggestw * i) + 'px');
	else
	gal.tween('margin-left', (-biggestw * i) + 'px');
	//window.location = base_url + "#" + index;
}

///---------------------------------APPLICATION AGNOSTIC---------------------------------///
///---------------------------------APPLICATION AGNOSTIC---------------------------------///
///---------------------------------APPLICATION AGNOSTIC---------------------------------///
function toggle(who) {
	if(who.hasClass('hidden')) {
		show(who);
	} else {
		hide(who);
	}
}
function show(who) {
	who.removeClass('hidden');
}
function hide(who) {
	who.addClass('hidden');
}

function refreshItem(who,args) {
	loadURL(who.get('url'),who,args);
}

function submitForm(which,args) {
	new Request({
		url: which.action,
		data: which ? which : args,
		onComplete: function(response) { 
			if(args && args.onComplete)
			args.onComplete(response);
		}
	}).send();
}

function loadURL(url,div,args) {
	if(div)
	div.set('url',url);
	new Request({
		url: url,
		data: args && args.form ? args.form : args,
		onComplete: function(response) { 
			if(div)
			div.innerHTML = response;
			if(args && args.onComplete)
			args.onComplete(response);
		}
	}).send();
}

function getFocus() {
	var nodes = $$('.focus');
	for(var i=0; i<nodes.length; i++) {
		nodes[i].focus();
	}
}

function delete_obj(args) {
	var prompt = args.prompt ? args.prompt : "Are you absolutely sure you'd like to delete this?";
	if(!confirm(prompt)) return;
	try {
		args.node.destroy();
	}catch(err){
	
	}
	try {
		$(args.node).destroy();
	}catch(err){
	
	}
	new Request({
		url: base_url + 'ajax/misc/delete',
		data: args,
		method: 'post',
		onComplete: function(response) { 
			if(args.onComplete)
			args.onComplete();
		}
	}).send();
}

function killTiny() {
	var textareas = $$('.tinymce_input');
	for(var i=0; i<textareas.length; i++) {
		tinyMCE.execCommand('mceRemoveControl', true, textareas[i].get('id'));
	}
}

function reviveTiny() {
	var textareas = $$('.tinymce_input');
	for(var i=0; i<textareas.length; i++) {
		tinyMCE.execCommand('mceAddControl', true, textareas[i].get('id'));
	}
}