/*  All of the code in this document is copyrighted by Elliott Huff. */

jQuery.noConflict();

jQuery(document).ready(function(){

	// Load Settings
	var hostURL = jQuery('#hostURL').html();
	var host = jQuery('#host').html();
	var skin = jQuery('#skin').html();
	var hue = '#999'
	switch (skin)
	{
		case 'wood' : hue = '#333';
		case 'rose' : hue = '#555';
		case 'ocean' : hue = '#555';
		case 'white' : hue = '#777';
	}
	skin = host + '/styles/' + skin + '/intro.png';

	// Browser detection
	browser = ''; jQuery.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	if (jQuery.browser.msie) { browser = 'IE'; userAgent = jQuery.browser.version; }
	if (jQuery.browser.chrome) { browser = 'chrome'; }
	if (jQuery.browser.opera) { browser = 'opera'; }
	
	// Browser alignment fixes
	if (browser == 'IE') { jQuery('.box, .sub-menu').css('border', '1px solid black'); }
	window.setTimeout(function(){ jQuery('#bottom-style').css('top', jQuery('#copyright').offset().top - 135); }, 50);
	
	// Cufon
	Cufon.replace('.social-button, #feedback-submit, #copyright, #footer-links, .menu-link span, h1, h2, h3, h4, h5, h6, #footer-left, #footer-right, .button, .widgettitle, .tab-trigger, .drop-cap', { fontFamily: 'Aller Light', hover: true });
	Cufon.replace('.social-button span, .button span, .widget h5 span', { fontFamily: 'Aller Light', hover: true, textShadow: '1px 1px ' + hue });
	
	// prettyPhoto
	jQuery("a[rel^='prettyPhoto']").prettyPhoto();
	
	// Hover images
	jQuery('.hover').each(buildHover);
	function buildHover() { jQuery(this).append('<div class = "hover-background"></div><div class = "hover-img"></div>'); }
	jQuery('.hover').hover(function(){ 
	jQuery('.hover-background, .hover-img', this).animate({ 'opacity' : '0.5' }, 200);
	jQuery('.hover-img', this).animate({ 'opacity' : '1' }, 200);
	}, function(){ 
	jQuery('.hover-background, .hover-img', this).animate({ 'opacity' : '0' }, 200);
	});
	
	// Feedback FORM
	jQuery('#feedback-form').live('submit', function(){ 
		jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/ajax.gif');
		jQuery('#feedback-ajax').css('display', 'block'); 
		window.setTimeout(function(){  
		
		jQuery.get(hostURL + "/lib/scripts/email.php", { name : jQuery('#feedback-name').val(), email : jQuery('#feedback-email').val(), message : jQuery('#feedback-message').val() }, function(data){ 
		if (data == 'success') { jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/success.png').css('left', '5px'); jQuery('#feedback-result').html('Thanks for the message!'); } 
		else if (data == 'name') { jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/alert.png'); jQuery('#feedback-result').html('You forgot your name.'); } 
		else if (data == 'email') { jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/alert.png'); jQuery('#feedback-result').html('Please enter a valid email.'); } 
		else if (data == 'message') { jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/alert.png'); jQuery('#feedback-result').html('You forgot your message.'); } 
		else { jQuery('#feedback-ajax .ajax').attr('src', hostURL + '/images/alert.png'); jQuery('#feedback-result').html('There was an error.'); } 
		
		});
		
		}, 1000);
		return false; 
	});
	
	// Toggles 
	jQuery('.toggle .toggle-trigger').addClass('toggle-inactive');
	jQuery('.toggle').each(setToggleWidth);
	function setToggleWidth() { if (jQuery(this).attr('data-width')) { jQuery('.toggle-content', this).css('width', jQuery(this).attr('data-width') + 'px'); } }
	jQuery('.toggle-trigger').live('click', function(){ 
	
		if (jQuery(this).hasClass('toggle-inactive')) { jQuery(this).removeClass('toggle-inactive').addClass('toggle-active'); jQuery(this).next().slideDown('fast'); }
		else { jQuery(this).removeClass('toggle-active').addClass('toggle-inactive'); jQuery(this).next().slideUp('fast'); }
	
	});
	
	// Preset our background for the CGS
	jQuery('.designsaga-gallery').each(preSetupGalleries);
	function preSetupGalleries()
	{
		g = jQuery(this);
		// Setup gallery dimensions
		var width = g.attr('data-width'); 
		var height = g.attr('data-height');
		g.css({ 'width' : width + 'px', 'height' : height + 'px' });
		g.addClass('cgs-background');
	}
	
	// Get CGS image clicks working
	jQuery('.gallery-image-click:not(.gallery-custom-thumb)').live('click', function(){ if (jQuery(this).attr('data-link')) { window.location = jQuery(this).attr('data-link'); } });
	
	// Tabs
	var tabDefault = '';
	jQuery('.tab').each(positionTabs); function positionTabs() { jQuery(this).css('width', jQuery(this).attr('data-width') + 'px');  }
	jQuery('.tab').children().each(buildTabs);
	function buildTabs()
	{
		if (jQuery(this).attr('data-trigger')) { jQuery(this).addClass('tab-trigger'); if (jQuery(this).attr('data-default')) { tabDefault = jQuery(this).attr('data-trigger'); jQuery(this).addClass('active-tab-trigger'); } 
		else { jQuery(this).addClass('inactive-tab-trigger'); } }
		if (jQuery(this).attr('data-tab')) { jQuery(this).addClass('tab-content'); if (jQuery(this).attr('data-tab') == tabDefault) { jQuery(this).addClass('active-tab'); } else {  jQuery(this).addClass('inactive-tab'); } }
	}
	jQuery('.tab span').live('click', function(){  
	
		jQuery(this).parent().find('span').removeClass('active-tab-trigger').addClass('inactive-tab-trigger');
		jQuery(this).removeClass('inactive-tab-trigger').addClass('active-tab-trigger');
		jQuery(this).parent().find('div[data-tab]').removeClass('active-tab').addClass('inactive-tab');
		jQuery(this).parent().find('div[data-tab*="'+ jQuery(this).attr('data-trigger') +'"]').removeClass('inactive-tab').addClass('active-tab');
	
	});

});

jQuery(window).load(function(){
	
	// Load Settings
	var hostURL = jQuery('#hostURL').html();
	var host = jQuery('#host').html();
	var skin = jQuery('#skin').html();
	
	// MENUS
	var linkColor = '#888';
	var linkHover = '#555';
	
	switch (skin) 
	{
		case 'wood' : linkColor = '#777777'; linkHover = '#555'; break;
		case 'rose' : linkColor = '#777777'; linkHover = '#555'; break;
		case 'ocean' : linkColor = '#666666'; linkHover = '#333'; break;
		case 'white' : linkColor = '#999'; linkHover = '#333'; break;
	}
	
	var menuList = [];
	var menu = jQuery('#menu li');
	jQuery('.menu-link a:first').css('color', linkColor);
	if (browser == 'chrome') { window.setTimeout(delayedMenus, 200); } else { delayedMenus(); }
	function delayedMenus() { jQuery('.menu-link').each(createSubMenus); }
	function createSubMenus()
	{
		var menus = jQuery(this); var blah = '';
		var left = (menus.width() / 2);
			menus.addClass('drop-menu'); if (!jQuery('ul', this).length) { blah = 'style = "display: none !important; opacity: 0;  filter: alpha(opacity=0);"'; }
			menus.append('<div '+ blah +' class = "sub-menu-background" style = "left: '+ (left - 85) +'px;"><img src = "'+host+'/styles/'+skin+'/caret.png" class = "menu-caret" />' +
			'<div class = "sub-menu-slide" style = "left: 0px;"><div class = "sub-menu-real"></div></div>' +
			'</div>');
			jQuery('> ul:first', this).css('display', 'none').addClass('tree');
			jQuery('.sub-menu li').each(function(){ 
			
				if (jQuery('ul', this).length) 
				{
					jQuery(this).addClass('drop-menu-right');
					var copy = jQuery(this); var next = copy.clone();
					next.find('ul li ul').remove();
					var title = next.find('a:first').html(); next.find('a:first').remove();
					var content = '<div class = "menu-background"></div><div class = "menu-title"><h5>'+ title +'</h5></div><div class = "sub-menu-slide">'+ next.html() +'<div class = "menu-back">Back</div></div>';
					next.html(content);
					jQuery(this).attr('data-next', next.html());
				}
			
			});
		
	} 
	
	jQuery('#menu li').hoverIntent(function(){ 		
	
		// HOVER ON
		jQuery('a span:first', this).css('color', linkHover); Cufon.replace('.menu-link span');
		if (jQuery('.sub-menu', this).length) { 
		var content = "<div class = 'menu-background'></div><ul class = 'sub-menu' style = 'border-top: 1px solid #777; display: block;'>" + jQuery('.tree', this).html() + "</ul>";
		jQuery('.sub-menu-background .sub-menu-real', this).html(content);
		jQuery('.sub-menu-slide').fadeIn('fast');
		if (browser != 'IE') { jQuery('.sub-menu-background', this).css('display', 'block').animate({ 'left' : '100px', 'opacity' : '1'}, 200); jQuery('.sub-menu-background', this).css('top', '-44px');}
		else { jQuery('.sub-menu-background', this).css({ 'display' : 'block', 'left' : '40px' }); }
		menuList.push(content); }
		
		}, function(){ 		
		
		// HOVER OFF
		if (browser != 'IE') { jQuery('.sub-menu-background .sub-menu-real .sub-menu', this).animate({ 'top' : '0px', 'opacity' : '0' }, 200, function(){ jQuery(this).css('display', 'none'); });
		jQuery('.sub-menu-background', this).animate({ 'top' : '0px', 'opacity' : '0' }, 200, function(){ jQuery(this).css('display', 'none'); }); }
		else { jQuery('.sub-menu-background .sub-menu-real .sub-menu, .sub-menu-background', this).css('display', 'none'); }
		jQuery('a span:first', this).css('color', linkColor); 
		Cufon.replace('.menu-link span');
		menuList = [];
		
	}); 

	jQuery('.sub-menu li').live('click', function(){ 
		
		// SUB MENU CLICK
		if (jQuery(this).attr('data-next')) { jQuery(this).parents('.sub-menu-real').html(jQuery(this).attr('data-next')); 
		menuList.push(jQuery(this).attr('data-next')); jQuery('.sub-menu-slide').animate({ 'left' : '0px' }, 180); }
		jQuery('#menu li .sub-menu-background .sub-menu-real .sub-menu:first').css('display', 'block');
		menu.find('.sub-menu-real .sub-menu:first', this).css('display', 'block');
		Cufon.replace(':header');
		
	});
	
	jQuery('.menu-back').live('click', function(){
	
		// BACK CLICK
		if (browser == 'IE' || browser == 'opera') { menu.unbind('mouseout'); menu.unbind('mouseenter'); }
		menuList.pop(); jQuery(this).parents('.sub-menu-real').html(menuList[menuList.length - 1]);
		menu.find('.sub-menu-real').css('padding-right', '400px');
		jQuery('.sub-menu-slide').css('left', '-180px'); jQuery('.sub-menu-slide').animate({ 'left' : '0px' }, 180, function(){ menu.find('.sub-menu-real').css('padding-right', '10px'); }); 
		menu.find('.sub-menu-real .sub-menu:first', this).css('display', 'block');
		Cufon.replace(':header');
	
	});	
	
	// IE & OPERA fix
	if (browser == 'IE' || browser == 'opera') { 
		jQuery('#main').live('click', function(){ 
			if (browser != 'IE') { jQuery('.sub-menu-background .sub-menu-real .sub-menu').animate({ 'top' : '0px', 'opacity' : '0' }, 200, function(){ jQuery(this).css('display', 'none'); });
			jQuery('.sub-menu-background').animate({ 'top' : '0px', 'opacity' : '0' }, 200, function(){ jQuery(this).css('display', 'none'); }); }
			else { jQuery('.sub-menu-background').css('display', 'none'); }
			jQuery('.menu-link span').css('color', linkColor); 
			Cufon.replace('.menu-link span');
			menuList = [];
			menu.bind('mouseenter');
		}); 
		jQuery('#menu li').live('mouseenter', function(){
			jQuery('a span:first', this).css('color', linkHover); Cufon.replace('.menu-link span');
			if (jQuery('.sub-menu', this).length) { 
			var content = "<div class = 'menu-background'></div><ul class = 'sub-menu' style = 'border-top: 1px solid #777; display: block;'>" + jQuery('.tree', this).html() + "</ul>";
			jQuery('.sub-menu-background .sub-menu-real', this).html(content);
			jQuery('.sub-menu-slide').fadeIn('fast');
			if (browser != 'IE') { jQuery('.sub-menu-background', this).css('display', 'block').animate({ 'top' : '40px', 'opacity' : '1'}, 200); }
			else { jQuery('.sub-menu-background', this).css({ 'display' : 'block', 'top' : '40px' }); }
			menuList.push(content); }
		});
	}
	
	
	// Default Border Color
	var border = jQuery('#skin').html();
	
	switch(border)
	{
		case 'brown' : border = '#BB7E00'; break;
		case 'teal' : border = 'teal'; break;
		case 'blue' : border = '#5779C8'; break;
		case 'red' : border = '#D66C49'; break;
		case 'green' : border = '#48A44D'; break;
		case 'purple' : border = '#9966CC'; break;
		case 'pink' : border = '#D36989'; break;
		case 'olive' : border = '#94B458'; break;
		case 'lightblue' : border = '#57C0DF'; break;
		case 'wine' : border = '#993300'; break;
		case 'silver' : border = 'silver'; break;
		case '' : border = 'black'; break;
	}

	// Drop Shadows
	jQuery('.drop-shadow').each(insertDropShadow);
	function insertDropShadow()
	{
		jQuery(this).append('<img class = "drop-shadow-img" style = "width: '+ (jQuery('img', this).width() + 25) +'px;" src = "'+hostURL+'/images/drop-shadow.png" />');
	}
	
	// Transparent Borders
	jQuery('.transparent-border').each(addBorders);
	function addBorders()
	{
		var img = jQuery(this);
		if (!img.attr('data-color')) { var color = border; } else { color = img.attr('data-color'); }
		thickness = img.attr('data-thickness') || 10, // This is the default thickness
		opacity = img.attr('data-opacity') || 0.3; // This is the default opacity
		
		// Here is a webkit hack to get correct dimensions
		jQuery('img', this).removeAttr("width").removeAttr("height");
		var width = jQuery('img', this).width(); 
		var height = jQuery('img', this).height();
		img.css('width', width); 
		img.css('height', height);
		
		// And here we are inserting our borders and changing the dimensions as we go
		img.append('<div class = "transparent-image-border-top" style = "background-color: '+ color +'; width: ' + (width - (thickness * 2)) + 'px; left: ' + thickness + 'px; height: ' + thickness+ 'px;"></div>');
		img.append('<div class = "transparent-image-border-bottom" style = "background-color: '+ color +'; width: ' + (width - (thickness * 2)) + 'px; left: ' + thickness + 'px; height: ' + thickness+ 'px;"></div>');
		img.append('<div class = "transparent-image-border-left" style = "background-color: '+ color +'; height: ' + height + 'px; width: ' + thickness + 'px;"></div>');
		img.append('<div class = "transparent-image-border-right" style = "background-color: '+ color +'; height: ' + height + 'px; width: ' + thickness + 'px;"></div>');
		jQuery('.transparent-image-border-top', this).css('opacity', opacity); jQuery('.transparent-image-border-bottom', this).css('opacity', opacity);
		jQuery('.transparent-image-border-left', this).css('opacity', opacity); jQuery('.transparent-image-border-right', this).css('opacity', opacity);
	}
	
	// Design Saga Custom Gallery
	var imageCounter = 0;
	jQuery('.designsaga-gallery').each(setupGalleries);
	function setupGalleries()
	{
		g = jQuery(this);
		// Setup gallery dimensions
		var width = g.attr('data-width'); 
		var height = g.attr('data-height');
		g.css({ 'width' : width + 'px', 'height' : height + 'px' });
		g.removeClass('cgs-background');
		
		// If there is an interval then set the starting timer
		if (g.attr('data-interval') != undefined) { g.attr('data-timer', g.attr('data-interval')); }
		
		// Set the starting slide to 1
		g.attr('data-current', 1);
		
		// Set the zoom params
		g.attr('data-zoom', 'no');
		
		// Setup the classes
		jQuery('img:not(.drop-shadow-img, .gallery-thumb-holder img, .bl, .br, .tl, .tr)', this).addClass('gallery-image-inactive');
		jQuery('img:first', this).addClass('gallery-image-active').removeClass('gallery-image-inactive');
		imageCounter = 0;/* g.append('<div class = "gallery-thumb-holder"></div><div class = "gallery-caption-background"></div><div class = "gallery-caption"></div>');*/ jQuery('img:not(.drop-shadow-img)', this).each(formatImages);
		
		// Align the captions if using a transparent border
		if (g.attr('data-thickness') != '') { jQuery('.gallery-caption-background, .gallery-caption', this).css({ 'left' : g.attr('data-thickness') + 'px', 'bottom' : g.attr('data-thickness') + 'px' }); }
		if (g.attr('data-opacity') != '') { jQuery('.gallery-caption-background', this).css('opacity', g.attr('data-opacity')); }
		if (jQuery('img:first', this).attr('data-caption')) {
		jQuery('.gallery-caption:first, .gallery-caption-background:first', this).css('display', 'block');
		jQuery('.gallery-caption:first', this).html(jQuery('img:first', this).attr('data-caption'));
		Cufon.replace('.gallery-caption :header'); }
		
		// Setup the corners
		if (g.hasClass('gallery-corner')) { g.append("<img class = 'tl' src = '"+hostURL+"/images/tl.png' />" +
		"<img class = 'tr' src = '"+hostURL+"/images/tr.png' />" +
		"<img class = 'br' src = '"+hostURL+"/images/br.png' />" +
		"<img class = 'bl' src = '"+hostURL+"/images/bl.png' />"); }
		
		// Cycle through images
		jQuery('> img', this).each(function(){ var url = jQuery(this).attr('data-zoom'); var id = g.attr('data-id'); var title = jQuery(this).attr('title'); var alt = jQuery(this).attr('alt'); jQuery('#trash-bin').append('<a title = "'+title+'" alt = "'+alt+'" href = "'+url+'" rel = "prettyPhoto['+id+']"></a>'); });
		
		// Stuff in our zoom image
		g.append('<a class = "zoom" alt = "'+jQuery('img:first', this).attr('alt')+'" title = "'+jQuery('img:first', this).attr('title')+'" href = "'+ jQuery('img:first', this).attr('data-zoom') +'" rel = "prettyPhoto['+g.attr('data-id')+']"></a>');
		
		// Get the floater image set
		g.attr('data-floater', jQuery('img:first', this).attr('data-zoom'));
		g.attr('data-floater-alt', jQuery('img:first', this).attr('alt'));
		g.attr('data-floater-title', jQuery('img:first', this).attr('title'));
		jQuery('#trash-bin').find('a[href='+g.attr('data-floater')+']').remove();
		
		// Bind our mouseenter and mouseleave events
		if (jQuery(this).attr('data-interval') != undefined) { jQuery(this).attr('data-canslide', 'yes'); }
		jQuery(this).bind('mouseenter', function(){ jQuery(this).attr('data-canslide', 'no'); jQuery('.gallery-thumb-holder', this).css('display', 'block'); });
		jQuery(this).bind('mouseleave', function(){ jQuery(this).attr('data-canslide', 'yes'); jQuery('.gallery-thumb-holder', this).css('display', 'none'); });
		jQuery("a[rel^='prettyPhoto']").prettyPhoto();
	}
	window.setTimeout(customGallerySlides, 1000);
	function customGallerySlides()
	{ window.setTimeout(customGallerySlides, 1000); jQuery('.designsaga-gallery').each(customSlides); }
	function customSlides()
	{
		if (jQuery(this).attr('data-canslide') == 'yes' && jQuery('.pp_pic_holder').css('display') != 'block') {
			if (jQuery(this).attr('data-interval') != undefined) {
				cinterval = jQuery(this).attr('data-interval');
				ctimer = jQuery(this).attr('data-timer');
				ccurrentSlide = jQuery(this).attr('data-current');
				ctimer -= 1000;
				if (ctimer <= 0) {
					ccurrentSlide++;
					jQuery(this).attr('data-timer', jQuery(this).attr('data-interval'));
					if (ccurrentSlide > jQuery(this).attr('data-maximage')) { ccurrentSlide = 1; }
					jQuery(this).attr('data-current', ccurrentSlide);
					
							// Start transitions
							if (jQuery(this).attr('data-transition') == 'fade') {
							
								jQuery('img:not(.drop-shadow-img, .gallery-thumb-holder img, .bl, .br, .tl, .tr)', this).removeClass('gallery-image-active').addClass('gallery-image-inactive').css('display', 'none');
								jQuery('img[data-id='+ ccurrentSlide +']', this).removeClass('gallery-image-inactive').fadeIn();
								if (jQuery('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')) { jQuery('.gallery-caption', this).html(jQuery('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')); jQuery('.gallery-caption-background, .gallery-caption', this).fadeIn(); Cufon.replace('.gallery-caption :header');
								} else { jQuery('.gallery-caption-background, .gallery-caption', this).fadeOut(); }
							
							}
							else {
							
								jQuery('img:not(.drop-shadow-img, .gallery-thumb-holder img, .bl, .br, .tl, .tr)', this).removeClass('gallery-image-active').addClass('gallery-image-inactive');
								jQuery('img[data-id='+ ccurrentSlide +']', this).addClass('gallery-image-active').removeClass('gallery-image-inactive');
								if (jQuery('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')) { jQuery('.gallery-caption', this).html(jQuery('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')); jQuery('.gallery-caption-background, .gallery-caption', this).fadeIn(); Cufon.replace('.gallery-caption :header');
								} else { jQuery('.gallery-caption-background, .gallery-caption', this).fadeOut(); }
							
							}

					var previousURL = jQuery(this).attr('data-floater');
					var previousALT = jQuery(this).attr('data-floater-alt');
					var previousTITLE =jQuery(this).attr('data-floater-title');
					var currentURL = jQuery('img[data-id='+ccurrentSlide+']', this).attr('data-zoom');
					var currentALT = jQuery('img[data-id='+ccurrentSlide+']', this).attr('alt');
					var currentTITLE = jQuery('img[data-id='+ccurrentSlide+']', this).attr('title');
					var rel = jQuery(this).attr('data-id');
					
					jQuery('#trash-bin').find('a[href='+currentURL+']').remove();
					jQuery('#trash-bin').append('<a title = "'+previousTITLE+'" alt = "'+previousALT+'" href = "'+previousURL+'" rel = "prettyPhoto['+rel+']"></a>');
					jQuery(this).attr('data-floater', currentURL);
					jQuery(this).attr('data-floater-alt', currentALT);
					jQuery(this).attr('data-floater-title', currentTITLE);
					jQuery('> .zoom', this).attr('href', currentURL);
					jQuery('> .zoom', this).attr('title', currentTITLE);
					jQuery('> .zoom', this).attr('alt', currentALT);
					jQuery("a[rel^='prettyPhoto']").prettyPhoto(); 
				}
				else
				{
					jQuery(this).attr('data-timer', ctimer);
				}
			}
		} 
	}
	jQuery('.gallery-thumb-holder img').live('click', function(){  
		ccurrentSlide = jQuery(this).attr('data-thumb');
		jQuery(this).parents('.designsaga-gallery').attr('data-current', ccurrentSlide);
		var g = jQuery(this).parents('.designsaga-gallery');

			if (g.attr('data-transition') == 'fade') {
			
				g.find('img:not(.drop-shadow-img, .gallery-thumb-holder img, .bl, .br, .tl, .tr)').removeClass('gallery-image-active').addClass('gallery-image-inactive').css('display', 'none');
				g.find('img[data-id='+ ccurrentSlide +']').removeClass('gallery-image-inactive').fadeIn();
				if (g.find('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')) { g.find('.gallery-caption', this).html(g.find('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')); g.find('.gallery-caption-background, .gallery-caption', this).fadeIn(); Cufon.replace('.gallery-caption :header');
				} else { g.find('.gallery-caption-background, .gallery-caption', this).fadeOut(); }
			
			}
			else {
			
				g.find('img:not(.drop-shadow-img, .gallery-thumb-holder img)').removeClass('gallery-image-active').addClass('gallery-image-inactive');
				g.find('img[data-id='+ ccurrentSlide +']').addClass('gallery-image-active').removeClass('gallery-image-inactive');
				if (g.find('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')) { g.find('.gallery-caption', this).html(g.find('img[data-id='+ ccurrentSlide +']', this).attr('data-caption')); g.find('.gallery-caption-background, .gallery-caption', this).fadeIn(); Cufon.replace('.gallery-caption :header');
				} else { g.find('.gallery-caption-background, .gallery-caption', this).fadeOut(); }
			
			}
			
			var g = jQuery(this).parents('.designsaga-gallery');
			var previousURL = g.attr('data-floater');
			var previousALT = g.attr('data-floater-alt');
			var previousTITLE = g.attr('data-floater-title');
			var currentURL = g.find('img[data-id='+ccurrentSlide+']', this).attr('data-zoom');
			var currentTITLE = g.find('img[data-id='+ccurrentSlide+']', this).attr('title');
			var currentALT = g.find('img[data-id='+ccurrentSlide+']', this).attr('alt');
			var rel = g.attr('data-id');
			
			jQuery('#trash-bin').find('a[href='+currentURL+']').remove();
			jQuery('#trash-bin').append('<a title = "'+previousTITLE+'" alt = "'+previousALT+'" href = "'+previousURL+'" rel = "prettyPhoto['+rel+']"></a>');
			g.attr('data-floater', currentURL);
			g.attr('data-floater-alt', currentALT);
			g.attr('data-floater-title', currentTITLE);
			g.find('> .zoom', this).attr('href', currentURL);
			g.find('> .zoom', this).attr('title', currentTITLE);
			g.find('> .zoom', this).attr('alt', currentALT);
			jQuery("a[rel^='prettyPhoto']").prettyPhoto(); 
	});
	function formatImages()
	{
		imageCounter++;

		var thumb = jQuery(this).clone();
		thumb.addClass('gallery-custom-thumb').removeClass('gallery-image-inactive gallery-image-active');
		var width = jQuery(this).parent().attr('data-width');
		var height = jQuery(this).parent().attr('data-height');
		thumb.attr('data-thumb', imageCounter);
		thumb.css('width', (width * .15) + 'px');
		thumb.css('height', (height * .15) + 'px');
		jQuery(this).parent().find('.gallery-thumb-holder').append(thumb);
		
		jQuery(this).parent().attr('data-maximage', imageCounter);
		jQuery(this).attr('data-id', imageCounter);
	}
	
	// Tooltips
	jQuery('.tooltip-trigger').each(positionTooltips);
	function positionTooltips()
	{
		var left = jQuery(this).position().left; var top = jQuery(this).position().top; var bottom = '';
		var triggerwidth = jQuery(this).outerWidth(true); var tooltipwidth = jQuery(this).next().attr('data-width'); var height = jQuery(this).outerHeight(true); var toolheight = jQuery(this).next().outerHeight(true);
		var offset = (tooltipwidth - triggerwidth) / 2; 
		if (jQuery(this).next().attr('data-position')) 
		{ 
			if (jQuery(this).next().attr('data-position') == 'top') 
			{ 
				bottom = height - 20;
				jQuery(this).next().css({ 'left' : offset * -1, 'bottom' : bottom, 'width' : tooltipwidth + 'px' }).append('<div class = "large-spacer"></div>');
			} 
			else if (jQuery(this).next().attr('data-position') == 'bottom')
			{ 
				top = height; if (jQuery(this).hasClass('reflect')) { top -= 30; }
				jQuery(this).next().css({ 'left' : offset * -1, 'top' : top - 10, 'width' : tooltipwidth + 'px' }).prepend('<div class = "large-spacer"></div>');
			}
			else if (jQuery(this).next().attr('data-position') == 'right')
			{
				top = (height / 2) - ((toolheight - 20) / 2);
				jQuery(this).next().css({ 'left' : triggerwidth + 20, 'top' : top, 'width' : tooltipwidth + 'px' }).prepend('<div style = "position: absolute; left: -20px; width: 20px; height: 100%;"><img src = "'+hostURL+'/images/tooltip-left.png" style = "z-index: 201; position: absolute; left: 10px; top: '+ ((toolheight / 2) - 20) +'px;" /></div>');
			}
			else if (jQuery(this).next().attr('data-position') == 'left')
			{
				top = (height / 2) - ((toolheight - 20) / 2); left1 = triggerwidth * -1; left2 = tooltipwidth * -1;
				jQuery(this).next().css({ 'left' : left2 - 15, 'top' : top, 'width' : tooltipwidth + 'px' }).prepend('<div style = "position: absolute; right: -20px; width: 20px; height: 100%;"><img src = "'+hostURL+'/images/tooltip-right.png" style = "z-index: 199; position: absolute; left: -1px; top: '+ ((toolheight / 2) - 20) +'px;" /></div>');
			}
		}
		
	}
	jQuery('.tooltip').bind('mouseenter', function() { jQuery('.tooltip-content', this).fadeIn(); });
	jQuery('.tooltip').bind('mouseleave', function() { jQuery('.tooltip-content', this).fadeOut(); });
	
});

/**
* hoverIntent is similar to jQuery's built-in "hover" function except that
* instead of firing the onMouseOver event immediately, hoverIntent checks
* to see if the user's mouse has slowed down (beneath the sensitivity
* threshold) before firing the onMouseOver event.
* 
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* hoverIntent is currently available for use in all personal or commercial 
* projects under both MIT and GPL licenses. This means that you can choose 
* the license that best suits your project, and use it accordingly.
* 
* // basic usage (just like .hover) receives onMouseOver and onMouseOut functions
* jQuery("ul li").hoverIntent( showNav , hideNav );
* 
* // advanced usage receives configuration object only
* jQuery("ul li").hoverIntent({
*	sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
*	interval: 100,   // number = milliseconds of polling interval
*	over: showNav,  // function = onMouseOver callback (required)
*	timeout: 0,   // number = milliseconds delay before onMouseOut function call
*	out: hideNav    // function = onMouseOut callback (required)
* });
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function(jQuery) {
	jQuery.fn.hoverIntent = function(f,g) {
		// default configuration options
		var cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		// override configuration options with user supplied object
		cfg = jQuery.extend(cfg, g ? { over: f, out: g } : f );

		// instantiate variables
		// cX, cY = current X and Y position of mouse, updated by mousemove event
		// pX, pY = previous X and Y position of mouse, set by mouseover and polling interval
		var cX, cY, pX, pY;

		// A private function for getting mouse position
		var track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};

		// A private function for comparing current and previous mouse position
		var compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			// compare mouse positions to see if they've crossed the threshold
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				jQuery(ob).unbind("mousemove",track);
				// set hoverIntent state to true (so mouseOut can be called)
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				// set previous coordinates for next time
				pX = cX; pY = cY;
				// use self-calling timeout, guarantees intervals are spaced out properly (avoids JavaScript timer bugs)
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};

		// A private function for delaying the mouseOut function
		var delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};

		// A private function for handling mouse 'hovering'
		var handleHover = function(e) {
			// next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
			var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(e) { p = this; } }
			if ( p == this ) { return false; }

			// copy objects to be passed into t (required for event object to be passed in IE)
			var ev = jQuery.extend({},e);
			var ob = this;

			// cancel hoverIntent timer if it exists
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }

			// else e.type == "onmouseover"
			if (e.type == "mouseover") {
				// set "previous" X and Y position based on initial entry point
				pX = ev.pageX; pY = ev.pageY;
				// update "current" X and Y position based on mousemove
				jQuery(ob).bind("mousemove",track);
				// start polling interval (self-calling timeout) to compare mouse coordinates over time
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}

			// else e.type == "onmouseout"
			} else {
				// unbind expensive mousemove event
				jQuery(ob).unbind("mousemove",track);
				// if hoverIntent state is true, then call the mouseOut function after the specified delay
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};

		// Browser alignment fixes
		if (browser == 'IE') { jQuery('.box, .sub-menu').css('border', '1px solid black'); }
		window.setTimeout(function(){ jQuery('#bottom-style').css('top', jQuery('#copyright').offset().top - 135); }, 50);
		
			// Load Tweets
	if (jQuery('#twitter_update_list').length) { jQuery('#tweetAJAX').html('<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script><script type="text/javascript" src="http://twitter.com/statuses/user_timeline/designsaga.json?callback=twitterCallback2&amp;count=5"></script>'); }
		
		// bind the function to the two event listeners
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);
