var current_space = 0;
var featured_spaces = [];
var categoryList = [];
var hotspots = { '': [] };
var is_hotspot_initialized = false;
var cur_hover = "";
var tmpReturn = false;
var scrolling = false;
var reducedResolution = false;
var contactReason;
var carouselScrolling;

var	animTimeout1;
var animTimeout2;

var curURL;

var navigating = false;

var subSectionOpacity;
var subSectionButton;

var pulseInterval;

var introFinished = false;

$(function() {

	// Data Load
	load_hotspots();
	load_featured_products();
	loadCategories();
	
	// Check if need to use smaller sized featured images, and set listener for resize events
	if( $(window).width() < 1460 || $(window).height() < 940 ) reducedResolution = true;
	checkResolution();

	if ($.address.value() == "/" || $.address.value() == undefined) {
		// create intro box
		var introBox = $("<div id=\"dekla-intro\">Inspired, Customized design by dekla</div>")
		$(introBox).hide();
		$("#main-content").prepend(introBox);

		// Initialize (hide) interface
		$("#main-footer-logo").hide();
		$("#main-footer-nav").hide();
		$("#navbar").css("opacity", "0");
		$("#navbar .subnav").hide();
		//$("#slides li").css("opacity", "0");
		// $(".hotspot").css("opacity", "0");
		$("#featured").hide();
		$("#introImage1").hide();
		$("#introImage2").hide();
		$("#introImage3").hide();
		$('#prev').css("opacity", "0");
		$('#next').css("opacity", "0");
		$("#hotspots").css("opacity", "0");
		$("#hotspots").hide();
		$("#slides").css("opacity", "0");
		$("#slides li img").css("opacity", "0");	
	}
	
	// Bind event listeners
	initEventListeners();
	
	$.address.change(hashUpdated);
	featuredHashUpdated();
	// Create the featured products carousel
	carousel('#slides', '#prev', '#next', clear_hotspots, show_hotspots, true);
	// $("#carousel").carousel(); 
	
	
	if ($.address.value() == "/" ) {

		$("#dekla-intro").fadeIn(1000).delay(2000).fadeOut(1000);
		// Image Intro
		
		$('#introImage1').delay(4000).fadeIn(1000);
		$('#introImage2').delay(6000).fadeIn(1000);
		$('#introImage3').delay(8000).fadeIn(1000);
	
		if($.browser.msie){
            animTimeout1 = setTimeout('$("#slides").css("opacity", ""); $("#slides li img").css("opacity", ""); $("#featured-products-slider li").css("opacity", "");', 8000);	
        }
		else {
			animTimeout1 = setTimeout('$("#slides").css("opacity", "1"); $("#slides li img").css("opacity", "1"); $("#featured-products-slider li").css("opacity", "1");', 8000);	
		}
		animTimeout2 = setTimeout(intro, 10000);
	}
	
});

function initEventListeners() {
	
	// Bind event listeners
	$('#prev').bind('click.featured', prevKitchen);
	$('#next').bind('click.featured', nextKitchen);
	$('#hotspot_slides .close').click( function() { hotspot_close(); } );
	$("#main_logo").live('click', closeSection);
	$("#consult").live('click', function() { window.contactReason = "design_consult"; loadSection($(this).attr("href")); return false; });

	$("#navbar li#process a").live('click', function() { loadSection($(this).attr("href"), 1); return false; });
	$("#navbar li#products a").live('click', function() { loadSection($(this).attr("href"), 1); return false; });
	$("#navbar li#contract a").live('click', function() { loadSection($(this).attr("href"), 1); return false; });
	$("li#contact a").bind('click', function() { loadSection("contact.html", 1, true); return false; });
	$("li#news a").bind('click', function() { loadSection($(this).attr("href"), 1, true); return false; });
	$("li#legal_notice a").bind('click', function() { loadSection($(this).attr("href"), 1, false); return false; });
	$("li#privacy_policy a").bind('click', function() { loadSection($(this).attr("href"), 1, false); return false; });

	$("#hotspot-content #links a.gallery").live('click', function() { loadSection($(this).attr("href"), 1); return false; });
	
	$("#back").live('click', closeSection);
	$(".homeButton").live('click', closeSection);
	
	//$("#hotspots").click(featuredSpaceClick);
	$("#hotspots").click(hotspot_close);
	
	$(".introImage").click(introClick);
}

