Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7272 amit.gupta 1
/**
2
 * jQuery Slidy - A Transition Plugin - http://wbotelhos.com/slidy
3
 * ---------------------------------------------------------------------------------
4
 *
5
 * jQuery Slidy is a plugin that generates a customizable transitions automatically.
6
 *
7
 * Licensed under The MIT License
8
 *
9
 * @version         0.2.0
10
 * @since           11.16.2010
11
 * @author          Washington Botelho dos Santos
12
 * @documentation   wbotelhos.com/slidy
13
 * @twitter         twitter.com/wbotelhos
14
 * @license         opensource.org/licenses/mit-license.php MIT
15
 * @package         jQuery Plugins
16
 *
17
 * Usage with default values:
18
 * ---------------------------------------------------------------------------------
19
 * $('#banner').slidy();
20
 *
21
 * <div id="banner">
22
 *   <img src="image-1.jpg"/>
23
 *   <img src="image-2.jpg"/>
24
 *   <img src="image-3.jpg"/>
25
 * </div>
26
 *
27
 */
28
 
29
;(function($) {
30
 
31
	$.fn.slidy = function(settings) {
32
 
33
		if (this.length == 0) {
34
			debug('Selector invalid or missing!');
35
			return;
36
		} else if (this.length > 1) {
37
			return this.each(function() {
38
				$.fn.slidy.apply($(this), [settings]);
39
			});
40
		}
41
 
42
		var opt			= $.extend({}, $.fn.slidy.defaults, settings),
43
			$this		= $(this),
44
			id			= this.attr('id'),
45
			elements	= $this.children(opt.children),
46
			quantity	= elements.length,
47
			images		= (opt.children == 'img') ? elements : elements.find('img'),
48
			timer		= 0,
49
			isAnimate	= false;
50
 
51
		if (id === undefined) {
52
			id = 'slidy-' + $this.index();
53
			$this.attr('id', id); 
54
		}
55
 
56
		$this
57
		.data('options', opt)
58
		.css({
59
			'cursor':	opt.cursor,
60
			'height':	opt.height + 'px',
61
			'overflow':	'hidden',
62
			'position':	'relative',
63
			'width':	opt.width + 'px'
64
		});
65
 
66
		elements.each(function(i) {
67
			$(this)
68
			.css({ 'position': 'absolute', 'z-index': quantity - i })
69
			.attr('id', $this.attr('id') + '-' + (i + 1));
70
		});
71
 
72
		images.attr({ height: opt.height, width: opt.width }).css('border', '0');
73
 
74
		if (opt.children == 'a' && opt.target != '') {
75
			elements.attr('target',	opt.target);
76
		}
77
 
78
		elements.hide().first().show();
79
 
80
		if (opt.menu) {
81
			$menu = $('<ul/>', { id: id + '-slidy-menu', 'class': 'slidy-menu' }).insertAfter($this);
82
		}
83
 
84
		var stop = function() {
85
			clearTimeout(timer);
86
		}, overBanner = function() {
87
			stop();
88
		}, overMenu = function(thiz) {
89
			stop();
90
 
91
			var $this		= $(this),
92
				index		= $this.index(),
93
				$current	= $this.parent().children('.slidy-link-selected'),
94
				last		= $current.index();
95
 
96
			if (index != last) {
97
				$current.removeClass('slidy-link-selected');
98
				$this.addClass('slidy-link-selected');
99
 
100
				change(last, index);
101
			}
102
		}, outBanner = function(thiz) {
103
			go($(thiz.target).parent('a').index());
104
		}, outMenu = function() {
105
			var $this		= $(this),
106
				index		= $this.index(),
107
				$current	= $this.parent().children('.slidy-link-selected'),
108
				last		= $current.index();
109
 
110
			go(last);
111
		}, clickMenu = function(thiz) {
112
			stop();
113
 
114
			var $this		= $(this),
115
				index		= $this.index(),
116
				$current	= $this.parent().children('.slidy-link-selected'),
117
				last		= $current.index();
118
 
119
			if (index != last) {
120
				$current.removeClass('slidy-link-selected');
121
				$this.addClass('slidy-link-selected');
122
			}
123
 
124
			go(index);
125
		};
126
 
127
		if (opt.menu) {
128
			var target	= (opt.target != '') ? 'target="' + opt.target + '"' : '',
129
				menu	= '',
130
				parent,
131
				img;
132
 
133
			images.each(function() {
134
				img		= $(this);
135
				parent	= img.parent(opt.children);
136
 
137
				menu += '<li><a href="' + parent.attr(parent.is('a') ? 'href' : 'title') + '" ' + target + '>' + img.attr('title') + '</a></li>';
138
			});
139
 
140
			$menu.html(menu);
141
 
142
			var	space	= parseInt((opt.width / quantity) + (quantity - 1)),
143
				diff	= opt.width - (space * quantity),
144
				links	= $menu.children('li');
145
 
146
			if (opt.action == 'mouseenter') {
147
				links.mouseenter(overMenu).mouseleave(outMenu);	
148
			} else if (opt.action == 'click') {
149
				links.click(clickMenu);
150
			} else {
151
				debug('action attribute must to be "click" or "mouseenter"!');
152
				return;
153
			}
154
 
155
			links.css('width', space)
156
				.first().addClass('slidy-link-selected')
157
			.end()
158
				.last().css({ 'border-right': '0', 'width': (space + diff) - (quantity - 1) });
159
 
160
			if (opt.animation == 'slide' || opt.animation == 'fade') {
161
				links.mousemove(function() {
162
					var $this = $(this);
163
 
164
					if (!$this.hasClass('slidy-link-selected')) {
165
						$this.mouseenter();
166
					}
167
				});
168
			}
169
		}
170
 
171
		go(0);
172
 
173
		if (opt.pause) {
174
			$this.mouseenter(overBanner).mouseleave(outBanner);
175
		}
176
 
177
		function go(index) {
178
			var total	= quantity - 1,
179
				last	= null;
180
 
181
			if (index > total) {
182
				index = 0;
183
				last = total;
184
			} else if (index <= 0) {
185
				index = 0;
186
				last = total;
187
			} else {
188
				last = index - 1;
189
			}
190
 
191
			change(last, index);
192
 
193
			timer =	setTimeout(function() {
194
					go(++index);
195
				}, opt.time);
196
		}
197
 
198
		function change(last, index) {
199
			if (!isAnimate) {
200
				isAnimate = true;
201
 
202
				if (opt.animation == 'fade') {
203
					elements
204
						.eq(last).fadeOut(opt.speed)
205
					.end()
206
						.eq(index).fadeIn(opt.speed, function() {
207
							selectMenu(index);
208
							isAnimate = false;
209
						});
210
				} else if (opt.animation == 'slide') {
211
					elements.css('z-index', 0)
212
						.eq(index).css('z-index', quantity).slideDown(opt.speed, function() {
213
							elements.eq(last).hide();
214
							selectMenu(index);
215
							isAnimate = false;
216
						});
217
				} else {
218
					elements
219
					.eq(last).hide()
220
					.end()
221
					.eq(index).show(0, function() {
222
						selectMenu(index);
223
						isAnimate = false;
224
					});
225
				}
226
			}
227
		};
228
 
229
		function selectMenu(index) {
230
			if (opt.menu) {
231
				$this
232
					.next('ul.slidy-menu')
233
						.children().removeClass('slidy-link-selected')
234
							.eq(index).addClass('slidy-link-selected');
235
			}
236
		};
237
 
238
		return $this;
239
	};
240
 
241
	function debug(message) {
242
		if (window.console && window.console.log) {
243
			window.console.log(message);
244
		}
245
	};
246
 
247
	$.fn.slidy.defaults = {
248
		action:		'mouseenter',
249
		animation:	'fade',
250
		children:	'img',
251
		cursor:		'default',
252
		height:		200,
253
		menu:		false,
254
		pause:		false,
255
		speed:		600,
256
		target:		'',
257
		time:		3600,
258
		width:		710
259
	};
260
 
261
})(jQuery);