function initPageHeight() {
	var lch = $(".leftnav").height(); //lch - left column height
	var pbh = $(".page-body").height(); //pbh = page body height
	var rch = $(".rightnav").height(); //rch = right column height

	if (lch > rch && lch > pbh){
		$(".page-body").css("height", lch + "px");
		$(".rightnav").css("height", lch + "px");
	}

	if (rch > lch && rch > pbh){
		$(".page-body").css("height", rch + "px");
		$(".leftnav").css("height", rch + "px");
	}

	if (pbh > rch && pbh > lch){
		$(".leftnav").css("height", pbh + "px");
		$(".rightnav").css("height", pbh + "px");
	}

	$(".fill-height").each(function() {
		var regex = new RegExp("\\D", "g");

   		$(this).height($(this).parent().height() -
   				( $(this).offset().top - $(this).parent().offset().top ) +
   				$(this).parent().css("border-top-width").replace(regex,"") * 1  +
   				( $(this).parent().css("padding-top").replace(regex,"")  * 1 +
   				  $(this).parent().css("padding-bottom").replace(regex,"")  * 1 ) -
   				( $(this).css("padding-top").replace(regex,"")  * 1   +
   				  $(this).css("padding-bottom").replace(regex,"")  * 1  ) -
   				( $(this).css("border-top-width").replace(regex,"")  * 1  +
   				  $(this).css("border-bottom-width").replace(regex,"")  * 1)
   		);
	  });

}

$(function(){
	$(".button").assignMouseEvents();
	initPageHeight();
});


// Old style functions 
var searchInstructions = "Enter keyword or item #";

function submitSearchForm(theForm) {
	   if(doSearchValidation (theForm)) theForm.submit();	
}
	
function doSearchFocus(component) {
    var searchVal = component.value;
    if (searchVal == searchInstructions) {
        component.value = "";
    }
}

function doSearchBlur(component) {
    var searchVal = component.value;
    if (strTrim(searchVal) == '') {
        component.value = searchInstructions;
    }
}

function doSearchValidation (theForm) {
	var searchVal = theForm.keyword.value ;
	if (searchVal == "" || searchVal == searchInstructions) {
		alert ("Please enter a search term and try your search again") ;
		return false ;
	}
	return true ;
}

function strTrim(s) {
    // Remove leading spaces and carriage returns
    while (s.substring(0,1) == ' ') {
        s = s.substring(1, s.length);
    }
    // Remove trailing spaces and carriage returns
    while (s.substring(s.length-1, s.length) == ' ') {
        s = s.substring(0, s.length-1);
    }
    return s;
}

/*
 * This function launches a new web browser window to a specified width, height and features.
 * Features string is a comma separated window's feature needed for this new window. For Instance
 * If a new window needs a toolbar the feature string must be "toolbar" like needs scroll bar and
 * and toolbar then it must be "toolbar,scrollbar". Note that the order of the feature is not required.
 * Also it's case insensitive. Therefore, "scrollbar,toolbar" is identical to "Toolbar,ScrollBar".
 *
 * If the features string is ommitted then all the features are turned off. To turn all the features on
 * use the word "all" for features instead of specifying each feature.
 */

function openWindow(address, width, height,features)
{
	/* Find out what features need to be enable
	 *
   */
	if(features)
		features = features.toLowerCase();
	else
		features = "";

	var toolbar = (features == "all" ? 1 : 0);
	var menubar = (features == "all" ? 1 : 0);
	var location = (features == "all" ? 1 : 0);
	var directories = (features == "all" ? 1 : 0);
	var status = (features == "all" ? 1 : 0);
	var scrollbars = (features == "all" ? 1 : 0);
	var resizable = (features == "all" ? 1 : 0);


	if(features != "all")
	{
		//split features
		var feature = features.split(",");
		for(i = 0; i < feature.length; i++)
		{
		 	if(feature[i] == "toolbar")
			   toolbar = 1;
			else if(feature[i] == "menubar")
			   menubar = 1;
			else if(feature[i] == "location")
			   location = 1;
			else if(feature[i] == "directories")
			   directories = 1;
			else if(feature[i] == "status")
			   status = 1;
			else if(feature[i] == "scrollbars")
			   scrollbars = 1;
			else if(feature[i] == "resizable")
			   resizable = 1;
		}

	}
	features = "toolbar=" + toolbar + ",";
	features += "menubar=" + menubar + ",";
	features += "location=" + location + ",";
	features += "directories=" + directories + ",";
	features += "status=" + status + ",";
	features += "scrollbars=" + scrollbars + ",";
	features += "resizable=" + resizable;

	var newWindow = window.open(address, 'Popup_Window', 'width=' + width + ',height=' + height + ',"' + features + '"');
	newWindow.focus();
}