function intro() {
	$('#introImages img').fadeOut(1000);
	if (!$.browser.msie) {
		$('#dekla-intro').fadeOut(1000);

	} else {
		$('#dekla-intro').hide();

	}
	
	$('#main-footer-logo').delay(1000).fadeIn(1000);
	$('#main-footer-nav').delay(1000).fadeIn(1000);
	$('#navbar, #prev, #next').delay(1000).animate({opacity :1}, 1000, function() {
		if($.browser.msie){
            $(this).css({opacity:''});
        } 
	} );
	
	if(!$.browser.msie) {
		$(".hotspot").delay(1000).animate({opacity: 1}, 1000);
		$("#hotspots").delay(1000).animate({opacity: 1}, 1000);
		show_hotspots();
	} else {
		$(".hotspot").delay(1000).css("opacity", "");
		$("#hotspots").delay(1000).css("opacity", "");
		show_hotspots();
	}
	
	introFinished = true;
};

function introClick() {
	$("#introImage1").stop(true);
	$("#introImage2").stop(true);
	$("#introImage3").stop(true);
	$("#introImages").stop(true);
	clearTimeout(window.animTimeout1);

	if(!$.browser.msie) {
		$("#slides").css("opacity", "1");
		$("#slides li img").css("opacity", "1"); 
		$("#featured-products-slider li").css("opacity", "1");
	} else {
		$("#slides").css("opacity", "");
		$("#slides li img").css("opacity", ""); 
		$("#featured-products-slider li").css("opacity", "");
	}
	clearTimeout(window.animTimeout2);
	
	switch($(this).attr("id")) {
		case "introImage1":
			current_space = 0;
			break;
		case "introImage2":
			current_space = 8;
			break;
		case "introImage3":
			current_space = 9;
			break;
		default:
			break;
	}
	
	$.address.value("/featured/" +current_space);
	intro();
	return false;
}

function featuredSpaceClick() {
	$.address.value("/featured/" + current_space);
}

function hotspot_open( num ) {

	// Move list to correct hotspot
	var size = $('#hotspot_details_'+num).prevAll().size();
	
	if( !is_hotspot_initialized ) size++; // Account for carousel initialization
	
	if( size == 0 ) // In very first spot
	{
		// Move last element to front of list
		var element = $('#hotspot-slider ul li:last').detach();
		$('#hotspot-slider ul').prepend( element );		
	}
	else if( size > 1 ) //Skip since already in correct spot
	{
		// Move all preceding elements to end of list (minus 1)
		for( var i = 0; i < size-1; i++ )
		{
			var element = $('#hotspot-slider ul li:first').detach();
			$('#hotspot-slider ul').append( element );
		}
	}

  // $('#hotspot_slides_bg').show();
	if (!$.browser.msie) {
		$('#hotspot_slides').fadeIn(750, "easeOutQuart", function() {
			if (!is_hotspot_initialized) {
			    carousel('#hotspot-slider', '#hotspot-prev', '#hotspot-next', null, null, true);
			    is_hotspot_initialized = true;
		  	}
		});
	} else {
		$('#hotspot_slides').show();
		if (!is_hotspot_initialized) {
			carousel('#hotspot-slider', '#hotspot-prev', '#hotspot-next', null, null, true);
			is_hotspot_initialized = true;
		} 
  	}

};

function hotspot_close() {

	if(!$.browser.msie) {
		$('#hotspot_slides').fadeOut(750, "easeOutQuart", function() {
			$.address.value("/featured/" + current_space);
		});
	}
	else {
		$('#hotspot_slides').hide();
		$.address.value("/featured/" + current_space);
	}
	
  // $('#hotspot_slides_bg').hide();
};

