/*
 * QP Carrusel
 * 
 * (c) 2011 QP Image Builders, LLC.
 */
$(function() {

	function dlog(ll) {
		//if (window.console)
		//window.console.info(ll);
	}

	$(".frm-carrusel").each(
			function() {
				$(this).data(
						"carrusel",
						{
							opt : {
								fw : 800, // frame width
								fh : 360, // frame height
								rt : -1, // right position
								dt : 0, // left position 
								tp : 60, // front top position
								lt : -1, // from left poistion
								ht : 300, // front height
								wh : 300, // front width
								op : 1, // max opacity
								zi : 99, // max z-index
								ri : 2, // shrinkratio
								ln : 0, // number of objects 
								md : 0
							},
							self : $(this),
							container : $(this).parent().parent(),
							sto : null,
							paused : false,
							init : function(x) {
								this.opt = x;
								this.opt.ln = this.self.children("img").length;
								this.opt.md = (this.opt.ln - 1) / 2.;
								if (x.rt < 0) {
									this.opt.rt = x.fw - x.dt - (x.wh / x.ri);
								}
								this.opt.dt = (this.opt.rt - x.dt) / (this.opt.ln - 2);
								if (x.lt < 0) {
									this.opt.lt = (x.fw - x.wh) / 2;
								}
								dlog("init " + this.opt.ln + "," + this.opt.md + ","
										+ this.opt.rt + "," + this.opt.dt);
								this.self.parent().css("max-width", this.opt.fw).css(
										"max-height", this.opt.fh);
							},
							pause : function() {
								dlog("pause " + this.container.attr("id"));
								this.paused = true;
							},
							resume : function() {
								dlog("resume" + this.container.attr("id"));
								this.paused = false;
							},
							play : function() {
								dlog("play" + this.container.attr("id"));
								clearInterval(this.sto);
								this.paused = false;
								this.next();
							},
							stop : function() {
								dlog("stop" + this.container.attr("id"));
								clearInterval(this.sto);
								this.paused = true;
								this.self.children("img").css("max-width", this.opt.wh).css(
										"max-height", this.opt.ht).css("top", "0").css("left",
										"0");
							},
							next : function() {
								if (this.paused == true
										|| this.container.css("display") == "none") {
									return;
								}
								dlog("next: " + this.container.attr("id"));
								var rt = this.opt.rt; //620; 
								var tp = this.opt.tp; //100;
								var lt = this.opt.lt; //250;
								var ht = this.opt.ht; //300;
								var wh = this.opt.wh; //300;
								var op = this.opt.op; //1;
								var zi = this.opt.zi; //99;
								var ll = this.self.children("img").last();
								ll.prependTo(this.self); // make the last the first!
								var cc = this.self.children("img").first();
								cc.css("z-index", 100);
								dlog("bye bye " + $(".frm-item-text:visible").attr("id"));
								$(".frm-item-text:visible").animate( {
									opacity : 'hide'
								}, 'slow', function() {
								});
								for (ii = 0; ii < this.opt.ln; ii++) {
									dlog(this.container.attr("id") + ":" + cc.attr("id") + ","
											+ this.opt.dt + "," + rt + "," + tp + "," + lt
											+ "," + ht + "," + wh + "," + op + "," + zi + ","
											+ this.opt.md);
									fst = "#" + cc.attr("id") + "-txt";
									cc.animate( {
										top : tp,
										left : lt,
										maxHeight : ht,
										maxWidth : wh,
										opacity : op
									}, 2000,
											function() {
												fst = "#" + $(this).attr("id") + "-txt";
												if ($(this).index() == 0) {
													dlog("helloooooo " + $(this).attr("id")
															+ ":" + fst);
													$(fst).animate( {
														opacity : 'show'
													}, 'slow', function() {
													});
												}
											});
									cc = cc.next();
									lt = rt;
									if (ii < this.opt.md) {
										tp /= this.opt.ri;
										ht /= this.opt.ri;
										wh /= this.opt.ri;
										//lt -= wh / 2;
										op = .90 / (ii + 1);
										cc.css("z-index", zi--);
									} else if (ii > this.opt.md) {
										tp *= this.opt.ri;
										ht *= this.opt.ri;
										wh *= this.opt.ri;
										//lt += wh / 2;
										op = .90 / (this.opt.ln - ii - 1);
										cc.css("z-index", zi++);
									} else {
									}
									rt -= this.opt.dt;
								}
							}
						});
			});

	window.sto = setInterval(function() {
		dlog("interval!");
		$(".frm-carrusel").each(function() {
			$(this).data("carrusel").next();
		});
	}, 8000);

	$(".carrusel-play").click(function() {
		if ($(".carrusel-playing").length > 0) {
			// stop carrusel first
			return;
		}
		tid = $(this).attr("id");
		cid = "#" + tid.split("-play", 1); /*dlog(cid);dlog($(cid).attr("id"));*/
		if ($(cid).css("display") == "none") {
			$(this).addClass("carrusel-playing");
			$(cid).show(function() {
				$(".show-n-tell").each(function() {
					$(this).scrollable().stop();
				});
				$(this).find(".frm-carrusel").data("carrusel").play();
			});
		} else {
			$(this).removeClass("carrusel-playing");
			$(cid).find(".frm-carrusel").data("carrusel").stop();
			$(cid).hide(function() {
			});
		}
	});

	$(".frm-close").click(function() {
		$(".carrusel-playing").removeClass("carrusel-playing");
		$(this).next(".frm-carrusel").data("carrusel").stop();
		$(this).parent().parent().hide(function() {
		});
		$(".show-n-tell").each(function() {
			$(this).scrollable().play();
		});
	});

	$(".frm-carrusel >img").hover(function(evt) {
		evt.preventDefault();
		$(this).parent().data("carrusel").pause();
	}, function(evt) {
		evt.preventDefault();
		$(this).parent().data("carrusel").resume();
	});

	$("#services-carrusel").find(".frm-carrusel").data("carrusel").init( {
		fw : 800, // frame width
		fh : 360, // frame height
		rt : -1, // right position
		dt : 30, // left position 
		tp : 100, // front top position
		lt : -1, // from left poistion
		ht : 300, // front height
		wh : 300, // front width
		op : 1, // max opacity
		zi : 99, // max z-index
		ri : 2, // shrinkratio
		ln : 0, // number of objects 
		md : 0
	});
	$(".frm-carrusel", "#portfolio").data("carrusel").init( {
		fw : 900,
		fh : 600,
		rt : -1,
		dt : 8,
		tp : 126,
		lt : -1,
		ht : 446,
		wh : 526,
		op : 1,
		zi : 99,
		ri : 1.75,
		ln : 0,
		md : 0
	});
});
