
$.fn.reverse = [].reverse; 

//take advantage of all available space
function handleResize () {
	if ($(window).width() < 1390 && $(window).width() >= 1265) {
		$("body").addClass('size1280');
		$("body").removeClass('size1024');
	} else	if ($(window).width() < 1265) {
		$("body").addClass('size1024');
		$("body").removeClass('size1280');
	} else {
		$("body").removeClass('size1024');
		$("body").removeClass('size1280');
	}	
}



$(window).resize(handleResize);


function addSemiTransparentBackground(cssSelector, className, noBackgroundClassName) {
	$(cssSelector).each(function () {
		var ow = $(this).outerWidth();
		var oh = $(this).outerHeight();
		var w = $(this).width();
		var h = $(this).height();
		var t = $(this).css('top');
		var l = $(this).css('left');
		
		var innerHtml = $(this).html();
		
		$(this).addClass(noBackgroundClassName);
		$(this).html('<div class="inner-content" style="width:' + w + 'px;height:' + h + 'px;">' + innerHtml + '</div>');
		//$(this).append('<div class="inner-content" style="width:' + w + 'px;height:' + h + 'px;"></div>');
		$(this).append('<div class="' + className + '" style="position:absolute;top:0;left:0;width:' + ow + 'px;height:' + oh + 'px;"></div>');
		
		/*$(this).resize(function() {
			
		}*/
	});
}