var carousel = function(div_id, prev_id, next_id, before_slide, after_slide, init) {
  var ul = $(div_id + ' ul');
  var item_width = $(div_id + ' li').outerWidth();
  var left_value = item_width * (-1);

  if(init) {
	$(div_id + ' li:first').before($(div_id + ' li:last'));
	ul.css({'left' : left_value});
  }
  
  $(prev_id).bind( 'click.'+div_id, function() {
	if (!carouselScrolling) {
    slide_carousel(ul, item_width, left_value,
        $(div_id + ' li:first'),
        $(div_id + ' li:last'), 'right', before_slide, after_slide);
	}
  });

  $(next_id).bind( 'click.'+div_id, function() {
	if (!carouselScrolling) {
    slide_carousel(ul, item_width, left_value,
        $(div_id + ' li:first'),
        $(div_id + ' li:last'), 'left', before_slide, after_slide);
	}
  });
};

var slide_carousel = function(ul, item_width, left_value, first, last, direction, before_slide, after_slide) {
  if (!window.carouselScrolling)
  {
	  if (before_slide) {
		before_slide();
	  }
	  var left_indent = parseInt(ul.css('left')) + (((direction == 'left') ? -1 : 1) * item_width);
	  window.carouselScrolling = true;
	  ul.animate({'left' : left_indent}, 1000, function() {
	  	window.carouselScrolling = false;
	    if (direction == 'left') {
	      last.after(first); // Move the first item and put it as last item
	    } else {
	      first.before(last); // Move the last item and put it as first item
	    }
	    ul.css({'left' : left_value});
	    if (after_slide) {
/* 		  window.carouselScrolling = false; */
	      after_slide();
		  if ($.browser.msie) { $("#hotspots").attr("style", " "); }
	    }
	  });
	}
};


var clear_hotspots = function() {
  $('#hotspots').hide().empty();
};


function show_hotspots() {

  var hotspots = featured_spaces[current_space].hotspots;
  if( hotspots == null ) return;
  
  $('#hotspot-slider ul').empty();
  $('#hotspots').empty();
  
  var category = featured_spaces[current_space].category;
  var catNum = 0;
	for( var i = 0; i < categoryList.length; i++ ) {
		if( categoryList[i] == category ) {
			catNum = i;
			break;
		}
	}

  for (var i in hotspots) {
    var hotspot = hotspots[i];
    
    //Proportional positioning
    var imageData = hotspot.coords.split("|");
	hotspot.coord_x = Math.ceil(imageData[1] / 1250 * 100);
	hotspot.coord_y = Math.ceil(imageData[2] / 690 * 100);
    
    var properties = { top: hotspot.coord_y+'%', left: hotspot.coord_x+'%', display: "none" };
	$('#hotspots').append($('<span class="hotspot_wrap" id="hotspot_'+i+'"><span class="hotspot" ></span></span>').css(properties));
	$('#hotspot_'+i).unbind();
	if(!$.browser.msie) $('#hotspot_'+i).hover(startPulse, stopPulse);
	$('#hotspot_'+i).click(function(event) { 
		var hotspotId = $(this).attr('id').substr(8);
		$.address.value("/featured/" + current_space + "/" + hotspotId);
		event.stopPropagation(); 
	});
	if($.browser.msie) $('#hotspot_'+i).delay(10).show(0);
		else $('#hotspot_'+i).show(0);
	
	// Update hot spot details
	// TODO: move inline styles to style sheet (currently just copied from index.html)
	var html = '<li id="hotspot_details_'+i+'"><div class="hotspot-hotspot"><img src="images/'+hotspot.image+'" alt="'+hotspot.title+'" />';
	html += '<div style="margin: 10px 0 0 0; text-align: left; font-weight: bold;">'+hotspot.title+'</div>';
	html += '<div style="margin: 0 0 0 0; text-align: left; padding-right: 15px;">'+hotspot.content+'</div></div></li>';
	$('#hotspot-slider ul').append( html );
	
  }
  
	$("#links .gallery:first").attr( "href", "gallery/"+ catNum + "/1" );
	
	$("#links .designer_link").attr({
		href : featured_spaces[current_space].url,
		target: "_blank"
	});
	$("#links .designer_link").html(featured_spaces[current_space].designer + " Website");
	
	if (addthis) {addthis.button(".addthis_toolbox"); }
	
	if(!$.browser.msie) { 
		$('#hotspots').delay(1000).fadeIn(1000);  
	} else {
		setTimeout("checkResolution();", 10);		// Force IE to re-check resolution
	}
};

