$.noConflict();
jQuery(document).ready(function($) {
	var ua = $.browser;
	/*============================================
		Filtering (Case Studies, Testimonials)
	==============================================*/
	$('#filter').each(function(){
		$('#filter ul li div.window').each(function(){
			$(this).css({top: $('#filter ul').outerHeight()-1, width: $('#filter ul').outerWidth() });
		});
	});
	// Filter Option Buttons
	$('#filter ul li:not(ul ul li)').each(function(){
		// Add Arrow to buttons
		$('<span class="arrow"></span>').insertBefore($(this).find('div:first'));
		$(this).hover(function(){
			var e = $(this);
			slightPause = window.setTimeout(function(){
				$(e).addClass('hover');
				if(ua.msie && ua.version.slice(0,1) == 6 ){
					$(e).find('div.window').show();
				}
			}, 100, e);
		}, function(){
			window.clearTimeout(slightPause);
			$(this).removeClass('hover');
			if(ua.msie && ua.version.slice(0,1) == 6 ){
				$(this).find('div.window').hide();
			}
		});
	});
	$('#company-opts li').each(function(){
		// Handle Company Names with Multiple Industries
		var classNames = $(this).attr('class').split(" ");
		if(classNames.length > 1){
			$(this).css({ backgroundImage: 'url(marketing_images/icon_'+classNames[0]+'_20x20.png)' });
			$(this).removeClass(classNames[0]);
		}
		// Handle Hover for IE6
		if(ua.msie && ua.version.slice(0,1) == 6 ){
			$(this).hover(function(){
				$(this).addClass('ie6hover');
			}, function(){
				$(this).removeClass('ie6hover');
			});
		}
	});
	
	/*============================================
		Case Study Listing
	==============================================*/
	$('.caseStudyList').each(function(){
		$(this).click(function(){
			location.href = $(this).find('a:first').attr('href');
		});
		// Handle Hover for IE6
		if(ua.msie && ua.version.slice(0,1) == 6 ){
			$(this).hover(function(){
				$(this).addClass('ie6hover');
			}, function(){
				$(this).removeClass('ie6hover');
			});
		}
	});
	
	/*============================================
		Testimonial Video Overlay
	==============================================*/
	$('.testimonial').each(function(index){
		var videoIndex = index;
		$(this).find('a').click(function(e){
			e.preventDefault();
			// Create Overlay
			$('<div id="blackout"></div> \
				<div id="testimonial_video"> \
					<div id="close"><a href="#"></a></div> \
					<div id="video_container"> \
						<span id="clientsSpeakModuleSpan"> \
							<br /><br /><br /><a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank"><img src="marketing_images/testimonials_noFlash.jpg" width="198" alt=""></a> \
							<span id="noFlash"></span> \
						</span> \
					</div> \
				</div>').appendTo('body');
				$($('.testimonial')[videoIndex]).find('h4').clone().appendTo('#testimonial_video');
				$('#testimonial_video h4 a').replaceWith($('#testimonial_video h4 a').text()); // Remove link from title
				$($('.testimonial')[videoIndex]).find('p.companyName').clone().appendTo('#testimonial_video');
				$($('.testimonial')[videoIndex]).find('div.desc').clone().appendTo('#testimonial_video');
				$($('.testimonial')[videoIndex]).find('p.industry').clone().appendTo('#testimonial_video');
				$($('.testimonial')[videoIndex]).find('p.logo').clone().appendTo('#testimonial_video');
				if( $(this).parents('#main_home_right').length > 0){
					$('<p><a href="?reqPage=results_testimonials">Watch more testimonials</a></p>').appendTo('#testimonial_video');
				}

				// Load SWF params for player.swf
				var flashvars = {
					autoStart: "true",
					plugins: "gapro-1",	// Google Analytics Tracking
					'gapro.accountid': "UA-5915809-1",
					'gapro.trackstarts': true,
					'gapro.trackpercentage': true,
					'gapro.tracktime': true
//					bufferlength: 10	// Number of Flash file seconds to buffer before starting video
				};
				var params = {
					allowscriptaccess: "always",
					wmode: "opaque"
				};
				var attributes = {
					id: "clientsSpeak"
				};
			swfobject.embedSWF($($('.testimonial')[videoIndex]).find('h4 a').attr('href'), "clientsSpeakModuleSpan", "480", "294", "9.0.0", false, flashvars, params, attributes);
			// Position Overlay
			$.positionDiv('#testimonial_video');
			// Show Overlay
			$('#testimonial_video').show();
			// Handle Closing the Overlay
			$('#blackout, #testimonial_video #close').click(function(e){
				e.preventDefault();
				$('#blackout, #testimonial_video').remove();
			});
		});
	});
	// Position element centered on the screen
	//========================================
	$.positionDiv = function(id){
		var l = ($(window).width()-$(id).width())/2;
		var t = ($(window).height()-$(id).height())/2;
		$(id).css("left",l);
		$(id).css("top",t);	
		//If the User resizes the window, adjust the #container height
		$(window).bind("resize", function(){
			updatePosition(id)
		});
		
	}
	function updatePosition(id,w) {
		var l = ($(window).width()-$(id).width())/2;
		var t = ($(window).height()-$(id).height())/2;
		if(w!=1){
			$(id).css("left",l);
			$(id).css("top",t);	
		} else {
			$(id).css("left",l);
		}
	}
	
	
	/*============================================
		White Papers Listing
	==============================================*/
	$('.whitePaperList').each(function(){
		$(this).click(function(){
			location.href = $(this).find('a:first').attr('href');
		});
		// Handle Hover for IE6
		if(ua.msie && ua.version.slice(0,1) == 6 ){
			$(this).hover(function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});
		}
	});
});

