/*
 * JustIS main web application file
*/


	/* Array of head/content modulenames for menu start pages */
	var justis_loadstart	= [
									[ '000000','000000' ],
									[ '000001','000001' ],
									[ '000002','000002' ],
									[ '000003','000003' ],
									[ '000004','000004' ],
									[ '000005','000005' ]
								];

	/* Array of head/content modulenames for submenu start pages */
	var justis_loadpages	= [
									[	/* Pages for submenu 0 */
										[ '' ,'000000' ]	/* Pages for submenu 0, entry 0 */
									],
									[	/* Pages for submenu 1 */
										[ '' ,'000010' ],	/* Pages for submenu 1, entry 0 */
										[ '' ,'000011' ],	/* Pages for submenu 1, entry 1 */
										[ '' ,'000012' ],	/* Pages for submenu 1, entry 2 */
										[ '' ,'000013' ]	/* Pages for submenu 1, entry 3 */
									],
									[	/* Pages for submenu 2 */
										[ '' ,'000020' ],	/* Pages for submenu 2, entry 0 */
										[ '' ,'000021' ],	/* Pages for submenu 2, entry 1 */
										[ '' ,'000022' ],	/* Pages for submenu 2, entry 2 */
										[ '' ,'000023b' ],	/* Pages for submenu 2, entry 3 */
										[ '' ,'000024' ],	/* Pages for submenu 2, entry 4 */
										[ '' ,'000025' ],	/* Pages for submenu 2, entry 5 */
										[ '' ,'000026' ],	/* Pages for submenu 2, entry 6 */
										[ '' ,'000027' ]	/* Pages for submenu 2, entry 7 */
									],
									[	/* Pages for submenu 3 */
										[ '' ,'000030' ],	/* Pages for submenu 3, entry 0 */
										[ '' ,'000031' ]	/* Pages for submenu 3, entry 1 */
									],
									[	/* Pages for submenu 4 */
										[ '' ,'000040' ],	/* Pages for submenu 4, entry 0 */
										[ '' ,'000041' ],	/* Pages for submenu 4, entry 1 */
										[ '' ,'000042' ],	/* Pages for submenu 4, entry 2 */
										[ '' ,'000043' ],	/* Pages for submenu 4, entry 3 */
										[ '' ,'000044' ]	/* Pages for submenu 4, entry 4 */
									],
									[	/* Pages for submenu 5 */
										[ '' ,'000050' ]	/* Pages for submenu 5, entry 0 */
									]
								];

	/***********************************************************************************************/
	
	function justis_Website() {
		
		/* root url der website */
		this.justis_rootURL			= '/justis/root/index.php';
		/* path to images of HOME button */
		this.home_imagePath			= '/justis/mods/home/000000/img/home_';

		/* path to the head modules */
		this.head_modPath			= '/justis/mods/head/';
		/* path to the content modules */
		this.content_modPath		= '/justis/mods/content/';
		/* path to the popup modules */
		this.popup_modPath			= '/justis/mods/popup/';

		/* nr of currently active menu entry */
		this.menu_actMenu			= 0;
		/* indicates that a menu entry has been selected */
		this.menu_selMenu			= false;
		this.menu_imgPath			= '/justis/mods/menu/000000/img/button_';

		/* number of active submenu group */
		this.submenu_selected		=	-1;
		/* number of actual active submenu entry */
		this.submenu_selected_entry	=	-1;
		/* reference to the old active submenu entry */
		this.submenu_ref_old		=	null;
		/* Path to the submenu image buttons */
		this.submenu_imgpath		=	'/justis/mods/submenu/000000/img/button_';

		/* nr of selected slide in start window */
		this.content_mainSlide		= 0;
		/* nr of selected slide in content windows */
		this.content_actualSlide	= 0;
		/* indicates pulldown detailviewer active or not */
		this.content_pulledDown = false;

		this.opticon_imagePath		= 	'/justis/mods/opticon/000000/img/';
		/* names of logos to show per menu entry */
		this.opticon_imageLogos 	= new Array( 'flimmertv.gif','logo_01.png','logo_02.png',
											'logo_03.png','logo_04.png','logo_05.png' );

		/* pointer to function for closing tooltips */
		this.util_obsTooltip = null;
		/* pointer to function for following mousemove tooltip */
		this.util_outTooltip = null;
		/* path to tooltip modules */
		this.tooltip_modPath = '/justis/mods/tooltip/';
		/* Indicates popup box is closing, but not fully closed and hidden */
		this.popup_isClosing = false;

		/* delay while increasing the ticker text */
		this.ticker_inDelay			=	100;
		/* delay while decreasing the ticker text */
		this.ticker_outDelay		=	100;
		/* delay to wait decrease the ticker text */
		this.ticker_waitDelay		=	4000;
		/* number of actual ticker text in array of text messages */
		this.ticker_nextMsg			=	0;
		/* variable to hold actual ticker text */
		this.ticker_actText			=	'';
		/* array of ticker text messages */
		this.ticker_textMsgs		=	new Array (
											'm2ws@apw -  Automotive Power Pack - Das optimierte Web2.0-Redaktionssystem für den Automobilhandel - Es bleiben keine Wünsche offen !'
										);

	/* Modul: ticker methods */
		this.ticker_init	= function() {
			this.ticker_textMsgs[0] = $('ticker_text').innerHTML;
		}
		this.ticker_startTicker = function() {
			this.ticker_actText = this.ticker_textMsgs[0];
			this.ticker_doTicker( 0, 1 );
		}
		this.ticker_doTicker = function( pos, dir) {
			var out = '&nbsp;' +this.ticker_actText.substring(0, pos) + '&nbsp;';
		
			$( 'ticker_text' ).innerHTML = out;
			pos += dir;
		
			if(pos > this.ticker_actText.length) {
				setTimeout( this.ticker_doTicker.bind(this, pos, -dir), this.ticker_waitDelay );
			} else {
				if( pos < 0 ) {
					if( ++this.ticker_nextMsg >= this.ticker_textMsgs.length ) {
						this.ticker_nextMsg = 0;
					}
					this.ticker_actText = this.ticker_textMsgs[this.ticker_nextMsg];
					dir = -dir;
				}
				if( dir == 1 ) {
					setTimeout( this.ticker_doTicker.bind(this, pos, dir), this.ticker_inDelay );
				} else {
					setTimeout( this.ticker_doTicker.bind(this, pos, dir), this.ticker_outDelay );
				}
			}
		}

	/* Modul: opticon methods */
		this.opticon_enableLogo = function() {
			$( 'opticon_logo' ).show();
		}
		this.opticon_disableLogo = function() {
			$( 'opticon_logo' ).hide();
		}
		
		this.opticon_changeLogo = function( nr ) {
			$( 'opticon_image' ).src = this.opticon_imagePath + this.opticon_imageLogos[nr];
		}

	/* Modul: home methods */
		this.home_handleButton = function ( evttype, ref ) {
				switch( evttype ) {
					case 0:
						document.location.href = this.justis_rootURL;
						break;
					case 1:
						ref.src = this.home_imagePath + 'h.png';
						break;
					case 2:
						ref.src = this.home_imagePath + 'n.png';
						break;
					default:;
				}
		}

	/* Modul: links methods */
		this.links_handleLink = function ( nr ) {
				this.popup_openPopup(nr);
		}

	/* Modul: popup methods */
		this.popup_handlePopup = function () {
				this.popup_closePopup();
		}
		this.popup_centerPopup = function() {
			ref = document.viewport.getDimensions();

			winwidth = ref.width;
			winheight = ref.height;
			leftpos = (winwidth/2 - 300);
			if( leftpos<0 ) leftpos = 0;
			
			/*
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				$( 'popup' ).style.pixelLeft = leftpos;
			}else{
				$( 'popup' ).style.left = leftpos+'px';
			}
			*/
			$( 'popup' ).setStyle( 'left:'+leftpos+'px;' );
		}
		this.popup_openPopup = function ( whichone ) {
			this.util_disable_Menus();
			this.popup_centerPopup();
			$( 'popup_content' ).innerHTML = '';
			$( 'popup' ).show();
			this.popup_isClosing = false;
			switch( whichone ) {
				case 0:
					this.util_loadPage( 'popup_content', this.popup_modPath + '/000000/index_0.php' );
					break;
				case 1:
					break;
				case 2:
					this.util_loadPage( 'popup_content', this.popup_modPath + '/000002/index_0.php' );
					break;
				case 3:
					this.util_loadPage( 'popup_content', this.popup_modPath + '/000003/index_0.php' );
					break;
			}
		}
		this.popup_closePopup = function() {
			if( !this.popup_isClosing ) {
				this.popup_isClosing = true;
				Effect.Fold( 'popup' );
				this.util_enable_Menus();
			}
		}

	/* Modul: menu methods */
		this.menu_changeButton = function( ref, over, nr ) {
				if( !this.util_menusEnabled ) return;
				if( !this.menu_selMenu ) {
					this.opticon_changeLogo( over ? nr : 0 );
				}
				if( nr != this.menu_actMenu ) {
					ref.src = this.menu_imgPath + nr + (over ? '_h.png' : '_n.png');
				}
		}
		this.menu_handleMenu = function( ref, menunr ) {
				if( !this.util_menusEnabled ) return;
				if( menunr == 0 ) {
					document.location.href = this.justis_rootURL;
				} else {
					if( menunr != this.menu_actMenu ) {
						$( 'menu_' + this.menu_actMenu ).src = this.menu_imgPath + this.menu_actMenu + '_n.png';
						$( 'menu_' + menunr ).src = this.menu_imgPath + menunr + '_h.png';
						this.menu_selMenu = true;
						this.opticon_disableLogo();
					}
					this.menu_selectMenu( menunr );
					this.menu_actMenu = menunr;
					this.submenu_initSubmenu();
				}

		}
		this.menu_selectMenu = function( menunr ) {
			if( this.menu_actMenu != 0 ) {
				this.submenu_hideSubmenu( this.menu_actMenu );
			}
			this.submenu_showSubmenu( menunr );
			this.head_loadPage( true, menunr, -1, 0 );
			this.content_loadPage( true, menunr, -1, 0 );
		}

	/* Modul: submenu methods */
		this.submenu_showSubmenu = function( nr ) {
			$( 'submenu_' + nr ).show();
		}
		this.submenu_hideSubmenu = function( nr ) {
			$( 'submenu_' + nr ).hide();
		}
		this.submenu_initButton = function() {
			this.submenu_ref_old.src = this.submenu_imgpath + this.submenu_selected + '_' + this.submenu_selected_entry + '_' + 'n.png';
		}
		this.submenu_initSubmenu = function() {
			if( this.submenu_ref_old != null ) {
				this.submenu_initButton();
			}
			this.submenu_selected = -1;
			this.submenu_selected_entry = -1;
			this.submenu_ref_old = null;
		}
		this.submenu_changeButton = function(high, nr, subnr) {
			if( !this.util_menusEnabled ) return;
			if( this.submenu_selected_entry != subnr ) {
				if( high ) {
					$( 'submenu_' + nr + '_' + subnr ).src = this.submenu_imgpath + nr + '_' + subnr + '_h.png';
				} else {
					$( 'submenu_' + nr + '_' + subnr ).src = this.submenu_imgpath + nr + '_' + subnr + '_n.png';
				}
			}
		}
		this.submenu_handleSubmenu = function( nr, subnr ) {
			if( !this.util_menusEnabled ) return;
			if( this.submenu_selected_entry != subnr ) {
				if( this.submenu_ref_old != null ) {
					this.submenu_initButton();
				}
			}
			this.submenu_selected = nr;
			this.submenu_selected_entry = subnr;
			this.submenu_ref_old = $( 'submenu_' + nr + '_' + subnr );
		
			this.head_loadPage( false, nr, subnr, 0 );
			this.content_loadPage( false, nr, subnr, 0 );
		}
		
	/* Modul: head methods */
		this.head_loadPage = function( startpage, menunr, subnr, pagenr ) {
			ref = (startpage ? justis_loadstart[menunr] : justis_loadpages[menunr][subnr] );
			if( ref[0] != '' ) {
				this.util_loadPage( "head", this.head_modPath + ref[0] + '/index.php' )
			}
		}

	/* Modul: content methods */
		this.content_openMainSlide = function( ref, nr ) {
			if( nr != this.content_mainSlide ) {
				$('content_slide_' + this.content_mainSlide).hide();
				/* now set the arrow image to normal state */
				ref = $( 'content_head_arrow_' + this.content_mainSlide );
				if( ref != undefined ) {
					ref.src = '/justis/mods/content/000000/img/head_arrow_n.png';
				}
				Effect.BlindDown('content_slide_' + nr);
				/* now set the arrow image to selected state */
				ref = $( 'content_head_arrow_' + nr );
				if( ref != undefined ) {
					ref.src = '/justis/mods/content/000000/img/head_arrow_h.png';
				}
			} else {
				$( 'content_slide_'+this.content_mainSlide ).toggle();
				/* now toggle the state of the arrow image vica versa */
				ref = $( 'content_head_arrow_' + this.content_mainSlide );
				if( ref != undefined ) {
					pos = ref.src.lastIndexOf('/');
					imagename = ref.src.substr(pos+1);
					if(	imagename == 'head_arrow_n.png' ) {
						ref.src = '/justis/mods/content/000000/img/head_arrow_h.png';
					} else {
						ref.src = '/justis/mods/content/000000/img/head_arrow_n.png';
					}
				}
			}
			this.content_mainSlide = nr;
		}
		this.content_hoverHead = function( ref, high ) {
			if( high ) {
				$(ref).setStyle( {backgroundColor:'#DDE0E7'} );
			} else {
				$(ref).setStyle( {backgroundColor:'#FFFFFF'} );
			}
		}
		this.content_initContent = function(nr) {
			if( nr != -1 ) {
				ref = $('content_slide_' + nr);
				if( ref != undefined ) {
					ref.show();
				}
				ref = $( 'content_head_arrow_' + nr );
				if( ref != undefined ) {
					ref.src = '/justis/img/content/arrow_down.gif';
				}
				this.content_actualSlide = nr;
			}
		}
		this.content_openSlide = function( ref, nr ) {
			if( nr != this.content_actualSlide ) {
				$('content_slide_' + this.content_actualSlide).hide();
				/* set arrow image to normal state */
				ref = $( 'content_head_arrow_' + this.content_actualSlide );
				if( ref != undefined ) {
					ref.src = '/justis/img/content/arrow_right.gif';
				}
				Effect.BlindDown('content_slide_' + nr);
				/* set arrow image to selected state */
				ref = $( 'content_head_arrow_' + nr );
				if( ref != undefined ) {
					ref.src = '/justis/img/content/arrow_down.gif';
				}
			} else {
				$( 'content_slide_'+this.content_actualSlide ).toggle();
				/* toggle arrow image between normal and selected state */
				ref = $( 'content_head_arrow_' + this.content_actualSlide );
				if( ref != undefined ) {
					pos = ref.src.lastIndexOf('/');
					imagename = ref.src.substr(pos+1);
					if(	imagename == 'arrow_right.gif' ) {
						ref.src = '/justis/img/content/arrow_down.gif';
					} else {
						ref.src = '/justis/img/content/arrow_right.gif';
					}
				}
			}
			this.content_actualSlide = nr;
		}
		this.content_loadPage = function( startpage, menunr, subnr, pagenr ) {
			ref = (startpage ? justis_loadstart[menunr] : justis_loadpages[menunr][subnr] );
			if( ref[1] != '' ) {
				this.util_loadPage( "content", this.content_modPath + ref[1] + '/index_0.php' );
			}
		}
		this.content_loadPageN = function( path, nr ) {
			this.util_loadPage( "content", this.content_modPath + path + '/index_' + nr + '.php' );
		}
		this.content_pullDown = function(path) {
			if( !this.util_menusEnabled ) return;
			if( !this.content_pulledDown ) {
				this.util_loadPage( 'pulldown_content', '/justis/mods/detail/'+path+'/index.php' );
				/*
				Effect.BlindUp('content');
				Effect.BlindDown('pulldown');
				*/
				this.content_pulledDown = true;
			}
		}
		this.content_pullUp = function() {
			Effect.BlindUp('pulldown');
			Effect.BlindDown('content');
			this.content_pulledDown = false;
		}
		this.content_pullInit = function() {
			this.content_pulledDown = false;
			$('pulldown').hide();
			$('content').show();
		}
		this.content_pageNavi = function( ref, mod, imgname ) {
			ref.src = this.content_modPath + mod + '/img/' + imgname;
		}

	/* Internal: utility methods */
		this.util_menusEnabled = true;
		this.util_disable_Menus = function() {
			
			this.util_menusEnabled = false;
			$$('img.menu_button').invoke( 'setStyle', 'opacity:0.5' );
			$$('img.submenu_button').invoke( 'setStyle', 'opacity:0.5' );
			
		}
		this.util_enable_Menus = function() {
			
			this.util_menusEnabled = true;
			$$('img.menu_button').invoke( 'setStyle', 'opacity:1.0' );
			$$('img.submenu_button').invoke( 'setStyle', 'opacity:1.0' );
			
		}
		this.util_pageLoaded = function(xhr,into) {
			switch( into ) {
				case 'head':
					Effect.Appear( $(into).firstChild, {duration: 1.50, from: 0.0 } );
					break;
				case 'content':
					this.content_pullInit();
					this.content_initContent(0);
					break;
				case 'pulldown_content':
					Effect.BlindUp('content');
					Effect.BlindDown('pulldown');
					break;
				case 'tooltip':
					break;
				default:;
			}
		}
		this.util_loadPage = function( into, url ) {
			new Ajax.Updater( into, url, {
				method: "get",
				parameters: {into: into, url: url},
				evalScripts: true,
				onComplete: (function(xhr) { this.util_pageLoaded(xhr,into) }).bind(this)
			} );
		}
		this.util_windowResize = function( evt ) {
			if( $('popup').visible() ) {
				this.popup_centerPopup();
			}
		}
		this.util_initTooltip = function() {
			this.util_closeTooltipFunc = this.util_closeTooltip.bindAsEventListener(this);
			this.util_followTooltipFunc = this.util_followTooltip.bindAsEventListener(this);
		}

		this.util_setupTooltip = function(evt, path, w, opt) {
			mouseX = Event.pointerX(evt);
			mouseY = Event.pointerY(evt);
			$('tooltip').setStyle( 'width:'+w+'px' );
			offX = -($('tooltip').getWidth() + 10);
			offY = 10;

			$('tooltip').setStyle( {top: (mouseY + offY)+'px',left: (mouseX + offX)+'px', opacity: 0.85} );
			return true;
		}
		this.util_openTooltip = function(evt, path, w, opt) {
			if( !this.util_menusEnabled ) return;
			if( this.util_setupTooltip( evt, path, w, opt ) ) {
				Event.element(evt).observe('mousemove', this.util_followTooltipFunc);
				Event.element(evt).observe('mouseout', this.util_closeTooltipFunc );
				$('tooltip').innerHTML = '<p align="center" style="font-size:12px;">Loading ...<br/><img src="/justis/img/misc/ajax-loader.gif"/></p>';
				$('tooltip').show();
				this.util_loadPage( 'tooltip', this.tooltip_modPath + path + '/index.php' );
			}
		}
		this.util_closeTooltip = function(evt) {
			Event.element(evt).stopObserving( 'mousemove', this.util_followTooltipFunc );
			Event.element(evt).stopObserving( 'mouseout', this.util_closeTooltipFunc );
			$('tooltip').innerHTML = '';
			$('tooltip').hide();
		}
		this.util_followTooltip = function(evt) {
			mouseX = Event.pointerX(evt);
			mouseY = Event.pointerY(evt);
			offX = -($('tooltip').getWidth() + 10);
			offY = 10;

			$('tooltip').setStyle( {top: (mouseY + offY)+'px',left: (mouseX + offX)+'px'} );
		}
		/*
		** Lädt ein Layout in ein Layout-Element
		** lyt_path - Pfad oder ID eines Layouts
		*/
		this.util_loadLayout = function( area, lyt_path) {
		}
		/*
		** Entfernt ein Layout aus einem Layout-Element
		**
		*/
		this.util_removeLayout = function( area ) {
		}
		/*
		** Lädt ein Modul in ein Element-Container
		** position - 1-N Angabe an wievielter Stelle das Modul erscheinen soll
		**              0 Modul an erster Stelle plazieren
		**             -1 Modul an letzter Stelle plazieren
		*/
		this.util_loadModule = function( area, position, mod_path) {
		}
		/*
		** Entfernt ein Modul aus einem Element-Container
		** position - 1-N Angebe, welches Modul entfernt werden soll
		**              0 Alle Module entfernen
		*/
		this.util_removeModule = function( area, position) {
		}
		this.util_window = null;
		this.util_openWindow = function( wname, width, height, url ) {
		  this.util_window = window.open(url, wname, "width="+width+",height="+height);
		  this.util_window.focus();
		}
		this.init_Website = function() {
			/* Initialize Module: ticker */
			this.ticker_startTicker();
			/* Initialize Tooltips */
			this.util_initTooltip();
			/* Initialize opticons of pulldown window */
			$('icon_print').setStyle({opacity:0.5});
			$('icon_downl').setStyle({opacity:0.5});
			$('icon_email').setStyle({opacity:0.5});
			/* Initialize window resize handler */
			if( document.onresize ) {
				Event.observe( document, 'resize', this.util_windowResize.bindAsEventListener(this) );
			} else {
				Event.observe( window, 'resize', this.util_windowResize.bindAsEventListener(this) );
			}
		}
		this.justis_Website = function() {
			alert( 'start' );
		}
	}