function startPulse() {
	pulse($(this).attr("id"));
}

function pulse(itemId) {
	var RATE = 600;
	if (!pulseInterval) pulseInterval = setInterval('pulse("' + itemId + '");', RATE * 2);
	$("#" + itemId).animate({opacity: 0.8}, RATE).animate({opacity: 1}, RATE);
}

function stopPulse() {
	clearInterval(pulseInterval);
	pulseInterval = null;
	$(this).animate({opacity: 1}, 400);
}

function prevKitchen(splash) {
	
	if(carouselScrolling) return;

	if (current_space == 0) {
		current_space = (featured_spaces.length - 1);
	} else {
		current_space--;
	}

	if(featured_spaces[current_space] == null) {
		prevKitchen(splash);
	}
/*
	if (window.splash === undefined || splash == true) {
		designer_splash();		
	}
*/	
	window.navigating = true;
	$.address.value("/featured/" + current_space);

}

function nextKitchen(splash) {
	
	if(carouselScrolling) return;
	
	if (current_space == (featured_spaces.length - 1)) {
	  current_space = 0;
	} else {
	  current_space++;
	}
	
	if(featured_spaces[current_space] == null) {
		nextKitchen(splash);
	}
/*	
	if (window.splash===undefined || splash == true) {
		designer_splash();		
	}
*/
	window.navigating = true;
	$.address.value("/featured/" + current_space);

}

function loadSection(url, opacity, hideButton) {

	// Convert jQuery objects into just a URL
	if (typeof(url) === "object") { 
		url = $(url.target).attr("href");
		return;
	};
	
	// If nothing else is open
	if ( $(".content_window").length == 0 ) {
		// Update the hash;
		url = url.replace(".html", "/");
		if ($.address.value() != "/" + url) {
			$.address.value("/" + url);
		}
		window.subSectionOpacity = opacity;
		window.subSectionButton = hideButton;
	} else {
		// There's already an open window and its not what we're currently viewing
		var targetVal = url;
		url = url.replace(".html", "");
		if ($.address.value() != "/" + url) {
			changeSection(url);
		}
	}
	
	return false;
}

