twyn.from = new Date().getTime();
twyn.counter = 0;
twyn.oflags = twyn.op ? "promo" : (twyn.pp ? "push" : (twyn.flags ? twyn.flags : "direct"));
twyn.block = false;

addthis_config = {
	ui_language : twyn.afp.lg,
	ui_header_color : "#333",
	services_compact : "favorites,email,facebook,delicious,twitter,myspace,digg,google,live,blogger,misterwong,print"
};

addthis_share = {
	templates : {
		twitter : '{{title}} - {{lurl}}'
	}
};

twyn.doblock = function() {
	twyn.block = true;
	window.setTimeout("twyn.block = false", 3000);
};

twyn.window = function(p, tracked) {
	window.open(tracked ? ("/redirect.jsv?redir=" + escape(p.p)) : (twyn.tr + p.t +
			(twyn.flags ? "&ac=" + twyn.flags : "") + "&redirect=" + escape(p.p)), "Promotion", "width=" +
			p.w +
			",height=" +
			p.h +
			(p.co ? ",scrollbars=" + (p.sb ? "yes" : "no") + ",location=yes,menubar=yes,resizable=yes,status=yes,toolbar=yes"
					: ""));
};

twyn.click = function(t) {
	if (twyn.ot) {
		jQuery("img.zpixel").append("<img src='" + twyn.tu.replace(/\[timestamp\]/, new Date().getTime()) + "'/>");
	}
	var id = t.parent("div.channel").attr("id");
	twyn.open(id);
	return false;
};

// fallback bis twynbox initialisiert ist
twyn.open = function(id) {
	twyn.counter++;
	var p = twyn.proms[id];
	if (typeof p !== 'undefined') {
		twyn.window(p);
	}
};

twyn.subpage = function(url, w, h, t) {
	window.open(url, t, "width=" + w + ",height=" + h +
			",scrollbars=yes,location=no,menubar=no,resizable=yes,status=yes,toolbar=no");
};

function initTwynbox() {
	try {
		if (typeof parent.jQuery.fn.twynbox !== 'undefined') {
			twyn.twynbox = function(p, tracked) {
				// alert("Promotion '"+p.wtl+"' " + p.t);
				try {
					parent.jQuery &&
							parent.jQuery.fn.twynbox( {
								iframe : true,
								innerWidth : p.w,
								innerHeight : p.h,
								title : p.wtl,
								href : tracked ? ("/redirect.jsv?redir=" + escape(p.p)) : (twyn.tr + p.t +
										(twyn.flags ? "&ac=" + twyn.flags : "") + "&redirect=" + escape(p.p)),
								onOpen : function() {
									parent.document.body.style.overflow = "hidden";
									twyn.cc && twyn.cc.trigger("mouseover");
								},
								onCleanup : function() {
									try {
										parent.jQuery("iframe[src*=tracknet]")[0].src = 'about:blank';
									} catch (e) {
									}
								},
								onClosed : function() {
									parent.document.body.style.overflow = "visible";
									twyn.cc && twyn.cc.trigger("mouseout");
								}
							});
				} catch (f) {
				}
			};

			twyn.subpage = function(url, w, h, t) {
				try {
					parent.jQuery && parent.jQuery.fn.twynbox( {
						iframe : true,
						innerWidth : w,
						innerHeight : h,
						title : t,
						href : url
					});
				} catch (f) {
				}
			};
		}
		// flash in den hintergrund
		parent.jQuery("embed").attr("wmode", "opaque");
	} catch (e) {
		twyn.twynbox = twyn.window;
	}

	if (typeof twyn.twynbox === 'undefined') {
		// fallback
		twyn.twynbox = twyn.window;
	}

	if (typeof twyn.subpage === 'undefined') {
		// fallback
		twyn.subpage = function(url, w, h, t) {
			window.open(url, t, "width=" + w + ",height=" + h +
					",scrollbars=yes,location=no,menubar=no,resizable=yes,status=yes,toolbar=no");
		};
	}

	// id, schon getracked (extern aufgerufen)
	twyn.open = function(id, tracked) {
		twyn.counter++;
		var p = twyn.proms[id];
		if (typeof p !== 'undefined') {
			// force overlay
			if (twyn.fo || (!twyn.afp.so && !p.so && p.w < twyn.viewWidth && p.h < twyn.viewHeight)) {
				twyn.twynbox(p, tracked);
			} else {
				twyn.window(p, tracked);
			}
		}
	};
}

function openTwynProms() {
	try {
		if (typeof twyn.op !== 'undefined') {
			// open promotion, schon getracked
			twyn.open(twyn.op, true);
		} else if (typeof twyn.pp !== 'undefined') {
			// push promotion, tracking
			twyn.open(twyn.pp);
		}
	} catch (e) {
	}
}