$(document).ready(function() {

	//addSemiTransparentBackground(".section-navigation", "section-navigation-translucent-background", "section-navigation-no-background");
	addSemiTransparentBackground(".what-we-do .services", "services-translucent-background", "services-no-background");
	
	handleResize();

    // put delimeters for top navigation
    
    $('.top-navigation').find('.top-navigation-item').not('.top-navigation-item:first-child').each(function() {
        var separator = document.createElement('li');
        separator.innerHTML = '&nbsp;';
        $(separator).addClass('separator');
        $(separator).addClass('top-navigation-item');
        $(this).before($(separator));
    });
    $('.header-navigation').find("li:contains('contact us')").hide();
    $('.header-navigation').find("li:contains('/'):last").hide();
    Cufon.replace(".aaux-light", {fontFamily:'Avenir LT Std',hover: true});
    Cufon.replace('.microsite-title', {fontFamily:'Avenir LT Std'});
    Cufon.replace('.microsite-sub-title', {fontFamily:'Avenir LT Std'});
    Cufon.replace('.microsite-byline', {fontFamily:'Avenir LT Std'});
    Cufon.replace('.microsite-sidebar-item', {fontFamily:'HelveticaNeue'});
    Cufon.replace(".helvetica-neue", {fontFamily:'HelveticaNeue', hover: true});
    Cufon.replace(".section-navigation li", {fontFamily:'Avenir LT Std', hover: true});
    //text rendering
	Cufon.replace(".main-navigation-link", {hover:true, separate: "words"});
	Cufon.replace(".sub-navigation-link");
	Cufon.replace(".footer-navigation a", {fontFamily:'Avenir LT Std', hover:true, separate: "words"});
	Cufon.replace(".header-navigation", {fontFamily:'Avenir LT Std',letterSpacing:'-1px', hover: true, separate: "words"});
	Cufon.replace(".section-navigation h2", {hover: true, separate: "words"});
	Cufon.replace(".copyright", {separate : "none"});
	Cufon.replace(".breadcrumb-text", {separate : "none", hover: true});
	Cufon.replace(".team h2");
	Cufon.replace(".what-we-do-title",{fontFamily:'Avenir LT Std'});
	Cufon.replace(".records-nav .entries-nav", {separate : "none"});
	Cufon.replace(".top-navigation-link", {fontFamily:'Avenir LT Std',fontWeight:'600',letterSpacing:'0px', separate : "none", hover: true});
	Cufon.replace(".top-navigation-item.separator", {fontFamily:'Avenir LT Std',separate : "none"});
	Cufon.replace(".portfolio-nav a", {separate:"none", hover: true});
	Cufon.replace(".portfolio-featured h1");
	Cufon.replace(".action h3"),{fontFamily:'Avenir LT Std'};
	Cufon.replace(".action h2"),{fontFamily:'Avenir LT Std'};
	Cufon.replace(".arrow",{fontFamily:'AauxLight'});
	Cufon.replace(".team-featured-section h3");
	Cufon.replace(".company .primary_message p",{fontFamily:'Avenir LT Std'}); 
	Cufon.replace(".who-we-are .primary_message p",{fontFamily:'Avenir LT Std'});
	Cufon.replace(".main-content .sub-title p",{fontFamily:'Avenir LT Std'});
	Cufon.replace(".contact-us h2");
	Cufon.replace(".sub-content .section h3");
	Cufon.replace(".notfound .message h3");
	$(".ui-accordion").accordion({header:"h5", active:false, autoHeight:false, collapsible:true, icons: { 'header': 'more-icon', 'headerSelected': 'less-icon'} }  );
			
	//main navigation mouseovesr
	$(".main-navigation").find("a").each(function(i) {
	    this.onmouseover = function(){
	        $(this).addClass('hover');
	        Cufon.replace(".main-navigation-link.hover");
	        Cufon.replace(".sub-navigation-link.hover");
	        if ($(this).hasClass("main-navigation-link"))
	        {
	            $(this).addClass('hover');
	            Cufon.replace(".main-navigation-link");
	            Cufon.replace(".sub-navigation-link");
	        }
	        $(this).css('cursor','pointer');
	    };
	    this.onmouseout = function(){
	        $(this).removeClass('hover');
			window.setTimeout(function () {	//defeat IE 6-8 repainting delay bug 
		        Cufon.replace(".main-navigation-link");
		        Cufon.replace(".sub-navigation-link");
	        }, 1);
	    };
	 });
	 
	 
	 $(".main-navigation-link.submenu-active").each(function(i) {
	    $(this).parent().next().show();
	 });
	 $(".main-navigation").find("h2 .active").not(".submenu-active").parent().next().slideDown(750);
     
     $(".portfolio-image").ImageGallery();
     
     if ($(".portfolio-image").length > 0 && window.location.hash == '#case-study') {
        QSite.modalPanel('#caseStudyContent','.portfolio-featured');
     }
     
     if ($(".page-news").length > 0 && window.location.hash.indexOf("#") > -1) 
     {
        el = $('.news-item a');
        el = jQuery.grep(el, function(n, i) {
             return ('#' + n.name == window.location.hash);
        });
        par = $(el).parents('.news-item');
        if (par.length > 0) {
            $(par).click();
            $(par).prevAll('.news-item-extra').andSelf().reverse().each(
                function(i,n) {
                    $(n).removeClass('news-item-extra');
                });
            if ($('.news-item-extra').length == 0) {
                $('.news-see-all-link').hide();
            }
        }
       setTimeout(function() {window.location.href = window.location.hash}, 300);
     }
	 
	 //Company mouse overs
	 $(".action .title").each(function(i) {
	    this.onmouseover = function(){
	        $(this).addClass('hover');
	       // Cufon.replace(".action .title", {fontFamily: 'Avenir LT Std'});
	        $(this).css('cursor','pointer');
	    };
	    this.onmouseout = function(){
	        $(this).removeClass('hover');
			window.setTimeout(function () {	//defeat IE 6-8 repainting delay bug 
		   //     Cufon.replace(".action .title", {fontFamily: 'Avenir LT Std'});
			}, 1);
	    };
	    
	});
	
	//news page News Link
	
	$('.news-see-all-link').each(function(){
	    this.onclick = function() {
	        $('.news-item-extra').each(function(i,el) {
	            if (i < 5) { 
	                $(el).removeClass('news-item-extra');
	            }
	        });
	        
	        if ($('.news-item-extra').length == 0)
	        {
	            $(this).hide();
	        }
	        return false;
	    }
	});
});



