
/*global $, setInterval,setTimeout,clearInterval  */
var slideshow = {
	id 			: 0,
	position	: 'top',
	current 	: 1, // current position for playing
	autostart 	: 1,
	slideTime 	: 1, // seconds
	count 		: 0, // slides number
	t 			: '', // callback for slide function
	width		: 500,
	height		: 300,
	backgroundcolor : 'FFF',
	paginatorColor : 'dark',
	paginatorPlacement : 'bottomright',
	isPlaying	: 0,
	transition	: 'none',
	reverseTransition : 'hide',
	
	start: function( id ){
		var that = this;
		that.id = id;
		$('.sls_s').not(':first').hide();
		that.getDetails( id );
		that.registerClicks();
		that.registerPlayStop();
	},
	getDetails: function( id ){
		var that = this;
		$.post(
			'/jx.php',
			{ s : "10|1|"+ id + "|0|0"},
			function( data ){
				that.id = id;
				$.extend( that, data );
				$("#sls_container_" + id).css({ 
					width		: that.width + "px", 
					height		: that.height + "px", 
					background	: "#" + that.backgroundcolor
				});
				$("#sls_s_" + id ).css({
					position	: 'absolute'
				});
				$("#sls_s_"+ id +" div.sls_s").css({
					width		: that.width + "px", 
					height		: that.height + "px"
				});
				$("#sls_control_" + id).css({ 
					width		: that.width + "px"
				});
			
				if(that.paginatorColor == 'dark'){
					$(".sls_control").addClass('dark');
				} else{
					$(".sls_control").addClass('light');
				}
				
				switch( that.paginatorPlacement ){
					case 'bottomright':
					case 'bottomleft':
						$(".sls_control").css({ bottom: 0});
					break;
					case 'topright':
					case 'topleft':
						$(".sls_control").css({ top: 0});
					break;
					case 'none':
                                            $(".sls_control").hide();
                                        break;
					default:
						$(".sls_control").hide();
					break;
				}
				switch( that.paginatorPlacement ){
					case 'bottomright':
					case 'topright':
						$(".sls_slides").css({ 'float': 'right', 'position':'relative'});
					break;
					case 'bottomleft':
					case 'topleft':
						$(".sls_slides")
							.css({ 'float': 'left', 'position':'relative', 'padding-left' :'2px'})
							.next().css({ 'float': 'left', 'position':'relative'});
					break;
					case 'none':
                                        break; 
					default:
					break;
				}

				$(".sls_c").html('');
				$.each($(".sls_c"), function(x,y){
					var id = $(y).attr('id').split('-')[1];
					var el = $(y);
					var set = (that.paginatorColor == 'dark')? 0 : '-26px';
					var poz = -id * 11 - 2 * id ;
					el.css({ 
						'background-position': poz + "px " + set
					});
				});
				$(".sls_slides").children('div').hover( 
					function(){
						var poz = $(this).css('background-position').split(' ')[0];
						var set = (that.paginatorColor == 'dark')? 0 : '-39px';
						$(this).css({ 'background-position': poz + ' ' + set});
					},
					function(){
						var poz =$(this).css('background-position').split(' ')[0];
						var set = (that.paginatorColor == 'dark')? 0 : '-26px';
						var id = $(this).attr('id').split('-')[1];
						if (that.current !== id) {
							$(this).css({
								'background-position': poz + ' ' + set
							});
						}
					}
				);
				if( that.autostart ){
					that.play();
				}
			},
			'json'
		);
	},
        /**
         * controls the slides automatic changing
         */
        play : function(){
            var that = this;
            if (!that.t) {
                if ( !that.isPlaying && that.slideTime) {
                    // play pause button
                    var el = $("#sls_ps_" + this.id);
                    var set = (this.paginatorColor == 'dark')? 0 : '24px';
                    el.css({ 'background-position': '0 '+ set});
                    that.isPlaying = 1;
                    // roll 
                    that.t = setInterval( function(){
                        if ( that.current <= that.count) {
                            //$("#sls_s_"+ that.id + "-" + that.current ).show().siblings().hide();
                            // current slide
                            var slide = $("#sls_s_" + that.id +" #sls_s_"+ that.id + "-" + that.current);
                            var prev = $("#sls_s_" + that.id +" #sls_s_"+ that.id + "-" + (that.current -1) );
                            if( that.transition === 'none'){
                                slide.show().siblings().hide();
                            } else {
                                switch( that.transition ){
                                    case 'fade':
                                        that.reverseTransition = 'fadeOut';
                                        prev.fadeOut(1000 , function(){ slide.siblings().hide();slide.fadeIn();  });
                                    break;
                                    case 'slide':
                                        if (prev.length) {
                                            prev.fadeOut();
                                        }
                                        setTimeout( function(){
                                            slide.siblings().hide();
                                            slide.fadeIn();
                                        },300);
                                    break;
                                    default:
                                        //console.log('transition type is: ' , that.transition );
                                    break;
                                }
                            }
                            that.controllerActive( that.id, that.current );
                            that.current++ ;
                        } else {
                            var el = $("#sls_s_" + that.id +" #sls_s_"+ that.id +"-1");
                            el.siblings().hide();
                            el.fadeIn();
                            that.current = 1;
                        }
                    }, this.slideTime * 1000);
                }
            }
        },
	pause: function(){
		var el = $("#sls_ps_" + this.id);
		var set = (this.paginatorColor == 'dark')? '-13px' : '11px';		
		el.css({ 'background-position': '0 '+ set});
		clearInterval( this.t ) ;
		this.t = null;
		this.isPlaying = 0;
	},
	controllerActive: function( slideID, slide){
		var that = this;
		var el = $("#sls_c_"+ slideID +"-" + slide );
		var els = el.siblings();
		var p = el.css("background-position");
		var r;
		if (!p || typeof(p) === "undefined" ) {
                    r = el.css("background-position-x") + " " + el.css("background-position-y");
		} else {
                    r = p;
		}
		var poz = r.split(' ')[0];
		var set = (that.paginatorColor == 'dark')? '-13px' : '-39px';
		el.css({ 'background-position': poz + ' '+ set});
		$.each( els , function(x,y){
			var b = $(this).css('background-position');
			var c;
			if (typeof(b) === "undefined") {
				c = $(this).css("background-position-x") + " " + $(this).css("background-position-y");
			} else {
				c = b;
			}
			var poz = c.split(' ')[0];
			var set = (that.paginatorColor == 'dark')? "0px" : '-26px';
			$(this).css({ 'background-position': poz + ' '+ set});
		});
	},
	registerPlayStop : function(){
		var that = this;
		$("#sls_ps_" + that.id).click(function(){
			if(that.isPlaying ){
				that.pause();
			} else {
				that.play();
			}
		});
	},
	registerClicks: function(){
		var that = this;
		$('.sls_c').click( function(ev){
			that.pause();
			var id = $(this).attr('id').split('_')[2].split('-')[1];
			that.current = id;
			$("#sls_s_"+ that.id +"-" + id ).show().siblings().hide();
			that.controllerActive( that.id, that.current );
		});	
	}
};