function hashUpdated() {

	// Reset navigation visibility
	$("#navbar").css("visibility", "visible");

	// Reposition the previous & next arrows further down to compensate
	// for off-center positioning on homepage
	if ($.address.value() == "/" || $.address.value().split("/")[1] == "featured") {
		$('#prev').animate({ 'margin-top': 15 }, 600);
		$('#next').animate({ 'margin-top': 15 }, 600);
	} else if ($.address.value().split("/")[1] != "gallery") {
		$('#prev').animate({ 'margin-top': -40 }, 600);
		$('#next').animate({ 'margin-top': -40 }, 600);
	}
		
	// If user is trying to go home, close section.
	// Otherwise split up the url by deep links
	if ($.address.value() == "/") {
		if (introFinished) {
			closeSection();
			hotspot_close();
		}
		return false;
	} else {
		url = $.address.value().split("/");
	}
	

	// featured is actually homepage
	// intercept it and return
	if (url[1] == "featured") {
		if( $(".content_window").length != 0) {
			closeSection( true );
		}
		featuredHashUpdated();
		return;
	}
	
	// If user is trying to navigate to a sub-section,
	// leave that to the section's script
	if (url[1] != undefined) {
		if (url[1] != "featured" && $(".content_window").length != 0) {
			// If switching between sub-sections with back/forward button, remove old sections and continue
			if( $(".content_window").first().data( "section" ) != url[1] ) {
				$(".content_window").hide();
				$(".content_window").each(function(i) {
					$(this).remove();
				});
			}
			else {
				eval(url[1] + "HashUpdated()");
				return;
			}
		} 
	}

		
	// Create/show a back button/hide nav
	if (!window.subSectionButton) {
		var backButton = $("<li id=\"back\"><a href=\"#\">Home</a></li>");
		backButton.attr("href", window.location.hash);
		$("ul#navbar").append(backButton);
	} 
	
	$("ul#navbar li").hide();
	
	// Load the URL with an AJAX request
	$.get(url[1] + ".html", function(data) {

		// Unbind the main carousel event listeners
		$('#prev').unbind();
		$('#next').unbind();

		// Add markup to DOM
		var item = $(data);			
		$("#main-content").append(item.hide());
		item.data( "section", url[1] );
		
		checkResolution();
		
		// Set the background opacity to whatever user has passed
		item.css("backgroundColour", '#ffffff');
		var opacity = window.subSectionOpacity;
		if (opacity === undefined || opacity == 1) { 
			opacity = 1;
		}
		else if( opacity == 0 ) {
			item.css("background-colour", 'transparent');
		}
		else {
			item.css("background", 'rgba(255,255,255,' + parseFloat(opacity) + ')');
		}

		// Fade it in
		item.fadeIn(600);
		
		if (opacity == 1) {
			if ($.browser.msie) {
				$("#slides").css({ "opacity": "" });
				$("#hotspots").css({ "opacity": "" });
				$("#hotspot_slides").css({ "opacity": "" });
			} else {
				$("#slides").animate({ opacity: 0 });
				$("#hotspots").animate({ opacity: 0 });
				$("#hotspot_slides").animate({ opacity: 0 });
			}
			if ($.browser.msie) item.css("background", "#FFFFFF");
		}

		// Get the related script, if possible
		scriptname = "js/" + url[1] + ".js";
		$.getScript(scriptname);
		
	});		

	// Prevent default link behaviour
	return false;	
}

function closeSection(home, callback) {
	
	if( !home ) {
		$.address.value("/");
		current_space = 0;
		showFeaturedSpace(current_space + 1);
	}
	
	// Hide/remove back button, fade in navigation
	$("ul#navbar #back").animate({opacity: 0}, 600);
	$("ul#navbar li").fadeIn();
	$("ul#navbar #back").remove();
	
	// Fade out and delete content
	$(".content_window").fadeOut(600, function(e) { 
		$(".content_window").each(function(i) { $(this).remove(); });
			// initEventListeners();
	});
	
	// Reset slide display immediately so it will appear before closing transition
	$("#slides").css({ visibility: 'visible' });

	// Make homepage/feature content visible	
	if($.browser.msie) {
		$("#slides").css({opacity:''});
		$("#hotspots").css({opacity:''});
		$("#hotspot_slides").css({opacity:''});
	} else {
		$("#slides").animate({ opacity: 1 }, 600);
		$("#hotspots").animate({ opacity: 1 }, 600);
		$("#hotspot_slides").animate({ opacity: 1 }, 600);
	}
	
	$('#prev').unbind();
	$('#next').unbind();
	
	// Rebind click events for featured window
	$('#prev').bind('click', prevKitchen);
	$('#next').bind('click', nextKitchen);
	carousel('#slides', '#prev', '#next', clear_hotspots, show_hotspots, false);/**/
	show_hotspots();	
	
	$("#prev").fadeIn();
	$("#next").fadeIn();
	
	return callback;

}

function changeSection(url) {

	// Fade out and delete content
	if($.browser.msie){
		$(".content_window").hide();
		$(".content_window").each(function(i) {
			$(this).remove();
			loadSection(url);
		});
	} else {
		$(".content_window").fadeOut(600, function(e) { 
			$(".content_window").each(function(i) {
				$(this).remove();
				loadSection(url);
			});
		});
	}
	
	// Make sure slide content doesn't flash as we're transitioning
	$("#slides").css({ visibility: 'hidden' });
	$("#hotspots").hide();
	$("#hotspot_slides").hide();
}


