$(init);

var pageIndex=1;
var totalPages=0;

function init(){
	$("body.index").click(indexClick);

	$("#design-link").hover(function(){$('ul.submenu.company').hide();$("ul.submenu.design").show();});
	$("#design-link").mouseleave(hideSubmenu);
	$("ul.submenu").mouseleave(hideSubmenu);
	$("#company-link").hover(function(){$('ul.submenu.design').hide();$("ul.submenu.company").show();});
	$("#company-link").mouseleave(hideSubmenu);

	if($('div#images').length>0){
		var slideshow=true;
		var transTime=2000;
		var slidechange=function(){};
		if($('body').hasClass('design-index') || $('body').hasClass('company-probono')){
			slideshow=false;
			transTime=1000;
			slidechange=updateDots;
			$('div#nav a').click(function(){
				$("div#slideshow").fadeOut();
			});
		}
		 var gallery = $('div#images').galleriffic({
			delay:                     4000, // in milliseconds
			numThumbs:                 20, // The number of thumbnails to show page
			preloadAhead:              40, // Set to -1 to preload all images
			enableTopPager:            false,
			enableBottomPager:         false,
			maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
			imageContainerSel:         '#slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
			controlsContainerSel:      '', // The CSS selector for the element within which the slideshow controls should be rendered
			captionContainerSel:       '#caption', // The CSS selector for the element within which the captions should be rendered
			loadingContainerSel:       '', // The CSS selector for the element within which should be shown when an image is loading
			renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
			renderNavControls:         false, // Specifies whether the slideshow's Next and Previous links should be rendered
			playLinkText:              'Play',
			pauseLinkText:             'Pause',
			prevLinkText:              'Previous',
			nextLinkText:              'Next',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
			enableKeyboardNavigation:  false, // Specifies whether keyboard navigation is enabled
			autoStart:                 slideshow, // Specifies whether the slideshow should be playing or paused when the page first loads
			syncTransitions:           true, // Specifies whether the out and in transitions occur simultaneously or distinctly
			defaultTransitionDuration: transTime, // If using the default transitions, specifies the duration of the transitions
			onSlideChange:             slidechange, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
			onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
			onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
			onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
			onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
			onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
			onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
		});

		gallery.unbind('click');

		$('body.design-index #click_left').click(function(){
			gallery.previous();
			updateCaption();
		});

		$('body.design-index #click_right').click(function(){
			gallery.next();
			updateCaption();
		});

		$('body.design-index #gallery_btns a').click(function(e){
			//alert($(this).attr('href'));
			gallery.clickHandler(e,this);
		});

		
		setTimeout("updateCaption()",100);
		
	}

	var maxKids=10;
	var startPage=1;
	var hash=window.location.hash;
	var h;
	if(hash.length>1)startPage=hash.replace('#','');
	pageIndex=startPage;
	$('div#nav ul').each(function(index){
		var kids = ($(this).children('li'));
		if(kids.length > maxKids){
			$(this).addClass('page1').addClass('paginated');
			var i=1;
			while(kids.length > maxKids){
				i++;
				var ul = $("<ul style='display:none;' class='paginated page"+i+"'>");
				var slice = kids.slice(maxKids,maxKids*2);
				slice.each(function(){
					$(this).children('a').attr('href',$(this).children('a').attr('href')+'#'+i);
				});
				ul.append(slice);
				
				
				//ul.css('height',h);
				$(this).parent().append(ul);
				kids = $(this).children('li');
				totalPages=i;
			}
			h = $('ul.paginated.page1').height();
			$('ul.paginated').css('height',h+"px");

			$(this).parent().children('a').append(' <span id="currentPage">'+startPage+'/'+totalPages+'</span>');

			var div = $("<div id='pagination'><a class='prev'>PREV</a> | <a class='next'>NEXT</a></div>");
			div.data('parent',$(this));
			$(this).parent().append(div);
			$('div#pagination a.next').click(function(){
				pageIndex++;
				if(pageIndex > totalPages)pageIndex=1;
				changePage();

				/*var nextPage=pageIndex;
				nextPage++;
				if(nextPage > totalPages)nextPage=1;
				$('div#pagination a.next').attr('href','#'+pageIndex);*/
			});
			$('div#pagination a.prev').click(function(){
				var prevPage=pageIndex;
				pageIndex--;
				if(pageIndex < 1)pageIndex=totalPages;
				changePage();

				//$('div#pagination a.prev').attr('href','#'+prevPage);
			});
			
			//alert($('ul.paginated').html());
			$('ul.paginated').hide();
			$('ul.paginated.page'+startPage).show();

		}
	});


	//weird hack code for grid of identity logos
	if($('div#cat-splash div.section').length > 10){
		$('div#cat-splash div.section a').each(function(){
			$(this).attr('href','#');
			$(this).click(function(){
				var linkTitle=$(this).attr('title');
				linkTitle=linkTitle.replace(/\s/gi,'');
				$("div#nav li.galleryItem a").each(function(){
					var t = $(this).text();
					t = t.replace(/\s/gi,'');
					t = t.toLowerCase();
					//alert("x"+linkTitle+"x"+t+"x");
					if(t.indexOf(linkTitle)>-1){
						window.location=$(this).attr('href');
					}
				});
			});
		});
	}

	$("div#caption-more").click(expandCaption);

	

}


function expandCaption(){
	$("div#caption").data('h','28px')//$("div#caption").height());
	$("div#details").data('h','58px');//$("div#details").height());
	$("div#caption").css('height','auto');
	$("div#details").css('height','auto');
	$("div#caption-more a").text('Hide')
	$("div#caption-more").addClass('inside');
	$("div#caption-more").unbind('click');
	$("div#caption-more").click(shrinkCaption);
}

function shrinkCaption(){
	$("div#caption").css('height',$("div#caption").data('h'));
	$("div#details").css('height',$("div#details").data('h'));
	$("div#caption-more a").text('More');
	$("div#caption-more").removeClass('inside');
	$("div#caption-more").unbind('click');
	$("div#caption-more").click(expandCaption);
}



function changePage(){
	$('ul.paginated').hide();
	$('ul.page'+pageIndex).show();
	$('span#currentPage').text(pageIndex+"/"+totalPages);
}

function updateDots(pindex,nindex){
	$('body.design-index #gallery_btns a[name="'+pindex+'"]').removeClass('active');
	$('body.design-index #gallery_btns a[name="'+nindex+'"]').addClass('active');
	shrinkCaption();
}

function updateCaption(){
	//alert($('div#caption').html());
	if($("div#caption span.image-caption.current").height() * 2 > 26){
		$("div#caption-more").show();
	}
	else{
		$("div#caption-more").hide();
	}
	
	$("body.company-probono div#caption-more").show();
	
	if($("ul.thumbs li.selected div.title").text().length>0){
		if($("div#details h4").data('orig-title')==undefined){
			$("div#details h4").data('orig-title',$("div#details h4").text());
		}
		$("div#details h4").html($("ul.thumbs li.selected div.title").text());
	}
	else $("div#details h4").html($("div#caption h4").data('orig-title'));
}

function hideSubmenu(e,o){
	if($(e.relatedTarget).is('#design-link') || $(e.relatedTarget).is('ul.submenu'))return;
	$('ul.submenu').hide();
}

function indexClick(){
	$("body.index").unbind('click');
	$("body").removeClass('index');
	$("body").addClass('main');
}

