var mousemove = true;
var delay = 1500;
var count = 0;
var open = false;
var width    = 200;
var thumbs = $$('div.thumb img');
var center_height = (window.getHeight() / 2)- 40;
var overlay = new Element('div', { 
	'id': 'overlay', 
	'styles': { 
		'height': window.getScrollHeight(), 
		'width': window.getWidth(), 
		'opacity': 0 
	}
}).injectBottom(document.body);
var closeIntro = function() {
	if(document.id('loading')) document.id('loading').fade('out').dispose();
	mousemove = true;
	new Fx.Tween('inboxdesign', { duration: 1000, onComplete: function() {
		new Fx.Morph('container', { duration: 2500 }).start({
			'display': ['none', 'block']
		});
		thumbs.each(function(thumb) {
			var open     = false;
			var top      = thumb.getPosition().y;
			var left     = thumb.getPosition().x;
			var new_left = (thumb.getParent('div').get('class').test('right')) ? (left - width) : left;
			var fx       = new Fx.Morph(thumb, { duration: 1000, onComplete: function() { open = (open==true) ? false : true; } });
			thumb.addEvents({
				'mouseenter': function() { 
					if(open==false) {
						top      = thumb.getPosition().y;
						left     = thumb.getPosition().x;
						new_left = (thumb.getParent('div').get('class').test('right')) ? (left - width) : left;
						fx.start({
							'width'   : [width, 470], 
							'top'     : [top, (top - 80)],
							'left'    : [left, new_left]
						});
						thumb.setStyle('z-index', 1000);
					}
					thumbs.fireEvent('mouseleave');
				},
				'mouseleave': function() { 
					if(open==true) {
						fx.start({
							'width'   : [470, width], 
							'top'     : [(top - 80), top],
							'left'    : [new_left, left]
						});
						thumb.setStyle('z-index', 1);
					}
				}
			});
		});
	}}).start('margin-top', center_height, '100px');
}
var login_form_morph = function() {
	var form_top  = document.id('login-form').getPosition().y - 200;
	var form_left = document.id('login-form').getPosition().x;
	var form_focus = false;
	var inputs = $$('#login-form input[type!="submit"]');
	inputs.each(function(inpt) {
		inpt.addEvent('focus', function(blr) {
			open = false;
			if(form_focus==false) {
				form_focus = true;
				overlay.fade(0.5);
				var fieldset = document.id('login-form').getElement('fieldset');
				var fx_fieldset = new Fx.Morph(fieldset, { 
					duration: 1500, 
					onComplete: function() { 
						var legend = fieldset.getElement('legend');
						legend.setStyle('background-color', '#ffffff');
						new Element('img', { 'src': 'http://static.inboxdesign.co.nz/assets/images/sprites/close.png', 'width': '18px', 'height': '18px'}).injectBottom(legend).addEvent('click', function(clk) {
							clk.stop();
							form_focus = false;
							overlay.fade(0);
							inputs.each(function(inpt) {
								new Fx.Morph(inpt, { duration: 1500, }).start({ 'width': '220px', 'font-size': '12px'});
							});
							this.dispose();
							new Fx.Morph(fieldset, { duration: 1500 }).start({
								'top': form_top,
								'left': form_left,
								'padding': '10px',
								'width':' 240px'
							});
							legend.setStyle('background-color', 'transparent');
							fieldset.setStyle('background-color', 'transparent');
						});
					}
				}).start({
					'top': form_top,
					'left': form_left - 350,
					'padding': '30px',
					'width':' 500px'
				});
				fieldset.setStyles({'z-index': 120000, 'background-color': '#ffffff'});
				inputs.each(function(inpt) {
					new Fx.Morph(inpt, { duration: 1500, }).start({ 'width': '480px', 'font-size': '16px'});
				});
			}
		});
	});
	document.id('login-form').addEvent('submit', function(submt) {
		submt.stop();
		alert('Login Faild, Please try again.');
	});
	document.id('reset-password').addEvent('click', function(clk) {
		clk.stop();
		load_forgot_password();
	});
}
var load_login_form = function() {
	new Request.HTML({
		method: 'get', 
		url: 'jaxx.php?action=login_form',
		onComplete: function(r1, r2, html, js) {
			$exec(js);
			document.id('login-wrap').fade(0).empty().set('html', html).fade(1);
			login_form_morph();
		}
	}).send();
}
var load_forgot_password = function() {
	new Request.HTML({
		method: 'get', 
		url: 'jaxx.php?action=forgot_password',
		onComplete: function(r1, r2, html, js) {
			$exec(js);
			document.id('login-wrap').fade(0).empty().set('html', html).fade(1);
			login_form_morph();
		}
	}).send();
}
window.addEvents({
	'mousemove': function() {

		if(mousemove==false) closeIntro();
		
	},
	'scroll': function() {
		
		if(mousemove==false) closeIntro();
		open = false;
		
	}, 
	'load': function() {
		
		(function() { 
			mousemove = false; 			
		}).delay(delay);
		
		(function() {
			(function() {
				count+=1;
				if(mousemove == false) {
					if(!document.id('loading')) {
						new Element('img', {'id': 'loading', 'src' : 'http://static.inboxdesign.co.nz/assets/images/sprites/loading.gif', 'styles': {
								'position': 'absolute',
								'bottom': 10,
								'right': 10,
								'opacity': 1
							}
						}).injectBottom(document.body);
					}
					if(count == 4) closeIntro();
				}
			}).periodical(500);
		}).delay(delay / 2);
		
	},
	'domready': function() {
		
		load_login_form();
		
		new Fx.SmoothScroll(document.body).toTop();
		var lat  = geoip_latitude();
		var long = geoip_longitude();
		var city = geoip_city();
		var out  = '<iframe style="float:right;" width="350" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q='+ lat +','+ long +'&amp;sll='+ lat +','+ long +'&amp;sspn=0.045396,0.098448&amp;ie=UTF8&amp;ll='+ lat +','+ long +'&amp;spn=0.0227,0.049224&amp;z=15&amp;output=embed"></iframe>'+ 
			'<ul>'+
			'<li>Latitude: ' + lat + '</li>'+
			'<li>Longitude: ' + long + '</li>'+
			'<li>City: ' + city + '</li>'+
			'<li>Region: ' + geoip_region() + '</li>'+
			'<li>Region Name: ' + geoip_region_name() + '</li>'+
			'<li>Postal Code: ' + geoip_postal_code() + '</li>'+
			'<li>Country Code: ' + geoip_country_code() + '</li>'+
			'<li>Country Name: ' + geoip_country_name() + '</li>'+
			'</ul>';
		document.id('info').set('html', out);
		
		document.id('inboxdesign').setStyle('margin-top', center_height);
		
		Cufon.replace('h1, h2');

	}
});