
(function($){

	"use strict";

	/* DOM ready. */
	$(document).ready(function(){

		initImageSlide();

	});

	/**
	 * @constant
	 */
	var SWITCH_DURATION = 500;

	/**
	 * @property {Number} for auto Image slide.
	 */
	var timerId;

	/**
	 * Initialize Image slide.
	 */
	function initImageSlide() {
		$.ajax({
			url: '/xml/top.xml',
			data: new Date().getTime().toString(),
			error: function(xhr, status, e) {
			},
			success: function(data) {
				buildImageSlide(data);
				setupImageSlide();
				startImageSlide();
			}
		});
	}

	/**
	 * Build Image slide HTML.
	 *
	 * @param {Object} XML Object.
	 */
	function buildImageSlide(xml) {
		var $images = $(xml).find('image'),
			imageList = [],
			navList = []
		;

		/* Add images. */
		$images.each(function(){
			var src = $(this).attr('path');
			var url = $(this).attr('link');
			var target = $(this).attr('target') || '_self';

			if (url) {
				imageList.push('<li><a href="'+ url +'" target="'+ target +'"><img src="'+ src +'" /></a></li>');
			}
			else {
				imageList.push('<li><img src="'+ src +'" /></li>');
			}
		});

		if ($images.length > 0) {
			$('#image_slide').append('<ul>'+ imageList.join('') +'</ul>');

			if ($images.length > 1) {
				/* Add nav items. */
				for (var i = 0, n = $images.length; i < n; i++) {
					navList.push('<li><span>&nbsp;</span></li>');
				}
				$('#image_slide').append('<ol>'+ navList.join('') +'</ol>');
			}
		}
	}

	/**
	 * Setup Image slide.
	 */
	function setupImageSlide() {
		$('#image_slide')
			/* Hide all images. */
			.find('ul > li')
				.css({opacity: 0})
			.end()

			/* Set nav items. */
			.find('ol > li')
				/* Hide nav at first. */
				.css({opacity: 0})

				/* Set click handler. */
				.click(function(){
					/* Prevent current. */
					if ($(this).hasClass('current')) {
						return;
					}
					switchActiveImage(
						$('#image_slide > ol > li').index(this)
					);
				})

				/* Set hover handler. */
				.hover(
					function(){
						if ($(this).hasClass('current')) {
							return;
						}
						$(this).children('span')
							.stop()
							.animate({opacity: 1}, SWITCH_DURATION, 'easeOutQuart')
						;
					},
					function(){
						if ($(this).hasClass('current')) {
							return;
						}
						$(this).children('span')
							.stop()
							.animate({opacity: 0}, SWITCH_DURATION, 'easeOutQuart')
						;
					}
				)
			.end()
		;
	}

	/**
	 * Start Image slide.
	 */
	function startImageSlide() {
		switchActiveImage(0);

		/* Set auto Image slide. */
		timerId = setInterval(function(){
			var $images = $('#image_slide > ul > li'),
				nextIndex = $images.index($images.filter('.current')) + 1
			;

			if (nextIndex >= $images.length) {
				/* Stop auto Image slide. */
				clearInterval(timerId);
				switchActiveImage(0);

				/* Show nav. */
				setTimeout(function(){
					$('#image_slide > ol > li')
						.stop()
						.animate({opacity: 1}, SWITCH_DURATION, 'easeOutQuart')
					;
				}, 800);
			}
			else {
				/* Step next. */
				switchActiveImage(nextIndex);
			}
		}, 5000);
	}

	/**
	 * Switch active image.
	 *
	 * @param {Number} A image index (0 based) to activate.
	 */
	function switchActiveImage(index) {
		var $images = $('#image_slide > ul > li'),
			$navItems = $('#image_slide > ol > li')
		;

		/* Switch images. */
		$images
			/* Reset all images. */
			.removeClass('current')
			.stop()
			.animate({opacity: 0}, SWITCH_DURATION, 'easeOutQuart')

			/* Set current image. */
			.eq(index)
				.addClass('current')
				.stop()
				.animate({opacity: 1}, SWITCH_DURATION, 'easeOutQuart')
			.end()
		;

		/* Switch nav items. */
		$navItems
			/* Reset all nav items. */
			.removeClass('current')
			.children('span')
				.stop()
				.animate({opacity: 0}, SWITCH_DURATION, 'easeOutQuart')
			.end()

			/* Set current nav item. */
			.eq(index)
				.addClass('current')
				.children('span')
					.stop()
					.animate({opacity: 1}, SWITCH_DURATION, 'easeOutQuart')
				.end()
			.end()
		;
	}

}(jQuery));



/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright ﾂｩ 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	}
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright ﾂｩ 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */

