var effectDuration = 700;
var defaultDuration = 5;
var showWidth=100;
var showHeight=100;
var defaultTransitionType = 'Fade';

jqlib.fn.extend({ 	    
    startSlideshow: function() { 
	    var obj = jqlib(this).children('img:first');
	    obj.show();
	    var pauseDuration = jqlib(this).getPauseDuration(); 
	    jqlib(this).delay(pauseDuration).queue(function() {jqlib(this).rotateSlideshow();} );
    },
    rotateSlideshow: function () {	
        var currentImg = jqlib(this).children('img:visible');
        var nextImg = currentImg.next('img');
        if ( jqlib(this).children('img').length<=1 ) return; 
        var pauseDuration = jqlib(this).getPauseDuration(); 
        var transitionType = jqlib(this).getTransitionType();
        if ( nextImg == null || nextImg.attr('src') == undefined ) nextImg = jqlib(this).children('img:first');
        switch (transitionType) {
        case "HORIZSLIDE":
            nextImg.css('z-index','2');
            currentImg.css('z-index','1');
            nextImg.css('left',jqlib(this).width());
            nextImg.show().animate({left:0},effectDuration);             
            currentImg.animate({left:0-jqlib(this).width()},effectDuration, function(){ jqlib(this).hide();});
            break;
        case "VERTSLIDE":
            nextImg.css('z-index','2');
            currentImg.css('z-index','1');
            nextImg.css('top',jqlib(this).height());
            nextImg.show().animate({top:0},effectDuration);             
            currentImg.animate({top:0-jqlib(this).width()},effectDuration, function(){ jqlib(this).hide();});
            break;
        case "NONE":            
            currentImg.hide();   
            nextImg.show(); 
            break;
        default: //fade
            currentImg.fadeOut(effectDuration, function(){      
                nextImg.fadeIn(effectDuration);
            });  
        }
       
        jqlib(this).clearQueue().delay(pauseDuration).queue(function() {jqlib(this).rotateSlideshow();});
    },
    getPauseDuration: function () {
        var delay = parseInt(getVar(jqlib(this),'_gallery_pause'));
        if ( delay == null || delay < 5 ) delay = defaultDuration;
        return delay*1000;
    },
    getTransitionType: function() {
        var transitionType = getVar(jqlib(this),'_gallery_transition');
        if ( transitionType == null || transitionType == "" ) transitionType = defaultTransitionType;
        return transitionType.toUpperCase();
    },
	initialisePhotoGalleries: function() { 
		var scope = jqlib(this);
		scope.find('.Slideshow_gallery_type > img.deferred_loading').each( function() {  
			var img = jqlib(this);
			img.attr("src", img.attr('deferredSrc')).removeClass('deferred_loading').removeAttr('deferredSrc');
		}); 

		scope.find('.Slideshow_gallery_type').each( function() {
			if( jqlib(this).children('img').length > 0 ) 
				jqlib(this).startSlideshow();
		});
		
		scope.find('.PopUp_gallery_type').each(function(){
			var thumbWidth = parseInt(getVar(jqlib(this),'_gallery_thumb_width'));
			var thumbHeight = parseInt(getVar(jqlib(this),'_gallery_thumb_height'));
			jqlib(this).find('h3.image_title').css('width',thumbWidth+'px');
			jqlib(this).find('.gallery_popup_thumb_image').css('width',thumbWidth+'px').height(thumbHeight);
			jqlib(this).find('.gallery_popup_thumb_image').css('line-height',thumbHeight+'px');
			jqlib(this).show();
		});
		
		jqlib.ImageBox.init();
		
		scope.find('.HorizThumbs_gallery_type').each( function() {
			var pauseDuration = jqlib(this).getPauseDuration();
			var imageWidth = parseInt(getVar(jqlib(this),'_gallery_image_width'));
			var imageHeight = parseInt(getVar(jqlib(this),'_gallery_image_height'));
			var thumbHeight = parseInt(getVar(jqlib(this),'_gallery_thumb_height'));
			var transitionType = jqlib(this).getTransitionType();
			
			var effect = 'slide-hori'; 
			if (transitionType == 'VERTSLIDE') effect = 'slide-vert'
			else if (transitionType == 'FADE') effect = 'fade'
			else if (transitionType == 'NONE') effect = 'none'
			
			var slideshow = {
				speed: pauseDuration
			};
			var gallerySettings = {
				width:imageWidth,
				height:imageHeight,
				slideshow: slideshow,
				effect: effect
			};			
			
			jqlib('.ad-gallery .ad-thumbs li a img').height(thumbHeight);
			var galleries = jqlib('.ad-gallery').adGallery(gallerySettings);
			
		});
	}
});

function getVar(obj, varName) {
    var regEx = new RegExp('[0-9a-z]*(?='+varName+')', 'gi');
    var matches = regEx.exec(obj.attr('class'));
    if (matches != null && matches.length > 0){ return matches[0]; }
    return 0;
}

jqlib(document).ready( function() {
	jqlib(document).initialisePhotoGalleries();
	//jqlib('.dotgallery').width(903).galleria();
});
