/**
 * @author firstlast
 */

 NOU = {}
 
 NOU.show_iframe_flash = function(){
 	try{
		$(window.frames[0].document).find('.swap').hide();
		$(window.frames[0].document).find('object').show();
	}catch(err){}
 }
 
 NOU.hide_iframe_flash = function(){
 	try{
		$(window.frames[0].document).find('object').hide();
		$(window.frames[0].document).find('.swap').show();
	}catch(err){}
 }
 
NOU.story = {
	init: function(){
		$("#news_river li").live('click', NOU.story.show_modal);
		$(".story_modal").live('click', NOU.story.show_modal);
		
		$("#story_modal").dialog({
			bgiframe: true,
			modal: true,
			draggable: false,
			resizable: false,
			height:505,
			width:600,
			autoOpen: false
		});
	},
	
	show_modal: function(e) {
		e.preventDefault();
		$('#story_modal').bind('dialogopen', function(event, ui) {
			NOU.hide_iframe_flash();
		});
		
		$('#story_modal').bind('dialogclose', function(event, ui) {
			NOU.show_iframe_flash();
		});

		$("#story_modal").dialog('open');
	}
}

NOU.calculator = {
	
	lastTarget: null,
	keyupTimeout: null,
	
	init: function(){
		$("input[name=book], input[name=chapter], input[name=to_verse], input[name=from_verse]").keyup(NOU.calculator.keyupTimeoutFunction);
	},
	
	keyupTimeoutFunction: function(e){
		if(NOU.calculator.keyupTimeout){
			clearTimeout(NOU.calculator.keyupTimeout);
		}
		NOU.calculator.lastTarget = e.target;
		NOU.calculator.keyupTimeout = setTimeout('NOU.calculator.keyup()', 10);
	},
	
	keyup: function(e){
		var target = $(NOU.calculator.lastTarget);
		var name = target.attr('name');
		var last = target.attr('last_num');
		var book = parseRomanToInt(target.val());
//		console.debug(book);
//		console.debug(typeof(book));
//		console.debug(target.attr('last_num'));
		if(typeof(book) == 'number' && book != 0 && target.attr('last_num') != book){
			target.attr('last_num', book);
			target.removeClass('error');
			switch(name){
				case 'book':
//					$("#equals .book").fadeOut('fast', function(){
//						$("#equals .book").html(BOOKS[book-1]).fadeIn();
//					});
					$("#equals .book").html(BOOKS[book-1]);
					break;
				case 'chapter':
//					$("#equals ."+name).fadeOut('fast', function(){
//						$("#equals ."+name).html(book).fadeIn();
//					});
					$("#equals ."+name).html(book);
					break;
				case 'from_verse':
//					$("#equals ."+name).fadeOut('fast', function(){
//						$("#equals ."+name).html(":&#160;" + book).fadeIn();
//					});
					$("#equals ."+name).html(":&#160;" + book)
					break;
				case 'to_verse':
//					$("#equals ."+name).fadeOut('fast', function(){
//						$("#equals ."+name).html("-"+book).fadeIn();
//					});
					$("#equals ."+name).html("-"+book);
					break;
				default:
					break;
			}
		}else if(typeof(book) != 'number'){
			target.addClass('error').attr('last_num', 0);
		}
	},
	
	validateKeys: function(e){
		switch(e.keyCode){
			case 73:
			case 86:
			case 88:
				return true;
			default:
				if(e.keyCode <= 47 || e.keyCode >= 91){
					return true;
				}
				return false;
		}
		return false;
	}
}

BOOKS = ["Genesis","Exodus","Leviticus","Numbers","Deuteronomy","Joshua","Judges","Ruth","1 Samuel","2 Samuel","1 Kings","2 Kings","1 Chronicles","2 Chronicles","Ezra","Nehemiah","Esther","Job","Psalm","Proverbs","Ecclesiastes","Song of Solomon","Isaiah","Jeremiah","Lamentations","Ezekiel","Daniel","Hosea","Joel","Amos","Obadiah","Jonah","Micah","Nahum","Habakkuk","Zephaniah","Haggai","Zechariah","Malachi","Matthew","Mark","Luke","John","Acts","Romans","1 Corinthians","2 Corinthians","Galatians","Ephesians","Philippians","Colossians","1 Thessalonians","2 Thessalonians","1 Timothy","2 Timothy","Titus","Philemon","Hebrews","James","1 Peter","2 Peter","1 John","2 John","3 John","Jude","Revelation"];

$(document).ready(function(){
	NOU.story.init();
	NOU.calculator.init();	
});