function confirmClick(e)
{
    if (!confirm('Are you sure?')) {
        Event.stop(e);
        return false;
    }
    
    return true;
}

var LaterLosers = {
	user: null,
    getLogin: function() {
        new Ajax.Request('/js.php?get=user', { onSuccess: LaterLosers.processUser});
    },
    processUser: function(xhr) {
        try {
            var user = eval("(" + xhr.responseText + ")");
            LaterLosers.user = user;
        } catch (e) {
        }
        LaterLosers.initUser();
    },
	showDialogBox: function() {
		scrollTo(0,0);
		LaterLosers.shade();
		
		var width = $('dialog_box').getDimensions().width;
		var screenX = document.viewport.getWidth();
		
		var left = Math.floor( (screenX / 2) - (width/2));
		
		$('dialog_box').style.left = left + 'px';
		$('dialog_box').style.display='block';
	},
	hideDialogBox: function() {
		LaterLosers.unshade();
		$('dialog_box').innerHTML = '';
		$('dialog_box').style.display='none';
	},
	unshade: function() {
		if (!$('dialog_shade')) {
			return;
		}
		$('dialog_shade').style.display='none';
	},
	shade: function() {
		var shade = $('dialog_shade');
		if (!shade) {
			var shade = document.createElement('div');
			shade.id='dialog_shade';
			$('body').appendChild(shade);
			shade = $('dialog_shade');
		}

		shade.style.width=document.viewport.getWidth() + 'px';
		shade.style.height=document.viewport.getHeight() + 'px';
		shade.style.display='block';
	},
	updateChat: function()
	{
		new Ajax.Updater($('post_entries'), '/js.php?get=chat_posts&post_id=' + $('post_id').getValue());
		new Ajax.Updater($('post_images'), '/js.php?get=chat_images&post_id=' + $('post_id').getValue());
	},
	processWord: function(req)
	{
		var data = eval("(" + req.responseText + ")");
		$('ll_word').innerHTML = data['word'];
		$('ll_word_code').value = data['word_code'];
	},
	hasPriv: function(priv)
	{
		if (LaterLosers.user && LaterLosers.user.privs) {
			var ret = false;
			LaterLosers.user.privs.each(function(_priv) {
				if (_priv.priv==priv) {
					ret = true;
				}
			});
			return ret;
		} else {
		 	return false;
		}
		
	},
	showNewImage: function(e) {
		if (e) {
			Event.stop(e);
		}
		if (!$('new_image_form')) {
			new Ajax.Updater($('dialog_box'), '/js.php?get=new_image_form&post_id=' +$('post_id').getValue(), { onComplete: function() { LaterLosers.showNewImage() } });
			return false;
		}
		
		$('post_cancel').onclick = LaterLosers.hideDialogBox;
		$('new_image_form').onsubmit = LaterLosers.submitNewImage;
		LaterLosers.showDialogBox();
		return false;
	},
	submitNewImage: function(e) {
		if ($('uploadTarget')) {
			$('uploadTarget').remove();
		}
		var iframe = document.createElement('iframe');
		iframe.id='uploadTarget';
		iframe.name='uploadTarget';
		iframe.onload=LaterLosers.processImages;
		iframe.style.display = 'none';
		$('dialog_box').appendChild(iframe);
		if(window.frames['uploadTarget'].name != 'uploadTarget') { 
			/* *** IMPORTANT: This is a BUG FIX for Internet Explorer *** */ 
			window.frames['uploadTarget'].name = 'uploadTarget';
		}
		
		$('new_image_form').setAttribute('target', 'uploadTarget');
		$('upload_result').innerHTML = 'Uploading...';
		
		return true;
	},
	processImages: function() {
		var iframe = $('uploadTarget');
		var doc, body;
	
		if (iframe.contentDocument) {
			// For NS6
			doc = iframe.contentDocument; 
		} else if (iframe.contentWindow) {
			// For IE5.5 and IE6
			doc = iframe.contentWindow.document;
		} else if (iframe.document) {
			// For IE5
			doc = iframe.document;
		} else {
			$('upload_result').innerHTML = 'Unable to view result. Please try again';
			return true;
		}

		body = doc.body;
		if (body.innerHTML) {
			try {
				var result = eval("(" + body.innerHTML + ")");
			} catch(e) {
				alert("Error: " + e);
			}
			
            if (result.error_message) {
            	$('upload_result').innerHTML = result.error_message;
            } else {
				LaterLosers.updateChat(); 
				LaterLosers.hideDialogBox();
            }
		}
	},
	showNewPost: function(e) {
		if (e) {
			Event.stop(e);
		}
		if (!$('new_post_form')) {
			new Ajax.Updater($('dialog_box'), '/js.php?get=new_post_form&post_id=' +$('post_id').getValue(), { onComplete: function() { LaterLosers.showNewPost() } });
			return false;
		}
		
		$('post_cancel').onclick = LaterLosers.hideDialogBox;
		$('new_post_form').onsubmit = LaterLosers.submitNewPost;
		LaterLosers.showDialogBox();
		return false;
	},
	submitNewPost: function(e) {
	
		var post_id = $('post_id').getValue();
		var opts = {
			'post':'post_text',
			'post_id': post_id,
			'post_text': $('post_text').getValue()
		}
		new Ajax.Request('/js.php', { parameters: opts, onComplete: function() { LaterLosers.updateChat(); LaterLosers.hideDialogBox(); }});
		Event.stop(e);
		return false;
	},
	initUser: function() {
	
		if (LaterLosers.user) {
		}
	},
	init : function() {
	    $$('.confirm').invoke('observe', 'click', confirmClick);

		if ($('ll_word')) {
			new Ajax.Request('/ll_captcha.php', { onSuccess: LaterLosers.processWord });
		}
		
		if ($('new_chat_post')) {
			$('new_chat_post').observe('click', function(e) { Event.stop(e); LaterLosers.showNewPost();});
		}

		if ($('new_chat_image')) {
			$('new_chat_image').observe('click', function(e) { Event.stop(e); LaterLosers.showNewImage();});
		}

		if ($('chat_post_id')) {
			setInterval(LaterLosers.updateChat, 15000);
		}
		
		LaterLosers.hideDialogBox();
		LaterLosers.getLogin();
	}
}

window.onload = LaterLosers.init;
