/* Barlow Group global.js - Horn Group Inc. */

// ---------------------------------------------------------------
// -- Landing Page Grid ------------------------------------------
// ---------------------------------------------------------------


	function flashSend(str) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		var isSafari = (/Safari/.test(navigator.userAgent)) ? true : false;
		var flashMovie = (isIE || isSafari) ? window["flashobject"] : document["flashembed"];
		flashMovie.requestFromJS(str);
	}

	function prepLandingGrid(){
		jQuery('#video-control').children('a').removeClass().addClass('skip');
		jQuery('#video-control a').unbind().click(function(){
			flashSend('skip');
			return false;
		});
	
		// makes the static CMS-added image into a dynamic CSS-effected image
		jQuery('#grid li').each(function(){
			var currentImg = jQuery(this).find('img').attr('src');
			jQuery(this).find('img').remove();
			jQuery(this).find('a').css({'background-image':'url(' + currentImg + ')'});
			jQuery(this).append('<span></span>');
		});
		
	}
	
	
	// triggers after the Intro Flash has finished
	function initLandingGrid(){

		jQuery('#flash-window').css({'visibility':'hidden'});

		jQuery('#video-control').children('a').removeClass().addClass('replay');
		jQuery('#video-control a').unbind().click(function(){
			reloadFlashIntro();
			flashSend('replay');
			return false;
		});

		// adds the appropriate borders
		jQuery('#grid li:nth-child(even)').find('a').addClass('even');
		jQuery('#grid li').slice(5,10).addClass('middlerow');
		jQuery('#grid li').slice(1,5).addClass('columns');
		jQuery('#grid li').slice(6,10).addClass('columns');
		jQuery('#grid li').slice(11,15).addClass('columns');

		// the staggered fade-in animation		
		var timer = 250; 
		var time = 100;
		var multiplier = 1;

		jQuery('#grid li').each(function(){
			jQuery(this).find('span').css({opacity: 1}); 
			timer = (timer*multiplier + time); 
			jQuery(this).find('span').animate({opacity: 1}, timer).animate({opacity: 0}, 250, function(){
				jQuery(this).remove();
			});
		});

		jQuery('#grid li a').unbind().click(function(){
			jQuery('#ajax-window').remove();
			var currentUrl = jQuery(this).attr('href');
			jQuery(this).css({'display':'none'}).parent().addClass('current');
			jQuery('#main-display').append('<div id="ajax-window" style="display:none;"></div>');
			jQuery('#ajax-window').load(currentUrl + ' .testimonial',function(){
				jQuery('#ajax-window').fadeIn(250);
				do_sIFR();
				jQuery('a.close').bind("click", function(e){
					jQuery('#grid li.current').removeClass('current').children('a').css({'display':'block'});
					jQuery('#ajax-window').fadeOut(250);
					return false;
				});
			});
			return false;
		});
	}
			
	// triggers the Intro Flash
	function reloadFlashIntro(){
		jQuery('#flash-window').css({'visibility':'visible'});
		jQuery('#video-control').children('a').removeClass().addClass('skip');
		jQuery('#video-control a').unbind().click(function(){
			flashSend('skip');
			initLandingGrid();
			return false;
		});
	}
			
	// Landing Grid Ajax Error
	jQuery('#grid li a').ajaxError(function(){
		jQuery('#ajax-window').html("<div id='error'><p class='first'>You clicked it weird.</p><p>Click anywhere to return to the previous page.<p><span>(try not to screw this one up, k?)</span></div>");		jQuery('#ajax-window').bind("click", function(e){
			jQuery('#ajax-window').fadeOut(250);
			jQuery('#grid li.current').removeClass().children('a').css({'display':'block'});
			return false;
		});
	});


	// used to skip initial play of video -- AD 100223
	function skipToLandingGrid(){
		flashSend('skip');
		initLandingGrid();
		return false;	
	}



// ---------------------------------------------------------------
// -- Landing Page Buckets ---------------------------------------
// ---------------------------------------------------------------
	function initLandingBuckets(){
		jQuery('#buckets div').click(function(){
			var isBlank = jQuery(this).parents('li').find("a").attr('target');
			if(isBlank != ""){
				window.open(jQuery(this).parents('li').find("a").attr("href"));
			} else {
				window.location = jQuery(this).parents('li').find("a").attr("href");
			}
			return false;
		});
		
		jQuery('#news ul').innerfade({
			 animationtype: 'fade', 
			 speed: 1000, 
			 timeout: 6000
		 }); 
		
		jQuery('#client ul li').hide().filter(':first').show();
		setTimeout("jQuery('#client ul').innerfade({animationtype: 'fade', speed: 1000, timeout: 6000});",3000);
	}
	