jQuery(document).ready(
		function() {

			if (!twyn.int) {
				jQuery(window).unload(
						function() {
							jQuery.get('/zone_stats.jsv?affiliate=' + twyn.afp.sc + '&duration=' +
									(new Date().getTime() - twyn.from) + '&from=' + twyn.oflags + '&proms=' + twyn.counter);
						});
			}

			var layout = twyn.afp.lo;

			jQuery("#pagefaq").click(function() {
				twyn.subpage("/subpage/faq.jsv", 700, 500, "H&auml;ufige Fragen");
				return false;
			});
			jQuery("#pageabout").click(function() {
				twyn.subpage("/subpage/about.jsv?shortcut=" + twyn.afp.sc, 580, 220, "Impressum");
				return false;
			});

			// twynbox nach 250 ms initialisieren
			window.setTimeout("initTwynbox()", 1250);
			window.setTimeout("openTwynProms()", 1500);

			jQuery("div.karussellPanel").hide();

			twyn.cc = jQuery("#ccarousel").CloudCarousel( {
				xPos : 277,
				yPos : layout === 3 ? 40 : 70,
				xRadius : 240,
				yRadius : layout === 3 ? 45 : 60,
				reflHeight : 50,
				reflGap : 4,
				speed : 0.06,
				autoRotate : 'right',
				autoRotateDelay : 2000,
				altBox : jQuery("#ccteaser"),
				titleBox : jQuery("#ccheadline"),
				FPS : 25
			});

			jQuery("div.karussellPanel").fadeIn("250");
			jQuery("div.karussellPanel").css("visibility", "visible");

			// jQuery("#ccheadline").hide();
			// jQuery("div.karussellPanel").hover(function() {
			// jQuery("#ccheadline").fadeIn();
			// }, function() {
			// jQuery("#ccheadline").fadeOut();
			// });

			jQuery("td.categorieName,span.categorieName").click(function() {
				var bar = jQuery(this).parents("div.categorieBar");
				bar.toggleClass("balken_blue_aktiv");
				bar.toggleClass("balken_blue");
				bar.next().slideToggle("fast");
				return false;
			});

			twyn.flag = function(string) {
				if (typeof twyn.flags === 'undefined') {
					twyn.flags = string;
					return;
				}
				if (twyn.flags.indexOf(string) === -1) {
					twyn.flags += "_" + string;
				}
			};
			
			twyn.shareEventHandler = function(evt) { 
		    if (evt.type == 'addthis.menu.share') { 
		    		twyn.flag("addthis");
		    		twyn.flag(evt.data.service);
		    }
			};
			
			addthis.addEventListener('addthis.menu.share', twyn.shareEventHandler);

			jQuery("div.addthis").mouseout(addthis_close);
			jQuery("div.addthis").mouseover(
					function() {
						var id = jQuery(this).parent("div.channel").attr("id");
						var p = twyn.proms[id];
						return addthis_open(this, '', twyn.su + (twyn.su.indexOf("?") != -1 ? "&" : "?") + "quelle=" + twyn.afp.sc +
								"&pid=" + id + "&subafp=" + twyn.flags, p.wtl);
					});

			jQuery("div.addthisZone").mouseout(addthis_close);
			jQuery("div.addthisZone").mouseover(
					function() {
						return addthis_open(this, '', twyn.su + (twyn.su.indexOf("?") != -1 ? "&" : "?") + "quelle=" + twyn.afp.sc +
								"&subafp=" + twyn.flags, "Premiumpromotions");
					});

			try {
				twyn.viewWidth = parent.jQuery(parent.window).width();
				twyn.viewHeight = parent.jQuery(parent.window).height();
				twyn.locked = false;
			} catch (e) {
				twyn.viewWidth = jQuery(window).width();
				twyn.viewHeight = jQuery(window).height();
				twyn.locked = true;
			}

			if (!twyn.locked) {
				if (typeof parent.twyn === 'undefined') {
					parent.twyn = {};
					parent.twyn.open = function(id) {
						twyn.open(id);
					};
				}
			}

			twyn.frameWidth = jQuery("body").width();
			twyn.frameHeight = jQuery("body").height();
			
			jQuery("div.aktionPanel a.promo").click(function() {
				if (!twyn.block) {
					twyn.doblock();
					twyn.flag("single");
					return twyn.click(jQuery(this));
				}
				return false;
			});

			jQuery("div.top a.promo").click(function() {
				if (!twyn.block) {
					twyn.doblock();
					twyn.flag("top");
					return twyn.click(jQuery(this));
				}
				return false;
			});

			jQuery("div.zonepromos a.promo").click(function() {
				if (!twyn.block) {
					twyn.doblock();
					twyn.flag("zone");
					return twyn.click(jQuery(this));
				}
				return false;
			});

			if (!twyn.locked) {
				twyn.iframe = parent.jQuery("iframe[name=" + twyn.ifr + "]");
				if (twyn.iframe.size() === 0) {
					twyn.iframe = parent.jQuery("iframe[name=premiumpromotions]");
				}
				// iframe.css("width", frameWidth);
				if (twyn.frameHeight > 1600) {
					twyn.iframe.css("height", twyn.frameHeight + 10);
				}

				try {
					parent.jQuery(parent.window).resize(function() {
						if (typeof twyn !== 'undefined') {
							twyn.viewWidth = parent.jQuery(parent.window).width();
							twyn.viewHeight = parent.jQuery(parent.window).height();
						}
					});
				} catch (ex) {
				}
			}

		});