/** * AsyncSlider * JQuery plugin * * Created by: Arlind Nushi * Author email: arlindd@gmail.com * * Last Update: August 16, 2011 */ (function($){ // Container var ct; var p = this; var rotations_count = 0; // Callback Functions var callback = function(){}; // Background Mask var bg_mask; var bg_mask_2; // Slides var slides = []; // Default Options var min_time = 500; var max_time = 1000; var direction = 'horizontal'; var easing = 'easeInOutSine'; // Default var easing_in = ''; var easing_out = ''; var keyboard_navigate = false; var prev_next_nav = true; var center_prev_next_nav = true; var prev_next_nav_margin = 5; var slides_nav = true; var random = true; /* Version 1.1 */ var autoswitch = false; var autoswitch_interval = 5000; // Default var autoswitch_interval_func = null; // Other Vars var current_index = 0; var total_slides = 0; var timeout; var horizontal_direction = ''; var vertical_direction = ''; var base_zindex = 1000; var busy = false; var _next_a, _prev_a; var slides_nav_el; // Slider Prepare function sliderPrepare(el, options) { if( typeof ct == 'undefined' ) { // Define Container ct = $(el); // Set Container Optioms ct.css({position: 'relative'}); // Process Options if( typeof options == 'object' ) { // Define Direction if( typeof options.direction == 'string' ) { if( options.direction.toLowerCase() == 'vertical' ) { direction = 'vertical'; } else { direction = 'horizontal'; } } // Random Time if( typeof options.random != 'undefined' ) { random = options.random; } // Timing Setup if( typeof options.minTime != 'undefined' ) { min_time = options.minTime; } if( typeof options.maxTime != 'undefined' ) { max_time = options.maxTime; } // Easing Setup if( typeof options.easing == 'string' ) { easing = options.easing; } // Easing IN if( typeof options.easingIn == 'string' ) { easing_in = options.easingIn; } // Easing OUT if( typeof options.easingOut == 'string' ) { easing_out = options.easingOut; } // Keyboard navigation if( typeof options.keyboardNavigate != 'undefined' ) { keyboard_navigate = options.keyboardNavigate ? true : false; } // Add Prev Next Navigation if( typeof options.prevNextNav != 'undefined' ) { prev_next_nav = options.prevNextNav; } // Center the Prev Next Nav if( typeof options.centerPrevNextNav != 'undefined' ) { center_prev_next_nav = true; prev_next_nav_margin = parseInt(options.centerPrevNextNav, 10); } // Add Slide Buttons Navigation if( typeof options.slidesNav != 'undefined' ) { slides_nav = options.slidesNav; } /* Version 1.1 */ // Auto Switch Feature if( typeof options.autoswitch == 'number' && options.autoswitch > 0 ) { autoswitch = true; autoswitch_interval = options.autoswitch; _autoSwitch("on"); } } else { options = {}; } // Parse Slider Items ct.children().each(function(i) { var slide = $(this); var slide_elements = slide.find('*'); slide.hide().css({zIndex: base_zindex+10, position: 'relative'}); // Get Slide Properties slide.height = slide.outerHeight() > slide.height() ? slide.outerHeight() : slide.height(); slide.width = slide.outerWidth(); slide.background = false; // Set Slide Background var sbg = ""; var _bg_color = slide.css("background-color"); var _bg_image = slide.css("background-image"); var _bg_repeat = slide.css("background-repeat"); var _bg_position = slide.css("background-position"); var _bg_property = _bg_color + ' ' + (_bg_image == 'none' ? '' : _bg_image) + ' ' + _bg_position + ' ' + _bg_repeat; if( _bg_image != 'none' || _bg_color ) { sbg = _bg_property; slide.css("background", "none"); } // Background by Markup Attribute if( sbg || slide.attr("data-background") ) { slide.background = sbg; if( slide.attr("data-background") ) { slide.background = slide.attr("data-background"); } // Check if there image, then preload it var img_url = []; img_url = slide.background.match(/url\((.*?)\)/); if( img_url && img_url.length > 0 ) { // Preload Background Image var img = new Image(); img.src = img_url[1]; } } // Slide Sub Elements slide.elements = []; slide_elements.each(function() { var $this = $(this); var def_left = parseInt($this.css("left"), 10); var def_top = parseInt($this.css("top"), 10); $this.def_left = 0; $this.def_top = 0; if( def_left ) { $this.def_left = def_left; } if( def_top ) { $this.def_top = def_top; } if( $this.css('position') != 'absolute' ) { $this.css({ position: 'relative' }); } $this.css({zIndex: base_zindex + 10}); // Skip Break Line Elements if( $this.get(0).tagName != "BR" ) { slide.elements.push( $this ); } }); slide.index = parseInt(i, 10); slides.push(slide); // First Slide if( i === 0 ) { slide.show(); ct.height(slide.height).css({overflow: 'hidden'}); // Background Mask bg_mask = $('
'); bg_mask_2 = $('
'); slide.before(bg_mask); slide.before(bg_mask_2); bg_mask.css({position: 'absolute', top: 0, left: 0, zIndex: base_zindex+1, padding: 0, margin: 0, width: ct.outerWidth(), height: ct.outerHeight()}); bg_mask_2.css({position: 'absolute', top: 0, left: 0, zIndex: base_zindex+2, padding: 0, margin: 0, width: ct.outerWidth(), height: ct.outerHeight()}); $(window).resize(function() { bg_mask.width( ct.outerWidth() ); bg_mask_2.width( ct.outerWidth() ); }); // Set Slide BG (if have) if( slide.background ) { bg_mask.css("background", slide.background); } } }); // Get Total Number of Slide Items total_slides = slides.length; // First Slide (by User) if( typeof options != 'undefined' && typeof options.firstSlide != 'undefined' ) { var fs_index = options.firstSlide - 1; if( fs_index >= 0 && fs_index < total_slides ) { var tmp_min_time = min_time, tmp_max_time = max_time, tmp_callback = callback; min_time = max_time = 0; callback = function(){ }; current_index = fs_index; switchSlide(current_index, 0); min_time = tmp_min_time; max_time = tmp_max_time; callback = tmp_callback; } } // Terminate if there is 0 or 1 item in slider if( ct.children().length < 2 ) { return; } // Add Keyboard Arrows Navigation (or not) $(window).keydown(function(e){ if( keyboard_navigate ) { switch( e.keyCode ) { case 37: // Left - previous prevSlide(); break; case 39: // Right - next nextSlide(); break; } } }); // Add Previous Next Slide Navigation if( prev_next_nav && total_slides > 1 ) { var next_prev_nav_el = $('