/***************************************************************
*  Copyright notice
*
*  (c) 2008 ITSolving <http://www.itsolving.com/> and 3Digit <http://www.3digit.de/>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * 
 *
 * @author Ra�l Fernando Casallas Malaver <fernando.casallas@itsolving.com>
 */
var slides = new Array();
var reloadFunctions = new Array();

window.addEvent('domready', function(){
//-----Window.onDomReady(function(){
	var forms = $$('form.tx_3dgeventcommunity_form');
	var counter = 1;
	forms.each(
	              function( form )
	              {
	                  var id = 'tx_3dgeventcommunity_form_' + counter;
	                  form.setProperty('id' , id );	                  
	                  counter++;
	              }
	          );
	var divs = $$('div.newsDiv');
	divs.each(
	             function( div )
	             {
	                 var parent = div;
	                 while( true )
	                 {
	                     parent = parent.getParent();
	                     if( parent == null || parent.hasClass( 'tx_3dgeventcommunity_form' ))
	                         break;
	                 } 
	                 if( parent != null )
	                 {
		                 var match = parent.getProperty( 'id' ).match( /tx_3dgeventcommunity_form_([0-9]+)/ );
		                 var uidForm = match[1];
		                 div.setProperty( 'id', 'newsDiv_'+uidForm );
		             }
	             }
	         );	
	var divs = $$('div.foldout');
	divs.each(
	 		     function( div )
	 		     {
	 		        var slide = new Object();
					slide.slideId = div.getProperty( 'id' );
					slide.slideEvent = new Fx.Slide( slide.slideId );
					slide.slideEvent.hide();		
					slides[ slides.length ] = slide;
					//
	 		        var label = $( div.getProperty( 'id' )+'_label' );
	 		        label.addEvent( 'click',
	 		                        function( e )
	 		                        {
	 		                            e.stop();
	 		                            toggleOptions( div.getProperty( 'id' ) );	 		                            
	 		                        }
	 		                      );
					//
	 		     	var childrens = div.getChildren('input');
	 		     	childrens.each(
	 		     					function( input )
	 		     					{	 		     					    
	 		     					    input.addEvent('click',
	 		     					                        function( e )
	 		     					                        {	 		     					                            
	 		     					                            updateLabel( div.getProperty( 'id' ) );
	 		     					                        } 
	 		     					                   );
	 		     					}
	 		     				   );
	 		        updateLabel( div.getProperty( 'id' ) );
	 		          
	 		     }
			 );
//	image_enlargue();	
});

//reloadFunctions[ reloadFunctions.length ] = "image_enlargue()";

var imagesEnl  = new Object();
var imageCount = 0;
function image_enlargue()
{
	var imgs = $$('img');
	imgs.each(
		function( img )
		{
			img = $( img );
			var parent = img.getParent();
			if( parent.hasClass('image') )
			{				
				parent.addEvent('click', function( e )
										{
											e.stop();
											if( parent.getProperty('id') == null || parent.getProperty('id') == '' || parent.getProperty('id') == undefined )
												parent.setProperty('id', imageCount++ );
											
											if( imagesEnl[ parent.getProperty('id') ] == true )
										    {										        
										    	parent.morph('.imgNor');
										    	img.morph('.imgNor');
										    	imagesEnl[ parent.getProperty('id') ] = false;										    	
										    }
										    else
										    {
										        parent.morph('.imgBig');
										        img.morph('.imgBig');
										        imagesEnl[ parent.getProperty('id') ] = true;										        
										    }
										}
				);				
			}
		}
	);
}


function getform( el )
{
    var parent = $(el);
	while( true )
	{		
	    if( parent.hasClass( 'tx_3dgeventcommunity_form' ) )
	    	break;
	 	parent = parent.getParent();
	}	
	return parent; 
}

function getformid( el )
{
    var parent = $(el);
	while( true )
	{	    
	    if( parent.hasClass( 'tx_3dgeventcommunity_form' ))
	    	break;
	 	parent = parent.getParent();
	}	
	return parent.getProperty('id');
}


function getnewsdiv( el )
{
   var form = getform( el );
   var match = form.getProperty( 'id' ).match( /tx_3dgeventcommunity_form_([0-9]+)/ );
   var uidForm = match[1]; 
   var newsDiv = $( 'newsDiv_'+uidForm );
   return newsDiv;
}

