var isIE;
var isIE6;
var isIE7;
var isIE8; 
var isLessThanIE8;
var buttonMarkup;
var rightrailHeight = 556;

$(document).ready(function() {
	
	//initialize
	isIE = $.browser.msie;
	isIE6 = isIE && $.browser.version=='6.0';
	isIE7 = isIE && $.browser.version=='7.0';
	isIE8 = isIE && $.browser.version=='8.0'; 
	isLessThanIE8 = isIE && (isIE7 || isIE6);
	
	//remove text from h1 and h2
	$('#LRWrapper #LR #header h1 a span').html('');
	
	//remove period from clears
	$('.clear').html('');
	
	
	/*
	//close button for modal
	buttonMarkup = '<a href="javascript:;" class="btn_modalclose"></a>';
	
	//replace pngs with gifs if the user is on IE6
	if(isIE6){
		$('#affinity #header h1').css('background','url(images/affinity-real-estate-logo.jpg) 0 0 no-repeat');
		$('#affinity #main #rightrail .pod .icons-contact li a img').each(function(){
			var url = $(this).attr('src');
			var urlNoExt = url.substr(0,url.length-3);
			var urlGIF = urlNoExt + 'gif';
			$(this).attr('src', urlGIF);
		});
		buttonMarkup = '<a href="javascript:;" class="btn_modalclose btn_modalclose_IE6"></a>';
		$('#affinity #navtop ul li#search').css('width','324px');
	}
	
	
	//increase height of the last pod if the content pane is shorter than the right rail
	if($('#content').outerHeight(true) < rightrailHeight){
		var lastPod = $('#content .pod:last');
		var lastPodHeight = $(lastPod).outerHeight();
		var staticHeight = $('#content').outerHeight() - lastPodHeight;
		var heightToAdd = rightrailHeight - staticHeight - 10; //subtract 10 to account for bottom margin
		if(isIE){ heightToAdd += 15; }
		$(lastPod).css('height', heightToAdd+'px');
	}
	
	//modal
	$('#phone').click(function(){
		$('body').append(getModalMarkup('<div id="modalHTML"><div id="modal_box"><h2>Phone</h2><p>Whether you are in the market for a new residential or business property, or just have a question about the services we offer, please don\'t hesitate to give us a call.</p><p>Brad Cope:  816-200-2737<br />JJ Kramer:  816-200-7368</p></div>'));
		openModal('#modalContainer', 50);
		return false;
	});
	
	$('#email').click(function(){
		$('body').append(getModalMarkup('<div id="modalHTML"><div id="modal_box"><h2>Email</h2><p>We\'re happy to answer any questions you might have about real estate and the services we provide.  Send us and email and we\'ll answer it within 48 hours.</p><p>Brad Cope:  <a href="mailto:bradcope@gmail.com?Subject=Affinity%20Real%20Estate">BradCope@gmail.com</a><br />JJ Kramer:  <a href="mailto:j2kramer@gmail.com?Subject=Affinity%20Real%20Estate">J2Kramer@gmail.com</a></p></div>'));
		getHTMLContent($(this),'#modalHTML');
		openModal('#modalContainer', 50);
		return false;
	});
	
	$('.moreinfo_a').toggle(
		function(){ 
			$(this).text('Less...');
			$(this).parent('p').next('.moreinfo').animate({height:'show'}, 500, 'swing', function(){$(this).children().animate({color:'#333333'}, 500)});
			return false;
		},
		function(){
			$(this).text('More...');
			$(this).parent('p').next('.moreinfo').animate({height:'hide'}, 500, 'swing', function(){$(this).children().css({color:'#ffffff'})});
			return false;
		}		
	);
	
	$('a.more').toggle(
		function(){
			$(this).html('&laquo;').attr('title','less...');
			$(this).parent('td.description').children('.hidden').css('display','block');
			return false;
		},
		function(){
			$(this).html('&raquo;').attr('title','more...');
			$(this).parent('td.description').children('.hidden').css('display','none');
			return false;
		}
	);
*/
});

/*======================================================================================
	getHTMLContent()
	getModalMarkup()
	openModal()
	closeModal()
---------------------------------------------------------------------------------------- 
	Modal functions
----------------------------------------------------------------------------------------*/
//pull modal content from an external html page
function getHTMLContent(selector, element) {
	$.get($(selector).attr('rel'), function(data) {
		$(element).html(data);
	});
}//endfun getHTMLContent()

//return markup to create a modal
function getModalMarkup(targetDiv, modalContainerStyle) {
	return '<div id="blackout"></div><div id="modalContainer" style="'+modalContainerStyle+'"><div class="modal_content">'+buttonMarkup+targetDiv+'</div></div></div>';
}//endfun

function openModal(modalId, thisTop){
	var blackoutHeight;
	
	$('#blackout').css({'width':$(window).width(),'height':$(document).height()}).fadeIn(500).fadeTo(100,0.8, function(){
		var modalCalc;
		if(isIE){ modalCalc = ($(window).width()/2)-($(modalId).outerWidth(true)); } else { modalCalc = ($(window).width()/2)-($(modalId).outerWidth(true)/2); }
		$(modalId).css({'top': thisTop+'px', 'left': modalCalc, 'height': $(modalId).outerHeight(true)}).fadeIn(500);
		blackoutHeight = $('#blackout').outerHeight(true);
		if( blackoutHeight-thisTop <= $(modalId).outerHeight(true)){
			$('#blackout').css('height',blackoutHeight+(thisTop)+'px');
		 }
	});		    			
	$('#blackout, div.modal_content .btn_modalclose, div.modal_content .btn_modalclose_IE6').bind('click', function(){ closeModal(modalId); return false; });
	if(isIE6){ 
		$('html').scrollTop(0);
	} else {
		$('html','body').animate({scrollTop:0},'fast');
	}
}//endfun getModalMarkup()

function closeModal(modalId){
	$('#blackout').remove(); 
	$(modalId).remove(); 
	return false;
}//endfun closeModal()
