(function($) {

$.extend({
			arg: null,
			cnt: 0,
			sett: {'fade': {
							fx: 'fade',
							timeout:       200,
							speed: 300,
							continuous: false,
							autostop:      1
						}
				},
			ops : [
				[".demo .final:first", {opacity:0, width: "75px", height: "75px", top: "128px", "left": "332px"},
					{
						duration: 1200,
						easing: "bounceout",
						complete: function () {
							$(".demo").trigger("_next");
						}
					}
				],
				[".demo .final:last", {opacity:0},
					{
						duration: 600,
						complete: function () {
							$(".demo").trigger("_next");
						}
					}
				],
				[".demo .text:eq(0)", {right: "40px"},
					{
						duration: 1400,
						easing: "bounceout",
						complete: function () {
							$(".demo").trigger("_next");
						}
					}
				],1600,
				[".demo .text:eq(0)", {opacity: 0, "font-size": "60px", right: "80px"},
					{
						duration: 700,
						easing: "linear",
						complete: function () {
							$(".demo").trigger("_next");
						}
					}
				]
			]
		});
		$.extend({
			aChain: {
				cnt: 0,
				stack: $.ops,
				_pause: function (t) {
					return [".left", {opacity: 1}, {
							duration: t,
							complete: function() {
								$(".demo").trigger("_next");
							}
						}
					];
				},
				_next: function () {
					if(typeof console != "undefined") console.log('>>>'+$.aChain.cnt);
					if(typeof $.aChain.stack[$.aChain.cnt] != "undefined") {
						var o = $.aChain.stack[$.aChain.cnt];
						if(typeof o == "number") o = $.aChain._pause(o);
						if(typeof console != "undefined") console.log('next2'+'='+o[0]);
						//
						$.aChain.cnt++;
						$(o[0]).show().animate(o[1], o[2]);
						//$(".demo").trigger("_next");
						if(typeof console != "undefined") console.log('next3');
					} else {
						$.cnt = 0;$.aChain.cnt = 0;$.arg = null;
						if(typeof console != "undefined") console.log('next4');
						$(".demo").stopShow().slideShow($.sett["fade"]);
					}
				}
			}
		});

		$.fn.extend({
			stopShow: function () {
				clearTimeout(this.cycleTimeout);
				return this;
			},
			slideShow: function (arg) {
				$('img', this).show().css({
					'top': '0px', 'left': '0px', 'width': '490px', 'height': '490px', 'opacity': 1
				});
				var $this = $(this);
				var o = $.extend({}, $.sett[arg]);
				o = {
							fx: 'fade',
							timeout:       200,
							speed: 300,
							continuous: false,
							autostop:      1
						};
				o.end = function (o) {$this.stopShow().slideShow(arg);}
				$.cnt++; o.slideExpr = ".a"+$.cnt;//alert(o.slideExpr);
				if(typeof console != "undefined") console.log(o.slideExpr);
				if($(o.slideExpr, this).length == 2) {
					if(typeof console != "undefined") console.log("go");
					$('img', this).not(o.slideExpr).hide();//alert(1);
					//var str = '';for (var i in o) str+=i+'='{alert(1);+o[i]+'\n';alert(str);
					return $(this).cycle(o);
				} else {if(typeof console != "undefined") console.log("final");
					$(":not(.final)", this).hide();
					if(typeof console != "undefined") console.log('!!!');
					//$(".demo").trigger("_next");
					$(".demo .final:first").animate(
					{opacity:0, width: "75px", height: "75px", top: "128px", "left": "332px"},
					{
						duration: 1200,
						easing: "bounceout",
						complete: function () {
							//$(".demo").trigger("_next");
						}
					});
					setTimeout(function () {
						$(".final:last").animate({opacity:0},
							{
								duration: 600,
								complete: function () {
									//$(".demo").trigger("_next");
									$(".text").show();
								}
							});
					}, 1500);
					setTimeout(function () {
						$(".text").eq(0).fadeTo(1,1).animate({right: "80px"},
							{
								duration: 1400,
								easing: "bounceout",
								complete: function () {
									//$(".demo").trigger("_next");
								}
							});
					}, 2500);
					setTimeout(function () {
						$(".text").eq(1).fadeTo(1,1).animate({left: "80px"},
							{
								duration: 1400,
								easing: "bounceout",
								complete: function () {
									//$(".demo").trigger("_next");
								}
							});
					}, 3500);
					setTimeout(function () {
						$(".text").animate({opacity: 0, "font-size": "60px", right: "80px"},
							{
								duration: 700,
								easing: "linear",
								complete: function () {
									//$(".demo").trigger("_next");
								}
							});
					}, 5500);
				}
				setTimeout(function () {
					$(".demo .logo img").show();
					$(".demo .logo").show().animate({left:"40px"}, {duration:600, easing: "backout"});
				},6000);


				setTimeout(function () {
					$(".demo .logo").animate({left:"-380px"}, {duration:200, easing: "backinout"});
				}, 7500);

				setTimeout(function () {
					$.cnt = 0;
					$(".demo .a1:first").fadeTo(1,0, function () {$(this).show();}).animate({opacity:1}, {duration:600, complete: function () {
						$(".text").eq(0).css({right: "540px"}).show();
						$(".text").eq(1).css({left: "520px"}).show();
						$(".demo").slideShow(arg);
					}})

				}, 8500);
				return $(this);
			}
		});
		$(function () {//alert($.fn.cycle);
			//$("#sq").animate();
			$(".demo").bind("_next", function () {if(typeof console != "undefined") console.log('next');
				$.aChain._next();
			});
			//$('.left a').click(function () {
				$('.demo').stopShow().slideShow('slide')
				return false;
			//}).click();
		});
		
})(jQuery);
