$(document).ready(function(){
	txtBoxClear.init();
	anchors.addBehaviors();
	
	// Homepage Expand/Collapse Footer
	$(".expand").click(function(e) {
		e.preventDefault();
		$("#expandCollapse").removeClass("hiddencontent");
		$(this).addClass("hiddencontent");
	});
	$(".collapse").click(function(e) {
		e.preventDefault();
		$("#expandCollapse").addClass("hiddencontent");
		$(".expand").removeClass("hiddencontent");
	});
	
	// Salesforce Form Validate
	$("#salesforceForm").submit(function() {
		// Do some JS Validation
		clearValidation();
		var passedV = new Array();
		var pv = 0;
		var validAmt = 12;
		
		if (validateLength("first_name",0)) { passedV[pv] = true; pv++; };
		if (validateLength("last_name",0)) { passedV[pv] = true; pv++; };
		if (validateLength("company",0)) { passedV[pv] = true; pv++; };
		if (validateLength("phone",0)) { passedV[pv] = true; pv++; };
		if (validateLength("title",0)) { passedV[pv] = true; pv++; };
		if (validateLength("industry",0)) { passedV[pv] = true; pv++; };
		if (validateLength("endpoints",0)) { passedV[pv] = true; pv++; };
		if (validateLength("status",0)) { passedV[pv] = true; pv++; };
		if (validateEmail("email")) { passedV[pv] = true; pv++; };
		if (validateLength("state",0)) { passedV[pv] = true; pv++; };
		if (validateLength("country",0)) { passedV[pv] = true; pv++; };
		if (validateCheckBox("agree")) { passedV[pv] = true; pv++; };
		
		if (passedV.length == validAmt) {
			return true;
		} else {
			return false;
		}
	});
	
	// Flash Demo
	$("a.demo").click(function(e) {
		e.preventDefault();
		$.fancybox({
			'href'			: $(this).attr("href"),
			'titleShow'		: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'width'			: 954,
			'height'		: 657,
			'type'			: 'iframe'
		});
	});
	
	//Homepage Image Rotation
	homepageSeq(0);
	
	//Homepage Image Sequence
	$("#sequenceBtns li a").click(function(e) {
		e.preventDefault();
		var slide = $(this).attr("class").replace("image","");

		$('#sequence').cycle('destroy');

		homepageSeq(slide);
	});
	
	//Homepage Logo Slider
	$(".scrollable").scrollable({circular: true});
	
	//Level One Mega Menu
	var levelOneConfig = {    
		over: levelOneOpen,
		timeout: 500,
		interval: 200,
		out: levelOneClose
	};
	$("#navLevelOne li.levelOne").hoverIntent( levelOneConfig );
	
	//Share Mega Menu
	var shareConfig = {    
		over: shareOpen,
		timeout: 50,
		interval: 200,
		out: shareClose
	};
	$("#constants-Share").hoverIntent( shareConfig );

	// Customer Tabs
	var customerNavWidth = 0;
	var customerNavCount = $('#customerNav').children('li').length;
	$('#customerNav').children('li').each(function(i) {
		if (i < customerNavCount - 1) {
			customerNavWidth = customerNavWidth + $(this).width() + 1;
		}
		if (i == customerNavCount - 1) {
			var lastItemWidth = 714 - customerNavWidth;
			$(this).css("width",lastItemWidth);
			$(this).children("a").css("width",lastItemWidth-17).css("background-position","-"+ customerNavWidth +"px -36px");
		}
	});
	$('#customers').filterable({
		useHash: true,
		animationSpeed: 0,
		tagSelector: '#customerNav li a'
	});
	
	// Landing Page Social Icons
	$(".iconList li a").click(function(e) {
		e.preventDefault();
		$(".iconList li a").removeClass("selected");
		$(this).addClass("selected");
		$("#iconHolder div").addClass("hiddencontent");
		$("#iconHolder div"+$(this).attr("href")).removeClass("hiddencontent");
	});
	
	
	// Fancybox Image Default
	$("a.fancybox").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
});

/* Homepage Sequence Starter */
function homepageSeq(slide) {
	$('#sequence').cycle({ 
    	delay:  2000,
	    speed:  500,
		startingSlide: slide,
		after: onAfter
	});
}
function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $("#sequenceBtns li a").removeClass("selected");
	if (index > 0) {
		$("#sequenceBtns li a.image"+index).addClass("selected");
	}
}

