//image index
var index=0;
//slide direction
var dir = "forward";

//dropdown menus
function createNav(){
	$("ul#nav").superfish({
		animation: {height: "show"},
		delay: 0
	});
	//collapsing submenu if present
	//showSublist();
	//build breadcrumb nav at top
	//buildCrumbs();
	languages();
	contactus();
}

//slide out the languages bar
function languages(){
    $("#languageholder").hover(function(){
        $("#languagelist").animate({opacity: "show"});
    }, function() {
        $("#languagelist").animate({opacity: "hide"});
    });
}

function contactus(){
    $("#contactusholder").hover(function(){
        $("#contactuslist").animate({opacity: "show"});
    }, function() {
        $("#contactuslist").animate({opacity: "hide"});
    });
}


function showContact(){
	if($("#contact").length > 0) {	
		$("#contact").css("display","none");
		$("#closeform").css("display","block");
		$("#submit").css("display","none");
		$("#expand").click(function(){
			$("#contact").animate({height: "show"});
			$("#expand").css("display","none");
			$("#submit").animate({height: "show"});
		});
		$("#closeform").click(function(){
			$("#contact").animate({height: "hide"});	
			$("#submit").css("display","none");
			$("#expand").animate({height: "show"});
		});
		
		var pageTitle = document.title;
		var pageAddr = location.href + "?status=RMI1";
		document.cform.rpagetitle.value = pageTitle;
		document.cform.rpageaddress.value = pageAddr;	
	}
}

//function showSublist(){
//	$("#subnav ul li ul").css("display","none");
//	$("#subnav ul li").click(function(){
//		$("ul", this).toggle('slow');
//	});
//}

function buildCrumbs(){
	var runningLink = "";
	var breadtext = "<a href='#' class='breadlink'>Home</a>";
	var finalAry = new Array();
	var fullUrl = location.href;
	var startPos = fullUrl.indexOf("/aon/")+5;
	var goodUrl = fullUrl.substring(startPos);
	var urlAry = new Array();
	urlAry = goodUrl.split("/");
	for(i=0;i<urlAry.length-1;i++){
		breadtext += " / ";
		if(i==urlAry.length-2){
			breadtext += " <span class='current'>";
		} else {
			runningLink += urlAry[i]+"/"
			breadtext += "<a href='/"+runningLink+"' class='breadlink'>";	
		}
		var crumbAry = new Array();
		crumbAry = urlAry[i].split("_");
		for(x=0;x<crumbAry.length;x++){
			tmpChar = crumbAry[x].substring(0,1);
			utmpChar = tmpChar.toUpperCase();
			captext = utmpChar+crumbAry[x].substring(1);
			breadtext += captext + " ";
		}
		if(i==urlAry.length-2){
			breadtext += "</span>";
		} else {
			breadtext += "</a>";	
		}
	}
	if(document.getElementById("breadlinks1")){
		document.getElementById("breadlinks1").innerHTML = breadtext;
	}
}

function animbuttons(){
	$("#rewind").click(function(){
		dir = "reverse";
		manualSlide();
		//$("#swapImg").attr("src","images/fpo_main1.jpg");
	});
	$("#forward").click(function(){
		dir = "forward";
		//$("#swapImg").attr("src","images/fpo_main2.jpg");
		manualSlide();
	});
	$("#pause").click(function(){
		dir = "pause";
	});
	
	anim();
}


function anim() {
	$("#swapImg")
	.animate({opacity: 0}, 3000, startSlides)
	.animate({opacity: 1.0}, 6000, function(){
		$("#homeimg").css({backgroundImage:"url("+ $('#swapImg').attr('src') +")"})
		$("#swapImg").attr("alt", altAry[index]);
	$("#startlink").attr("href", linkAry[index]);
	})
}


function startSlides(){ 
	//cycle through background images	
	if(dir=='forward'){
		if(index==(imageAry.length-1)){
			index=0;
		}else{
			index++;
		}
	} else if(dir=='reverse'){
		if(index==0){
			index=imageAry.length-1;
		} else {
			index--;
		}
	} else {
		index=index;
	}
	$("#swapImg").attr("src",imageAry[index]);
	anim();
}

function manualSlide(){
	if(dir=='forward'){	
		if(index==(imageAry.length-1)){
			index=0;
		}else{
			index++;
		}
	} else if(dir=='reverse'){
		if(index==0){
			index=imageAry.length-1;
		} else {
			index--;
		}
	} else {
		index=index;
	}
	$("#swapImg").attr("src",imageAry[index]);
	$("#swapImg").attr("alt", altAry[index]);
	$("#startlink").attr("href", linkAry[index]);	
	$("#homeimg").css({backgroundImage:"url("+ imageAry[index] +")"})
}

