
var sizeViewer = function(image) {
	var $image= $(image);
	
	if($image.data('previous')==image.src) { return true; } //opening the same image. no need to do any of this.
	$image.data('previous', image.src);
	
	var $viewer = $image.parent();
	var $window = $(window);
	$viewer.width($image.width()); // this helps ie not splode.
	var ih = $viewer.outerHeight();
	var iw = $viewer.outerWidth();
	var wh = window.innerHeight ? window.innerHeight : $(window).height(); // Opera 9.5 + jQuery bug
	var ww = $window.width();

	if(wh <= ih && ww <= iw) { $($image.parent()).css({'top': 0, 'margin-top' : 0, 'left': 0, 'margin-left' : 0}); return true; }
	else if(wh > ih && ww <= iw) { $($image.parent()).css({'top' : '50%', 'margin-top': -ih/2+'px', 'left': 0, 'margin-left' : 0}); return true; }
	else if(wh <= ih && ww > iw) { $($image.parent()).css({'top': 0, 'margin-top' : 0, 'left': '50%', 'margin-left': -iw/2+'px'}); return true; }
	else if(wh > ih && ww > iw) { $($image.parent()).css({'top': '50%', 'margin-top': -ih/2+'px', 'left': '50%', 'margin-left': -iw/2+'px'}); return true; }
	else { return false; }
}


$(document).ready(function() {
						   
	// button stylez
	$('button, a.more').addClass('btn').wrapInner(document.createElement('span'));

	if ($('.tabs').length > 0) {
		$('.tabs').tabs();
		// this binds a Google Analytics function to tabs so that they are tracked the same way that pageviews are.
		$('.tabs').bind('tabsselect', function(e, ui) { if(typeof(pageTracker) == "object") {
													  					var page = ui.tab.href.substr(ui.tab.href.lastIndexOf("/")+1);
													  					pageTracker._trackPageview(page.replace(/#/, "/"));
													  				}
												});
	}
	
	/* this binds 2 functions to the video selection links on the videos page.
	** the first one fires a function that sends the flash player the link to the correct video.
	** the second is a Google Analytics funciton to track videos being played on the site.
	*******/
	$('#videos #links a').click(
		function() {
			 // don't do anything if the video player hasn't initialized
			 if (AC_FL_RunContent != 0) {
				 var video = this.href;
				 var name = this.innerHTML;
				 setVideo(video);
				 pageTracker._trackEvent('Videos', 'Play', name);
			 }
			 return false;
	 	}
	 );
	

	// open blog link in new window
	$('#nav #blog a, a[rel="external"]').click(function() { window.open(this.href); return false; });

	$('.file_list a:not([href$="df"])').click(function() { window.open(this.href); return false; });

	// design center image viewer 
	if ($('.design-center #images')) {
		  var $gallery = $('#gallery_image');
		  var $viewer = $('<div id="image-zoom"><img /><div class="close"><span>Close</span></div></div>');
		  var imagecount = 0;
		  $viewer.appendTo('body').hide().jqm();
		  $viewer.find('img').load(function(){sizeViewer(this);});
		  $viewer.find('.close span').click(function(){ $viewer.jqmHide(); });
		  $gallery.addClass('js')
		  .find('.image-link')
		  .filter(':first').addClass('on').end()
		  .each(function(){
				$(this).click(function(){ $viewer.find('img').attr('src', this.href).end().jqmShow(); return false; });
				imagecount++;
		  }).end()
		  .append('<div id="browse_controls"><a class="back" href="#">&laquo; Back</a><span class="count">Image <span class="currentcount">1</span> of <span class="total">'+imagecount+'</span></span><a class="next" href="#">Next &raquo;</a></div>')
		  .find('.back').click(function(){
				var $current=$('#gallery_image .on');
				var currentcount = $gallery.find('.currentcount').text();
				if($current.prev().hasClass('image-link')) { 
					$current.removeClass('on').prev().addClass('on');
					$gallery.find('.currentcount').text(--currentcount);
				}
				else { 
					$current.removeClass('on');
					$('#gallery_image .image-link:last').addClass('on');
					$gallery.find('.currentcount').text(imagecount);
				}
			}).end()
		  .find('.next').click(function(){
				var $current=$('#gallery_image .on');
				var currentcount = $gallery.find('.currentcount').text();
				if($current.next().hasClass('image-link')) {
					$current.removeClass('on').next().addClass('on');
					$gallery.find('.currentcount').text(++currentcount);
				}
				else { 
					$current.removeClass('on');
					$('#gallery_image .image-link:first').addClass('on');
					$gallery.find('.currentcount').text('1');
				}
			});
		  
	}

	// design center accordians
	if ($('.design_center')) {
		$('.design_center ul:not(:first)').addClass('accordian');
		$('.design_center li:not(:first)').click(function(e) {
		   e.stopPropagation();
		   //if ($(this).parent('ul').hasClass('file_list')) return false;
		   var $this = $(this);
		   if ($this.hasClass('open')) {
				$this.removeClass('open').children('ul').slideUp('fast').find('.open').removeClass('open').children('ul').hide();
		   }
		   else { $this.addClass('open').children('ul').slideDown('fast'); }
		});
		$('.design_center li li span').hover(function() { $(this).addClass('over'); }, function() { $(this).removeClass('over'); });
	}

	// homepage show/hide div formatting (dp_wraparound is empty and pushes text away from skiprope girl)
	$('#showhide01, #showhide02, #showhide03, #showhide04, #news').addClass('showhide').prepend('<div class="dp_wraparound"></div>');
	$('#news').show();

	// sitewide newsletter signup "lighbox" style.
	if($('#signup_form').length > 0) {
		$('#signup_form,').jqm({modal : true, trigger : '#signup_bug .signup'}).jqmAddTrigger('#footer #newsletter');
		$('.jqmClose').hover(function(){$(this).addClass('over');},function(){$(this).removeClass('over');});
	}
}); // end jQuery functions

function showHide(pass) { 
	pass = (!pass) ? 'news' : pass;
	$('.visible').removeClass('visible').hide(0, reveal(pass));
	function reveal(id) {
		$('#content').removeClass('iefix');
		if(id && ($('#'+id).length > 0)) {

			var setDisplayBlock = function() { $('#'+id).fadeIn(600).addClass('visible'); $('#content').addClass('iefix'); }
			setTimeout(setDisplayBlock, 250);
		}
	}
}