// ---------------------------------------------------------------
// -- News Archives  ---------------------------------------------
// ---------------------------------------------------------------
	function initNews(){
		jQuery('ul.archive li ul li').click(function(){
			window.location = jQuery(this).find("a").attr("href");
			return false;
		});
	
		jQuery('ul.archive li ul').css({'display':'none'});
	
		jQuery('ul.archive > li').toggle(
			function(){
				jQuery(this).find('ul').slideDown(250);
				jQuery(this).children('p').children('a').addClass('current');
			},
			function(){
				jQuery(this).find('ul').slideUp(250);
				jQuery(this).children('p').children('a').removeClass();
			}
		).filter(':first').click();
	}



// ---------------------------------------------------------------
// -- Team Tabs --------------------------------------------------
// ---------------------------------------------------------------
	function initTeam(){	
		// turn inline images into css backgrounds
		jQuery('#team ul.tabs > li').each(function(){
			var currentImg = jQuery(this).find('img').attr('src');
			jQuery(this).find('img').remove();
			jQuery(this).find('a').css({'background-image':'url(' + currentImg + ')'});
		});
	
		// tab behavior
		var tabContainers = jQuery('div#team .right > div');
		
		jQuery('#team ul.tabs > li > a').click(function(){
			tabContainers.hide().filter(this.hash).show();
			jQuery('ul.tabs li a').removeClass('selected');
			jQuery(this).addClass('selected');
			do_sIFR();
			return false;
		}).filter(':first').click();
	}
	

