$(document).ready(function(){


		$('.gallery_platform .jcarousel-skin-tango').addClass('gallery_show'); // adds new class name to maintain degradability
		
		$('.gallery_platform ul.gallery_show').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.8);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next Image >>');
				
				
				$('#main_image').trigger('img_change');

			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.8';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.8); } // don't fade out if the parent is active
				)
			}
		});
		
	   $('.gallery_platform').prepend("<div class='autoControl'>Pause</div>");
		var active = true;
		var gal = jQuery('.gallery_platform');
		var ctrl = gal.find('.autoControl')

    $(function() {
        //var slideshow = $("gallerytitle");

        if (active) {
            ctrl.css("background-position", "0 0");
            gal.everyTime('10s', 'slideshow', function() {
                $.galleria.next();
            });
        } if (!active) {
            ctrl.css("background-position", "0 -23px");
        };

        ctrl.css("cursor", "pointer").click(function() {
            if (!active) {
                active = true;
                $.galleria.next();
                ctrl.css("background-position", "0 0").html("Pause");
                gal.everyTime('10s', 'slideshow', function() {
                    $.galleria.next();
                });
            } else if (active) {
                active = !active;
                ctrl.css("background-position", "0 -23px").html("Play");
                gal.stopTime('slideshow');
            }
        });
        
        
	});
	
	
	jQuery('#gallery_carousel').jcarousel({
		scroll: 5,
		  initCallback: gallery_carousel_initCallback
	 });

	 $.hotkeys.add('left', function(){$.galleria.prev();});
	 $.hotkeys.add('right', function(){$.galleria.next();});


	$('.gallery_platform .main_image_wrapper').prepend("<div class=\"side_nav\"><a class='prevImg' href='#'>Previous Image</a></div>");
	$('.gallery_platform .main_image_wrapper').append("<div class=\"side_nav\"><a class='nextImg' href='#'>Next Image</a></div>");

	$('.gallery_platform .prevImg').click(function(){
		$.galleria.prev();
		if (active) {
			active = !active;
			ctrl.css("background-position", "0 -23px").html("Play");
			gal.stopTime('slideshow');
			
		}
		return false;
	});
	$('.gallery_platform .nextImg').click(function(){
		$.galleria.next();
		if (active) {
			active = !active;
			ctrl.css("background-position", "0 -23px").html("Play");
			gal.stopTime('slideshow');
		}
		return false;
	});
		
	$('.gallery_platform .galleria_wrapper, .gallery_platform .jcarousel-item, .gallery_platform .jcarousel-prev, .gallery_platform .jcarousel-next').click(function(){
		if (active) {
			active = !active;
			ctrl.css("background-position", "0 -23px").html("Play");
			gal.stopTime('slideshow');
		}
	});


	
	});



 $(function() { 
				//var slideshow = $("gallerytitle");
				
				var active = false;
				var gal = jQuery('#gallerytitle');
				gal.find('.start').css("cursor", "pointer").click(function() {
					if (!active) {
						active = !active;
						$.galleria.next();
						gal.everyTime('5s', 'slideshow', function() {
							$.galleria.next();
						});
					}
				});
				//}).end().find('.stop').css("cursor", "pointer").click(function() {
				gal.find('.stop').css("cursor", "pointer").click(function() {
					if (active) {
						active = !active;
						gal.stopTime('slideshow');
					}
				});
		});


	

function gallery_carousel_initCallback(carousel) {
	var listTotal = jQuery('#gallery_carousel li').length;

	jQuery('#main_image').bind('img_change', function() {
	    var num = parseInt((jQuery('.caption').text()).split(":", 1)[0]) - 1;
	    var location = parseInt(jQuery('.gallery_platform .caption').text().split(";", 2)[0]) + 1;

	    jQuery('.gallery_platform .caption').html(jQuery('.caption').text().split(";", 2)[1]);
	   
		carousel.scroll(num);
	    return false;
	});
};


