$(document).ready(function () {

	if( $("#slideShow").length > 0 ) {

		var currentSlideItem = 1;

		$.ajax({  
			url: "layout/custom/SlideShowHome.xml",
			type: "GET",  
			dataType: "html",  
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",  
			success: function(xml) {
				$.xmlDOM( xml )
					.find('item')
						.each(function() {
							if( $(this).attr("type") == "video" ) {
								var timer = $.trim( $(this).attr("timer") );
								var location = $.trim( $(this).find('location').text() );
								var target = $.trim( $(this).find('action').attr("target") );
								var action = $.trim( $(this).find('action').text() );
								var video = $.trim( $(this).find('video').text() );

								if(action=="") {
									action = "javascript:void(0);";
								}

								if(target=="" || target=="same") {
									target = "_self";
								}

								$("#slideShow div.slideshow-screen").append('<div class="slide-item slide-item-video" id="slideItem' + currentSlideItem + '"></div>');
								$("#slideItem" + currentSlideItem).append('<a class="media-container" href="#"><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() { 
									$("div.slide-item-current a.play-button").show();
									$("div.slide-item-current a.player").hide();
									$("div.slide-item-current img").show();
									slideShow.isVideoPlaying = false;
								});

								$("#slideItem" + currentSlideItem + " a.play-button").click(function() {
									$("img", $(this).parent()).hide();
									$("a.player", $(this).parent()).show().css("visibility","visible");
									$(this).hide();
									setTimeout(function() {
										flowplayer( $("div.slide-item-current a.player").attr("id") ).play();
									}, 1500);
								});
								
								jQuery("#slideItem" + currentSlideItem + " a.media-container").click(function(e){
									e.preventDefault();
									jQuery("a.play-button", jQuery(this).parent()).click();
								});

								$("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									$("#slideItem" + currentSlideItem).addClass("slide-item-current");
									$("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;

							} else if( $(this).attr("type") == "photo" ) {
								var timer = $.trim( $(this).attr("timer") );
								var location = $.trim( $(this).find('location').text() );
								var target = $.trim( $(this).find('action').attr("target") );
								var action = $.trim( $(this).find('action').text() );

								$("#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";
								}

								$("#slideItem" + currentSlideItem).append('<a href="' + action + '" target="' + target + '"><img src="' + location + '" /></a>');

								$("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									$("#slideItem" + currentSlideItem).addClass("slide-item-current");
									$("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;

							} else if( $(this).attr("type") == "animation" ) {
								var timer = $.trim( $(this).attr("timer") );
								var location = $.trim( $(this).find('location').text() );

								$("#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";
								}

								$("#slideItem" + currentSlideItem).append('<div id="animationSlide' + currentSlideItem + '"></div>');

								swfobject.embedSWF(location, "animationSlide" + currentSlideItem, $("#slideShow div.slideshow-body").width(), $("#slideShow div.slideshow-body").height(), "9.0.0","/layout/flash/expressInstall.swf", null, {wmode: "transparent"} );

								$("div.slideshow-controls ul.slides").append('<li><a href="javascript:void(0);" onclick="slideShow.goTo(' + currentSlideItem + ');">Slide ' + currentSlideItem + '</a></li>');

								if(currentSlideItem == 1) {
									$("#slideItem" + currentSlideItem).addClass("slide-item-current");
									$("div.slideshow-controls ul.slides a").addClass("current");
								}

								currentSlideItem++;
							}
						});

						if(currentSlideItem <= 2) {
							$("#slideShow").height( $("#slideShow").height() - $("div.slideshow-controls").height() );
							$("div.slideshow-controls").hide();
						}
						
				$("div.slideshow-controls-body").width( $("div.slideshow-controls a.slideshow-controls-prev").width() + $("div.slideshow-controls a.slideshow-controls-next").width() + $("div.slideshow-controls ul.slides").width() + 20 );

			}
		});

		$("div.slideshow-controls a.slideshow-controls-prev").click(function() {
			slideShow.prev();
		});

		$("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( $("div.slide-item").length );
				return false;
			} else if( slide == ($("div.slide-item").length+1) ) {
				slideShow.goTo( 1 );
				return false;
			} else {

				if($("div.slide-item-current a.player").length > 0) {
					if(slideShow.isVideoPlaying) {
						flowplayer( $("div.slide-item-current a.player").attr("id") ).stop();
						slideShow.isVideoPlaying = false;
					}
					$("div.slide-item-current a.media-container, div.slide-item-current a.play-button").show();
					$("div.slide-item-current a.player").hide();
					$("div.slide-item-current img").show();
				}

				slideShow.isChanging = true;

				if($("div.slide-item").hasClass("slide-item-animation") && $.browser.msie) {
					$("div.slide-item").hide();
					$(this).removeClass("slide-item-current").css("display","none");
				} else {
					$("div.slide-item").fadeOut(150, function() {
						$(this).removeClass("slide-item-current").css("display","none");
					});
				}

				if($("div.slide-item").eq(slide-1).hasClass("slide-item-animation") && $.browser.msie) {
					$("div.slide-item").eq(slide-1).show();
					$(this).addClass("slide-item-current");
					slideShow.isChanging = false;
				} else {
					setTimeout(function() {
						$("div.slide-item").eq(slide-1).fadeIn(150, function() {
							$(this).addClass("slide-item-current");
							slideShow.isChanging = false;
						});
					}, 200);
				}
			}

			$("div.slideshow-controls ul.slides a.current").removeClass("current");
			$("div.slideshow-controls ul.slides a").eq(slide-1).addClass("current");

			this.currentSlide = slide;
		}
	}
};