// ---------------------------------------------------------------
// -- Clients Table ----------------------------------------------
// ---------------------------------------------------------------
	function initClientTable(){
		// sets table column widths
		jQuery("#clientTable tbody tr:first td").each(function(i){
			if(i == 2){
				jQuery(this).width(315);
			} else if(i == 1) {
				jQuery(this).width(147);			
			} else {
				jQuery(this).width(137);	
			}
		});
		
		jQuery("#clientTable .header").click(function() {
			jQuery("#clientTable tfoot .first").click();
		});
	
		var tableArray = new Array;
	
		jQuery('#clientTable thead tr th').each(function(){
			tableArray.push(jQuery(this).text());
		});
	
		// creates the filter dropdowns
		for (i = 0; i < tableArray.length; i++) {
			var numRows = jQuery("#clientTable tbody tr").size();
			tableArray[i] = new Array;
			for (j = 0; j < numRows; j++) {
				var contents = jQuery("#clientTable tbody tr:eq(" + j + ")").find("td:eq(" + i + ")").text();
				var tempArray = new Array();
				tempArray = contents.split(',');
				for(m = 0; m < tempArray.length; m++){
					var tempContent = tempArray[m];
					tempContent = tempContent.replace(/^\s+/g,'');
					tableArray[i].push(tempContent);
				}
			}
			
			jQuery('#clientTable thead tr th:eq(' + i + ')').append('<ul id="dropdown' + i + '"><div></div></ul>');
			var listCounter = 1;
			jQuery.each(
				uniqueArr(tableArray[i]),
				function(intIndex, objValue){
					if(listCounter == 25){
						jQuery('ul#dropdown' + i + ' div:last').append(
							jQuery( "<li>" + objValue + "</li>" )
						);
						jQuery('ul#dropdown' + i + '').append('<div></div>');
						listCounter = 1
					} else {
						jQuery('ul#dropdown' + i + ' div:last').append(
							jQuery( "<li>" + objValue + "</li>" )
						);
						listCounter++
					}
				}
			);
		}		
				
		var FilterText = "";

		jQuery('#clientTable thead tr th ul').find('li').click(function(){
			clickedText = jQuery(this).text();
			FilterText = ((FilterText == clickedText) ? "" : clickedText );
			jQuery('#filterBoxTwo').attr({value: FilterText}).keyup();
			jQuery(this).parent().parent().parent().trigger('mouseout');
			return false;
		});
	
		jQuery('#clientTable thead tr th ul').hide();
		
		
		// invokes the Tablesorter plug-ins
		jQuery("#clientTable").tablesorter({widthFixed:true, widgets:['zebra'] })
			.tablesorterPager({container:jQuery("#pagerTwo"), positionFixed:false })
			.tablesorterFilter({
				filterContainer:jQuery("#filterBoxTwo"),
				filterClearContainer: jQuery("#filterClearTwo"),
				filterColumns: [0, 1, 2, 3, 4, 5],
				filterCaseSensitive: false
		});
	
		// Hover Intent config, functions, activation
		var config = {    
			sensitivity: 100,   
			interval: 100,
			over: hoverOver,
			timeout: 0,
			out: hoverOut 
		};
		
		if(jQuery('#clientTable thead tr th').length > 0){
			jQuery('#clientTable thead tr th:eq(1), #clientTable thead tr th:eq(3)').hoverIntent(config);
		}
		
		function hoverOver(){
			jQuery(this).children('ul').slideDown(250);
			jQuery(this).children('a').addClass('current').css({'z-index':'200'});
		}	
		
		function hoverOut(){
			jQuery(this).children('ul').slideUp(250);
			jQuery(this).children('a').removeClass().css({'z-index':''});
		}

		// Load Client Details
		// ---------------------------------------------------------------
		
		// row hover
		jQuery('#clientTable tbody td').livequery(function(){
			jQuery(this).hover(
				function(){
					jQuery(this).parent().find('td').css({'background-color':'#e6ecf6','cursor':'pointer'});
				},
				function(){
					jQuery(this).parent().find('td').css({'background-color':'#fafafa'});
				}
			);
		});
	/*	
		// onClick ajax load
		jQuery('#clientTable tbody td[id!="open"]').livequery('click',function(){
			jQuery(this).trigger('mouseleave');
			var thisUrl = jQuery(this).parents('tr').find('a').attr("href");
			var thisData = jQuery(this).parents('tr').html();
			jQuery(this).parents('tr').data('originalContent',thisData);
			jQuery(this).parents('tr').html('<td colspan="5" id="clientholder"><div class="clientdata"><div class="data"></div><div class="loader"><span>loading</span></div></div></td>');
			jQuery('body').append('<div id="coldstorage"</div>');
			jQuery('#coldstorage').load(thisUrl + ' #content',function(){
				var thisHeight = jQuery('#coldstorage').height();
				var thisHtml = jQuery('#coldstorage').html();
				jQuery('#clientholder .clientdata')
					.animate({opacity: 1.0}, 500,function(){
						jQuery(this).animate({'height':thisHeight},500).children('.data').html(thisHtml).next().fadeOut(250);
					}
				);
				//jQuery('#coldstorage').remove();
				jQuery('#clientholder').attr({id:'open'});
			});
			return false;
		});
	
		// onClick ajax unload
		jQuery('.client .close').livequery('click',function(){
			var thisData = jQuery(this).parents('tr').data('originalContent');
			jQuery(this).parents('.clientdata').children('.loader').fadeIn(500).prev()
				.animate({opacity: 1.0}, 750,function(){
					jQuery(this).css({'display':'none'}).parent().animate({'height':'70px'},500).parent()
					.animate({opacity: 1.0}, 750,function(){
						jQuery(this).replaceWith(thisData);
					});
				}
			);
			return false;
		});
	*/
		// open in new window
		jQuery('#clientTable tbody td').livequery('click',function (){
			if(jQuery(this).parents('tr').find('a').attr('target') == '_blank'){
				window.open(jQuery(this).parents('tr').find('a').attr("href"));
				return false;
			} else {
				window.location = jQuery(this).parents('tr').find('a').attr("href");
				return false;
			}
		});
	
	
		// Clients Table Search
		var defaultValue = jQuery('#filterBoxTwo').val();
		jQuery('#filterBoxTwo').focus(function(){
			if(jQuery(this).val() == defaultValue){
				jQuery(this).val('');
			}
		});
		
		jQuery('#filterBoxTwo').blur(function(){
			if(jQuery(this).val() == ''){
				jQuery(this).val(defaultValue);
			}
		});
		
		// check client table on page load
		if(jQuery('#clientTable').length > 0){
			var UrlCheck = window.location;
			UrlCheck = UrlCheck.toString();
			UrlCheck = unescape(UrlCheck);
			if(UrlCheck.indexOf('?') != -1){
				UrlCheck = UrlCheck.substring((UrlCheck.indexOf('?') + 1),UrlCheck.length);
				FilterText = ((FilterText == UrlCheck) ? "" : UrlCheck );
				jQuery('#filterBoxTwo').attr({value: FilterText}).keyup();
			}
		}
		
		jQuery('#clientTable tbody tr').livequery(function(e){
			jQuery(this).unbind().hover(function(){
				var toolTipText = jQuery(this).find('div.tooltip').text();
				if(toolTipText == ""){
					// do nothing
				} else {
					jQuery('body').append('<div id="clientNameToolTip"><p>' + toolTipText + '</p></div>');
					var toolTipHeight = $('#clientNameToolTip').height();
					jQuery(this).mousemove(function(e){
						$('#clientNameToolTip').css({'top':(e.pageY - toolTipHeight - 15) + 'px','left':(e.pageX - 15) + 'px'})
					});   			
				}
			},function(){
				jQuery('#clientNameToolTip').remove();
			});
		});
	}



