
(function($){
    $.fn.zslideshow = function() {
	$.inAnimation = false;
	$.paused = false;
	var options = $.extend($.fn.zslideshow.defaults, $.fn.zslideshow.options);
	$(window).bind("load", function(){
	    $('#zloading').hide();
	    $('#zslides').fadeIn('fast');
	    $('#zcontent').show();
	    if ($('#slideshow .activeslide').length == 0) $('#zslides a:first').addClass('activeslide');
	    if (options.slide_captions == 1) $('#zslidecaption').html($('#zslides .activeslide').find('img').attr('title'));
	    if (options.navigation == 0) $('#znavigation').hide();
	    //Slideshow
	    if(options.slideshow == 1){
		if(options.slide_counter == 1){ //Initiate slide counter if active
		    $('#zslidecounter .zslidenumber').html(1);
	    	    $('#zslidecounter .ztotalslides').html($("#zslides > *").size());
		    $('#zicurrent').html(1);
	    	}
		slideshow_interval = setInterval("nextslide()", options.slide_interval);
		if(options.navigation == 1){ //Skip if no navigation
		    $('#znavigation a').click(function(){  
   			$(this).blur();  
   			return false;  
   		    }); 	
		    //Slide Navigation
		    $('#znextslide').click(function() {
			if($.paused) return false; if($.inAnimation) return false;
			clearInterval(slideshow_interval);
			nextslide();
			slideshow_interval = setInterval(nextslide, options.slide_interval);
			return false;
		    });
		    $('#zprevslide').click(function() {
			if($.paused) return false; if($.inAnimation) return false;
			clearInterval(slideshow_interval);
			prevslide();
			slideshow_interval = setInterval(nextslide, options.slide_interval);
			return false;
		    });
		    $('#znextslide img').hover(function() {
			if($.paused == true) return false;
			$(this).attr("src", "assets/znext.gif");
		    }, function(){
			if($.paused == true) return false;
			$(this).attr("src", "assets/znext_dull.gif");
		    });
		    $('#zprevslide img').hover(function() {
			if($.paused == true) return false; 
			$(this).attr("src", "assets/zprev.gif");
		    }, function(){
			if($.paused == true) return false;
			$(this).attr("src", "assets/zprev_dull.gif");
		    });

		    $('#zpauseplay').click(function() {
			if($.inAnimation) return false;
			var src = ($(this).find('img').attr("src") === "assets/zplay.gif") ? "assets/zpause.gif" : "assets/zplay.gif";
      			if (src == "assets/zpause.gif"){
      			    $(this).find('img').attr("src", "assets/zplay.gif");
      			    $.paused = false;
			    slideshow_interval = setInterval(nextslide, options.slide_interval);  
			}else{
			    $(this).find('img').attr("src", "assets/zpause.gif");
			    clearInterval(slideshow_interval);
			    $.paused = true;
			}
      			$(this).find('img').attr("src", src);
			return false;
		    });
		    $('#zpauseplay').mouseover(function() {
			var imagecheck = ($(this).find('img').attr("src") === "assets/zplay_dull.gif");
			var imagecheck1= ($(this).find('img').attr("src") === "assets/zpause_dull.gif");
			if (imagecheck){
      			    $(this).find('img').attr("src", "assets/zplay.gif"); 
			}
			if (imagecheck1) {
			    $(this).find('img').attr("src", "assets/zpause.gif");
			}
		    });
		    $('#zpauseplay').mouseout(function() {
			var imagecheck = ($(this).find('img').attr("src") === "assets/zplay.gif");
			var imagecheck1= ($(this).find('img').attr("src") === "assets/zpause.gif");
			if (imagecheck){
      			    $(this).find('img').attr("src", "assets/zplay_dull.gif"); 
			}
			if (imagecheck1) {
			    $(this).find('img').attr("src", "assets/zpause_dull.gif");
			}
			return false;
		    });
		}
	    }
	});
	
	$(document).ready(function() {
	    $('#zslides').setposition(); 
	});
		
	//Pause when hover on image
	$('#zslides > *').hover(function() {
	    if (options.slideshow == 1 && options.pause_hover == 1){
	   	if(!($.paused) ){
		    if(options.navigation == 1) {
	   		$('#zpauseplay > img').attr("src", "assets/zpause.gif"); 
		    }
	   	    clearInterval(slideshow_interval);
	   	}
	    }
	}, function() {
	    if (options.slideshow == 1 && options.pause_hover == 1){
		if(!($.paused) ){
		    if(options.navigation == 1) {
			$('#zpauseplay > img').attr("src", "assets/zpause_dull.gif");
		    }
		    slideshow_interval = setInterval(nextslide, options.slide_interval);
		} 
	    }
	});
		
	$(window).bind("resize", function(){
    	    $('#zslides').setposition(); 
	});
		
	$('#zslides').hide();
	$('#zcontent').hide();
    };
	
    //Adjust image size
    $.fn.setposition = function() {
	var options = $.extend($.fn.zslideshow.defaults, $.fn.zslideshow.options);
	return this.each(function() {
	    var height = options.height;
	    var width = options.width;
	    var imagewidth = $(this).width();
	    var imageheight = $(this).height();
	    $(this).children().css('left', (width - $(this).width())/2);
	    $(this).children().css('top', (height - $(this).height())/2);
	    return false;
	});
    };
	
    $.fn.zslideshow.defaults = { 
	width: 480,  
	height: 640,
	slideshow: 1,
	navigation:1,
	transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
	pause_hover: 1,
	slide_counter: 1,
	slide_captions: 1,
	slide_interval: 5000
    };
})(jQuery);