/**
 * AJAX GET request to load the featured products and attach the corresponding hotspots.
 */	
function load_featured_products() {
  		
	$.ajax({ url: "lib.php",
		async: false,
		dataType: "json",
		data: { method: 'loadFeatured' },
		success: function(data) {
			var products = data.products;
			if( products == null ) return;
			for (var i in products) {
				var p = (products[i]).product;
			
				p.hotspots = hotspots[p.nid];
				featured_spaces.push(p);
				var border = $('table#border-01').clone();
				border.attr('id', 'fp-'+i).css('display', 'table');
				var li = $('<li id="' + p.display_order + '"/>').append(border);
				$('#slides ul').append(li);
				var img = $('<img/>').attr({'src': 'images/' + p.image, 'alt': p.title, 'width': '1250px', 'height': '690px'}).addClass('featured-space-image');
				$('table#'+border.attr('id')+' div').append(img);
			}
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
		alert( textStatus + " " + errorThrown );
	}
	});
}

function load_hotspots() {
  $.ajax({ url: "lib.php",
      async: false,
	  dataType: "json",
	  data: { method: 'loadHotspots' },
      success: function(data) {
		var hots = data.hotspots;
		if( hots == null ) return;
        for (var i in hots) {
          var hotspot = hots[i];
          if (hotspots[hotspot.fid] == null) {
            hotspots[hotspot.fid] = [];
          }
          hotspots[hotspot.fid].push(hotspot);
        }
      },
	  error: function(XMLHttpRequest, textStatus, errorThrown) {
		alert( textStatus + " " + errorThrown );
	  }
  });
  return hotspots;
}

function loadCategories() {
	$.ajax({ url: "lib.php",
      async: false,
	  dataType: "json",
	  data: { method: 'loadCategories' },
      success: function(data) {
		categoryList = data;
		categoryList.splice( 2, 0, "Appliance" );
      },
	  error: function(XMLHttpRequest, textStatus, errorThrown) {
		alert( textStatus + " " + errorThrown );
	  }
  });
}


