$(document).ready(function () {
	/* Modal Closer Calls */
	$("#fadeContainer").live('click', hideModal);
	$("#modalClose").live('click', hideModal)
	$(".closeBtn").live('click', hideModal)
	$(".modalLink").live('click',function(e){
		e.preventDefault();
		parent.loadContent($(this).attr('href'));	
		parent.showModal("modalContainer");
	});
	$(".mapModalLink").live('click',function(e){
		e.preventDefault();
		parent.loadMapContent($(this).attr('href'));
		parent.showModal("mapModalContainer");
	});			
	/* Popup windows */
	$(".newWinPop").click(newWinPop);
});

function loadContent(href) {
	$("#modalContent").load(href +" #modalDisplay");
}

function loadMapContent(href) {
	$('<iframe scrolling=no id="mapFrame" name="mapFrame"">').appendTo('#mapContent');
	$('#mapFrame').attr('src', href + '?showBasicMap=1');
}

function newWinPop(){window.open($(this).attr('href'),'terms','scrollbars=yes,width=700,height=600');return false;}

/* Absolute Centering Code */
$.clientCoords=function(){var a={width:0,height:0};if(document.documentElement){a.width=document.documentElement.scrollWidth;a.height=document.documentElement.scrollHeight}else if(window.innerWidth&&window.innerHeight){a.width=window.innerWidth;a.height=window.innerHeight}return a};var win=$(window),getViewport=function(){return{top:win.scrollTop(),left:win.scrollLeft(),width:win.width(),height:win.height()}};

/* Submit Form Waiting Dialog */
function showWaiting() {
	$("#submitButton").hide();
	$("#waiting").show();
}

function showWaitingIE() {
	// This might look at little ridiculous, but it's
	// what you have to do to get the animated GIF to
	// work correctly in IE.
	html = $('#waiting').html();
	$("#submitButton").hide();
	$('#waiting').html(html).show();
	document.forms['superUpload'].submit();
	return false;
}

/* Form Map Modal */
function formMapModal(v,z){	
	if ($('#mapFrame').length == 0) {
		$('<iframe scrolling="no" frameBorder="0" id="mapFrame" name="mapFrame"">').appendTo('#mapContent');
		$('#mapFrame').attr('src', v+'?showBasicMap=1&zipcode='+z); 
	}
	showModal("mapModalContainer");
}

/* Modal Code */
function showModal(divName) {
	$(".modalHide").hide();
	$("#fadeUpContent").removeClass();
	$("#fadeUpContent").addClass("fadeUp");		
	var ctntDiv = "#"+divName;
	$(".fadeUp").hide();
	var fadeCont = $("#fadeContainer"); 
	fadeCont.hide();
	fadeCont.addClass("transparent");
	fadeCont.css("width", "100%").css("height", $(document).height());
	fadeCont.show();
	$(ctntDiv).css("left", ($.clientCoords().width - $(ctntDiv).width())/2);
	$(ctntDiv).fadeIn("slow");
	if($(ctntDiv).css('height')!='auto'){
		var mH = $(ctntDiv).css('height').substring(0,$(ctntDiv).css('height').length-2);	
	}else{
		var mH = 120;
	}
	var cTop = getViewport().height - mH;
	cTop=cTop/2;
	cTop=cTop-30;
	$(ctntDiv).css("top",getViewport().top+cTop);	
	return false;
}
hideModal = function(callBack){	
	$(".modalHide").show();
	$("#fadeContainer").hide();
	$("#fadeContainer").removeClass();
	$("#fadeCtn").fadeOut("slow");
	$(".fadeUp").each(function(i, el){ 
			if(!$(el).is(":hidden")){
				if(typeof callBack == 'function'){ $(el).fadeOut("slow", callBack); }
				else{ $(el).fadeOut("slow"); }
			}
		}
	);
	$("#mapContent").html(' ');
	$("#modalContent").html(' ');
	return false;
}

$(document).ready(function() {
 
	function filterPath(string) {
		return string
			.replace(/^\//,'')
			.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
			.replace(/\/$/,'');
	}
	
	function scrollTo(targetOffset, target) {
		$(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
			location.hash = target;
		});
	}
	  
	var locationPath = filterPath(location.pathname);
	var scrollElem = scrollableElement('html', 'body');

	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if (locationPath == thisPath
				&& (location.hostname == this.hostname || !this.hostname)
				&& this.hash.replace(/#/,'') ) {
			
			var $target = $(this.hash);
			var target = this.hash;
			
			
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					scrollTo(targetOffset, target);
				});
			}
		}
	});

	  // use the first element that is "scrollable"
	function scrollableElement(els) {
		for (var i = 0, argLength = arguments.length; i <argLength; i++) {
			var el = arguments[i],
			$scrollElement = $(el);
			if ($scrollElement.scrollTop()> 0) {
				return el;
			} else {
				$scrollElement.scrollTop(1);
				var isScrollable = $scrollElement.scrollTop()> 0;
				$scrollElement.scrollTop(0);
				if (isScrollable) {
					return el;
				}
			}
		}
		return [];
	}

});

function change_parent_url(url)
{
	document.location=url;
}	
