var xmldata;
var servicedata;
var defaultservicetitle;
var defaultservicedesc;

var pillarsxmldata;
var pillarsdata;
var defaultpillarstitle;
var defaultpillarsdesc;

this.randomquote = function(){
	var length = $("div.randomQuote blockquote").length;
	var ran = Math.floor(Math.random()*length);
	//alert(ran);
	$("div.randomQuote blockquote:eq(" + ran + ")").show();
};

//GETTING URL VARIABLES FOR VARIOUS (SEARCH OR OTHERWISE) FUNCTIONS
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


//var buttonWidth;
$(document).ready(function() {
	
	//HoverIntent
	(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
	
	//Highlight Team List
	var title = $('body').attr('class');
	if(title){
	$('div#left-sidebar dd#'+title).addClass('selected');
	}
	
	
	//Box Clicks
	$('div.clickbox').click(function(){
									 window.location = $(this).find('a.learnMore').attr('href')
									 return false
									 });
	
	
	//Navigation z-index fix
	
	$('div#topNavContainer').hover(function(){
			$(this).css('z-index', 100);								
											},
											function(){
												$(this).css('z-index', 1);
											});
	
	
	//Random Quotes
	if($('div.randomQuote').length){
		randomquote();
	}
	
	
	
	//Top Bar Navigation Dropdown
	$('ul.subnav').each(function(){
					var buttonWidth = $(this).parent().width();
					
					$(this).css('width', buttonWidth+'px').children('li').css({'width': buttonWidth+'px'});
						
					
								 });
	
	
	var topNavconfig = {    
     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: tnDrop, // function = onMouseOver callback (REQUIRED)    
     timeout: 250, // number = milliseconds delay before onMouseOut    
     out: tnRaise // function = onMouseOut callback (REQUIRED)    
	};

	
	function tnDrop(){
							var buttonWidth = $(this).width();
							$(this).addClass('hovering');
							var subnav = $(this).find('ul.subnav');
							
							//subnav
							
							

							$(subnav).slideToggle('medium');

							/*
							REPLACE THIS
							subnav.slideDown().children('li').animate({ 
								opacity: 1
								  }, 300 ).find('a').each(function(){
										  var buttonHeight = $(this).height();
													  $(this).parent('li').css({'height': buttonHeight+'px'});
								  });*/
	}
	
	function tnRaise(){
	$(this).removeClass('hovering').find('ul.subnav').slideToggle('medium');
	}
	
	$('li.tn-button:has(ul.subnav)').hoverIntent(topNavconfig);

	
//	$('li.tn-button').hover(
//							function(){
//							buttonWidth = $(this).width();
//							$(this).addClass('hovering');
//							var subnav = $(this).find('ul.subnav');
//							subnav.css('width', buttonWidth+'px');
//							//subnav
//							subnav.slideDown().children('li').css({'opacity' : '0', 'width': buttonWidth+'px'}).animate({ 
//								opacity: 1,
//								  }, 300 );
///*							subnav.slideDown('slow', function(){
//															  $(this).children('li').css({'opacity' : '0', 'width': buttonWidth+'px'}).animate({ 
//								opacity: 1,
//								  }, 300 );
//															  });*/
//							},
//							function(){
//							$(this).removeClass('hovering').find('ul.subnav').slideUp().children('li').animate({ 
//								opacity: 0,
//								  }, 300 );								
//							});
	
	
	
	
	//Services Graphic Actions
	
		//load up xml data
	if($('#Map').length){
	
	$.ajax({
        type: "GET",
	url: "/data/services.xml",
	dataType: "xml",
	success: function(xml) {
 			xmldata = xml
	}
});
		
	}
	
	defaultservicetitle = $('dt#inner-header').text();
	defaultservicedesc = $('dd#inner-desc').text();
	


	
	$('#Map area').hover(
				function(){
				var areaid = $(this).attr('id');				 
				
				switch(areaid)
        {
            case "area-1":
			$('div#services-bkg').css('background-position', '0 -524px');
           	
		   	
			break;
            case "area-2":
			$('div#services-bkg').css('background-position', '0 -1048px');
            
			break;
            case "area-3":
			$('div#services-bkg').css('background-position', '0 -1572px');
            
			break;
            case "area-4":
			$('div#services-bkg').css('background-position', '0 -2096px');
            
			break;
            case "area-5":
			$('div#services-bkg').css('background-position', '0 -2620px');
            
			break;
            case "area-6":
			$('div#services-bkg').css('background-position', '0 -3144px');
            
			break;
            case "area-7":
			$('div#services-bkg').css('background-position', '0 -3668px');
            
			break;
			default: 
			
           
        }

			servicedata = $(xmldata).find('service[id='+areaid+']');
			if(servicedata.length){
			$('dt#inner-header').text($(servicedata).find('heading').text());
		    $('dd#inner-desc').text($(servicedata).find('description').text());
			$('span#arrow').css('background-position', '-396px -30px');
			}
			else{
			$('dt#inner-header').text(defaultservicetitle);
		    $('dd#inner-desc').text(defaultservicedesc);
			$('span#arrow').css('background-position', '-367px -30px');
			}
			$("div#rolloverbox").removeClass('cssframe-dark').addClass('cssframe-darker');
								 },
				function(){
					$('div#services-bkg').css('background-position', '0 0px');
					$('dt#inner-header').text(defaultservicetitle);
		    		$('dd#inner-desc').text(defaultservicedesc);
					$('span#arrow').css('background-position', '-367px -30px');
					$('div#rolloverbox').removeClass('cssframe-darker').addClass('cssframe-dark');
				});
	
	
	//Four Pillars Graphic Actions 
	
		$('#Map-four-pillars area').hover(
				function(){
				var areaid = $(this).attr('id');				 
				
				switch(areaid)
        {
            case "area-1":
			$('div#four-pillars-bkg').css('background-position', '0 -274px');
           	
		   	
			break;
            case "area-2":
			$('div#four-pillars-bkg').css('background-position', '0 -548px');
            
			break;
            case "area-3":
			$('div#four-pillars-bkg').css('background-position', '0 -822px');
            
			break;
            case "area-4":
			$('div#four-pillars-bkg').css('background-position', '0 -1096px');
            
			default: 
			
           
        }

		
								 },
				function(){
					$('div#four-pillars-bkg').css('background-position', '0 0px');
				});
		
	if($('div#four-pillars-gallery').length){
	$('div#industries-gallery').css('width', '700px');
	var industryScrollable = $("div.scrollable").scrollable({
									size:1,
									clickable:false,
									api:true
															
															});

	var industryPop = $("div#four-pillars-gallery").overlay({  // one configuration property 
    
 
    // another property 
     // optional exposing effect 
    expose: '#595959', 
	api: true})	
	}
	
	
	
	
	
	$('a.four-pillar-click').click(function(){
				
				
				var areaindex = $(this).attr('rel');
			//	$('#gallery div.popupContent').empty().load('/data/industries-popup-html.php #'+areaid)
				industryPop.load();	
				industryScrollable.seekTo(areaindex-1);
				
						 return false;
						 });
	
	
	$('#Map-four-pillars area').click(function(){
												   
			var areaid = $(this).attr('id');
			var areaindex = $(this).attr('rel');
			//	$('#gallery div.popupContent').empty().load('/data/industries-popup-html.php #'+areaid)
				industryPop.load();	
				industryScrollable.seekTo(areaindex-1);
															   
											   return false;
											   });
	
/*		//load up xml data
	if($('#Map-four-pillars').length){
	
	$.ajax({
        type: "GET",
	url: "/data/four-pillars.xml",
	dataType: "xml",
	success: function(xml) {
 			pillarsxmldata = xml
	}
});
		
	}
	
	defaultpillarstitle = $('dt#inner-header').text();
	defaultpillarsdesc = $('dd#inner-desc').text();
	

	
	
	$('#Map-four-pillars area').hover(
				function(){
				var areaid = $(this).attr('id');				 
				
				switch(areaid)
        {
            case "area-1":
			$('div#four-pillars-bkg').css('background-position', '0 -274px');
           	
		   	
			break;
            case "area-2":
			$('div#four-pillars-bkg').css('background-position', '0 -548px');
            
			break;
            case "area-3":
			$('div#four-pillars-bkg').css('background-position', '0 -822px');
            
			break;
            case "area-4":
			$('div#four-pillars-bkg').css('background-position', '0 -1096px');
            
			default: 
			
           
        }

			pillarsdata = $(pillarsxmldata).find('pillar[id='+areaid+']');
			if(pillarsdata.length){
			$('dt#inner-header').text($(pillarsdata).find('heading').text());
		    $('dd#inner-desc').text($(pillarsdata).find('description').text());
			$('span#arrow').css('background-position', '-396px -30px');
			}
			else{
			$('dt#inner-header').text(defaultpillarstitle);
		    $('dd#inner-desc').text(defaultpillarsdesc);
			}
			$("div#rolloverbox").removeClass('cssframe-dark').addClass('cssframe-darker');
								 },
				function(){
					$('div#four-pillars-bkg').css('background-position', '0 0px');
					$('dt#inner-header').text(defaultpillarstitle);
		    		$('dd#inner-desc').text(defaultpillarsdesc);
					$('div#rolloverbox').removeClass('cssframe-darker').addClass('cssframe-dark');
				});
*/	
	

	
	//FORMS
	
	$('form#cotendoContact').submit(function(){
										
	 
											 return false;
											 
											 });
	
	
	$('form#cotendoPushToStart').submit(function(){
											 
											 return false;
											 
											 });
	
	
	
	
	//Search
	
	
	$('input.searchbox').focus(function() {
	if($(this).val() == "Search"){
  			$(this).val('');
	}
})
//	$('form#sitesearch').submit(function(){
//						
//	//					alert("You searched for: " + $('input#searchterm').val());
//
//	 
//								
//											 
//											 });



	
	//Team Rollover Switches
	    /*
        1st parameter: Images to be selected.
        2nd parameter: Preload the image or not, default = true.
        3rd parameter: Repeat the effect or not, default = true.
        4th parameter: Event for swap In, default = mouseenter.
        5th parameter: Event for swap Out, default = mouseleave.

        Valid events such as: mouseenter, mouseleave, click, dblclick.
    */
		if($('img.swapImage').length){
		$.swapImage('img.swapImage');
		}

	
	
	//Image Buttons and Galleries http://flowplayer.org/tools/overlay.html
	
	
	$('a.freetrial').hide();
	//Global Vars for overlay
	var overlayopts = {
		 // each trigger uses the same overlay with the id "gallery" 
    target: '#gallery', 
 
    // optional exposing effect 
    expose: '#595959',
	onLoad: function(event){$('a.freetrial').delay(2000).fadeIn(1000)},
	onClose: function(event){$('a.freetrial').hide()}
	}
	
	var galopts = {
		autohide: false,
		template: '<h4>${title}</h4> <span>Image ${index} of ${total}</span>',
		opacity: 1
	}
	//Apply the overlay and gallery
	if($('a.screenshotIMG-small').length){
	$("div.screenshot a.screenshotIMG-small").overlay(overlayopts).gallery(galopts);
	}
	//Do it for the large version as well
	//Apply the overlay and gallery
	if($('a.screenshotIMG-large').length){
	$("div.screenshot a.screenshotIMG-large").overlay(overlayopts).gallery(galopts);
	}
	//Separate the image button from the enlarge button so that there aren't duplicates
	if($('a.screenshotEnlarge').length){
	$("div.screenshot a.screenshotEnlarge").overlay(overlayopts).gallery(galopts);
	}
});
