////////////////////////////////////////////////////////////////////////////////
//
// Klasse:
//  ClAD.js
////  
// @Copyright:
//  Peppies-life
//  www.peppies-life.de
// @Autor:
//  Dennis Schmitt ( peppie23(at)gmail(dot)com )
// @Protocol:
//  08.02.2009 - Dennis Schmitt | Intial public release
//  

var cClAD =
{
	s_myversion: '0.0.5',
	
	s_optionField: '',
	//////////////////////////////////////////////////////////////////////////////
	// o_cladOptions : Object
	// Holds all options about clad'i
	o_cladOptions:	undefined,
	
	//////////////////////////////////////////////////////////////////////////////
	// o_allProducts : Array of objects
	// All products to place on page
	a_allProducts:	new Array,
	
	//////////////////////////////////////////////////////////////////////////////
	// o_xmlHttpRequest : Object
	o_xmlHttpRequest: undefined,
	
	//////////////////////////////////////////////////////////////////////////////
	// s_basePath : String
	s_basePath: '',
	
	b_inBox: false,
	
	//////////////////////////////////////////////////////////////////////////////
	// b_classLoaded : Object
	b_classLoaded: false,
	
	placeProducts: function()
	{
		if( !this.b_classLoaded ) return null;
		if( this.a_allProducts.length <= 0 ) return null;
		
		for( i=0; i<this.a_allProducts.length; i++ )
		{
			
			o_product = this.a_allProducts[i];
			
			this.replaceKeywords( this.getNodeValue( o_product, 'keywords' ),
														this.getNodeAttributes( this.getNode( o_product, 'keywords' ), 'mpp' ), i );
		}
	},
	
	//////////////////////////////////////////////////////////////////////////////
	// initClass() : null
	// initialize the clad'i class
	initClass: function( s_basePath, s_optioenFeld )
	{
		this.s_basePath = s_basePath;
		this.s_optionField = s_optioenFeld;

		if( this.loadOptions() )
			if( this.loadProducts() )
				this.b_classLoaded = true;
	},
	
	//////////////////////////////////////////////////////////////////////////////
	// getXmlHttpRequest() : Object
	// initialize the XmlHttpRequest
	getXmlHttpRequest: function()
	{
		axObjs = new Array( 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',
				                'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 
				                'Microsoft.XMLHTTP' );
			                
		for( var i=0; i<axObjs.length; i++ )
		{
			try
			{
				return new ActiveXObject( axObjs[i] );
			}
			catch( eXeption ){}
		}
		
		// for FF and Mozilla
		return new XMLHttpRequest();
	},
	
	//////////////////////////////////////////////////////////////////////////////
	// getUrl
	// s_url = String of the request url
	// setting up the XMLHTTPRequest object and send request
	getUrl: function( s_url )
	{
		try
		{
			// set 10seconds cache timer
			var sep = (-1 < s_url.indexOf("?")) ? '&' : '?';			
			s_url = s_url + sep + "__=" + encodeURIComponent((new Date()).getTime()).substr(0,8);
			
			this.o_xmlHttpRequest = this.getXmlHttpRequest();
			this.o_xmlHttpRequest.open( "GET", s_url, false );
			this.o_xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
			this.o_xmlHttpRequest.send(null);
			
			if( this.o_xmlHttpRequest.status == 200 )
			{
				var Respon = this.o_xmlHttpRequest.getResponseHeader('Content-type');
	      
	      // check the headerinformations from the reqeuested url
	      if( Respon.indexOf('xml') > -1 )
	        return this.o_xmlHttpRequest.responseXML;
	      else
	        return this.o_xmlHttpRequest.responseText;
			}
		}
		catch( exception ){ /**/ }
		
		return undefined;
	},
	
	//////////////////////////////////////////////////////////////////////////////
	// loadOptions() : boolean
	// load all options via ajax
	loadOptions: function()
	{
		this.o_cladOptions = this.getNode( this.getUrl( this.s_basePath+'/cladoptions.xml?v'+this.s_myversion ),
																			 'cladioptions' );
		
		return true;
	},

	//////////////////////////////////////////////////////////////////////////////
	// getOption( s_option: String ) : String
	// ---
	getOption: function( s_option )
	{
		return this.getNodeValue( this.o_cladOptions, s_option );
	},
	
	getNode: function( o_XMLDoc, s_root )
	{
		return o_XMLDoc.getElementsByTagName( s_root )[0];
	},
	
	getNodeValue: function( o_root, s_tag )
	{
		node = o_root.getElementsByTagName( s_tag );
	
		if( node != null && node.length > 0 )
			return node[0].firstChild.nodeValue;
		
		return undefined;
	},
	
	getNodeAttributes: function( o_root, s_attribute )
	{
		if( o_root )
			return o_root.getAttribute( s_attribute );
		else
			return null;
	},
	
	//////////////////////////////////////////////////////////////////////////////
	// loadProducts() : boolean
	// load our products via ajax
	loadProducts: function()
	{
		/*
		o_tmp = this.getNode( this.getUrl( this.s_basePath+'/cladproducts.php?m='+this.getOption( 'ppp' )+'&v='+this.s_myversion ),
													'cladproducts' );
		*/
		
		o_tmp = this.getNode( this.getUrl( this.s_basePath+'/products.xml'),
													'cladproducts' ); 
		
		for( i=0; i<o_tmp.childNodes.length; i++ )
		{
			this.a_allProducts.push( o_tmp.childNodes[i] );
		}
																			 
		return true;
	},
	
	replaceKeywords: function( s_keywords, n_maxReplace, n_produktId )
	{
		if( !document.getElementById( this.getOption(this.s_optionField) ) ) return;
		
		n_maxReplace = ( n_maxReplace == undefined ) ? 1000:n_maxReplace;
		s_textData = document.getElementById( this.getOption(this.s_optionField) ).innerHTML;
		
//		s_notAllowTags = "a|h[1-9]";
		s_notAllowTags = "a|h[1-9]|strong|em|b|i";
		s_notAllowFromOptions = this.getOption('notallowedTags');
		
		s_notAllowTags += ( s_notAllowFromOptions != '-' ) ? '|'+s_notAllowFromOptions:'';
		
		// "(?!(?:[^<]+>|[^>]+<\/("+ s_notAllowTags +")>))\\b("+ s_keywords +")\\b"
		// "(?!(?:[^<]+>|[^>]+<\/("+ s_notAllowTags +")>)|(?:[^\[]+\]|[^\]]+\[\/(.*)\]))\\b("+ s_keywords +")\\b"
		// "(?!(?:[^<]+>|[^>]+<\/("+ s_notAllowTags +")>)|(?:[^\[]+\]|[^\]]+\[\/(sourcecode|b)\]))\\b("+ s_keywords +")\\b"		
		s_link_patter 	= '(?:[^<]+>|[^>]+<\\/('+s_notAllowTags+')>)';
		s_brack_patter	= ( this.getOption('inc_bbcode') == '1' ) ? '|(?:[^\\[]+\\]|[^\\]]+\\[\\/(.*)\\])' : '';

		re = new RegExp( '(?!'+ s_link_patter + s_brack_patter +')\\b('+ s_keywords +')\\b', 'gi' );
		
		n_controll		= 0;
		n_Index				= 0;
		s_newTextData = '';
		
    while( re.exec( s_textData ) )
    {
			s_newTextData += s_textData.substr( n_Index,
																					re.lastIndex-RegExp.lastMatch.length-n_Index );
			
			s_restCheck = RegExp.rightContext;
			
			if( (s_restCheck.indexOf('>') < s_restCheck.indexOf('<')) || ( s_restCheck.indexOf('>') > -1 && s_restCheck.indexOf('<') == -1 ) )
			{
				s_newTextData += RegExp.lastMatch;
			}
			else
			{
				o_product = this.a_allProducts[n_produktId];
				
				sTextLink = this.setSpecialKeywords( this.getNodeValue( o_product, 'link' ), RegExp.lastMatch );
				
				s_newTextData += '<a href="'+ s_TextLink +'" class="contentlink" rel="nofollow" onmouseout="cClAD.setInbox(false);cClAD.sleepLong();" onmouseover="cClAD.setInbox(true);cClAD.placeLayer( cClAD.getTop(this), cClAD.getLeft(this), '+ n_produktId +', \''+ RegExp.lastMatch +'\' );">'+ RegExp.lastMatch +'</a>';
			}
			
			n_Index = re.lastIndex;
			
			n_controll++;
			if( n_controll == 999 ) { alert('Script overloaded, ClAD\'i.js'); break; }
			if( n_controll >= n_maxReplace ) break;
    }
    
    
    if( s_newTextData != '' )
    {
    	s_newTextData += RegExp.rightContext;
    	document.getElementById( this.getOption(this.s_optionField) ).innerHTML = s_newTextData;
    }
	},
	
	setSpecialKeywords: function( objectProdukt, matcher )
	{
			s_TextLink = objectProdukt;
			
			nGesamt = s_TextLink.length;
			nTmp = s_TextLink.indexOf( '{SUCHWORT}' );
			if( nTmp > -1 )
			{
				s_rightSide = s_TextLink.substr( nTmp+10, nGesamt );
				s_TextLink = s_TextLink.substr( 0, nTmp ) + matcher + s_rightSide;
			}
						
			return s_TextLink;
	},
	
	getTop: function( o_object )
	{
		return (o_object.offsetParent) ? o_object.offsetTop + this.getTop(o_object.offsetParent):o_object.offsetTop;
	},
	
	getLeft: function( o_object )
	{
		return (o_object.offsetParent) ? o_object.offsetLeft + this.getLeft(o_object.offsetParent):o_object.offsetLeft;
	},
	
	placeLayer: function( n_topPosition, n_leftPosition, s_content, s_match )
	{
		o_product = this.a_allProducts[s_content];
		sTextLink = this.setSpecialKeywords( this.getNodeValue( o_product, 'link' ), s_match );
		
		s_myImage = '';
		if( this.getNodeValue( o_product, 'image' ) != '-' && this.getNodeValue( o_product, 'image' ) != undefined )
			s_myImage = '<a href="'+ sTextLink +'" target="_blank" rel="nofollow"><img src="'+ this.getNodeValue( o_product, 'image' ) +'" border="0" width="60" alt="'+ this.getNodeAttributes( o_product, 'title' ) +'" title="'+ this.getNodeAttributes( o_product, 'title' ) +'" /></a>';
			
		s_myViewImage = '';
		if( this.getNodeValue( o_product, 'viewlink' ) != '-' )
			s_myViewImage = '<img src="'+ this.getNodeValue( o_product, 'viewlink' ) +'" border="0" style="position:absolute;border:0px;" width="1" height="1" />';
 							 	 	
 		s_myContnt = '<strong style="color:#'+ this.getOption('headercolor') +';"><u>'+ this.getNodeAttributes( o_product, 'title' ) +'</u></strong><p style="color:#'+ this.getOption('textcolor') +';">'+ this.setSpecialKeywords( this.getNodeValue( o_product, 'content' ), s_match ) +'</p>&raquo;&nbsp;<a href="'+ sTextLink +'" target="_blank"><u>'+ this.getNodeValue( o_product, 'linktext' ) +'</u></a>';
 		
		//Update 24.04.2009: Popup darf nicht über linke Fenstergrenze hinaus ragen
		pos_left = n_leftPosition-(245/2);
		if(pos_left<=0)
			pos_left = pos_left + (pos_left*-1) + 10;
		
		document.getElementById( 'flyingDiv' ).style.left = ( pos_left ) + 'px';
		document.getElementById( 'flyingDiv' ).style.top  = ( n_topPosition-(134) ) + 'px';
		document.getElementById( 'flyingDiv' ).innerHTML  = '<span><img src="'+ cClAD.s_basePath +'/images/round_x.png" onclick="cClAD.hideLayer(0);" width="12" height="12" alt="close" title="close" />'+ this.getNodeValue( o_product, 'viewlink' ) + '</span><div>'+ s_myImage + s_myViewImage + s_myContnt +'</div>';
		
		document.getElementById( 'flyingDiv' ).style.visibility = 'visible';
	},
	
	hideLayer: function( myFlag )
	{
		if( this.b_inBox || myFlag == 0 )
		{
			document.getElementById( 'flyingDiv' ).style.visibility = 'hidden';
			document.getElementById( 'flyingDiv' ).innerHTML = '';
			
			this.b_inBox = false;
		}
	},
	
	setInbox: function( bFlag )
	{
		this.b_inBox = !bFlag;
	},
	
	sleep: function()
	{
		window.setTimeout( "cClAD.hideLayer(1);", 200 );
	},
	
	sleepLong: function()
	{
		window.setTimeout( "cClAD.hideLayer(1);", 3000 );
	}
}
