//----GLOBAL VARIABLES
var base_URL="";
var recentHeight=0;
var fullHeight=0;
var isSet=0;

//----------------------ONLOAD FUNCTION---------------------
$(document).ready(function(){

	//clear out text boxes when clicked in (search and email box)
	$("input:text").focus(function() {     
		var element = $(this);     
		element.attr("rel",element.val()); 
		element.val('');   
	});   
	$("input:text").blur(function() {     
		var element = $(this);     
		if (element.val() == '') {
			element.val(element.attr("rel"));
		}
	}); 
	  
	  
});

function EnterPressed(e) {
// Code adapted from Jennifer Madden
// http://jennifermadden.com/162/examples/stringEnterKeyDetector.html
var characterCode
if(e && e.which){           // NN4 specific code
e = e
characterCode = e.which
}
else {
e = event
characterCode = e.keyCode // IE specific code
}
if (characterCode == 13) return true   // Enter key is 13
else return false
}
//----------------------STRING FUNCTIONS---------------------
function reverseString(str){
	str = findIndex(str,"[lbr]","\n");
	str = findIndex(str,"[amp]","&");
	str = findIndex(str,"[pnd]","#");
	str = findIndex(str,"[eql]","=");
	str = findIndex(str,"[apos]","'");
	str = findIndex(str,"[dpos]",'"');
	str = findIndex(str,"[ques]","?");
	
	return str
}
function findIndex(str,replace,replaceWith){
	if (str!=undefined){
		if (str.toString().indexOf(replace)>-1){
			str = str.toString().split(replace).join(replaceWith);
		}
	}
	return str
}
function isValidEmail(strEmail){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	if (strEmail.search(validRegExp) == -1){
		alert('A valid e-mail address is required.');
		return false;
	} 
	return true; 
}
function convertMonth(m){
	if (m=="01" || m=="1"){
		return "January";
	}else if (m=="02" || m=="2"){
		return "February";
	}else if (m=="03" || m=="3"){
		return "March";
	}else if (m=="04" || m=="4"){
		return "April";
	}else if (m=="05" || m=="5"){
		return "May";
	}else if (m=="06" || m=="6"){
		return "June";
	}else if (m=="07" || m=="7"){
		return "July";
	}else if (m=="08" || m=="8"){
		return "August";
	}else if (m=="09" || m=="9"){
		return "September";
	}else if (m=="10"){
		return "October";
	}else if (m=="11"){
		return "November";
	}else if (m=="12"){
		return "December";
	}
}
//----------------------REUSED FUNCTIONS---------------------
function notop(){};
function scrollToTop(){
	$('body').animate({scrollTop:0}, 'slow');
	$('html').animate({scrollTop:0}, 'slow');
	$('html, body').animate({scrollTop:0}, 'slow'); 
}

//-------------------LOAD SIDE RECOMMENDED READING FROM EXTERNAL FILE
function loadRecommended(){
	$.ajax({
		type: "GET",
		url: "data/blogXML/recommended_reading.xml",
		dataType: "xml",
		success: function(xml) {
		 $(xml).find('item').each(function(){
			//var id_text = $(this).attr('id')
			var name_text = $(this).find('name').text()
			var url_text = $(this).find('url').text()
			$('#recommendedList').append('<li><a href="'+url_text+'" target="_blank">'+name_text+'</a></li>')
		 });
		}
	});

}
//-------------------LOAD FOOTER CONTACT INFORMATION
function loadFooterContact(){
	$("#column_2").load("data/blogXML/footerContactInformation.html");
}

//-------------------NEWSLETTER SIGNUP
function submitAnnouncement(){
	if ($('#announcement').val()!="email"){
		if (isValidEmail($('#announcement').val())){
			$.ajax({ type: "POST",
				url: base_URL+"json_interface.php?action=submitAnnouncement&email="+$('#announcement').val(),
				dataType: "json",
				success: function(obj) {
					$('#announcement').val("email");
					$('.blockScreen').fadeIn(500, function(){
						setTimeout('complete();',1500);
					});
				}
			});
		}
	}
}
function complete(){
	$('.blockScreen').fadeOut(500);
}


//-------------------LOAD SIDE RECENT POSTS
function loadSideRecentPosts(page){
	var _ret='';
	$.getJSON(base_URL+"json_interface.php?action=requestItemsForBlog&blog= and active=1&order=blog_date",function(obj) {
		$.each(obj.posts,function(i,item){
			if (i<=7){
				var _bod=reverseString(item.title);
				if (_bod.length>45){
					_bod=_bod.substring(0,45)+'...';
				}
				
				if(page=="interior"){
					_ret+='<li><a href="javascript:notop();" onclick="javascript:loadPost(\'interior.html#/'+item.tbl+'/'+item.id+'\');">' + _bod + '</a></li>';
				}else{
					_ret+='<li><a href="interior.html#/'+item.tbl+'/'+item.id+'">' + _bod + '</a></li>';
				}
			}
		});
		$("#mostRecentPosts").html(_ret);
		//initiate paging for recent posts
		isSet=0;
		//FOR THE TIME BEING DONT SHOW PAGING
		$('#recentPaging').fadeOut(500);
	});
}
function moreRecent(){
	if (isSet==0){ //get the height just once
		recentHeight=$('#mostRecentPosts').css('height');
		recentHeight=recentHeight.split("px").join("");
		recentHeight=parseInt(recentHeight);
		fullHeight=recentHeight;
		isSet=1;
	}
	if ((recentHeight-265)>266){ //one entire page move
		recentHeight=recentHeight-266;
		$("#mostRecentPosts").animate({"top": "-=266px"}, "slow");
		if ((recentHeight-266)<5){
			recentHeight=0;
			$('#recentPaging').fadeOut(500);
			$('#lessRecentPaging').fadeIn(500);
		}
	}else{ //only move up partial page
		//alert((recentHeight-266)<5);
		$("#mostRecentPosts").animate({"top": "-="+(recentHeight-266)+"px"}, "slow");
		if ((recentHeight-266)<5){
			recentHeight=0;
			$('#recentPaging').fadeOut(500);
			$('#lessRecentPaging').fadeIn(500);
		}
	}
}
function lessRecent(){
	if ((recentHeight+265)>266){
		recentHeight=recentHeight+266;
		$("#mostRecentPosts").animate({"top": "+=266px"}, "slow");
		if ((recentHeight)<5){
			recentHeight=fullHeight;
			$('#recentPaging').fadeIn(500);
			$('#lessRecentPaging').fadeOut(500);
		}
	}else{ //only move up partial page
		$("#mostRecentPosts").animate({"top": "+="+(recentHeight+266)+"px"}, "slow");
		if ((recentHeight)<5){
			recentHeight=fullHeight;
			$('#recentPaging').fadeIn(500);
			$('#lessRecentPaging').fadeOut(500);
		}
	}
}

//read the page document height and adjust the height of the Block Screen DOM.
function determineBG_Height(){
	//This is a hack to determine the height of the webpage and set the BG to 100% of the page. FF doesn't like the CSS that does it.
	$('.blockScreenDetail').height(($(document).height()+10)+'px');
}