$(document).ready(function(){
	$('ul#showcase-nav li a.print').click(function() {
		$('body').prepend('<div class="print-header"><img src="/images/print-logo.png" /></div>');
	
		$('#footer-copyright').prepend('<p class="address">Acorn Sign Graphics, PO Box 11664, 4109 West Clay Street, Richmond, Virginia 23230. (800) 770-4744</p>');
		window.print();		
		$("div.print-header, p.address").remove();
		return false;
	});
	
	$('div.thumbnails a.link').each(function() {
		var href = '#' + $(this).attr('id');
		$(this).attr('href', href);
		$(this).attr('target', '');
	});
	
	// establish some vars
	var thumb = $('div.thumb'); 
	var showcase_number = $('.showcase-number');
	var showcase_title = $('.showcase-title'); 
	var showcase_img = $('.showcase-img');
	var showcase_description = $('.showcase-description');
	var showcase_img_link = $('.showcase-img-container a');
	
	
	thumb.each(function(el){		
		$(this).prepend('<span class="greenbox"></span>');
	});	
	
	$('.showcase-number,.showcase-title,.showcase-img, .showcase-description').hide();
	
	// should we load from the anchor or the first image?
	
	var url = document.location.toString();
	var reference = null;
	var slug = null;
	if (url.match('#')) {
		slug = url.split('#')[1];
	}
	
	if (slug) {
		var accessor = "div.thumbnails a#" + slug;
		reference = $(accessor);
	} else {
		reference = $("div.thumbnails:first a");
	}
	
	showcase_number.html(reference.find('.number').html());
	showcase_title.html(reference.attr('title'));
	showcase_img.attr('src', reference.attr('name'));
	showcase_description.html(reference.attr('alt'));
	showcase_img_link.attr('href',reference.attr('rel'));
	
	$('.showcase-number').attr('id', reference.attr('id'));
	

	var category = $('div#category-description');
	
	if (slug) {  // is this a permalink for a particular image?
        category.hide();
        setTimeout(function(){$('div.thumbnails').stop().scrollTo($("a[id$="+reference.attr('id')+"]"), 500 );}, 1300);
        reference.find('span.greenbox').animate({top:'0px'},{queue:true,duration:500});
    } else { // no particular image, so display category if it's long enough, else first image
		if (category.find('#body').html() && (category.find('#body').html().length > 0)){
			$("div.thumb:first").find('span.greenbox').animate({top:'68px'},{queue:true,duration:500});
		} else {
            category.hide();
			$("div.thumb:first").find('span.greenbox').animate({top:'0px'},{queue:true,duration:500});	
		}
	}
	
	setTimeout( function(){$('.showcase-number,.showcase-title,.showcase-img, .showcase-description').fadeIn("slow");}, 200);
	
	
	
	
	
	$('div.thumbnails a').click(function() {
		var theClass = $(this).attr('class');
	
		if (theClass == "active") {
			return false;
		}
	
		//- put project JPG url in "href"
		//- put project title in "title"
		//- put project description in "alt"
	
		$(this).find('span.greenbox').animate({top:'0px'},{queue:true,duration:500});
		$(this).siblings().find('span.greenbox').animate({top:'68px'},{queue:true,duration:500});
		$('.showcase-number,.showcase-title,.showcase-img, .showcase-description, #category-description').hide();
		$('.showcase-number,.showcase-title,.showcase-img, .showcase-description, #category-description').empty();
	
	
    var thumbImage = $(this).find('img');
    var showcaseImg = $('.showcase-img');
    var showcaseImgLink = $('.showcase-img-container a');
		$('.showcase-number').html($(this).find('.number').html());
		$('.showcase-title').html($(this).attr('title'));
		showcaseImg.attr('src', $(this).attr('name'));
		showcaseImg.attr('title', thumbImage.attr('title'));
		showcaseImg.attr('alt', thumbImage.attr('alt'));
    showcaseImgLink.attr('href', $(this).attr('rel'));
		$('.showcase-description').html($(this).attr('alt'));
		setTimeout( function(){$('.showcase-number,.showcase-title,.showcase-img, .showcase-description').fadeIn("slow");}, 600);
	
		$(this).addClass("active");
		$(this).siblings().removeClass("active");
		
		$('.showcase-number').attr('id', $(this).attr('id'));
		return true;
	});	
	
	$('a.dropdown').click(function() {
		var type = $(this).attr('id');
		$(this).next("ul").slideToggle(200);
		$(this).toggleClass("active");
		$("a.dropdown:not(#"+type+")").removeClass("active");
		$("ul.work-subnav:not(#"+type+"-menu)").slideUp(200);
		return false;
	});

	$("*:not(a.dropdown)").click(function() {
		$("a.dropdown").removeClass("active");
		$("ul.work-subnav").slideUp(200);
	});
	
	
	$.scrollTo.defaults.axis = 'xy'; 			
	$('div.thumbnails').scrollTo( 0 );//reset all scrollable panes to (0,0)
	$.scrollTo( 0 );//reset the screen to (0,0)
	//Target examples bindings
	var $paneTarget = $('div.thumbnails');
	inMotion = false;			
 	$('a.prev').click(function(){
    if (!inMotion ) {
      inMotion = true;
      $paneTarget.stop().scrollTo( '-=234', 500 );
    } 
    inMotion = false;	
    return false;
   });
  $('a.next').click(function(){
    if (!inMotion ) {
      inMotion = true;
      $paneTarget.stop().scrollTo( '+=234', 500 );
    } 
    inMotion = false;	
    return false;
  });
});

