/**
* Onload Manager
* calls all functions in queue on window.onload
* pqueue is a secondary queue running after the main queue is fully processed
*/
var OM = {	queue : function() {},
			pqueue : function() {},
			add : function(newfunc) { // add function to the end of the main queue
				var temp = OM.queue;
				OM.queue = function() {
					temp();
					newfunc();
				};
			},
			addf : function(newfunc) { // add function to the start of the main queue
				var temp = OM.queue;
				OM.queue = function() {
					newfunc();
					temp();
					
				};
			},
			addp : function(newfunc) { // add function to secondary queue
				var temp = OM.pqueue;
				OM.pqueue = function() {
					temp();
					newfunc();
				};
			},
			rdy : function() {
				OM.queue();
				OM.pqueue();
			}
		};
if(typeof jQuery != "undefined")		
$(document).ready(function() {
	if(typeof OM != "undefined")
		OM.rdy();
});

/**
* GET var manager
* parses query string looking for GET variables
* also retrieves TYPO3 language after real_url rewrite
*/
var GP = {	langs : ['de','en','es'],
			langConst : 'TYPO3_GPC_LANG',
			GET : [],
			parse : function() {
				var x = document.location.toString().split("?");
				var forLang = x[0];
				if(typeof x[1] != "undefined") {
					x = x[1].split('#');
					if(x[0].length > 0) {
						x = x[0].split('&');
						var y = x.length;
						for(var i=0; i < y; i++) {
							if(x[i] == '') continue;
							if(x[i].indexOf('=') == -1) {
								GP.GET[String(x[i])] = '';
							} else {
								x[i] = x[i].split('=');
								if(typeof x[i][1] != "undefined")
									GP.GET[String(x[i][0])] = x[i][1];
								else GP.GET[String(x[i][0])] = '';
							}
						}
					}
				}
				GP.parseLang(forLang);
			},
			parseLang : function(str) {
				var lang = '';
				for(var l in GP.langs) {
					if(str.indexOf('/'+GP.langs[l]+'/') != -1) {
						lang = GP.langs[l];
						break;
					}
				}
				if(lang == '') lang = GP.langs[0];
				GP.GET[GP.langConst] = lang;
			},
			getLang : function() {
				var l = GP.val(GP.langConst);
				if(l !== false) return l;
				else return GP.langs[0];
			},
			isset : function(key) {
				if(typeof GP.GET == "undefined") return false;
				return typeof(GP.GET[String(key)]) != "undefined";
			},
			val : function(key) {
				if(GP.isset(key)) 
					return GP.GET[String(key)];
				else return false;
			},
			dump : function() {
				var msg = "$_GET Variables:\n";
				for(var tmp in GP.GET) {
					msg = msg + tmp + ' : ' + GP.GET[tmp] + "\n";
				}
				alert(msg);
			}
};
if(typeof OM != "undefined") OM.add(GP.parse);


/**
* Trailer gallery
* retrieves list of images (separated by '\n') from incPath/dir.php
*/
var Trailer = {	targetDiv : '#trailerImage',
				affDiv : '#trailerImageA',
				incPath : '/fileadmin/lib/images/trailerStart/',
				incPathA: '/fileadmin/lib/images/trailerAffiliates/',
				listFile : 'dir.php',
				imageStyles : 'width:100%;height:100%;border:0;margin:0;padding:0;text-indent:0;',
				images : ['1.jpg'],
				lang : '',
				cache : [],
				allCached : false,
				changeEvery : 7,
				interval : 0,
				current : 0,
				intervalFunc : null,
				preloadImage : function() {
					var c;
					c = document.createElement('img');
					c.src = Trailer.incPath+Trailer.images[Trailer.current];
					Trailer.cache[Trailer.current] = c;
					if(Trailer.current == (Trailer.images.length-1)) {
						Trailer.allCached = true; }
				},
				getImages : function() {
					$.post((Trailer.incPath+Trailer.listFile),function(data) {
						if(data != 'ERROR') {
							Trailer.images = data.split("\n");
							for(var i=0;i<Trailer.images.length;i++) Trailer.cache.push('');
							Trailer.preloadImage(0);
							Trailer.next();
							Trailer.intervalFunc = setInterval(Trailer.run,1000);
						}
					});
				},
				next : function() {
					$(Trailer.targetDiv+' img').attr('src',Trailer.cache[Trailer.current].src).show();
					Trailer.current = (Trailer.current + 1)%Trailer.images.length;
					if(!Trailer.allCached) Trailer.preloadImage();
				},
				run : function() {
					Trailer.interval++;
					if(Trailer.interval >= Trailer.changeEvery) {
						if(Trailer.cache[Trailer.current] != '') {
							Trailer.interval = 0;
							Trailer.next();
						}
					}
				},
				init : function() {
					Trailer.lang = GP.getLang();
					var div;
					div	= $(Trailer.targetDiv);
					if(div.length > 0) {
						Trailer.incPath = Trailer.incPath + Trailer.lang + '/';
						div.html('<img src="'+Trailer.incPath+Trailer.images[0]+'" style="'+Trailer.imageStyles+'" />');
						Trailer.getImages();
					} else {
						div = $(Trailer.affDiv);
						if(div.length > 0) {
							//Trailer.images = ['1.png'];
							Trailer.incPath = Trailer.incPathA + Trailer.lang + '/';
							Trailer.targetDiv = Trailer.affDiv;
							div.html('<img src="" style="'+Trailer.imageStyles+';display:none;" />');
							//div.find('img').hide();
							Trailer.getImages();
						}
					}
				}
			};
