/**
 * jQuery jqGalView Plugin
 * Examples and documentation at: http://benjaminsterling.com/2007/08/24/jquery-jqgalscroll-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 1.0
 * @requires jQuery v1.1.3.1 or later
 * @optional jQuery Easing v1.1.1
 *
 * @name jqGalScroll
 * @type jQuery
 * @param Hash	options				additional options
 * @param String	options[ease]				refer to http://gsgd.co.uk/sandbox/jquery.easing.php for values
 * @param String	options[speed]				fast, slow, 1000, ext..
 * @param String	options[height]				the default height of your wrapper
 * @param String	options[navArrowOpacity]	the opacity of your up and down links
 * @param String	options[titleOpacity]		the opacity of your title bar (if present)
 * @return jQuery Object (chainable)
 */
 
 /********************************** Additions made to the code **********************************
 * PliggTemplates Club <http://www.pliggtemplates.eu>
 * @XrByte <info@exp.ee>, Grusha <grusha@feellove.eu>
 */
(function($) {
 
	jQuery.fn.jqGalScroll = function(options){
		/*	Set up our options */
		var o = jQuery.extend({}, jQuery.fn.jqGalScroll.defaults, options);
		return this.each(function() {
			var $parent = jQuery(this), $children = $parent.children(), index=0;
			var $wrap = jQuery('<div class="jqgsContainer"></div>').css({position:'absolute',height:o.height,width:o.width,overflow:'hidden'});
			var $navContainer = $('<div class="jqgsNav">');
			var $jqgstitle = $('<div class="jqgstitle">').css({opacity:o.titleOpacity}).hide();
			var $Navigation = $('<div class="navigation">').css({opacity:o.navArrowOpacity});
			var $jqgsPagination = $('<div class="jqgsPagination">');
			var $pictImg = $parent.find("li").find("img");
			var $NavPic = $parent.find("img").find("#navig");
			
			
				jQuery("#mygalone").css({height:o.height});
				jQuery("#mygalone").css({width:o.width});
				//jQuery("div.navigation").prepend("<img src='templates/easynews/images/slideshow/loading.gif' class='ldrgif' alt='loading...'/ >");
			
			$Navigation.append('<img id="navig" class="prev" src="' + o.prev_image + '"/>' +
				  '<img class="play" src="' + (o.playing ? o.pause_image : o.play_image) + '"/>' +
				  '<img class="next" src="' + o.next_image + '"/>').
					find('img').css('cursor', 'pointer').end().
					find('.prev').click(function(){
			
				js_stop_slideshow();						 
				if(index==0) return false;
				js_navigation_slideshow (--index);
				
			}).end().
        find('.next').click(function(){
					
				js_stop_slideshow();				 
				if($children.size()==(index+1)) return false;
				js_navigation_slideshow (++index);
									 
				 }).end().
		
		 find('.play').click(function() {o.playing ? js_stop_slideshow() : js_start_slideshow()
        return false
      });
		 
	 function js_navigation_slideshow (navig) {
		
		 
		 $pictImg.fadeOut("slow",function(){
								animate_slideshow ()		  
								$pictImg.fadeIn("slow");
					 });
				$jqgstitle.slideToggle($children[navig],function(){
						   jQuery.fn.jqGalScroll.getTitle($children[index],$jqgstitle);
						  
						 });
				$jqgsPaginationLinks.filter('.selected').removeClass('selected');
				$jqgsPaginationLinks.slice(index,index+1).addClass('selected');
				$jqgstitle.slideToggle( "slow");
				return false;
		
	 }
			
	  function js_start_slideshow() {
   $Navigation.find('.play').attr('src', o.pause_image)
    o.playing = setInterval(function(){
	if($jqgsPagination.find('.selected').parent().next().children('a').size()){
		$jqgsPagination.find('.selected').parent().next().children('a').click();
	}
	else{
		$jqgsPagination.find('li:eq(0)').children('a').click();
	}
}, o.slide_duration)
  }
  
  

  function js_stop_slideshow() {
    $Navigation.find('.play').attr('src', o.play_image)
    clearInterval(o.playing)
    o.playing = false
  }	
  
  function animate_slideshow () {
	
	 if(o.effect == 'top'){		
	$parent.animate({marginTop:-($children.height()*index)},o.speed, o.ease);
						return false;	 
	 }
	
	 if(o.effect == 'fade'){		
	$parent.css({marginTop:-($children.height()*index)},o.speed, o.ease);
						return false;	 
	 }
	
	 if(o.effect == 'left'){
	$wrap.css({position:'absolute',height:o.height,overflow:'hidden'});
	$parent.animate({marginLeft:-($children.width()*index)},o.speed, o.ease);
						return false;	 
	 }
	 
  }
			
			$parent.css({padding:0,margin:0,listStyle:'none'}).wrap($wrap).parent().append($navContainer).append($jqgstitle).append($jqgsPagination);
			$navContainer.append($Navigation);
			$children.css({height:o.height});
			jQuery.fn.jqGalScroll.getTitle($children[index],$jqgstitle);

			var $ul = $('<ul>');
			
			for(var i = 0; i < $children.size(); i++){
				var selected = '';
				if(i == 0) selected = 'selected';
				
				var $a = jQuery('<li><a href="#'+(i)+'" class="'+selected+'">'+(i+1)+'</a></li>').css({opacity:.70});
				$ul.append($a);
			};
			$jqgsPagination.append($ul)
			
			var $jqgsPaginationLinks = $jqgsPagination.find('a').click(function(){
				
				$jqgstitle.slideToggle( "slow",function(){
						   jQuery.fn.jqGalScroll.getTitle($children[href],$jqgstitle);
						 }); 
				var href = this.href.replace(/^.*#/, '');
				$jqgsPaginationLinks.filter('.selected').removeClass('selected');
				jQuery(this).addClass('selected');
				
				$pictImg.fadeOut("slow",function(){
					   			animate_slideshow ();
								$pictImg.fadeIn("slow");
					 });
				
				$jqgstitle.slideToggle( "slow");
				
				index = href;
			});
		});
	};
	jQuery.fn.jqGalScroll.getTitle = function(obj, $textTo){
		var alt = jQuery(obj).children('img').attr('alt');
		if(alt) $textTo.html(alt).show();
		else $textTo.hide();
	};
	jQuery.fn.jqGalScroll.defaults = {
		ease: 0,
		speed:0,
		height: 'auto',
		width: 'auto',
		navArrowOpacity : .30,
		titleOpacity : .60,
		effect: 'fade',
		slide_duration: 5000,
		next_image    : 'templates/easynews/images/slideshow/next.png',
		prev_image    : 'templates/easynews/images/slideshow/prev.png',
		play_image    : 'templates/easynews/images/slideshow/play.png',
		pause_image   : 'templates/easynews/images/slideshow/pause.png'
	};
})(jQuery);
