		$(document).ready(function () {
		    //IE background flickering
		    if (jQuery.browser.msie && jQuery.browser.version == 6.0)
		        document.execCommand("BackgroundImageCache", false, true);

		    $("a[rel=external]").bind("click", function () { $(this).attr("target", "_blank") });

		    $('img[@src$=png]').ifixpng();

		    /** sucker fish drop down */
		    $("ul.Nav0 > li").hoverIntent(
  	function () { $("ul", this).slideDown(150) },
  	function () { $("ul", this).slideUp(150) }
  );
		    //Hover(".Nav0 li");
		    var nav0SwapImage = function () {
		        $(".Nav0 > li").hover(
			function () {
			    if ($(this).hasClass("Active")) return;
			    var imgPath = $(this).find("img:first").attr("src");
			    $(this).find("img:first").attr("src", imgPath.replace(".gif", "Active.gif"));
			},
			function () {
			    if ($(this).hasClass("Active")) return;
			    var imgPath = $(this).find("img:first").attr("src");
			    $(this).find("img:first").attr("src", imgPath.replace("Active.gif", ".gif"));
			}
		);
		        $("ul.Nav0 > li.Active > a:first img").each(function () {
		            var imgPath = $(this).attr("src");
		            $(this).attr("src", imgPath.replace(".gif", "Active.gif"));
		        });

		    }
		    nav0SwapImage();

		    $('.GalleryList').prepend('<li class="first"></li>');




		    /** sucker fish drop down */
		    $(".AllOffices").hoverIntent(
	   function () { $("ul.Nav2", this).slideDown(200) },
	   function () { $("ul.Nav2", this).slideUp(200) }
	 );


		    /** Slider */
		    $(".ShowSlider").hover(
	   function () { $('.SlideUp', this).stop().animate({ top: 0 }, { queue: false, duration: 250, easeMethod: 'backout' }) },
	   function () { $('.SlideUp', this).stop().animate({ top: 105 }, { queue: false, duration: 250, easeMethod: 'backin' }) }
	 );
		    $(".Clients li,.Cases li").hover(
	   function () { $('.SlideUp', this).stop().animate({ top: 0 }, { queue: false, duration: 300, easeMethod: 'backout' }) },
	   function () { $('.SlideUp', this).stop().animate({ top: 105 }, { queue: false, duration: 300, easeMethod: 'backin' }) }
	 );



		    //swap active image on hover of main menu
		    var nav1SwapImage = function () {

		        $("ul.Nav1 > li").hover(
			function () {
			    if ($(this).hasClass("current")) return;
			    var imgPath = $(this).find("img:first").attr("src");
			    $(this).find("img:first").attr("src", imgPath.replace(".png", "Active.png"));
			},
			function () {
			    if ($(this).hasClass("current")) return;
			    var imgPath = $(this).find("img:first").attr("src");
			    $(this).find("img:first").attr("src", imgPath.replace("Active.png", ".png"));
			}
		);
		        $("ul.Nav1 > li.selectedLava > a:first img").each(function () {
		            var imgPath = $(this).attr("src");
		            //$(this).attr("src", imgPath.replace(".png", "Active.png"));
		        });

		    }
		    nav1SwapImage();

		    if ($('.HomeMainNav').length) {
		        //lavaLamp
		        $(".Nav1").lavaLamp({
		            fx: "backout",
		            speed: 800,
		            homeLeft: -120,
		            homeTop: 22
		        });

		    }
		    else {
		        //lavaLamp

		        $(".Nav1").lavaLamp({ fx: 'backout', speed: 800, returnDelay: 100, autoReturn: true });
		    }



		    // FirstChild and LastChild class
		    var FirstAndLastChild = function () {
		        if ($.browser.msie || $.browser.opera) {
		            $("li:first-child").addClass("FirstChild");
		            $("li:last-child").addClass("LastChild");
		        }
		        if ($.browser.safari) {
		            $("li:last-child").addClass("LastChild");
		        }
		    }
		    FirstAndLastChild();

		    paging();
		    accordion();
		    relatedLinks();
		    awardsPagination();
		    filter();
		    cStudies();

		    if ($('.PrintList').length)
		        imageGallery();

		    //popup
		    var invokePopup = function () {
		        $("a[rel=popup]").unbind("click").bind("click", function () {
		            popupWin(this.href, "Popup", { width: 480, height: 610 })
		            return false;
		        });
		    }
		    invokePopup();

		    boxExpand();
		});