/* Level One Open/Close Functions */
function levelOneOpen() {
	$(this).children(".menuTwo").removeClass("hiddencontent");
	$(this).children("a").addClass("active");
	$("#navLevelOne").addClass("navLevelOneHover");
	$("#navConstants").addClass("navConstantsHover");
}
function levelOneClose() {
	$(this).children(".menuTwo").addClass("hiddencontent");
	$(this).children("a").removeClass("active");
	$("#navLevelOne").removeClass("navLevelOneHover");
	$("#navConstants").removeClass("navConstantsHover");	
}
/* Share Menu Open/Close Functions */
function shareOpen() {
	$(this).children(".shareMenu").removeClass("hiddencontent");
	$(this).children("a").addClass("active");
	$("#navLevelOne").addClass("navLevelOneHover");
	$("#navConstants").addClass("navConstantsHover");
}
function shareClose() {
	$(this).children(".shareMenu").addClass("hiddencontent");
	$(this).children("a").removeClass("active");
	$("#navLevelOne").removeClass("navLevelOneHover");
	$("#navConstants").removeClass("navConstantsHover");	
}

/* =================================================================== */
// Updated link functionality script using delegation to speed up
// the page and handle dynamically added elements.
// dependencies: jQuery
var anchors = {
	addBehaviors: function() {
		$('body').delegate('a', 'click', function(e) {
			if ($(this).attr('rel') == 'external' || $(this).hasClass('external') || $(this).hasClass('pdf') || $(this).hasClass('popupFull')) {
				// external links
				e.preventDefault();
				return anchors.openWin(this,"");
			} else if ($(this).hasClass('popup')) {
				// popup
				e.preventDefault();
				return anchors.openWin(this,"height=550,width=600,scrollbars=yes");
			} 
			
			if ($(this).attr('href') == location.href ) {
				// onstate
				$(this).addClass('onstate');
			}
		});
	},
	openWin: function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
/* =================================================================== */

/* =================================================================== */
// use this to automatically clear a text box of it's default value
// if nothing is typed in the box, the script will put the default value back
// useful on sites where search boxes don't have a seperate label
// you can use it for more than one box per page using the txtBoxes array
txtBoxClear = {
	txtBoxes : ['searchField'],
	init: function() {
		for (i=0;i<txtBoxClear.txtBoxes.length;i++) {
			var oCurrentTxtBox = document.getElementById(txtBoxClear.txtBoxes[i]);
			if (!oCurrentTxtBox) { continue; }
			oCurrentTxtBox.defaultVal = oCurrentTxtBox.defaultValue;
			txtBoxClear.clearBox(oCurrentTxtBox);
		}
	},
	clearBox : function(txtBox) {
		txtBox.onfocus = function() {
			if (txtBox.value == txtBox.defaultVal) { txtBox.value = ''; } 
		};
		txtBox.onblur = function() {
			if (txtBox.value == '') { txtBox.value = txtBox.defaultVal; }
		};
	}
};
/* =================================================================== */

/* =================================================================== */
// get, set, and delete cookies
// ref: http://www.dustindiaz.com/top-ten-javascript/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) { return null; }
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) { end = document.cookie.length; }
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) { 
			document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
/* =================================================================== */


/* Salesforce Form Validation */
function validateEmailAddress(emailAddr) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddr);
}
function clearValidation() {
	if ($("label.errorfield").length > 0) {
		$("label.errorfield").each(function(i) {
			$(this).html($(this).html().replace(" (Required)",""));
		});
	}
	$(".errorfield").removeClass("errorfield");
}
function addError(field) {
	$("#"+field).siblings("label").addClass("errorfield");
	$("#"+field).siblings("label").html($("#"+field).siblings("label").html() + " (Required)");
}
function delError(field) {
	$("#"+field).siblings("label").removeClass("errorfield");
	$("#"+field).siblings("label").html($("#"+field).siblings("label").html().replace(" (Required)",""));
}
function validateLength(id,length) {
	if ($("#"+id).val().length <= length) {
		addError(id);
		return false;
	} else {
		delError(id);
	}
	return true;
}
function validateCheckBox(id) {
	if (!$("#"+id).is(':checked') && $("#"+id).val() != 1) {
		addError(id);
		return false;
	} else {
		delError(id);
	}
	return true;
}
function validateEmail(id) {
	if (!validateEmailAddress($("#"+id).val())) {
		addError(id);
		return false;
	} else {
		delError(id);
	}
	return true;
}
