﻿/*
copy right: liuyouji@163.com
date: 2010-08-28
*/

(function($) {

	$.PixViewerSet = function(p) {
		return $.extend({
			autofixed: true, // 是否自适应尺寸?
			width: 250,
			height: 200,
			numberheight: 20,
			titleheight: 20,
			speed: 500,
			setImgH: true,
			setImgW: false,
			data: null
		}, p);
	};

	$.PixViewerStart = function(spix) {
		var pix = $(spix);
		var tmr = pix.data('tmr_toggle');
		if (tmr != null) window.clearInterval(tmr);
		tmr = window.setInterval($.PixViewerAutoPlay(spix), 2000);
		pix.data('tmr_toggle', tmr);
	};

	$.PixViewerStop = function(spix) {
		var pix = $(spix);
		var tmr = pix.data('tmr_toggle');
		if (tmr != null) window.clearInterval(tmr);
		tmr = null;
		pix.data('tmr_toggle', tmr);
	};

	$.PixViewerAutoPlay = function(spix) {
		return function() {
			var pix = $(spix);
			var i, n;
			i = pix.data('idx_last') + 1;
			n = pix.data('idx_count');
			if (i >= n) i = 0;
			//alert(pix.html() + ':1' + i); ////
			//document.title = spix + ' : ' + i;
			$.PixViewerPlay(spix, i)();
		}
	};

	$.PixViewerPlay = function(spix, i) {
		return function() {
			var pix = $(spix);
			var lastidx, speed;
			var obj;
			//alert(pix.html() + ':2' + i); ////
			lastidx = pix.data('idx_last');
			speed = pix.data('speed');
			if (lastidx == i) return;
			//document.title = lastidx + ' >> ' + i;

			// 标题
			pix.find('.Titles a').css('display', 'none');
			pix.find('.Titles a:eq(' + i + ')').css('display', '');

			// 序号
			pix.find('.Numbers a').removeClass('selected');
			pix.find('.Numbers a:eq(' + i + ')').addClass('selected');

			// 图片
			pix.find('.Pictures em').css('z-index', 1);

			obj = pix.find('.Pictures em:eq(' + lastidx + ')');
			obj.css('z-index', 2);

			obj = pix.find('.Pictures em:eq(' + i + ')');
			obj.hide();
			obj.css('z-index', 3);

			// 动画
			var r = Math.random();
			if (r < 0.33)
				obj.fadeIn(speed);
			else if (r < 0.66)
				obj.slideDown(speed);
			else
				obj.show(speed);

			pix.data('idx_last', i);
		}
	};

	$.PixViewerSize = function(spix, p) {
		var pix = $(spix);
		if (p.autofixed) {
			p.width = pix.width();
			p.height = pix.height();
		}
		// set width and height
		pix.width(p.width);
		pix.children().width(p.width);
		pix.find('.Pictures em').width(p.width);
		if (p.setImgW) pix.find('.Pictures em img').width(p.width); //
		pix.find('.Titles em').width(p.width - 4);

		pix.height(p.height);
		pix.find('.Pictures').height(p.height - p.titleheight);
		pix.find('.Pictures em').height(p.height - p.titleheight);
		if (p.setImgH) pix.find('.Pictures em img').height(p.height - p.titleheight); //
		pix.find('.Numbers').height(p.numberheight);
		pix.find('.Titles').height(p.titleheight);

		var of = pix.offset();
		//document.title = 'of.left=' + of.left + ' of.top=' + of.top;
		// pix.find('.Numbers').offset({ left: (of.left + 2), top: (of.top + 2 + p.height - p.titleheight - p.numberheight) });
		pix.find('.Numbers').css('top', (of.top + 1 + p.height - p.titleheight - p.numberheight));
		pix.find('.Numbers').css('left', (of.left + 1));
		pix.find('.Focus').css('top', (of.top - 1));
		pix.find('.Focus').css('left', (of.left - 1));

		pix.find('.Numbers').css('display', 'block');
		pix.find('.Focus').css('display', 'block');
		pix.find('.Titles').css('display', 'block');
		//of = pix.find('.Numbers').offset();
		//document.title = 'of.left=' + of.left + ' of.top=' + of.top;

	};

	$.PixViewerHTML = function(spix, p) {
		var sNums = '', sTtls = '', sPics = '';
		if (p.data == null) return;
		var i, n;
		n = p.data.length;
		for (i = 0; i < n; i++) {
			sNums += '<em><a href="javascript:void(0);">' + (i + 1) + '</a></em>';
			sTtls += '<em><a href="' + p.data[i].url + '" target="' + p.data[i].target + '" title="' + p.data[i].title + '">' + p.data[i].title + '</a></em>';
			sPics += '<em><a href="' + p.data[i].url + '" target="' + p.data[i].target + '"><img src="' + p.data[i].imgUrl + '"/></a></em>';
		}
		$(spix).find('.Numbers').html(sNums);
		$(spix).find('.Titles').html(sTtls);
		$(spix).find('.Pictures').html(sPics);
	};
	$.fn.extend({
		lyjPixViewer: function(p) {
			var spix = this;
			// begin
			p = $.PixViewerSet(p);

			// set global parameters
			$(spix).data('idx_last', 0);
			$(spix).data('speed', p.speed);
			$(spix).data('tmr_toggle', null);

			var n = 0;
			if (p.data) n = p.data.length;
			$(spix).data('idx_count', n); //
			$(spix).data('idx_last', n - 1); // reset for first show

			// 更新HTML
			$.PixViewerHTML(spix, p);

			// 尺寸
			$.PixViewerSize(spix, p);

			// 事件处理
			$(spix).find('.Numbers em a').each(function(i) {
				$(this).click($.PixViewerPlay(spix, i));
			});

			$(spix).find('.Numbers').hover(
				function() { $.PixViewerStop(spix); },
				function() { $.PixViewerStart(spix); }
			);

			// 窗口尺寸
			$(window).resize(
				function() { $.PixViewerSize(spix, p); }
			);

			$.PixViewerAutoPlay(spix)();
			$.PixViewerStart(spix);
		}
	});
})(jQuery);