if(typeof OM != "undefined") OM.add(Trailer.init);

/**
* Login/Reg Form handler
* shows/hides login forms
*/

var LB = {	lLink   : '#loginlink',	// '#loginlink'
			rLink   : '.topRight a.register',		// '#reglink'
			lDiv    : '#log', 			// loginFormBox div
			rDiv    : '#reg', 			// regFormBox div
			imgDiv  : '#trailerImage',  // div below containing trailer
			hl      : 0,				// loginFormBox height
			hr      : 0,				// registerFormBox height
			error   : '',
			status  : 0, 				// 0 = minimized, 1 = loginbox, 2 = regbox
			speed   : 300,
			locked  : false, 			// true during animation
			loginOC : function() {
				if(!LB.locked) {
					if(LB.status == 0) {
						LB.locked=true;
						$(LB.lDiv).height(0).show().animate({height:(LB.hl+'px')},LB.speed,'swing',function(){LB.status = 1;LB.locked=false;});
					} else if (LB.status == 1) {
						LB.locked = true;
						$(LB.lDiv).animate({height:'0px'},LB.speed,'swing',function(){$(LB.lDiv).hide(); LB.status = 0;LB.locked=false;});
						LB.status = 0;
					} else {
						LB.locked=true;
						$(LB.rDiv).hide();
						$(LB.lDiv).height(0).show().animate({height:(LB.hl+'px')},LB.speed,'swing',function(){LB.status = 1;LB.locked=false;});
					}
				}
				return false;
			},
			regOC : function() {
				if(!LB.locked) {
					if(LB.status == 0) {
						LB.locked=true;
						$(LB.rDiv).css('height','0px').show().animate({height:(LB.hr+'px')},LB.speed,'swing',function(){LB.status = 2;LB.locked=false;});
					} else if (LB.status == 2) {
						LB.locked=true;
						$(LB.rDiv).animate({height:'0px'},LB.speed,'swing',function(){$(LB.rDiv).hide(); LB.status = 0;LB.locked=false;});
						LB.status = 0;
					} else {
						LB.locked=true;
						$(LB.lDiv).hide();
						$(LB.rDiv).css('height','0px').show().animate({height:(LB.hr+'px')},LB.speed,'swing',function(){LB.status = 2;LB.locked=false;});
						LB.status = 2;
					}
				}
				return false;
			},
			init : function() {
				var ll;
				//var rl;
				ll = $(LB.lLink);
				//rl = $(LB.rLink);
				if(ll.length > 0 /*&& rl.length > 0*/) {
					ll.click(LB.loginOC);
					//rl.click(LB.regOC);
					LB.hl = $(LB.lDiv).height();
					//LB.hr = $(LB.rDiv).height();
					if(LB.error == 'log') {LB.status=1; $(LB.lDiv).show();}
					//else if(LB.error == 'reg') {LB.status=2;$(LB.rDiv).show();}
					
				}
			}
		};
if(typeof OM != "undefined") OM.add(LB.init);