isNews = new Boolean();
currentlyClicked = new String();
currentlyClickedID = new String();

function flashLink(flurl, client) {
		$('#rednavscroll').unbind('mouseleave');
		$('#loadingwrapper').animate({"height":"0px", "opacity":"0"}, 800, '', loadSubContent);
			currentlyClicked = client;
			currentlyClickedID = "#"+client;
			
			if(client != undefined) {
			$(currentlyClickedID).attr('src','images/site/nav_'+client+'2.gif');
			}	
			$(currentlyClickedID).attr('src','images/site/nav_'+client+'1.gif');
		
		function loadSubContent() {
			$('.content').load(flurl, '',showSubContent());
		}
		function showSubContent() {
			$('.content')
				.ajaxComplete(function(event,request, settings){
					$.rednavexpand();
					var conheight = $('.content').height();
					$('#loadingwrapper').animate({"height":conheight, "opacity":"100"}, 800, '', $.gallery(currentlyClicked));
				});
			
		}
}

$(window).load( function(){

	var toLoad;
	var hideNavTimer;
	var beingShown;
	var shown;

	if(isNews == true){
		$.whatsnew(isNews);
	}
	else{
		null;
	}

		  $("#graynav").animate({ "marginLeft": "491px"}, 1200 );
		  $("#rednav").animate({ "marginLeft": "0px"}, 1200 );
		  $("#rednavscroll").animate({ "marginLeft": "50px"}, 1200 );
		  
		  $('#graynav img, #rednavlinks img, #rednavarchive img, #lscroll img').hover( function(){
			$(this).animate({"opacity":"0"}, 100);
			
			}, function(){
			
			$(this).animate({"opacity":"100"}, 400);
			
			});
		  
		  $('#work').click( function(){
				window.location.href = "index.html"
				return false;
			});
		  $('#work').mouseover( function(){
				if (hideNavTimer) clearTimeout(hideNavTimer);
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
					shown = true;
					$.rednavexpand();
					}
				});
		  $('#rednavscroll').mouseover( function(){
				if (hideNavTimer) clearTimeout(hideNavTimer);
				if (beingShown || shown) {
					return;
				} else {
					beingShown = true;
					shown = true;
					}
				});
		  $('#rednavscroll').mouseleave( function(){
				if(hideNavTimer) { clearTimeout(hideNavTimer); }
				hideNavTimer = setTimeout(function(){ 
					hideNavTimer = null;
					$.rednavcollapse();
					beingShown = false;
					shown = false;
					}, 2000);
				});
		  $('#rednavarrowr').mouseover( function(){
		  		redNavMargin = parseFloat($('#rednavlinkswrapper').width())-374;
		  		$('#rednavlinks').animate({"marginLeft":redNavMargin}, 400);
		  		});
		  $('#rednavarrowl').mouseover( function(){
		  		redNavMargin = 0;
		  		$('#rednavlinks').animate({"marginLeft":0}, 400);
		  		});
		
		$('.client').click(function(){
			if(currentlyClicked != undefined) {
				$(currentlyClickedID).attr('src','images/site/nav_'+currentlyClicked+'2.gif');
			}
				currentlyClicked = $(this).children("img").attr('id');
				currentlyClickedID = "#"+currentlyClicked;
				$(currentlyClickedID).attr('src','images/site/nav_'+currentlyClicked+'1.gif');
				toLoad = $(this).attr('href');
	     			$('#rednavscroll').unbind('mouseleave');
					$('#loadingwrapper').animate({"height":"0px"}, 800, '', loadSubContent);
		
		function loadSubContent() {
			$('.content').html("&nbsp;");
			$('.content').load(toLoad, '',showSubContent());	
		}
		function showSubContent() {
			$('.content')
				.ajaxComplete(function(event,request, settings){
					var conheight = $('.content').height();
					$('#loadingwrapper').animate({"height":conheight}, 800, '', $.gallery(currentlyClicked));
				});
			
		}
		return false;
	
		});
		
		$('#graynav ul li a').click(function(){
			if(currentlyClicked != undefined) {
				$(currentlyClickedID).attr('src','images/site/nav_'+currentlyClicked+'2.gif');
			}
				currentlyClicked = $(this).children("img").attr('id');
				currentlyClickedID = "#"+currentlyClicked;
				$(currentlyClickedID).attr('src','images/site/nav_'+currentlyClicked+'1.gif');
				toLoad = $(this).attr('href');
					$('#loadingwrapper').animate({"height":"0px"}, 800, '', loadContent);
					
				function loadContent() {
					$('.content').html("&nbsp;");
					$('.content').load(toLoad, '',showNewContent());	
				}
				function showNewContent() {
					$('.content').ajaxComplete(function(event,request, settings){
							var conheight = $('.content').height();
							$('#loadingwrapper').animate({"height":conheight}, 800, '', function(){
								
								if("whatsnew" == currentlyClicked){
									$.whatsnew();
								}
								if("about" == currentlyClicked){
									$.codaBubbles();
								}
								else{
									$('.content').unbind("ajaxComplete");
								}
							});
						});
			
		}
		return false;
	
		});
});
	
