$(document).ready(function() {

	resize_header();
	
	

	$(window).bind("resize", function(){
		resize_header();
	});
	
	$(".arc_links, .top_nav_img, .side_nav_img, .footer_links, .home_banner").mouseenter( function(){
		
		rollover( $(this).attr("id"), $(this).attr("src"), "on" );	
		
	});
	
	$(".arc_links, .top_nav_img, .side_nav_img, .footer_links, .home_banner").mouseleave( function(){
		
		rollover( $(this).attr("id"), $(this).attr("src"), "off" );	
		
	});
	
	
	$(".Map_Link").click( function(e){
		e.preventDefault();
		url = $(this).attr("href");
		opendWindow(url,750,575)
		$(this).blur();
	});
	
	$(".PDF_Link").click( function(e){
		e.preventDefault();
		url = $(this).attr("href");
		//url = url.replace(".pdf",".pdf#zoom=100");
		opendWindow(url,750,575)
		$(this).blur();
	});
	
	
	// class="calc_link" class="calc_link"
	$(".calc_link").click( function(e){
		e.preventDefault();
		url = $(this).attr("href");
		//url = url.replace(".pdf",".pdf#zoom=100");
		w = parseInt( $(window).width() * .75 );
		h = parseInt( $(window).height() * .8 );
		opendWindowScroll(url,w,h)
		$(this).blur();
	});
	
	$(".Site_Link").click( function(e){
		e.preventDefault();
		url = $(this).attr("href");
		//url = url.replace(".pdf",".pdf#zoom=100");
		opendWindow(url,750,575)
		$(this).blur();
	});
	
	$("#sb_email_form").submit( function(e){
		e.preventDefault();
		eml = $("#eml_shortcut").val();
		set_email = eml;
		fpath = $("#eml_frm_location").val() + "#signup_content";
		$("#sidebar_go").blur();
		if( (isEmpty(eml)) || (isNotEmail(eml))){
			alert("You must enter a valid email address");
		}else{
			open_modal(fpath);
		}
	});
	
	$("#eml_shortcut").focus( function(){
	
		eml = $("#eml_shortcut").val();
		if(eml == "Email Sign Up" ){
			$("#eml_shortcut").val("");
		}
	
	});
	
	//$('#fields_email').val("eml)");
	
	$("#eml_shortcut").blur( function(){
	
		eml = $("#eml_shortcut").val();
		if(eml == "" || eml == " " ){
			$("#eml_shortcut").val("Email Sign Up");
		}
	
	});
	
	
	/********************************************
		modal links
		*********************************************/
		//
		$("#modal_overlay, #modal_close").live( 'click', function(){
			close_modal();
		});
		$("#modal_close").live( 'mouseenter', function(){
			$(this).attr("src", "images/modals/close_on.png");
		});
		$("#modal_close").live( 'mouseleave', function(){
			$(this).attr("src", "images/modals/close_off.png");
		});
		
	
});

var set_email;

function opendWindow(url,width,height) {
    
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,location,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "subWind", windowFeatures);
}

function opendWindowScroll(url,width,height) {
    
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,location,resizable,scrollbars=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "subWind", windowFeatures);
}


function resize_header(){
	
	win_width = $(window).width();
	win_height = $(window).height();
	
	if( win_width <= 1000 ){
		$("#arc_nav").css({"left":"286px"});
		$("#top_nav_rule_img").attr("width","500");
	}else{
		$("#arc_nav").css({"left":"379px"});
		$("#top_nav_rule_img").attr("width","754");
	}

	var ua = $.browser;
  	//if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) {
    	//
	//	alert(win_height + "px");
	//	$("#container").css({"height": win_height + "px"});
	//	$("#home_container").css({"height": win_height + "px"});
  	//}
	
}


function rollover( img, src, state ){
	
	//temp = img.replace("_img","");
	//console.log( img, src, state )
	if( state == "on" ){
		src = src.replace("_off.","_on.");	
	}else{
		src = src.replace("_on.","_off.");	
	}
	
	$("#" + img).attr("src", src );

}

function preloadImages() { 

	imagesarray = new Array();
	imagesrc = new Array(
		"../images/nav/an1_on.png",
		"../images/nav/an2_on.png",
		"../images/nav/tn1_on.png",
		"../images/nav/tn2_on.png",
		"../images/nav/tn3_on.png",
		"../images/nav/sm1_on.gif",
		"../images/nav/sm2_on.gif",
		"../images/nav/sm3_on.gif",
		"../images/nav/sm4_on.gif",
		"../images/nav/sm5_on.gif",
		"../images/nav/sm6_on.gif",
		"../images/nav/sm7_on.gif",
		"../images/nav/sm8_on.gif",
		"../images/nav/sm9_on.gif",
		"../images/nav/sm10_on.gif",
		"../images/nav/sm11_on.gif",
		"../images/nav/sm12_on.gif",
		"../images/nav/sm13_on.gif",
		"../images/nav/sm14_on.gif",
		"../images/nav/sm15_on.gif",
		"../images/nav/sm16_on.gif",
		"../images/nav/sm17_on.gif",
		"../images/nav/sm18_on.gif",
		"../images/nav/sm19_on.gif",
		"../images/nav/sm20_on.gif",
		"../images/nav/sm21_on.gif"
	);
	
	for ( i = 0; i < imagesrc.length; i++ ){
		
		imagesarray[i] = new Image;
		imagesarray[i].src = imagesrc[i];
		//alert(imagesarray[i].src);
	}

	

}


function open_modal( url ){
	
	$("body").append('<div id="modal_overlay"></div>');
	$("#modal_overlay").css({opacity:0.45});
	$("#modal_overlay").fadeIn(800, function(){
		
		
		$("body").append('<div id="modal_container"><div id="modal_content"></div></div>');
		
		$("#modal_content").load(url, function(){
					
					

					temph =  $("#modal_container").height() ;

					var windowHeight = document.documentElement.clientHeight;
					var popupHeight = $("#modal_container").height() + temph ;
					var headerHeight = 160;
					var idealHeight = windowHeight/2-popupHeight/2;
					var windowWidth = document.documentElement.clientWidth;
					var popupWidth = $("#modal_container").width();

	
					if( idealHeight <= headerHeight ){
						 idealHeight = headerHeight;	
					}
					
					
					$("#modal_container").css({
						"position": "absolute",
						"top":  idealHeight,
						"left": windowWidth/2-popupWidth/2
					});
					
					$("#modal_content").show();
			
		});
		
		
		
	});
}

function close_modal(){
	

			$("#modal_overlay").fadeOut(500, function(){
				$("#modal_overlay").empty().remove();
			});
			if( $.browser.msie ){
				$("#modal_container").hide(); //fadeOut(500);
				$("#modal_container").empty().remove();
			}else{
				
				$("#modal_container").fadeOut(500, function(){
					$("#modal_container").empty().remove();
				});
			}
			//
			//$("#modal_container").empty().remove();
}

function isEmpty(val){
			if( (val=="") || (val==" ")){
				return true;
			}else{
				return false;
			}
		
		}
		
	function isNotEmail(val){
			if( (val.indexOf("@") < 0) || (val.indexOf(".") < 0)){
				return true;
			}else{
				return false;
			}
	}