function trim(s)
{
	// Remove leading spaces and carriage returns
	while (s.substring(0,1) == ' '){
		s = s.substring(1,s.length);
	}
	// Remove trailing spaces and carriage returns
	while (s.substring(s.length-1,s.length) == ' '){
		s = s.substring(0,s.length-1);
	}
	return s;
}
/*
$(function(){
	$("input[@type=text]").focus(function(){
		makeCurrent(this);
	});
	$("input[@type=text]").blur(function(){
		makeNormal(this);
	});
	$("textarea").focus(function(){
		makeCurrent(this);
	});
	$("textarea").blur(function(){
		makeNormal(this);
	});
});
function makeCurrent(elem){
	$(elem).css("background-color", "yellow");
}
function makeNormal(elem){
	$(elem).css("background-color", "white");
}-/

/*************************
	Footer Javascript
*************************/

function callEmailSignup() {
	var formAction = $("#subscribeForm").attr("action");
	$("#emailSignUp").html("Saving...").load(formAction, {"userEmail":$("#subscribeForm input[@name=userEmail]").val()});
}

$(function(){
	$("#subscribeForm input[@name=userEmail]").keydown(function(event) {
		if (event.keyCode == 13)
		{
			callEmailSignup();
			return false;
		}
	});

	$(".email-signup-contianer .signup-button").click(function(){
		callEmailSignup();
	});

	$(".email-signup-contianer .user-email").focus(function(){
		var userEmail = $(this).val();
		if (userEmail == "your email address"){
			$(this).val('');
		}
	});
	
	$(".email-signup-contianer .user-email").blur(function(){
		var userEmail = $(this).val();
		if (trim(userEmail) == ''){
			$(this).val("your email address");
		}
	});
});

