/*!
 * jQuery xmlDOM Plugin v1.0
 * http://outwestmedia.com/jquery-plugins/xmldom/
 *
 * Released: 2009-04-06
 * Version: 1.0
 *
 * Copyright (c) 2009 Jonathan Sharp, Out West Media LLC.
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */
(function($) {
	// IE DOMParser wrapper
	if ( window['DOMParser'] == undefined && window.ActiveXObject ) {
		DOMParser = function() { };
		DOMParser.prototype.parseFromString = function( xmlString ) {
			var doc = new ActiveXObject('Microsoft.XMLDOM');
	        doc.async = 'false';
	        doc.loadXML( xmlString );
			return doc;
		};
	}
	
	$.xmlDOM = function(xml, onErrorFn) {
		try {
			var xmlDoc 	= ( new DOMParser() ).parseFromString( xml, 'text/xml' );
			if ( $.isXMLDoc( xmlDoc ) ) {
				var err = $('parsererror', xmlDoc);
				if ( err.length == 1 ) {
					throw('Error: ' + $(xmlDoc).text() );
				}
			} else {
				throw('Unable to parse XML');
			}
		} catch( e ) {
			var msg = ( e.name == undefined ? e : e.name + ': ' + e.message );
			if ( $.isFunction( onErrorFn ) ) {
				onErrorFn( msg );
			} else {
				$(document).trigger('xmlParseError', [ msg ]);
			}
			return $([]);
		}
		return $( xmlDoc );
	};
})(jQuery);