$(function () {
	$.rednavexpand = function rednavExpand() {
				$('#rednavscroll').animate({"width" : "370px"}, 1200);
				beingShown = false;
				shown = true;
		}
	$.rednavcollapse = function rednavCollapse() {
				$('#rednavscroll').animate({"width" : "0px"}, 1200);
		}		
	$.gallery = function gallery(clientname) {
		$('.content').unbind("ajaxComplete");
		var pagenum = Math.ceil(($("#gallerylist").children().length/2)/3);
		var rowlength = Math.ceil($("#gallerylist").children().length/2);
		var curRow = 0;
		var headernum = $("#headerscroll > img").length;
		var curpage = 1;
		var curheader = 1;
		var pageMargin = $(".thumbnailpane").css("marginLeft");
		var headMargin = $("#headerscroll").css("marginTop");
		var curMargin = parseInt(pageMargin, 10);
		var newMargin = 0;
		var headcurMargin = parseInt(headMargin, 10);
		var maxHeader = (headernum+1) * 223;
		var galItems = $("#gallerylist").children().length;
		var galImages = new Array();
		var u = 0;
		var fixDiff = 0;
		var prevLogo = "TB Alliance";
		
		//CREATE THUMBNAIL ARRAY 
		for(i=0;i<2;i++){
			galImages[i]=new Array();
		}
		//GET & SORT THUMBNAILS
							for(num=0;num<galItems;num++){
									if((num%3) == 0 && num > 2){ 
										if(curRow == 0){ curRow = 1; } else{ curRow = 0; }
										}
									galImages[curRow].push("<td width=\"78\" height=\"58\" valign=\"top\"><div style=\"position: relative;\" class=\"tmbnl\"><div class=\"tmbnl_overlay\" style=\"width: 0px; height: 24px; position: absolute; left: 0px; top: 32px; z-index: 2;\"></div>"+$("#gallerylist li").eq(num).html()+"</div></td>");
							}
		//CREATE TABLE
		$(".thumbnailpane").html('<table id="thumbtable" border="0" cellpadding="0" cellspacing="0"></table>');
		$(".thumbnailpane > table").append("<tr></tr>");
		
							for(u=0; u<2; u++)
							{
								for(i=0; i<(galImages[0].length); i++)
								{
									$("#thumbtable tr:last").append(galImages[u][i]);
								}
							 	$("#thumbtable tr:last").after("<tr></tr>");
							}
		//PAGE NUMBERS
		if (0 == pagenum || 1 == pagenum){
			pagenum = 1;
			$('.galscrollarrows').css('display','none');
		}
		$("#galscrollpage").html("1 of "+pagenum);
		
		//On gallery load, click the first thumbnail.
		itemClick($('#thumbtable tr td a').eq(0));
		
		$('#thumbtable tr td').hover(function(){
					var overlaywidth = 0;
					var galtitle = $('a', this).attr('title');
					
					if (galtitle == "logo") { overlaywidth = "40px"; }
					else if (galtitle == "tv" || galtitle == "print" || galtitle == "radio" ) { overlaywidth = "45px"; } 
					else if (galtitle == "online") { overlaywidth = "49px"; }
					else if (galtitle == "outdoor") { overlaywidth = "60px"; }
					else if (galtitle == "events") { overlaywidth = "58px"; }
					else if (galtitle == "email") { overlaywidth= "53px";}
					else if (galtitle == "ooh") { overlaywidth = "44px";}
					else if (galtitle == "website") { overlaywidth = "64px";}
					else if (galtitle == "internal" || galtitle == "package") { overlaywidth = "68px";}
					else { overlaywidth="0px"; }
					
					$('div div', this).css('background','url(images/site/overlay_'+galtitle+'.gif)').animate({"width":overlaywidth}, 200);
		}, function(){
					$('div div', this).animate({"width":"0px"}, 400);
		});
		$('.tmbnl_overlay').click(function(){itemClick($(this).parent().children('a')); });
		$('.tmbnl a').click(function(){itemClick(this); return false;});
						
						function itemClick(element){

							var galimg = $(element).attr('href');
							var galrel = $(element).attr('rel');
							var lightboximg = $(element).attr('href').split('med/').pop();
							var galdesc = $(element).children('img').attr('alt'); 				
							var extension = (/[.]/.exec(galimg)) ? /[^.]+$/.exec(galimg) : undefined;
							var extensionSp = extension[0].split("?");
							
							if(clientname == "archive"){
								archiveheader(galrel);
							}
								if (extension[0] == "jpg" || extension[0] == "png" || extension[0] == "gif") {
										if($(element).attr('title') == "logo" || $(element).attr('title') == "package"){ 
											$('.largeimage').html("<center><img src=\""+galimg+"\" /></center>");  
										}
										else{
											$('.largeimage').html("<center><a href=\"images/gallery/"+clientname+"/large/"+lightboximg+"\" rel=\"prettyPhoto\"><img src=\""+galimg+"\" /></a><br/><small>Click to view larger.</small></center>");  
											jQuery(".largeimage a").prettyPhoto({padding: 20, theme: 'light_square'});
										}
									}
								else if (extension == "flv") { 
									var galrev = $(element).attr('rev');
									$('.largeimage').html('<object type="application/x-shockwave-flash" data="flvplayer.swf" width="446" height="334"><param name="movie" value="flvplayer.swf?flv='+galimg+'&amp;startimage='+galrev+'&amp;width=446&amp;height=334&amp;margin=0&amp;bgcolor1=ffffff&amp;bgcolor2=ffffff&amp;showstop=1&amp;showvolume=1&amp;showloading=never&amp;playercolor=FFFFFF&amp;buttoncolor=666666&amp;buttonovercolor=e2383f&amp;slidercolor1=666666&amp;slidercolor2=666666&amp;sliderovercolor=e2383f&amp;volume=200&amp;iconplaybgalpha=25" /><param name="allowFullScreen" value="true" /><param name="FlashVars" value="flv='+galimg+'&amp;startimage='+galrev+'&amp;width=446&amp;height=334&amp;margin=0&amp;bgcolor1=ffffff&amp;bgcolor2=ffffff&amp;showstop=1&amp;showvolume=1&amp;showloading=never&amp;playercolor=FFFFFF&amp;buttoncolor=666666&amp;buttonovercolor=e2383f&amp;slidercolor1=666666&amp;slidercolor2=666666&amp;sliderovercolor=e2383f&amp;volume=200&amp;iconplaybgalpha=25" /></object>'); 
									}
								else if (extension == "mp3") {
									var galrev = $(element).attr('rev');
									$('.largeimage').html('<object type="application/x-shockwave-flash" width="446" height="349" data="xspf_player_slim.swf?autoload=true&song_url='+galimg+'&large_image=images/gallery/smith/med/smith-1-04.jpg"><param name="bgcolor" value="#FFFFFF"/><param name="movie" value="xspf_player_slim.swf?autoload=true&song_url='+galimg+'&large_image='+galrev+'" /></object>');
									}
								else if (extensionSp[0] == "html") { 
									var galrev = $(element).attr('rev');
									$('.largeimage').html("<center><a href=\"images/gallery/"+clientname+"/large/"+lightboximg+extensionSp[1]+"\" rel=\"prettyPhoto\"><img src=\""+galrev+"\" /></a><br/><small>Click to play.</small></center>");  
									jQuery(".largeimage a").prettyPhoto({padding: 20, theme: 'light_square'});
									}
								if (galdesc == undefined){ 
									galdesc = ""; 
									}
							$('#description').html(galdesc);
						return false;
						}
						$('#gallarrow, #galrarrow').hover(function(){
								$(this).animate({"opacity":"0"}, 400);
							}, function(){
								$(this).animate({"opacity":"100"}, 400);
							});
						$('#gallarrow').click(function(){
							if(curpage == 1){ fixDiff = 14; } else {fixdif = 0 };
							if(curpage>1){
								itemClick($('#thumbtable tr td a').eq(curpage*3-6));
								curMargin += (262 + fixDiff);
								newMargin = curMargin + "px";
								curpage=(curpage-1);
								if(clientname == "nyu"){
									$('.thumbnailpane').animate({"marginLeft":newMargin}, 400, '', nyuheader(curpage, "left"));
									}
								else{
									$('.thumbnailpane').animate({"marginLeft":newMargin}, 400, '', headerScroll("left"));
									}
								$("#galscrollpage").html(curpage+" of "+pagenum);
								}
							else{
									return;
								}
						});
						$('#galrarrow').click(function(){
							if(curpage == 1){ fixDiff = 14; } else {fixdif = 0 };
							if(curpage<pagenum){
								itemClick($('#thumbtable tr td a').eq((curpage)*3));
								curMargin -= (262 + fixDiff);
								newMargin = curMargin + "px";
								curpage=(curpage+1);
								
								if(clientname == "nyu"){
									$('.thumbnailpane').animate({"marginLeft":newMargin}, 400, '', nyuheader(curpage, "right"));
									}
								else{
									$('#thumbnailpane').animate({"marginLeft":newMargin}, 400, '', headerScroll("right"));
									}
								$("#galscrollpage").html(curpage+" of "+pagenum);
								}
							else{
								return;
								}
	
						});
						
			function headerScroll(dir){
				var hmultiple = Math.round(pagenum/headernum);
					if((((curpage-1)%hmultiple) == 0) && (hmultiple >= 1)) {
						if(dir == "right"){ 
							if(curheader < headernum){
								headcurMargin -= 158;
								$("#headerscroll").animate({"marginTop":headcurMargin}, 200);
								curheader++;
							}
						}
						if(dir == "left"){
							if(curheader > 1){
								headcurMargin += 158;
								$("#headerscroll").animate({"marginTop":headcurMargin}, 200);
								curheader=(curheader-1);
							}
						}
					}
				}
			function nyuheader(page, dir){
				if(dir == "right" && page == "3"){
					headcurMargin -= 158;
					$("#headerscroll").animate({"marginTop":headcurMargin}, 200);
					curheader++;
					}
				else if(dir == "left" && page == "2"){
					headcurMargin += 158;
					$("#headerscroll").animate({"marginTop":headcurMargin}, 200);
					curheader=(curheader-1);
					}
				}
			function archiveheader(logo){
				var curLogo;
			
			if(logo != prevLogo){
				switch(logo){
					case "TB Alliance":
						curLogo = "header_tballiance.gif";
						break;
					case "University Continuing Education Association":
						curLogo = "header_ucea.gif";
						break;
					case "Bloomsbury Auctions":
						curLogo = "header_bloomsbury.gif";
						break;
					case "Crain's":
						curLogo = "header_crains.gif";
						break;
					case "Wiley":
						curLogo = "header_wiley.gif";
						break;
					case "New York Hospital Queens":
						curLogo = "header_nyhq.gif";
						break;
					}
					prevLogo = logo;
					
						$("#header2").css('backgroundImage', 'url(images/site/pages/work/archive/'+curLogo+')');
						$("#headerscroll").animate({"marginTop":"-159px"}, 200, 
							function(){ 																  
								$("#header1").css('backgroundImage', 'url(images/site/pages/work/archive/'+curLogo+')');
								$("#headerscroll").css('marginTop', '0px');
							});
				}
				else{
					return;
				}
			}
			
		}
	$.whatsnew = function whatsnew(isNews) {
		
		var headlinelist = new Array();
		var headlineid = new Array();
		var headlinedate = new Array();
		var headlineorg = new Array();
		var scrollheight = $('.leftcontentnew').height() - 442;
		var stillHidden = 0;
		var i = 0;
		
		if(isNews == false){
		$('.content').unbind("ajaxComplete");
		$('.leftcontentnew').load('news.html #leftcontentnew', '', function(){
		
			$('.leftcontentnew').ajaxComplete(function(event,request, settings){
				$('.leftcontentnew').unbind("ajaxComplete");
				whatsnew_success();
			});
		
		});
		}
		else{
			whatsnew_success();
		}
		function whatsnew_success() {
				$('.article').each(function(e){
					headlinelist[i] = $('#header',this).html();
					headlinedate[i] = $('#date', this).html();
					headlineorg[i] = $('#organization', this).html();
					if (headlineorg[i] == undefined || headlineorg[i] == null){ headlineorg[i] = ''; }
						else { headlineorg[i] = "/" + headlineorg[i]; }
					headlineid[i++] = $(this).attr('id');
				});
				$('#headlinelist').html("<br/><ul></ul>");
				for(n=0;n<i;n++){
					$('#headlinelist ul:first').append("<li><a href=\"#"+headlineid[n]+"\">"+headlinelist[n]+headlineorg[n]+"</a><ul><li><i>"+headlinedate[n]+"</i></li></ul></li>");
				}
			//SCROLLBAR
				var vH = $('.leftcontentnew').height() -  442;
				var sH = parseInt($('#scrollhandlearea').height());
				var vT = $('#scrollhandlearea').offset().top;
				var vL = $('#scrollhandlearea').offset().left;
			
				$('#scrollhandle').mousedown(function(){
						$(this).css('background','#555555');
						$('#scrollhandlearea').mousemove(function(e){
							var ypos=e.pageY-vT; //REL_Y
							var y = (ypos/sH)*vH;
							if(ypos > (sH-8)) { ypos = (sH-8); };
							$('#scrollhandle').css({top: ypos+'px'});
							$('.leftnewscroll').scrollTop(y);
						});
					});
				$('body').mouseup(function(){
						$('#scrollhandlearea').unbind('mousemove');
						$('#scrollhandle').css('background','#666666');
					});
				$('#headlinelist a').click(function(e){
					var eLink = $(this).attr('href').split("#");
					window.location.href = "#"+eLink[1];
					
					var vOffset = $('.leftnewscroll').scrollTop();
					var vHeight = $('.leftcontentnew').height();
					var sHeight = $('#scrollhandlearea').height()+80;
					var ypos = (vOffset/vHeight)*sHeight;
		
						$('#scrollhandle').css({top: ypos});
		
						return false;
					});
				
				$('#newscrollup').hover(function(){
						stillHidden = 0  +  parseInt($('.leftcontentnew').css('marginTop'));
						$('.leftnewscroll').animate({'scrollTop': 0}, 15000);
						$('#scrollhandle').animate({"top": 0}, 15000);
					}, function(){
						$('.leftnewscroll').stop();
						$('#scrollhandle').stop();
					});
				
				$('#newscrolldown').hover(function(){
						var vOffset = $('.leftnewscroll').scrollTop();
						var vHeight = $('.leftcontentnew').height() - 442;
						var sHeight = $('#scrollhandlearea').height() - 8;
						var ypos = (vOffset/vHeight)*sHeight;
						
						$('.leftnewscroll').animate({'scrollTop': scrollheight}, 15000);
						$('#scrollhandle').animate({"top": sHeight}, 15000);
								
								$('#statbox').text("vOffset = "+vOffset+" | vHeight = "+vHeight+"sHeight = "+sHeight+" equation = "+((vOffset/vHeight)*sHeight));
								
					}, function(){
						$('.leftnewscroll').stop();
						$('#scrollhandle').stop();
					});
		}
	}
$.codaBubbles = function codaBubbles(){
	$('.content').unbind("ajaxComplete");
			
			var scrollHeight 	= -($('#bubblewrap').height() - 350);
			var newMarginTop 	= parseInt($('#bubblewrap').css("marginTop"));
			var profileNo		= $('.trigger').length;
			var topThumb		= 0;
	
	$('#down').click(function(){
	if(newMarginTop > scrollHeight){
		newMarginTop -= 70;
		if(topThumb < profileNo){ 
			
					topThumb +=1; 
					
				} else if(topThumb >= profileNo){ 
					
					topThumb = profileNo; 
					
				}
		$('#bubblewrap').animate({"marginTop": newMarginTop+"px"}, 300);
		}
		return false;											
	});
	$('#up').click(function(){
	if(newMarginTop < 0){
			newMarginTop += 70;
			if(topThumb > 0){ 
					
					topThumb -= 1; 
					
				} else if(topThumb <= 0){ 
					
					topThumb = 0; 
					
				}
			$('#bubblewrap').animate({"marginTop": newMarginTop+"px"}, 300);
		}
		return false;
	});
	
	$('.bubbleInfo').each(function() {
    // options

			var distance = 10;
			var time = 250;
			var hidetime = 20;
			var hideDelay = 50;
		
			var hideDelayTimer = null;
		
			var beingShown = false;
			var shown = false;
			
			var trigger = $('.trigger', this);
			var popup = $('.popup', this).css('opacity', 0);
			var var_top;
			var var_tail;
	
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
    	if (hideDelayTimer) clearTimeout(hideDelayTimer);
    	if (beingShown || shown) {
        	return;
      	} else {
        	beingShown = true;
			var triggerPos = trigger.position();
			
			switch($(".trigger").index(this)){
				case topThumb:
					var_top = triggerPos.top;
					$('.bubbletail').css('top', '18px');
					break;
				case (topThumb+1):
					var_top = var_top = triggerPos.top - 70;
					$('.bubbletail').css('top', '88px');
					break;
				case (topThumb+2):
					var_top = (triggerPos.top+45) - parseInt($('.popup').eq(topThumb+2).height()/2);
					$('.bubbletail').css('top', '39%');
					break;
				case (topThumb+3):
					var_tail = parseInt($('.popup').eq(topThumb+3).height()) - 116;
					$('.bubbletail').css('top', var_tail);
					var_top = (triggerPos.top+134) - parseInt($('.popup').eq(topThumb+3).height());
					break;
				case (topThumb+4):
					var_tail = parseInt($('.popup').eq(topThumb+4).height()) - 46;
					$('.bubbletail').css('top', var_tail);
					var_top = (triggerPos.top+64) - parseInt($('.popup').eq(topThumb+4).height());
					break;
			}
					
				popup.css({
					top: var_top,
					left: (triggerPos.left)-261,
					display: 'block'
				}).animate({
					left: '-=' + distance + 'px',
					opacity: 1
					}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
      	}
			}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);  
			
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					left: '-=' + distance + 'px',
					opacity: 0
					}, time, 'swing', function () {
					shown = false;
					popup.css('display', 'none');
					});
				}, hideDelay);
			});
 		});
  	}
});