function checkResolution() {
	if( !reducedResolution ) {
		$('#hotspots').width( 1250 );
		$('#hotspots').height( 690 );
/*  		$('#hotspots').css( "margin", "65px 90px 0px 90px" );	 */
 		$('#hotspots').css( "margin", "0 90px 0px 90px" );	
		$('#slides').width( 1250 );
		$('#slides').height( 690 );
/* 		$('#slides').css( "margin", "65px 90px 0px 90px" ); */
		$('#slides').css( "margin", "0 90px 0px 90px" );
		$('#slides li').width( 1250 );
		$('.featured-space-image').width( 1250 );
		$('.featured-space-image').height( 690 );
		$('#main-content').height( 760 );
		$('#main-footer').width( 1416 );
		$('.introImage').width( 1250 );
 		$('.introImage').height( 690 );
		$('.introImage').css( "left", "90px" );
		$('.content_window').width( 1250 );
		$('.content_window').height( 690 );
		$('.content_window').css( "left", "90px" );
		$('.contract_process').width( 1216 );
		$('.contract_process').css( "left", "0px" );
		$('.contract_process_slide').css( "margin-left", "215px" );
		$('.contract_process_slide').css( "margin-right", "215px" );
		$('#contractSldes').width( 12744 );
		$('#processSldes').width( 7080 );
		$('#processTransition').width( 1250 );
		
		$('#catalog').width( 1216 );
		$('#catalog').css( "left", "0px" );
		$('.catalog-page').css( "margin", "0px 208px" );
		$('#catalog-slider').width( $('#catalog').width() * $('#catalog-slider li.catalog-page').size() );
		var left = ( parseInt( $('#catalog-slider').css( "left" ) ) / 1010 ) * 1416;
		$('#catalog-slider').css( "left", left+"px" );
		$('#featured h2').css("top", '130px');
		$('#view_gallery').css("bottom", '100px');
		$("body").removeClass("small");
	} else {
		$('#hotspots').width( 886 );
		$('#hotspots').height( 489 );
/* 		$('#hotspots').css( "margin", "65px 69px 0px 69px" ); */
		$('#hotspots').css( "margin", "0 69px 0px 69px" );
		$('#slides').width( 886 );
		$('#slides').height( 489 );
/* 		$('#slides').css( "margin", "65px 69px 0px 69px" ); */
		$('#slides').css( "margin", "0 69px 0px 69px" );
		$('#slides li').width( 886 );
		$('.featured-space-image').width( 886 );
		$('.featured-space-image').height( 489 );
		$('#main-footer').width( 1010 );
		$('.introImage').width( 886 );
		$('.introImage').height( 489 );
		$('.introImage').css( "left", "69px" );
		$('.content_window').width( 886 );
		$('.content_window').height( 489 );
		$('.content_window').css( "left", "69px" );
		$('.contract_process').width( 900 );
		$('.contract_process').css( "left", "-5px" );
		$('.contract_process_slide').css( "margin-left", "55px" );
		$('.contract_process_slide').css( "margin-right", "55px" );
		$('#contractSldes').width( 9090 );
		$('#processSldes').width( 5050 );
		$('#processTransition').width( 886 );
		
		$('#catalog').width( 900 );
		$('#catalog').css( "left", "-5px" );
		$('.catalog-page').css( "margin", "0px 50px" );
		$('#catalog-slider').width( $('#catalog').width() * $('#catalog-slider li.catalog-page').size() );
		var left = ( parseInt( $('#catalog-slider').css( "left" ) ) / 1416 ) * 1010;
		$('#catalog-slider').css( "left", left+"px" );
		$('#featured h2').css("top", '30px');
		$('#view_gallery').css("bottom", '20px');
		
		$("body").addClass("small");
	}
	
	// Set these for IE
	if ($.browser.msie) {
		$('#hotspots').css("display", "block");
		$('.processButton span:nth-child(1)').css( "line-height", "32px" );
		$('.processButton span:nth-child(1)').css( "padding-top", "6px" );
		$('.content_window#gallery #dropdown span:nth-child(1)').addClass( "first" );
		$('.content_window#gallery #dropdown span:nth-child(1)').addClass( "first" );	
	}

}




function validateExists (i) {
	
	if (typeof i == "object" && i instanceof jQuery) {
		var item = i;
	} else if (this && typeof this == "object") {
		var item = $(this);
	}
	
	// If value is nothing, test fails
	if (item.val() == "") {
		item.addClass("error");
		return false;
	} else {
		item.removeClass("error");
		return true;
	}
}


function validateEmail(i) {
	
	if (typeof i == "object" && i instanceof jQuery) {
		var item = i;
	} else if (this && typeof this == "object") {
		var item = $(this);
	}
	
	// Regex the target item
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	var isValid = emailPattern.test(item.val());
	
	if (isValid) {
		item.removeClass("error");
		return true;
	} else {
		item.addClass("error");
		return false;
	}
	
}


function showFeaturedSpace( id ) {
	// current_space = parseInt(id)-1;

	// Update carousel position
	var position = $('#'+id).prevAll().size();
	if( position == 0 ) {
		// Move last item to front
		var element = $('#slides ul li:last').detach();
		$('#slides ul').prepend( element );
	}
	else if( position != 1 ) {
		// Move all preceding items to end
		for( var i = 0; i < position-1; i++ ) {
			var element = $('#slides ul li:first').detach();
			$('#slides ul').append( element );
		}
	}
	
	show_hotspots();
}


function featuredHashUpdated() {
	var url = $.address.value().split("/");
	
	if (url[1] != undefined && url[1] == "featured") {
	
		current_space = parseInt(url[2]);
		
		// Only auto-navigate if the user isn't navigating themselves
		// (directly accessed the site)
		if (window.navigating == false) {
			showFeaturedSpace(current_space + 1);
			// show_hotspots();	
		}
	
		if (url[3] != undefined) {
			hotspot_open(parseInt(url[3]));
		} else {
			hotspot_close();
		}
		
		window.navigating = false;
	}
	
}