function validateForm() {
	document.getElementById("emailerr").style.display="none";		
	var willReturn = 1;
	//required fields
	var reqAry=new Array(7)
	for (i=0; i <7; i++)
	reqAry[i]=new Array(2)
	reqAry[0][0] = document.cform.C_FirstName.value;
	reqAry[0][1] = document.getElementById("fnamelabel");
	reqAry[1][0] = document.cform.C_LastName.value;
	reqAry[1][1] = document.getElementById("lnamelabel");
	reqAry[2][0] = document.cform.C_Title.value;
	reqAry[2][1] = document.getElementById("titlelabel");
	reqAry[3][0] = document.cform.C_EmailAddress.value;
	reqAry[3][1] = document.getElementById("emaillabel");
	reqAry[4][0] = document.cform.C_Company.value;
	reqAry[4][1] = document.getElementById("companylabel");
	reqAry[5][0] = document.cform.C_BusPhone.value;
	reqAry[5][1] = document.getElementById("phonelabel");
	reqAry[6][0] = document.cform.C_Country.value;
	reqAry[6][1] = document.getElementById("countrylabel");
	for(i=0; i<reqAry.length; i++){
		if(reqAry[i][0]==null||reqAry[i][0]==""){
			reqAry[i][1].className="error";
			willReturn = 0;
		} else {
			reqAry[i][1].className="";	
		}
	}
	if(willReturn==0){
		return false;
	} else {
		if(validate_email(reqAry[3][0])==true){
			return true;
		} else {
			document.getElementById("emailerr").style.display="inline";	
			return false;
		}
	}
}
function validate_email(value) {
	with (value) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
		  	return false;
		} else {
			return true;
		}
	}
}

function printPage(){
    var height = (screen.height*.50);
    var width = (screen.width*.70);
    var top = (screen.height - height)/2;
    var right = (screen.width - width)/2;
    //newWin=window.open(location.href, '', 'width='+width+',         height='+height+',scrollbars=yes,menubar=yes,top='+top+',right='+right+'');
    var thispage = location.href;
    if(thispage.indexOf("?")!=-1){
         newWin=window.open(thispage+"&style=print");
    } else {
         newWin=window.open(thispage+"?style=print");
    }
}

function checkOpener(){
    var thisPage = location.href;
    if(thisPage.indexOf("style=print")!=-1){
        window.onload=document.getElementById('primarystyle').href="/builders/properties/css/print.css";
    }
}




function pageInfo(){
	var pageTitle = document.title;
	var pageAddr = location.href + "?status=STF1";
	document.eform.pagetitle.value = pageTitle;
	document.eform.pageaddr.value = pageAddr;
}	

function validateEmailForm() {
	document.getElementById("emailtoerr").style.display="none";
	document.getElementById("emailfromerr").style.display="none";
	var willReturn = 1;
	//required fields
	var reqAry=new Array(3)
	for (i=0; i <3; i++)
	reqAry[i]=new Array(2)
	reqAry[0][0] = document.eform.C_To_Email_Address1.value;
	reqAry[0][1] = document.getElementById("emailtolabel");
	reqAry[1][0] = document.eform.C_From_Email_Address1.value;
	reqAry[1][1] = document.getElementById("emailfromlabel");
	reqAry[2][0] = document.eform.C_Subject1.value;
	reqAry[2][1] = document.getElementById("emailsubjlabel");
	for(i=0; i<reqAry.length; i++){
		if(reqAry[i][0]==null||reqAry[i][0]==""){
			reqAry[i][1].className="error";
			willReturn = 0;
		} else {
			reqAry[i][1].className="";	
		}
	}
	if(willReturn==0){
		return false;
	} else {
		if(validate_email(reqAry[0][0])==false){
			document.getElementById("emailtoerr").style.display="inline";	
			return false;
		} else if(validate_email(reqAry[1][0])==false) {
			document.getElementById("emailfromerr").style.display="inline";	
			return false;
		} else {
			return true;	
		}
	}
}

function showEmail(){
	pageInfo();
	$("#confirmform").hide();
	$("#emailform").show();
}
function hideEmail(){
	$("#emailform").hide();
	$("#confirmform").hide();
}

function formFunctions(){
	$('#emaillink').attr("href","javascript:showEmail();");	
	//check url for postback (querystring)
	var fullURL = location.href;
	if(fullURL.indexOf("status=STF1")!=-1){
		if(document.getElementById('primarystyle').href.indexOf("print.css")==-1){
			$("#confirmform").show();
		}
	}
	if(fullURL.indexOf("status=RMI1")!=-1){
		if(document.getElementById('primarystyle').href.indexOf("print.css")==-1){
			$("#cform").hide();
			$("#expand").hide();
			$("#required").hide();
			$("#contact").show();
			$("#contactconfirm").show();	
		}
	}
}
