/* ------------------------------------------------------------------------
LSlider
	
Developped By: Laurentiu Nat
Version: 2.0
	
Copyright: worldsoft ag
------------------------------------------------------------------------- */

(function($){  

    $.fn.LSlider = function(vars) {       
        
        var element     = this;
        var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 4500;
        var current     = null;
        var currNo	= null;
        var divId       = "#" + element[0].id;
        var timeOutFn   = null;
        var mOver       = false;
        var items       = $("#" + element[0].id + " .sliderContent .sliderImage");
        var itemsSpan   = $("#" + element[0].id + " .sliderContent .sliderImage span");
        var cItems	= $("#" + element[0].id + ' .sliderController img');
        var elemWidth 	= vars.elemWidth;
        var is_time_set = false;
        var sliderType  = (vars.sliderType != undefined) ? vars.sliderType : 3;
        var textAnimation = (vars.textAnimation != undefined) ? vars.textAnimation : 'fade';
        
        var imgWidth = (vars.imgWidth != undefined) ? vars.imgWidth : 720;
        var imgHeight = (vars.imgHeight != undefined) ? vars.imgHeight : 300;
        
        
        items.each(function(i) {
            $(items[i]).mouseover(function() {
                mOver = true;
            });
            $(items[i]).mouseout(function() {
                mOver   = false;
                nextTimeout(true);
            });
        });
        
        cItems.each(function(i) {
            $(cItems[i]).click(function(){
                //current = items[i];
                current = (current != null) ? current : items[(items.length-1)];
                currNo      = jQuery.inArray(current, items) + 1
                currNo = (currNo == items.length) ? 0 : (currNo - 1);

                mOver = false;
                if (is_time_set) {
                    clearTimeout(timeOutFn);
                    is_time_set = false;
                }

                $(itemsSpan).slideDown('fast', function() {
                    $(items[i]).fadeIn('fast', function() {
                        items.each(function(j){
                            if (i != j)
                                $(items[j]).fadeOut('fast');
                        });
                        cItems.removeClass('current');
                        $(cItems[i]).addClass('current');
                        if($(itemsSpan[i]).css('bottom') == 0) 
                            $(itemsSpan[i]).slideUp(timeOut/6);
                        else
                            $(itemsSpan[i]).slideDown(timeOut/6);
                        current = items[i];		
                       	
                        nextTimeout();	
                    });	
                });
            });
    		
        });
        	
        
        
        var nextTimeout = function(isMouseOut) {
            if(items.length > 0) {
                if (is_time_set) {
                    clearTimeout(timeOutFn);
                }
                timeOutFn = setTimeout(makeSlider, /*thisTimeOut*/ timeOut);
                is_time_set = true;
            } else {
        //console.debug("No elements");
        }
        }
        
        var showText = function(itemNo) {
        	if ($(itemsSpan[itemNo]).hasClass('emptySpan'))
        		return;
            if (textAnimation == 'slide') {
                $(itemsSpan[itemNo]).slideDown('slow');
            }
            else {
                $(itemsSpan[itemNo]).fadeIn('slow');
            }	
        }
        var hideText = function(itemNo) {
        	if ($(itemsSpan[itemNo]).hasClass('emptySpan'))
        		return;
        
            if (textAnimation == 'slide') {
                $(itemsSpan[itemNo]).slideUp('slow');
            }
            else {
                $(itemsSpan[itemNo]).fadeOut('slow');
            }
        }
        
        var changeImage = function(currNo, nextNo) {
        	if (vars.admin != undefined)
        		animation_specific_css(sliderType);
        		
            cItems.removeClass('current');
            $(items[currNo]).css('z-index', 9);
            $(items[nextNo]).css('z-index', 10);
        	
            //console.debug(sliderType);
            if (sliderType == 1 ) {
                hideText(currNo);
                $(items[currNo]).slideUp('slow');
                $(items[nextNo]).slideDown('slow', function(){
                    showText(nextNo);
                });
            }
            else if (sliderType == 2) {
                hideText(currNo);
                $(items[currNo]).hide('slow');
                $(items[nextNo]).show('slow', function(){
                    showText(nextNo);
                });
            }
            else if (sliderType == 3) {
                hideText(currNo);
                $(items[nextNo]).fadeIn('slow', function(){
                    showText(nextNo);
                });
                $(items[currNo]).fadeOut('slow');
	        	
            }
            else if (sliderType==4)  {
                hideText(currNo);
                $(items[nextNo]).appendTo($(divId + ' .sliderContent')).show();
                
                //$(items[currNo]).hide(sliderType, 'slow');
                //console.debug(firstTime);
                if (firstTime)
                	$(items[currNo]).show();
                	
                $(items[currNo]).animate({
                    width: 'toggle'//, 
//                    opacity: 'toggle'
                }, "slow", function(){
                    showText(nextNo);
                });
 
            }
            else if (sliderType=='drop' || sliderType=='slide'/**/) {
            	var sliderType2 = sliderType=='drop' ? 'slide' : 'drop';
            	hideText(currNo);
                $(items[currNo]).hide(sliderType, 'slow');
                $(items[nextNo]).show(sliderType2, 'slow', function(){
                    showText(nextNo);
                });
            }
            else {
                hideText(currNo);
                if (sliderType == 'explode')
                	$(items[currNo]).hide();
                else	
                	$(items[currNo]).hide(sliderType, sliderType == 'fold' ? 'fast' : 'slow');
                $(items[nextNo]).show(sliderType, 'slow', function(){
                    showText(nextNo);
                });
            }
        	
            $(cItems[nextNo]).addClass('current');
            currNo = nextNo;
            current = items[nextNo];
			
        }
        
        var firstTime;
        var makeSlider = function() {
            if(mOver)
                return;
            animation_specific_css(sliderType);
            firstTime = (current != null) ? false : true;
            current = (current != null) ? current : items[(items.length-1)];
            if (currNo != null) {
                currNo = jQuery.inArray(current, items) + 1
                currNo = (currNo == items.length + 1) ? 0 : (currNo - 1);
            }
            else currNo = items.length-1;
            var nextNo = (currNo + 1 == items.length) ? 0 : currNo +1;
			
            //console.debug(currNo, nextNo);
        	
            changeImage(currNo, nextNo);
            nextTimeout();
            
        }
        
        var animation_specific_css = function(type) {
            if (type == '1') {
                $(divId + ' .sliderContent').css({
                    width: imgWidth + 'px',
                    height: (2*imgHeight)+ 'px',
                    'position': 'absolute'
                });
            }
            else if (type == '2') {
                $(divId + ' .sliderContent').css({
                    width: (2*imgWidth) + 'px',
                    height: imgHeight+ 'px',
                    'position': 'relative',
                    'float' : 'left'
                });
            }
            else if (type == '3') {
                $(divId + ' .sliderContent').css({
                    width: imgWidth + 'px',
                    height: imgHeight+ 'px',
                    'position': 'absolute'
                });
                
            }
            else if (type == '4') {
            	$(divId + ' .sliderImage').css({
                	'position' :'relative',
                	'float' : 'left',
                	'left': 'auto'
            	});
            	$(divId + ' .sliderContent').css({
                	'width':(imgWidth*3) + 'px',
                	'height':imgHeight + 'px',
                	'position': 'relative'
            	});
            }
            else /*if (type == 'slide' || type == 'drop')*/{
            	$(divId + ' .sliderImage').css({
                	'position' :'absolute',
                	'float' : 'none',
                	'left': '0',
                	'right': '0'
                });
            	$(divId + ' .sliderContent').css({
                	'width':imgWidth + 'px',
                	'height':imgHeight + 'px',
                	'position': 'absolute'
            	});
            }
        }
        
        makeSlider();

        /*############ CONFIG VALUES ########################################*/ 
        if (vars.admin != undefined) {
            $('input[id^=cfg_]').blur(function(e){
                apply_cfg(e);
            });
            $('textarea[id^=cfg_]').blur(function(e){
                apply_cfg(e);
            });
            /* ########################################*/ 
            $('select[id^=cfg_]').change(function(e){
                apply_cfg(e);
            });
            $('select[id^=cfg_]').blur(function(e){
                apply_cfg(e);
            });
        
            var apply_cfg = function(e) {
                var cfg = e.target.id.split('_');
                var v = $(e.target).val();
                if (cfg[1] == 'image') {
                    var nr = cfg[2] - 1;
                    if (cfg[3] == 'titel') {
                        $(items[nr]).find('span strong').html(v);
                        $(cItems[nr]).attr('title', v);
                    } 
                    else if (cfg[3] == 'text') {
                        var title = $(items[nr]).find('span strong').html();
                        $(items[nr]).find('span').html('<strong>' + title + '</strong><br />' + v);
                    }
                    else if (cfg[3] == 'link')
                        $(items[nr]).find('a').attr('href', v);
                    else if (cfg[3] == 'textstyle')
                        $(items[nr]).find('span').attr('class', v);	
                }
                else if (cfg[1] == "sliderController") {
                    if (cfg[2] == 'width')
                        $(divId + ' .sliderController img').css('width', v + 'px');
                    else 
                        $(divId + ' .div.sliderController').css('margin-' + cfg[2], v + 'px');
                }
                else if (cfg[1] == 'sliderImage') {
                    $('.' + cfg[1] + ' span').css(cfg[2], v);
                /*if (cfg[2] == 'opacity') {
    				$('.' + cfg[1])
    					.css('-moz-opacity' , v)
    					.css('-khtml-opacity' , v)
    					.css('filter', 'alpha(opacity=' + (parseInt(v*100)) +')');
    			} */
                }
                else if (cfg[1] == 'stil1' || cfg[1] == 'stil2') {
                    if (cfg[2] == 'width' || cfg[2] == 'height') {
                        $('.' + cfg[1]).css(cfg[2], v+ 'px');
                    }	
                    else if (cfg[2].substr(0,4) == 'vert') {
                        $('.' + cfg[1])
                        .css('top', 'auto')
                        .css('bottom', 'auto')
                        .css($('#cfg_' + cfg[1] + '_vertical').val(), $('#cfg_' + cfg[1] + '_verticalval').val());
                    }
                    else if (cfg[2].substr(0,5) == 'horiz'){
                        $('.' + cfg[1])
                        .css('left', 'auto')
                        .css('right', 'auto')
                        .css($('#cfg_' + cfg[1] + '_horizontal').val(), $('#cfg_' + cfg[1] + '_horizontalval').val());
                    }
                }
                else if (cfg[1] == 'animation') {
                    if (cfg[2] == 'timeOut')
                        timeOut = v;
                    else if (cfg[2] == 'text')
                        textAnimation = v;
                    else if (cfg[2] == 'big') {
                        sliderType = v;
                        animation_specific_css(sliderType);
                        
                        
                    }
                }
            }
        }
    };  

})(jQuery);  