$(document).ready(function(){

if($.browser.msie && $.browser.version=="6.0") { $('#caseStudyExcerpts').addClass('msie6'); } else {

	$('#caseStudyExcerpts ul li img').mouseover( function() {

		$(this).parent('a').parent('li').parent('ul').addClass('highlighted');
		$(this).parent('a').siblings('span').addClass('visibleCaseStudy');
		$(this).parent('a').parent('li').addClass('emphasized');
		$(this).parent('a').parent('li').siblings('li').children('span').removeClass('visibleCaseStudy');
		$(this).parent('a').parent('li').siblings('li').removeClass('emphasized');
		return false;

		});

	//highlighted
	//VisibleCaseStudy

	var n = 0;
	$("#caseStudyExcerpts").bind("mouseleave",function(){

		$(this).children('ul').removeClass('highlighted');
		$(this).children('ul').children('li').removeClass('emphasized');
		$(this).children('ul').children('li').children('span').removeClass('visibleCaseStudy');

	});

}
	/* mouseover functionality for clients main page */
	$(".client").mouseover(function(){
      $(this).css("background","#eceded");
     }).mouseout(function(){
       $(this).css("background","white");
    });

	/* article detail accordion */
	$("h6.first").click(function(){
      	$(".first-info").slideToggle("slow");
			$(".second-info").hide("slow");
			$(".third-info").hide("slow");
	  });

	  $("h6.second").click(function(){
      		$(".first-info").hide("slow");
			$(".second-info").slideToggle("slow");
			$(".third-info").hide("slow");
	  });
	  $("h6.third").click(function(){
      		$(".first-info").hide("slow");
			$(".second-info").hide("slow");
			$(".third-info").slideToggle("slow");
	  });

	$(".reference").click(function() {
		var parentTag = $(this).parents().get(1).className;

		if (parentTag == "first-info") {
			$(".first-info").slideToggle("slow");
			$(".second-info").hide("slow");
			$(".third-info").hide("slow");
			alert("first");
		}

		if (parentTag == "second-info") {
			$(".first-info").hide("slow");
			$(".second-info").slideToggle("slow");
			$(".third-info").hide("slow");
			alert("second");
		}

		if (parentTag == "third-info") {
			$(".first-info").hide("slow");
			$(".second-info").slideToggle("slow");
			$(".third-info").hide("slow");
			alert("third");
		}
	});

	/* bios & partners rollover functions */
		$(".bio").hide();
		$(".partner").hide();
		$(".defaultText").show();
		$(".bio").eq(0).show();
		$(".partner").eq(0).hide();
		$(".name").eq(0).css("color", "#cb5d1e");
		$(".title").eq(0).css("color", "#cb5d1e");

		var bioIdx = 0;

		$(".group .name").click(function(){

			bioIdx = $(".group .name").index(this);

			$(".bio").each(function(){
					$(this).hide();
					$(".name").css("color","black");
					$(".title").css("color","black");

				});
			$(".partner").each(function(){
					$(this).hide();
					$(".defaultText").hide();
				});
			$(".bio").eq(bioIdx).show();
			$(".partner").eq(bioIdx).show();
			/*$(".name").eq(bioIdx).css("border","1px solid red;");*/
			$(".name").eq(bioIdx).css("color","#cb5d1e");
			$(".title").eq(bioIdx).css("color","#cb5d1e");
			});

		// Join Fry Photo Gallery Interaction

		$('#photoGallery ul li a').click(function(evt) {
		
			evt.preventDefault();
			
		
		});
	
		$('#photoGallery ul li a').hover(
			function () {
			
				var clickedPhoto = $(this).children('img').attr('src');
				var photoSource = clickedPhoto.split('d.jpg')[0];
				photoSource = photoSource.split('.jpg')[0];
				var saturatedPhoto = photoSource + '.jpg';
				var largerPhoto = photoSource + 'l.jpg';
				
				var returnPhoto = $('.active').children('img').attr('src');
				returnPhoto = returnPhoto.split('.jpg')[0];
				returnPhoto = returnPhoto + 'd.jpg';
				$('.active').children('img').attr('src', returnPhoto);
				
				$('#photoGallery ul li a').removeClass('active');
				$(this).addClass('active');
				$(this).children('img').attr('src', saturatedPhoto);
				$('#focalImage').attr('src', largerPhoto);
				
			}, 
			function () { });

    // Return boolean TRUE/FALSE for ipad/iphone/ipod users
    // display new flash content on homepage
    function isAppleMobile(){
        return (
            (navigator.platform.indexOf("iPhone") != -1) ||
            (navigator.platform.indexOf("iPod") != -1) ||
            (navigator.platform.indexOf("iPad") != -1)
            // Uncomment to test on Windows box
            // || (navigator.platform.indexOf("Win32") != -1)
        );
    }

    if(isAppleMobile()){
            $(".HomebannerSpacer").addClass('noFlash');
            $(".HomebannerTarget").hide();
        } else {
            var bannerHtml = $(".Homebanner").html();
            $(".HomebannerTarget").html(bannerHtml);
            $(".Homebanner").html("");
        }
});

var randomfeature = function(){
		var length = $("#cgyfeature li").length; 
		var ran = Math.floor(Math.random()*length) + 1;
		$("#cgyfeature li:nth-child(" + ran + ")").show();
		$("#cgyfeatureimg li:nth-child(" + ran + ")").show();			
	};