function tx_3dgeventcommunity_send_aux( el )
{
     var ajaxForm = getform( el );
     var newsDiv  = getnewsdiv( el );     
     newsDiv.empty();
     newsDiv.addClass('ajax-loading');
     ajaxForm.set( 'send',
                     {
                         onComplete : function( response )
                         {
                            newsDiv.removeClass('ajax-loading');
						    newsDiv.set( 'html', response ); 
						    for( i = 0; i < reloadFunctions.length; i++ )
						    {
						    	setTimeout( reloadFunctions[ i ], 100 ); 						    
						        //eval( reloadFunctions[ i ] );
						    }
                         }
                     } 
                 );
     ajaxForm.send();
 }
 
 function tx_3dgeventcommunity_send( el )
 {
     var ajaxForm = getform( el );
     ajaxForm['tx_ttnews[pointer]'].value = '';     
     tx_3dgeventcommunity_send_aux( el );
 }
 
 function tx_3dgeventcommunity_pagebrowser( el, pointer )
 {
//     var scrollFx = new Fx.Scroll(window).toTop();
     var ajaxForm = getform( el );
     ajaxForm['tx_ttnews[pointer]'].value = pointer != null ? pointer : '';     
     tx_3dgeventcommunity_send_aux( el );
     var scroll = new Fx.Scroll(window).toElement(ajaxForm);
 }
 
 var news = Array();
 
 function tx_3dgeventcommunity_news_link_eid( url, newsId, newsTypeDesc, on )
 {
     var debug = false;     
     var btn        = $( newsTypeDesc+'Btn_'+newsId );
     var state      = $( newsTypeDesc+'Sta_'+newsId );
     var div        = $( newsTypeDesc+'Div_'+newsId );
     var counter    = $( newsTypeDesc+'_COUNT_'+newsId );
     //
    var object = null;
    for( i = 0; i < news.length; i++ )
    {
        if( news[ i ].newsId == newsId )
        {
            object = news[ i ];
            break;
        }
    }
    if( object == null )
    {
        object = new Object();
        object.newsId = newsId;
        object.on    = ( on == 'true' ); 
        news[ news.length ] = object;
    }   
    // 
    if( div != null )
    {
	    div.removeClass('hide');
	    div.addClass('ajax-loading');
	}
    
    var request = new Request.HTML(
               	                  	{
               	                        url: url,
               	                        onSuccess: function( html )
               	                        {
               	                            var htmlResponse = new Element('div');
				                            htmlResponse.adopt( html );
				                            var error = htmlResponse.get('html').search(/error/) != -1;
               	                            //
               	                            if( !error )
               	                            {
               	                                if( debug ) alert( 'counter: '+counter.get('html') );
					                            if( !object.on )
					                            {
					                               btn.value   = $( newsTypeDesc+'_btnText_'+newsId ).value;
					                               if( state != null )
					                                   state.value = $( newsTypeDesc+'_stateText_'+newsId ).value;
					                               counter.set( 'html', 1*counter.get('html') + 1 );  
					                            }
					                            else
					                            {
					                               btn.value   = $( newsTypeDesc+'_btnTextNot_'+newsId ).value;
					                               if( state != null )
					                                   state.value = $( newsTypeDesc+'_stateTextNot_'+newsId ).value;
					                               counter.set( 'html', 1*counter.get('html') - 1 );
					                            }
					                            if( debug ) alert( 'counter[after]: '+counter.get('html') );
					                        }
				                            //    
				                            if( div != null )
				                            {
					                            div.removeClass('ajax-loading');
											    div.set( 'html', htmlResponse.get('html') ); 
					                            div.addClass('hide');
					                        }
					                        else
					                        {
					                           if( error )
					                               alert( htmlResponse.get('html') );
					                        }
				                            object.on = !object.on;
               	                    	},
               	                    	onFailure: function( xhr )
               	                    	{
               	                    	    alert('ajax failure');
               	                    	}
                                    }
								  );		                    
	request.send();	

 }
 


function updateLabel( id )
{	
	var div = $( id );
    var label = $( div.getProperty( 'id' )+'_label' );
	var labelText = label.get('html');
	var match = labelText.match( /.([0-9]+)./ );

	var oldCount = match != null ? match[1] : 0; 	
	var newCount = 0;
	var childrens = div.getChildren('input');
	childrens.each(
					function( input )
	 		     	{	 		     					    
	 		     	    if( input.selected || input.checked )
	 		     	        newCount++;
	 		     	}	
	 		     );
    
	if( newCount == 0 )
	{
	   if( match != null )	   
	       labelText = labelText.substring(0, labelText.indexOf( oldCount ) -1 );	   
	}
	else
	{
		if( match != null )
		    labelText = labelText.replace( oldCount, newCount );
		else
		    labelText = labelText + ' ('+newCount+')';
	}
	label.set('html', labelText );
}
 
function toggleOptions( id )
{
	var el = $(id);
	var slide = null;
    for( i = 0; i < slides.length; i++ )
    {    	
        if( slides[ i ].slideId == id )
        {
            slide = slides[ i ];            
            break;
        }
    }
	if( slide == null )
	{
		slide = new Object();
		slide.slideId = id;
		slide.slideEvent = new Fx.Slide( id );
		slide.slideEvent.hide();		
		slides[ slides.length ] = slide;
	}
	slide.slideEvent.toggle();
	//slide.slideEvent.addEvent('complete', function() {
	//	alert( id + ' ' + slide.slideEvent.open );
	//});
	
}

function enter_pressed(e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return false;
return (keycode == 13);
}

function fieldSort( sort, fieldName, el2, ajaxForm )
{
    var match = ajaxForm.getProperty( 'id' ).match( /tx_3dgeventcommunity_form_([0-9]+)/ );
	var uidForm = match[1];
    if( fieldName == sort )
    {
    	if( el2.hasClass('btnSel') )
    	{
    		var actVle = ajaxForm['tx_ttnews[search_params][ascDesc]'].value;	                	
			if( actVle == 'DESC' )
			{
				el2.addClass( 'btnSelAsc' );
			    el2.removeClass( 'btnSelDesc' );
			    ajaxForm['tx_ttnews[search_params][ascDesc]'].value = 'ASC';
			}
			else
			{
			    el2.addClass( 'btnSelDesc' );
			    el2.removeClass( 'btnSelAsc' );
			    ajaxForm['tx_ttnews[search_params][ascDesc]'].value = 'DESC';
			}
    	}
    	else
    	{
    	    el2.removeClass( 'btn' );
			el2.addClass( 'btnSel' );
	        el2.addClass( 'btnSelDesc' );
	        ajaxForm['tx_ttnews[search_params][ascDesc]'].value = 'DESC';
    	}
    }
    else
    {
        el2.removeClass( 'btnSel' );
		el2.addClass( 'btn' );
        el2.removeClass( 'btnSelDesc' );
        el2.removeClass( 'btnSelAsc' );
    }
}