jQuery(document).ready(function () {

	if( jQuery("#slideShow").length > 0 ) {

		var currentSlideItem = 1;

		jQuery.ajax({  
			url: "lib/slideshow.xml",  
			type: "GET",  
			dataType: "html",  
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",  
			success: function(xml) {
				jQuery.xmlDOM( xml )
					.find('item')
						.each(function() {
							if( jQuery(this).attr("type") == "video" ) {
								var timer = jQuery.trim( jQuery(this).attr("timer") );
								var location = jQuery.trim( jQuery(this).find('location').text() );
								var target = jQuery.trim( jQuery(this).find('action').attr("target") );
								var action = jQuery.trim( jQuery(this).find('action').text() );
								var video = jQuery.trim( jQuery(this).find('video').text() );

								if(action=="") {
									action = "javascript:void(0);";
								}

								if(target=="" || target=="same") {
									target = "_self";
								}

								jQuery("#slideShow div.slideshow-screen").append('<div class="slide-item slide-item-video" id="slideItem' + currentSlideItem + '"></div>');
								jQuery("#slideItem" + currentSlideItem).append('<a href="' + action + '" target="' + target + '"><img src="' + location + '" /></a><a class="player" href="' + video + '" id="player' + currentSlideItem + '"></a><a href="javascript:void(0);" class="play-button" rel="player' + currentSlideItem + '">Play</a>');

								var player = $f("player" + currentSlideItem, {src: "layout/flash/flowplayer.swf", wmode: "transparent"}, {
									clip: {
										autoPlay: false,
										autoBuffering: true
									},
									plugins:  { 
										controls: {
											all:false, 
											scrubber:true, 
											play:true, 
											mute:true, 

											backgroundColor: 'transparent',
											backgroundGradient: 'none',

											bufferColor: '#333333',
											progressColor: '#EFEFEF',
											buttonColor: '#949494', 
											buttonOverColor: '#CCCCCC', 

											tooltipColor: '#949494', 

											autoHide: 'always',
											hideDelay: 2500,

											width: '98%',  
											bottom: 5, 
											left: '50%', 
											borderRadius: 15
										}
									},
									play: {opacity: 0}
								});

								player.onStart(function() { 
									slideShow.isVideoPlaying = true;
								});

								player.onFinish(function() { 
									jQuery("div.slide-item-current a.play-button").show();
									jQuery("div.slide-item-current a.player").hide();
									jQuery("div.slide-item-current img").show();
									slideShow.isVideoPlaying = false;
								});

								jQuery("#slideItem" + currentSlideItem + " a.play-button").click(function() {
									jQuery("img", jQuery(this).parent()).hide();
									jQuery("a.player", jQuery(this).parent()).show().css("visibility","visible");
									jQuery(this).hide();
									setTimeout(function() {
										flowplayer( jQuery("div.slide-item-current a.player").attr("id") ).play();
									}, 1500);
								});

								jQuery("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									jQuery("#slideItem" + currentSlideItem).addClass("slide-item-current");
									jQuery("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;

							} else if( jQuery(this).attr("type") == "photo" ) {
								var timer = jQuery.trim( jQuery(this).attr("timer") );
								var location = jQuery.trim( jQuery(this).find('location').text() );
								var target = jQuery.trim( jQuery(this).find('action').attr("target") );
								var action = jQuery.trim( jQuery(this).find('action').text() );

								jQuery("#slideShow div.slideshow-screen").append('<div class="slide-item slide-item-photo" id="slideItem' + currentSlideItem + '"></div>');

								if(action=="") {
									action = "javascript:void(0);";
								}

								if(target=="" || target=="same") {
									target = "_self";
								}
								if(target=="new") {
									target = "_blank";
								}

								jQuery("#slideItem" + currentSlideItem).append('<a href="' + action + '" target="' + target + '"><img src="' + location + '" /></a>');

								jQuery("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									jQuery("#slideItem" + currentSlideItem).addClass("slide-item-current");
									jQuery("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;

							} else if( jQuery(this).attr("type") == "animation" ) {
								var timer = jQuery.trim( jQuery(this).attr("timer") );
								var location = jQuery.trim( jQuery(this).find('location').text() );

								jQuery("#slideShow div.slideshow-screen").append('<div class="slide-item slide-item-animation" id="slideItem' + currentSlideItem + '"></div>');

								if(action=="") {
									action = "javascript:void(0);";
								}

								if(target=="" || target=="same") {
									target = "_self";
								}

								jQuery("#slideItem" + currentSlideItem).append('<div id="animationSlide' + currentSlideItem + '"></div>');

								swfobject.embedSWF(location, "animationSlide" + currentSlideItem, jQuery("#slideShow div.slideshow-body").width(), jQuery("#slideShow div.slideshow-body").height(), "9.0.0","/layout/flash/expressInstall.swf", null, {wmode: "transparent"} );

								jQuery("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									jQuery("#slideItem" + currentSlideItem).addClass("slide-item-current");
									jQuery("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;
							}
						});

						if(currentSlideItem <= 2) {
							jQuery("#slideShow").height( jQuery("#slideShow").height() - jQuery("div.slideshow-controls").height() );
							jQuery("div.slideshow-controls").hide();
						}
						
				//jQuery("div.slideshow-controls-body").width( jQuery("div.slideshow-controls a.slideshow-controls-prev").width() + jQuery("div.slideshow-controls a.slideshow-controls-next").width() + jQuery("div.slideshow-controls ul.slides").width() + 20 );

			}
		});

		jQuery("div.slideshow-controls a.slideshow-controls-prev").click(function() {
			slideShow.prev();
		});

		jQuery("div.slideshow-controls a.slideshow-controls-next").click(function() {
			slideShow.next();
		});
		
	}
});

var slideShow = {
	currentSlide: 1,
	isChanging: false,
	isVideoPlaying: false,
	next:function() {
		this.goTo( parseInt(slideShow.currentSlide) + 1 );
	},
	prev:function() {
		this.goTo( parseInt(slideShow.currentSlide) - 1 );
	},
	goTo:function( slide ) {
		if(!slideShow.isChanging) {
			if( slide <= 0 ) {
				slideShow.goTo( jQuery("div.slide-item").length );
				return false;
			} else if( slide == (jQuery("div.slide-item").length+1) ) {
				slideShow.goTo( 1 );
				return false;
			} else {

				if(jQuery("div.slide-item-current a.player").length > 0) {
					if(slideShow.isVideoPlaying) {
						flowplayer( jQuery("div.slide-item-current a.player").attr("id") ).stop();
						slideShow.isVideoPlaying = false;
					}
					jQuery("div.slide-item-current a.play-button").show();
					jQuery("div.slide-item-current a.player").hide();
					jQuery("div.slide-item-current img").show();
				}

				slideShow.isChanging = true;

				if(jQuery("div.slide-item").hasClass("slide-item-animation") && jQuery.browser.msie) {
					jQuery("div.slide-item").hide();
					jQuery(this).removeClass("slide-item-current").css("display","none");
				} else {
					jQuery("div.slide-item").fadeOut(600, function() {
						jQuery(this).removeClass("slide-item-current").css("display","none");
					});
				}

				if(jQuery("div.slide-item").eq(slide-1).hasClass("slide-item-animation") && jQuery.browser.msie) {
					jQuery("div.slide-item").eq(slide-1).show();
					jQuery(this).addClass("slide-item-current");
					slideShow.isChanging = false;
				} else {
					setTimeout(function() {
						jQuery("div.slide-item").eq(slide-1).fadeIn(600, function() {
							jQuery(this).addClass("slide-item-current");
							slideShow.isChanging = false;
						});
					}, 600);
				}
			}

			jQuery("div.slideshow-controls ul.slides a.current").removeClass("current");
			jQuery("div.slideshow-controls ul.slides a").eq(slide-1).addClass("current");

			
		}
				}
}