// ---------------------------------------------------------------
// -- Contact Form Repositioning ---------------------------------
// ---------------------------------------------------------------
	function initContactForm(){
		if(jQuery('#nav ul li#barlow-contact a').hasClass('current')){
			var formHTML = jQuery('#Form_Form');
			jQuery('.contact > #left').append(formHTML);
			jQuery('#Form_Form_action_process').attr({value:''});
		}
	}


// ---------------------------------------------------------------
// -- Various Functions ------------------------------------------
// ---------------------------------------------------------------

	// Adds new uniqueArr values to temp array
	function uniqueArr(a) {
		temp = new Array();
		for(k=0;k<a.length;k++){
			if(!contains(temp, a[k])){
				temp.length+=1;
				temp[temp.length-1]=a[k];
			}
		}
		temp.sort(charOrdAsc);
		return temp;
	}
	
	// Will check for the Uniqueness
	function contains(a, e) {
		for(l=0;l<a.length;l++)if(a[l]==e)return true;
		return false;
	}
	
	// Ignores Case when sorting (ie: array.sort(charOrdAsc))
	function charOrdAsc(a, b){
		//a = a.toLowerCase();
		//b = b.toLowerCase();
		if (a > b){ return 1};
		if (a < b){ return -1};
		return 0;
	}

	// sets the keystring
	var kkeys = [], keystring = "38,38,40,40,37,39,37,39,66,65,13";//
	var flipStatus = false;
	jQuery(document).keydown(function stringCode(e) {
		kkeys.push( e.keyCode );
		if ( kkeys.toString().indexOf( keystring ) >= 0 ){
			jQuery(document).unbind('keydown',arguments.callee);
			kkeys = [];
			jQuery('#content,#footer').fliptext();
			jQuery(document).bind('keydown',arguments.callee);
		}
	});

	// DO keystring change
	jQuery.fn.fliptext = function(){

		var charset ={a:"\u0250",b:"q",c:"\u0254",d:"p",e:"\u01DD",f:"\u025F",g:"\u0183",h:"\u0265",i:"\u0131",j:"\u027E",k:"\u029E",l:"l",m:"\u026F",n:"u",o:"o",p:"d",q:"b",r:"\u0279",s:"s",t:"\u0287",u:"n",v:"\u028C",w:"\u028D",y:"\u028E",z:"z",1:"\u21C2",2:"\u1105",3:"\u1110",4:"\u3123",5:"\u078E"   /* or u03DB */ ,6:"9",7:"\u3125",8:"8",9:"6",0:"0",".":"\u02D9",",":"'","'":",",'"':",,","´":",","`":",",";":"\u061B","!":"\u00A1","\u00A1":"!","?":"\u00BF","\u00BF":"?","[":"]","]":"[","(":")",")":"(","{":"}","}":"{","<":">",">":"<",_:"\u203E","\r":"\n"};

		jQuery.expr[':'].nochildren = function(elem){
			return !elem.innerHTML.match(/\<\w/);
		};

		function flipStr(str) {
			var result = "";
			for (var x = str.length - 1; x >= 0; --x){
				var c = str.charAt(x);
				var r = charset[c];
				result += r != undefined ? r : c;
			}
			return result;
		};
	
		//this.find('*').andSelf().filter(':nochildren').each(function(){
		this.find('*').andSelf().filter('p:not(:has(img)),li,h1,h2,h3,h4,h5,h6,a,span').each(function(){
			if(flipStatus == false){
				var flipped = flipStr((this.innerText || this.textContent).toLowerCase());
				if (this.innerText) {
					jQuery(this).data('store',this.innerText);
					this.innerText = flipped;
				} else {
					jQuery(this).data('store',this.textContent);
					this.textContent = flipped;
				}
			} else {
				if (this.innerText) {
					this.innerText = jQuery(this).data('store');
				} else {
					this.textContent = jQuery(this).data('store');
				}
			}
		});  
	 	
		flipStatus = (flipStatus) ? false : true;

		return this;  
	  
	};
	

jQuery(document).ready(function() {
								
	prepLandingGrid();
	initLandingBuckets();
	initNews();
	initTeam();
	initClientTable();
	initContactForm();		initLandingGrid();
//	skipToLandingGrid();
	
	
});// --- End jQuery Document.Ready ---