//Slideshow Next Slide
function nextslide() {
    if($.inAnimation) return false;
    else $.inAnimation = true;
    var options = $.extend($.fn.zslideshow.defaults, $.fn.zslideshow.options);
    var currentslide = $('#zslides .activeslide');
    currentslide.removeClass('activeslide');
    
    if( currentslide.length == 0 ) currentslide = $('#zslides a:last');
			
    var nextslide =  currentslide.next().length ? currentslide.next() : $('#zslides a:first');
    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#zslides a:last');
		
    //Display slide counter
    if(options.slide_counter == 1){
	var slidecount = $('#zslidecounter .zslidenumber').html();
	currentslide.next().length ? slidecount++ : slidecount = 1;
	$('#zslidecounter .zslidenumber').html(slidecount);
	$('#zicurrent').html(slidecount);

    }
    $('.zprevslide').removeClass('zprevslide');
    prevslide.addClass('zprevslide');

    //Captions require img in <a>
    if (options.slide_captions == 1) $('#zslidecaption').html($(nextslide).find('img').attr('title'));
    nextslide.hide().addClass('activeslide')
    if (options.transition == 0){
	nextslide.show(); $.inAnimation = false;
    }
    if (options.transition == 1){
	nextslide.fadeIn(750, function(){$.inAnimation = false;});
    }
    if (options.transition == 2){
	nextslide.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 3){
	nextslide.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 4){
	nextslide.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 5){
	nextslide.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation = false;});
    }
    $('#zslides').setposition();//Fix for resize mid-transition
}
	
//Slideshow Previous Slide
function prevslide() {
    if($.inAnimation) return false;
    else $.inAnimation = true;
    var options = $.extend($.fn.zslideshow.defaults, $.fn.zslideshow.options);
    var currentslide = $('#zslides .activeslide');
    currentslide.removeClass('activeslide');
    
    if ( currentslide.length == 0 ) currentslide = $('#zslides a:first');
    
    var nextslide =  currentslide.prev().length ? currentslide.prev() : $('#zslides a:last');
    var prevslide =  nextslide.next().length ? nextslide.next() : $('#zslides a:first');
    
    //Display slide counter
    if (options.slide_counter == 1){
	var slidecount = $('#zslidecounter .zslidenumber').html();
	currentslide.prev().length ? slidecount-- : slidecount = $("#zslides > *").size();
	$('#zslidecounter .zslidenumber').html(slidecount);
	$('#zicurrent').html(slidecount);
    }
    
    $('.zprevslide').removeClass('zprevslide');
    prevslide.addClass('zprevslide');
    
    //Captions require img in <a>
    if (options.slide_captions == 1) $('#zslidecaption').html($(nextslide).find('img').attr('title'));
    
    nextslide.hide().addClass('activeslide')
    if (options.transition == 0){
	nextslide.show(); $.inAnimation = false;
    }
    if (options.transition == 1){
	nextslide.fadeIn(750, function(){$.inAnimation = false;});
    }
    if (options.transition == 2){
	nextslide.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 3){
	nextslide.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 4){
	nextslide.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation = false;});
    }
    if (options.transition == 5){
	nextslide.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation = false;});
    }
    
    $('#zslides').setposition();//Fix for resize mid-transition
}