// Trigger DropDown Nav in IE6
startList = function(){
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					subUL = this.getElementsByTagName("ul");
					if(subUL.length>0) subUL[0].style.display = 'block';
					//this.className+=" over";
				}
				node.onmouseout=function() {
					subUL = this.getElementsByTagName("ul");
					if(subUL.length>0) subUL[0].style.display = 'none';
					//this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
addLoadEvent(startList);



// Form Validation
function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("select") != -1){
		if(element.className.indexOf("req") != -1){
			if(!isSelected(element)){
				alert("Please select and option for the "+element.title+" field.");
			}
		} else {
			continue;
		}
	}
	if (element.className.indexOf("req") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.title+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The "+element.title+" field must be a valid email address.");
        return false;
      }
    }
  }
  return true;
}

function isSelected(field) {
  if (field.options[field.selectedIndex].value == field.options[field.selectedIndex].defaultSelected) {
    return false;
  } else {
    return true;
  }
}
function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}
function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}
// addLoadEvent(prepareForms);

// Adds functions to onload event
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){window.onload = func;}
	else{window.onload = function(){oldonload(); func();}}
}

// validateInputItems
function validateInputItemsPF(itemsList, errorDivId)
{
  var errString = "";
  var itemsArray = itemsList.split(";"); //Leads to an array of Item:Type name-type pairs
  var errDiv = document.getElementById(errorDivId); 
  var lRetVal = false;
  for (i = 0; i < itemsArray.length; i++)
  {
    errString += validateInputItemPF(itemsArray[i]);
  }
  errDiv.innerHTML = "<p class='error'>Please provide all of the following information:<br>" + errString + "</p>";
  if (errString != "")
  {
    errDiv.style.display = "block";
  }
  else
  {
    errDiv.style.display = "none";
    lRetVal = true;
  }  	
  return lRetVal;
}

// validateInputItem
function validateInputItemPF(itemNameTypePair)
{
  var lRetVal = new String();
  var nameTypeArray = itemNameTypePair.split(":");
  var itemValue = document.getElementById(nameTypeArray[0]).value;
  var itemTrimd = itemValue.replace(/^\s+|\s+$/g, '');
  var itemType = nameTypeArray[1];
  var itemDisplayName = nameTypeArray[2];
  if (itemType == "str1")
  {
    if (itemTrimd.length < 1)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  else if (itemType == "str3")
  {
    if (itemTrimd.length < 3)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  else if (itemType == "str5")
  {
    if (itemTrimd.length < 5)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  else if (itemType == "str10")
  {
    if (itemTrimd.length < 10)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  else if (itemType == "str25")
  {
    if (itemTrimd.length < 25)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  else if (itemType == "email")
  {
    if (itemTrimd.length < 7)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
    else if (itemTrimd.indexOf("@") < 0)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
    else if (itemTrimd.indexOf(".") < 0)
    {
      lRetVal = "-" + itemDisplayName + "<br/>";
    }
  }
  return lRetVal;
}
