/**
 * PortalManager
 */
protouch.portal = function () {

	if ($('#portal').length > 0 && !this.initialized.portal) {

		if (typeof(portalflash) == "undefined") {
			var portalflash = protouch.settings.portalflash;
		}

		try {
			var playerVersion = swfobject.getFlashPlayerVersion();
			if (playerVersion.major >= 9) {
				var settings = {
					trikots : "/static/img/portal",
					formaction : "/?action=test"
				};

				swfobject.embedSWF(portalflash, "portalflash", "482", "380", "9.0.0", "expressInstall.swf", settings , protouch.settings.flashparams, {});
			}
			delete(settings);
		}
		catch (e) {

		}

		if ($('#products .container .slider .item').length > 1) {
			protouch.vars.portalcount = $('#products .container .slider .item').length;
			$('#products .container .slider').css({width: (this.settings.portalsliderwidth * protouch.vars.portalcount) + "px"  });
			$('#products .right').addClass("active");

			var i = 0;
			$('#products .tabs ul li').each(function(i) {
				if (i == 0) {
					$(this).addClass("active");
				}
				this.categoryindex = i;
				i ++;
				$(this).click ( function() {
					$('#products .tabs ul li').removeClass("active");
					$(this).addClass("active");

					protouch.vars.categoryactive = this.categoryindex;

					if (this.categoryindex == 0) {
						$('#products .left').removeClass("active");
					}
					else {
						$('#products .left').addClass("active");
					}
					if (this.categoryindex == protouch.vars.categorygroups - 1) {
						$('#products .right').removeClass("active");
					}
					else {
						$('#products .right').addClass("active");
					}
					var leftvar = (this.categoryindex)* (- protouch.settings.portalsliderwidth);
					$('#products .container .slider').animate({left: leftvar+"px"});
				});
			});
			$('#products .left').click ( function() {
				if (protouch.vars.categoryactive == 0) {
					return;
				}
				protouch.vars.categoryactive --;
				$('#products .right').addClass("active");
				$('#products .tabs ul li').removeClass("active");
				$($('#products .tabs ul li')[protouch.vars.categoryactive]).addClass("active");


				var leftvar = (protouch.vars.categoryactive)* (- protouch.settings.portalsliderwidth);
				/* new width */
				$('#products .container .slider').animate({left: leftvar+"px"},function () {
					if (protouch.vars.categoryactive == 0) {
						$('#products .left').removeClass("active");
					}
				});

			});
			$('#products .right').click ( function() {
				if (protouch.vars.categoryactive == protouch.vars.portalcount - 1) {
					return;
				}
				protouch.vars.categoryactive ++;
				$('#products .left').addClass("active");
				$('#products .tabs ul li').removeClass("active");
				$($('#products .tabs ul li')[protouch.vars.categoryactive]).addClass("active");
				/* new width */
				var leftvar = (protouch.vars.categoryactive)* (- protouch.settings.portalsliderwidth);
				$('#products .container .slider').animate({left: leftvar+"px"},function () {
					if (protouch.vars.categoryactive == protouch.vars.portalcount - 1) {
						$('#products .right').removeClass("active");
					}
				});

			});
		}
		if ($("#portaldisquieter .box").length > 1) {
			$($("#portaldisquieter .box")[0]).fadeIn (200,function () {
				var portaldisquieter = window.setInterval(function() {

					$($("#portaldisquieter .box")[protouch.vars.portaldisquieter]).fadeOut(300,function() {
						protouch.vars.portaldisquieter ++;
						if (protouch.vars.portaldisquieter == $("#portaldisquieter .box").length) {
							protouch.vars.portaldisquieter = 0;
						}

						$($("#portaldisquieter .box")[protouch.vars.portaldisquieter]).fadeIn(200);

					});

					},6000);
			});
		}

		this.initialized.portal = true;
	}

};