function boxExpand()
{
 $('.BoxMore').append('<h4 class="Heading">' +$('.Box1 h4.Heading').html()+ '</h4><p class="Heading">'+ $("a.BoxExpand").attr('title')+'</p>');
 
 $("a.BoxExpand").unbind("click").bind("click",function(){
			return false;
	});
		
	$(".BoxExpand").hover(
	  function(){
	   $('div.BoxMore').show();
	  }, 
	  function(){
	   
	  }
	);
	
 $(".BoxMore").bind("mouseout",function(){
	   $(this).hide();
	});
	
}



//image gallery
function imageGallery()
{   
	//first element
	$('.PrintList li:eq(0)>div').show();
	$('.PrintList li:eq(0)>img').css({'opacity':'.25'});
	
	/** rollhover */
	$(".PrintList li").hover(
	  function(){
	   $(this).find('img:first').animate({opacity:0.25},{queue:false, duration:150, easeMethod: 'backout'});
	   $('.PrintList li:eq(0)>img').css({'opacity':'1'});
	   $('.PrintList li:eq(0)>div').hide();
	   $('div', this).show();
	  }, 
	  function(){
	   $(this).find('img:first').animate({opacity:1},{queue:false, duration:150, easeMethod: 'backin'});
	   $('div', this).hide();
	  }
	);
 
 $(".PrintList").hover(
	  function(){},
	  function(){
	   $('.PrintList li:eq(0)>div').show();
	   $('.PrintList li:eq(0)>img').animate({opacity:.25},{queue:false, duration:200, easeMethod: 'backin'});
	  }
 );
	
 $(".PrintList li a.More").unbind("click").bind("click",function(){
  
  var strSrc = $(this).attr('href');
  var strIndex = $('.PrintList li').index($(this).parents('li:eq(0)'));
  var strCount = $('.PrintList li').length;
  $('.Content').append('<div class="Overlay"><p><img src="../Common/Image/Generic/Close.gif" alt="" /></p><div><img src='+$(this).attr('href')+' alt="" class='+strIndex+' /></div><ul><li class="Previous"><img src="../Common/Image/Generic/MovePrevious.gif" alt="" /></li><li class="Next"><img src="../Common/Image/Generic/MoveNext.gif" alt="" /></li></ul></div>');
  
  if(strIndex == 0)
   $(".Overlay ul .Previous").hide();
  else if(strIndex == strCount-1)
   $(".Overlay ul .Next").hide();

    $('.Overlay p img').bind("click",function(){
     $('.Overlay').remove();
    });
    
    $(".Overlay ul .Previous").bind("click",function(){
     strIndex=strIndex-1;
     if(strIndex < 0){
      $(this).hide();
     }
     else{
      $('.Overlay div img').attr('src',$('.PrintList li:eq('+strIndex+') a.More').attr('href'));
      $(".Overlay ul .Next").show();
     }
    });
    
    $(".Overlay ul .Next").bind("click",function(){
     strIndex=strIndex+1;
     if(strCount > strIndex){
      $('.Overlay div img').attr('src',$('.PrintList li:eq('+strIndex+') a.More').attr('href'));
      $(".Overlay ul .Previous").show();
     }
     else{
      $(this).hide();
     }
    });

 	return false;
 });

 

	
}

// Pagining width Handler
function paging()
{
	if($(".GalleryList > li").length)
	{
	 var len = $(".GalleryList > li").size();
	 if (len == 15)
	  $(".Paging").addClass('Paging1');
	 else if(len>12 && len==14)
	  $(".Paging").addClass('Paging2');	
	 else if(len>12 && len==13)
	  $(".Paging").addClass('Paging3');	
	 else
	  $(".Paging").addClass('Paging4');	
	}
}

// Pagining width Handler
function accordion()
{
	$(".Accordion li>a").bind("click",function(){
		$(this).parent().parent().find(".Selected").removeClass();
		$(this).parent().addClass("Selected");
		
		if($(this).siblings('ul').length == 0)
		 return true
		else
		 return false
	});
}

// casestudies
function cStudies()
{
	$("ul.RadioList li").each(function(index){
		$(this).find("div").attr("id","radio"+(index+1));
		
		//var radio = new SWFObject("/memacogilvy/Common/Flash/rAdio pLayer.swf", "radio1", "463", "19", "8", "#ffffff");
		//radio.addParam("wmode", "transparent");
		//radio.addVariable("sNduRl",$(this).find("span").html());
		//radio.write("radio"+(index+1));
		
		var swf_id = 'embed';
		
	 	var flashvars = {
	 	    sNduRl: $(this).find("span").html()
	    };
		var params = {
			sNduRl: $(this).find("span").html()
		};
		var attributes = {};
		
		swfobject.embedSWF("/memacogilvy/Common/Flash/rAdio pLayer.swf", "radio"+(index+1), "463", "19", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	});
}

// Related Links Handler
function relatedLinks()
{
	$("ul.RelatedLinks").each(function(){
		var len = $(this).find(">li").length;
		if(len>3)
		{
		  var $copy = $(this).clone();
		  $copy.insertAfter(this);
		  $(this).find(">li:gt(2)").remove();
		  $copy.find(">li:lt(3)").remove();
		  if(window.location.href.indexOf('ar_ae')>0)
		   $(this).css({'padding-left':'15px'});
		  else
		   $(this).css({'padding-right':'15px'});
		  $(this).css({'clear':'both'});
		}
	});
}

// awards pagination
function awardsPagination()
{
	var pageHeight = $('.FixedHolder').height();
	var pageSize;
	if($('.Holder').length)
	{
		pageSize = Math.ceil($('.Holder').height()/pageHeight);
		if(pageSize>1)
		{
			$('.Paging').append('<ul></ul>');
		  for(i=1;i<=pageSize;i++)
		  {
		  	if(i==1)
		  		$(".Paging ul").append('<li class="Selected"><a href="#">'+i+'</a></li>');
		  	else
		  		$(".Paging ul").append('<li><a href="#">'+i+'</a></li>');
		  }
		  $('.Paging h5 span').text('Page 1/'+pageSize);
	  }
	  else
	  {
	  	$('.Paging').hide();
	  }
	  
	  //numbering
	  $(".Paging li>a").bind("click",function(){
	  	if($(this).parent().hasClass('Selected')) return false;
	  	$('.Holder').css({marginTop:'-'+($(this).text()-1)*pageHeight+'px'});
	  	$(this).parent().parent().find('li').removeClass('Selected');
	  	$(this).parent().addClass('Selected');
	  	$('.Paging h5 span').text('Page '+ $(this).text() +'/'+pageSize);
	  	return false;
	  });
	  
    //next click
    $(".Paging a.Next").bind("click",function(){
     var topMargin = 	Math.abs(parseInt($(".Holder").css('margin-top')));
     if($(".Holder").height()> topMargin+pageHeight)
     {
     	topMargin = topMargin + pageHeight;
      $(".Holder").css({marginTop:'-'+topMargin+'px'});
      $(".Paging ul").find(".Selected").removeClass("Selected").next("li").addClass("Selected");
      $('.Paging h5 span').text('Page '+ (topMargin/pageHeight+1) +'/'+pageSize);
     }
     return false;
    });
    
    
    //previous click
    $(".Paging a.Previous").bind("click",function(){
     var topMargin = 	Math.abs(parseInt($(".Holder").css('margin-top')));
     if(topMargin > 0)
     {
     	topMargin = topMargin - pageHeight;
      $(".Holder").css({marginTop:'-'+topMargin+'px'});
      $(".Paging ul").find(".Selected").removeClass("Selected").prev("li").addClass("Selected");
      $('.Paging h5 span').text('Page '+ (topMargin/pageHeight+1) +'/'+pageSize);
     }
     return false;
    });
	}
}

// filter
function filter()
{
	if($('.Nav2').length)
	{
		$(".Nav2 a").bind("click",function(){
			$(this).parent().parent().hide();
			$('.AllOffices h2').text($(this).text());
    });
	}
}

//desc Popup window function
function popupWin(sLink,sName,oAttrib){
	var popup;
	var width = oAttrib.width;
	var height = oAttrib.height;
	var left,top;
	left = oAttrib.left || (screen.width - width)/2;
	top = 0;//oAttrib.top || (screen.height - height)/2;
	try{
		popup = window.open(sLink,sName,"scrollbars=yes,width="+width+",height="+height+",left="+left+",top="+top+"");
		popup.focus();
	}
	catch(error){
		alert("You are trying to access a broken link. Please contact the website administrator.");
		return false;
	}
}

//youtube
function youtubeCasePage(videoID) {
    swfobject.embedSWF('http://www.youtube.com/v/' + videoID + '&enablejsapi=1&rel=0&fs=1', 'ytplayer_div', '460', '265', '8', null, null,
    { allowScriptAccess: 'always',
      allowFullScreen: 'true' },
    { id: 'ytplayer_object' });
}

function ytplayer_loadvideo(id) 
{
    var o = document.getElementById('ytplayer_object');
    if (o) {
        o.loadVideoById(id);
    }
}

function youtubeHomePage(videoID) {
    swfobject.embedSWF('http://www.youtube.com/v/' + videoID + '&enablejsapi=1&rel=0&fs=1', 'ytplayer_div', '215', '180', '8', null, null,
    { allowScriptAccess: 'always',
        allowFullScreen: 'true'
    }, { id: 'ytplayer_object' });
}



		function makeTall(){  $(this).animate({"height":175},200).html( $(this).html() + " makeTall" );}
		function makeShort(){ $(this).animate({"height":50},200).html( $(this).html() + " makeShort" );}
