var topmannequin;
if (!topmannequin)
{
    topmannequin = {};
}
else if (typeof homemaid != 'object')
{
    throw new Error("'topmannequin' already exists and is not an object");
}
if (!topmannequin.frontend)
{
    topmannequin.frontend = {};
}
else if (typeof topmannequin.frontend != 'object')
{
    throw new Error("'topmannequin.frontend' already exists and is not an object");
}

//----------------------------------------
//  All website frontend code
//----------------------------------------
topmannequin.frontend =
{
	/**
	 * Properties
	 */
	
	/**
	 * Initialize the frontend.
	 *
	 * @author	Gerrit Bertier
	 * @version	1.0
	 */
	init: function()
	{
		// Initialize any necessary hovers
		this.ui.init();
		
		// Photo feature
		if ($('#photo-feature').length > 0)
		{
			this.photofeature.init();
		}
		
		// Contender feature
		if ($('#contender-feature').length > 0)
		{
			this.contenderfeature.init();
		}
		
		// Twitter feed
		if ($('#twitter-feed').length > 0)
		{
			this.twitter.init();
		}
		
		// Picture album
		if ($('.pictures ul li').length > 0)
		{
			this.gallery.init();
		}
	},
	
	//----------------------------------------
	//  User interaction related code
	//----------------------------------------
	ui:
	{
		/**
		 * Initialize the UI elements.
		 *
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		init: function()
		{
			if ($('#register-cta').length > 0)
			{
				// Registration call to action button hover effect
				$('#register-cta a').hover(function()
				{
					$('#register-cta').addClass('hover');
				},
				function()
				{
					$('#register-cta').removeClass('hover');
				});
			}
		}
	},
	
	//----------------------------------------
	//  Contender feature related frontend code
	//----------------------------------------
	contenderfeature:
	{
		/**
		 * Properties
		 */
		intervalId: 0,
		currentPictureIndex: 0,
		
		/**
		 * Initialize the contender feature related code.
		 * 
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		init: function()
		{
			if ($('#contender-feature #slideshow').length > 0)
			{
				this.initSlideshow();
			}
		},
		
		/**
		 * Initialize the slideshow.
		 *
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		initSlideshow: function()
		{
			// Calculate total width
			var totalWidth = 0;
			if ($('#contender-feature #slideshow li').length > 1)
			{
				$('#contender-feature #slideshow li').each(function()
				{
					totalWidth += $(this).find('img').width();
				});
				
				// Set total width
				$('#contender-feature #slideshow ul').width(totalWidth);
				
				// Initialize interval
				this.intervalId = setInterval("topmannequin.frontend.contenderfeature.nextSlide()", 5000);
			}
		},
		
		/**
		 * Show the next slide.
		 *
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		nextSlide: function()
		{
			// Calculate index of picture to show
			if (this.currentPictureIndex < $('#contender-feature #slideshow li').length - 1)
			{
				this.currentPictureIndex++;
			}
			else
			{
				this.currentPictureIndex = 0
			}
			
			// Animate
			$('#contender-feature #slideshow ul').animate(
			{
				left: -($('#contender-feature #slideshow ul').width() / $('#contender-feature #slideshow li').length) * this.currentPictureIndex + 'px'
			}, 1000, "easeOutCubic");
		}
	},
	
	//----------------------------------------
	//  Photo feature related frontend code
	//----------------------------------------
	photofeature:
	{
		/**
		 * Initialize the photo feature related code.
		 * 
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		init: function()
		{
			$('#photo-feature a').hover(function()
			{
				$(this).parent().addClass('hover');
			},
			function()
			{
				$(this).parent().removeClass('hover');
			});
		}
	},
	
	//----------------------------------------
	//  Twitter related frontend code
	//----------------------------------------
	twitter:
	{
		/**
		 * Initialize the layout related code
		 * 
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		init: function()
		{
			// Get the search results for the term 'top mannequin'
			$.getJSON("http://search.twitter.com/search.json?q=topmannequin&count=4&callback=?", function(data) 
			{
				// Clear loading
				$('#twitter-feed ul').empty();
			
				// Loop over the results
				var counter = 0;
				$.each(data.results, function()
				{
					// Get tweet contents
					var text = this.text;
					
					// Construct tweet and append to our #tweets div
					var tweet = $("<li></li>").addClass('tweet').html(text);
						
					// Analyse our tweet text and turn urls into working links, hashtags into search links, and @replies into profile links.
					tweet.html(tweet.html()
						.replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1">$1</a>')
						.replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2">#$2</a>')
						.replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2">@$2</a>')
						+ '<br /><a href="http://www.twitter.com/' + this.from_user	+ '/status/' + this.id_str + '" class="post-time" title="Bekijk deze tweet">' + $.timeSince(this.created_at) + '</a>')
					.prepend('<a href="http://www.twitter.com/'	+ this.from_user + '" class="author" title="Bekijk de Twitter pagina van ' + this.from_user + '">' + this.from_user + '</a><br />')
					.appendTo('#twitter-feed ul')
					.fadeIn();
					
					// Increase counter
					counter++;
					
					if (counter == 3)
					{
						return false;
					}
				});
			});
		}
	},
	
	//----------------------------------------
	//  Picture gallery
	//----------------------------------------
	gallery:
	{
		/**
		 * Initialize the gallery.
		 * 
		 * @author	Gerrit Bertier
		 * @version	1.0
		 */
		init: function()
		{
			// Get the search results for the term 'top mannequin'
			$(".pictures ul li a").fancybox(
			{
				'transitionIn': 'none',
				'transitionOut': 'none',
				'titlePosition': 'inside',
				'titleFormat': function(title, currentArray, currentIndex, currentOpts)
				{
					return '<span id="fancybox-imagetitle">Foto ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
		}
	}
}

//----------------------------------------
// Document ready code
//----------------------------------------
$(document).ready(function()
{
	// Initialize the frontend
	topmannequin.frontend.init();
});

	
//----------------------------------------
// jQuery & JavaScript extension
//----------------------------------------

/**
* A function used to get a string returning the time since a given date/time.
*
* @return Returns a string indicating the time since a given date/time.
*
* @author Gerrit Bertier
* @version 1.0
*/ 
(function($)
{
    $.timeSince = function(time)
    {
        var date = new Date(time);
        var diff = ((new Date()).getTime() - date.getTime()) / 1000;
        var day_diff = Math.floor(diff / 86400);
        
        if (day_diff < 0 || day_diff >= 31 || isNaN(day_diff))
        {
            return "Bekijk tweet";
        }
        
        if(day_diff == 0)
        {
            if (diff < 60)
            {
                return Math.ceil(diff) + " seconden geleden";
            }
            else if(diff < 120)
            {
                return "1 minuut geleden";
            }
            else if(diff < 3600)
            {
                return Math.floor( diff / 60 ) + " minuten geleden";
            }
            else if(diff < 7200)
            {
                return "1 uur geleden";
            }
            else if(diff < 86400)
            {
                return Math.floor( diff / 3600 ) + " uren geleden";
            }
        }
        
        if(day_diff == 1)
        {
            return "Gisteren";
        }
        else if(day_diff < 7)
        {
            return day_diff + " dagen geleden";
        }
        else if(day_diff < 31)
        {
            return Math.ceil( day_diff / 7 ) + " weken geleden";
        }
        else {
            return "Bekijk tweet";
        }    
    }
})(jQuery);
