// jQuery preload image functionality
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

var navAnimateSpeed = 600;
var navOpen = false;
var cancelFlashIntroduction = false; // This will later be set to true if the flash into is meant to be cancelled

// This deals with images with spaces in their names
$(document).ready( function() {
	$("div#project div.image li").each( function() {
		var imageName = $(this).text();
		imageName = imageName.replace(/ /g, "%20"); 
		$(this).text(imageName);
	});	
	
	$("img").each( function() {
		var imageName = $(this).attr("src");
		imageName = imageName.replace(/ /g, "%20"); 
		$(this).attr("src",imageName);
	});	
	
	var isThereACarousel = $("div.carousel").length;
	if (isThereACarousel) { 
		var numCarouselEls = $("div.carousel ul:eq(0) li").length;
		if (numCarouselEls <= 3) {
			$("#folio").remove();
		}
	}

});

jQuery(document).ready(function () {
	
	var navCollapse = $('.nav .collapse');
	var hideWidth = navCollapse.width() - 6;
	navCollapse = navCollapse.find('.sub').css('marginRight', '-' + hideWidth + 'px');
	$('.nav a').click(function () {
		var href = $(this).attr('href');
		if (navOpen) {
			navCollapse.animate({ marginRight: '-' + hideWidth + 'px' }, navAnimateSpeed / 2, 'linear', function () {
				location.href = href;
			});
			return false;
		}
	});
	$('.nav li').hover(
		function () {
			$(this).addClass('select');
		},
		function () {
			$(this).removeClass('select');
		}
	);
	$('#nav-projects').toggle(
		function () {
			navCollapse.animate({ marginRight: 0 }, navAnimateSpeed);
			navOpen = ! navOpen;
			$(this).removeClass('select');
		},
		function () {
			navCollapse.animate({ marginRight: '-' + hideWidth + 'px' }, navAnimateSpeed);
			navOpen = ! navOpen;
			$(this).removeClass('select');
		}
	);
	$("div.nav").css("position", "absolute");
});

function noCacheSrc(source) {
	
	var date = new Date();
	return source + '?' + date.getTime();
}


function imageLinkOver() {
	
	$(this).addClass('hover');
}

function imageLinkOut() {
	
	$(this).removeClass('hover');
}

function preloadImage(filename) {
	
	var preloadedImage = new Image();
	preloadedImage.src = noCacheSrc(filename);
	return preloadedImage;
}

function leadingZero(x) {
	
	return x >= 10 || x < 0 ? x : "0" + x;
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function( r ) {
	
 var i = 0, l = this.length;
 if( !r ) { r = this.length; }
 else if( r > 0 ) { r = r % l; }
 else { i = r; r = l + r % l; }
 return this[ Math.floor( r * Math.random() - i ) ];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function( b ) {
	
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

/* JAVASCRIPT COOKIE FUNCTIONS
 *
 * Can't remember where I got them but they are very useful
 ************************************************************************************************/
function Set_Cookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) { return null; }
    var end = document.cookie.indexOf( ";", len );
    if (end == -1) { end = document.cookie.length; }
    return unescape(document.cookie.substring(len, end));
}


var isIE = window.ActiveXObject ? true : false; // ActiveX is only used in Internet Explorer
var isIE6=isIE;
	    
var isIE7=false;
if (window.external && (typeof window.XMLHttpRequest == "object")) {
	isIE7=true;
	isIE6=false;
}

var str2xml = function(xmlstr) {
	if (isIE7) {            
		var xmlobj = new ActiveXObject("MSXML2.DOMDocument");
		xmlobj.loadXML(xmlstr); 
	 	return xmlobj;
	}
	else if(isIE6) {
		var xmlobj = new ActiveXObject("Microsoft.XMLDOM");
		xmlobj.loadXML(xmlstr);
		return xmlobj;
	} 
	else { // other browsers            
		return xmlstr;
	}      
}



/* BUG SOLVE
*
* This code solves many problems related to when IE6 users have page loads set to always
************************************************************************************************/
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}