/*
 * by  Marco A. Mendes <andrade.m@gmail.com>
 * for Lueckendesign http://lueckendesign.de
 *
 * February 2010
 */


var PAGE = { // Letzte COntent for
	CONTENT: null,
	LEFT: null,
	RIGHT: null
};

var KEYS = {
	_content: 'CONTENT',
	_CONTENT: 'CONTENT',
	_left: 'LEFT',
	_LEFT: 'LEFT',
	_right: 'RIGHT',
	_RIGHT: 'RIGHT'
};

var page_onload = { 
	'BEISPIELE': null,
	'HOME': null,
	'JOBS': null,
	'KONTAKT': null,
	'KUNDEN': null,
	'NEWS': null,
	'REFERENZEN': null,
	'STUDIO': null
};

var change_Fordern_Sie_uns = function ( p ) {
	if ( p.innetHTML === '' || p.innerHTML.match(/^[^<>]+@[^<>]+$/) ) {
		var subject = p.innerHTML ? '?subject=' + p.innerHTML : '';
		var content = p.innerHTML || 'Anfrage';
		p.innerHTML = 'Fordern Sie uns! Unser Service f&uuml;r Sie:<br /><span class="color">Kostenlose Angebote und Designvorschl&auml;ge.</span><br />Jetzt anfordern: <a class="_mailto" href="mailto:info@luecken-design.de'+ subject +'" title="mailto: info@luecken-design.de">'+ content +'</a>';
		return true;
	} else { return false; }
}

var showErrrrMSG = function ( msg ) {
	alert(msg);
};

var showImage = function ( _target , _src ) {
	PAGE[KEYS[_target]] = _src;
	$(_target.toUpperCase()).innerHTML =
		'<img id="IMG" style="display: none;" width="310" height="310" src="'+ _src +'" alt="'+ _src +'" />'
	;
	Effect.Appear( $('IMG') , { duration: 1 , from: 0.0 , to: 1.0 } );
};

var changeContent = function ( target_div_id , target_file , PAGE_ID ) {
	PAGE[KEYS[target_div_id]] = target_file;
	if (  target_div_id !== null && target_file !== null ) {
		new Ajax.Updater( target_div_id.toUpperCase() , target_file , {
			onComplete:  function ( ) {
				updateLinks( target_div_id.toUpperCase() );
				page_onload[PAGE_ID] && page_onload[PAGE_ID]();
				//jQuery_makeForm( $ );
			},
			parameters: {
				time: ( new Date() ).getTime()
			},
			evalScripts: true
		});

	} else { alert( 'Errrr @ changeContent( null,"'+target_div_id+'" , "'+target_file+')' ); }
};


var updateLinks = function ( DIV_ID ) {

	$('Fordern_Sie_uns') && change_Fordern_Sie_uns( $('Fordern_Sie_uns') );
		
	$$('img').each( function ( img ) {
		if ( img.getStyle('display') == 'none' ) {
			Effect.Appear( img , { duration: 1 , from: 0.0 , to: 1.0 } );
		}
	});

	$('BN_NAVIG') && $('BN_NAVIG').insert({ bottom: '<div class="clear"></div>'	});

	$$('div#BN_NAVIG div a').each( function ( a ) {
		var title_for = { NEXT: 'weiter', BACK: 'zurueck' };
		var content_for = { NEXT: '&#xbb;', BACK: '&#xab;' };
		var parentDiv_id = a.parentNode.getAttribute('id');
		
		if ( parentDiv_id.match( /NEXT|BACK/ ) ) {
			a.readAttribute( 'title' ) || a.writeAttribute('title', title_for[parentDiv_id]);
			a.innerHTML = a.innerHTML || content_for[parentDiv_id];
		} else {
			a.innerHTML = '<b class="ERROR"> &nbsp; &nbsp; ? &nbsp; &nbsp; </b>';
			a.writeAttribute('title', 'Errrr @ class="'+parentDiv_id+'..." Alowed values: class="back|next"');
		}
	 });

	 $$('div#' + DIV_ID + ' a').each( function ( a ) {
		var _class = a.readAttribute( 'class' );
		if ( _class !== null && _class !== '' ) {
			if ( _class.match( /^_(right|left|top|blank|mailto|content|window)$/ ) ) {
				var _href = a.readAttribute( 'href' );
				var _target = _class.toUpperCase();
				if ( _class.match( /^_(right|left|content)$/ ) ) {
					var _onclick = a.readAttribute( 'onclick' );
					var regex = /^(changeContent|showImage).'(.+)','(.+)'.;?$/;
					var mM = null;
					if ( _onclick !== null ) {
						regex.exec(_onclick);
						mMatches = {
							_function: RegExp.$1,
							_target: RegExp.$2.toUpperCase(),
							_href: RegExp.$3
						};
					}
					a.observe( 'click' , function ( e ) {
						changeContent(  _target , _href );
						if ( mM !== null ) {
							if ( mM._function == 'changeContent' ) {
								changeContent( null, mM._target , mM._href );
							} else if ( mM._function == 'showImage' ) {
								showImage( mM._target , mM._href );
							}
							//
						}
					});
					a.writeAttribute( 'href' , 'javascript: void(0);' );
				} else if ( _class.match( /^_window$/ ) ) {
					var id = 'WIN_'+ ( new Date() ).getTime();
					a.writeAttribute( 'href' , 'javascript: openWindow(\'' + id + '\', \'' + _href + '\');' );
					/* Alles in openWindow() in jq.code.js
						jQuery('body').append('<div id="'+ id + '" class="WINDOW Briefing" style="display: none;"></div>');
						var idPro = 'div#' +id;
						var idjQ = '#'+ id;
						new Ajax.Updater( id , _href , {
							onComplete:  function ( ) {
								jQuery_makeForm( 'div#' +id );
							},
							parameters: {
								time: ( new Date() ).getTime()
							},
							evalScripts: true
						});
					*/
				} else if ( _class.match( /^_blank$/ ) ) {
					a.writeAttribute( 'target' , '_blank' );
				}
			} else { showErrrrMSG("ERROR: class-Attribute incorrect :(\n\nPlease set class=\"_left|_right|_top|_blank|_content|_window\" for link to\n\n" + a.innerHTML + '\nto: ' + a.toString() + "\n\nand enjoy :)\n\nMM");}
		}
	});
};

window.onload = function ( e ) { 
	var targets = [ '_CONTENT' , '_LEFT' , '_RIGHT' ];
	for ( var i = 0 ; i < targets.length ; i += 1 ) {
		MM[targets[i]] && changeContent( targets[i] , MM.page_id+'/'+MM[targets[i]] , MM.page_id );
	}
};


