/**
 * Loginbereich+Tiny
 * 
 * @copyright  Copyright (c) 2009 freshframes
 * @package Fresh_WWW
 * @version $Id: fresh.fx.js 377 2010-11-02 10:14:57Z riyuk $
 */


window.addEvent('domready', function(){
	
	if( !$defined( path ) ) {
		var path = $('freshFx').getProperty( 'rel' );
	}
	if( !$defined( notify ) ) {
		var notify = new Roar( {
		    container: 'stage',
		    position: 'upperLeft',
		    duration: 15000
		});
	}
	
	$$('a.tipp').each(function(element,index) {
		var content = element.get('title').split('::');
		element.store('tip:title', content[0]);
		element.store('tip:text', content[1]);
	});
	
	var Tips1 = new Tips( $$( '.tipp' ) );
	
	if ( $( 'login' ) ) {
		$( 'login' ).setStyle( 'height', 'auto' );
		var mySlide = new Fx.Slide( 'login' ).hide();  //starts the panel in closed state  
	
	    if ( $( 'toggleLogin' ) ) {
	    	$( 'toggleLogin' ).addEvent( 'click', function( e ){
				e = new Event( e );
				mySlide.toggle();
				e.stop();
			});
	    }
	    if ( $( 'closeLogin' ) ) {
		    $( 'closeLogin' ).addEvent( 'click', function( e ){
				e = new Event( e );
				mySlide.slideOut();
				e.stop();
			});
	    }
	}
	
	if( $( 'head_img' ) ) {
		
		$( 'head_img' ).setStyle( 'cursor', 'pointer' );
		$( 'head_img' ).addEvent( 'click', function( e ) {
			e = new Event( e );
			e.stop();
			window.location.href = path;
		});
	}
	

});

/**
 * Global functions
 * 
 */

/**
 * frSubmit
 * 
 * Validates form Submits
 */
function frSubmit( frm ) {
	
	if( $( frm ) ) {
		
		$( frm ).set( 'send', {
			'method': 'post',
			'onSuccess': function( resp ) {
				jsonObj = JSON.decode( resp );
				if( jsonObj && jsonObj.success ) {
					alert( 'success: ' + jsonObj.success );
				}
			}
		} );
		
		$( frm ).send();
		
	}
	
}

function flashLookup( param ) {
	
	if( param.indexOf( '\'' ) >= 0 ) return false;
	
	updateDiv = $( 'flashIt' );
	if( !updateDiv || null == updateDiv ) return false;
	
	var tmpSpinner = new Element( 'img', {
		'id': 'tmp_spinner',
		'src': path + 'images/spinner.gif',
		'border': 0,
		'styles': {
			'width': '16px',
			'height': '16px',
			'margin': '0'
		}
	} );
	
	if( $('tmp_spinner') ) $('tmp_spinner').destroy();
	updateDiv.empty();
	updateDiv.adopt( tmpSpinner );
	
	var request = new Request.JSON({
		url:		path,
		data: 		'api=flashLookup&search=' + escape( param ),
		method:		'get',
		onSuccess:	function(resp) {
			
			if( resp.result == true && resp.list ) {
				
				updateDiv.empty();
				
				for( i = 0; i < resp.list.length; i++ ) {
					updateDiv.adopt( new Element( 'p' ).adopt( 
							new Element( 'a', {
								html: resp.list[i].title,
								href: resp.list[i].url
						})
					));
				}
				
			} else {
				updateDiv.empty();
				updateDiv.adopt( new Element( 'p' ).set( 'html', 'No matches found.' ) );
			}
			if( $('tmp_spinner') ) $('tmp_spinner').destroy();
		}
	}).get();
}



function flashGet( param ) {
	updateDiv = $( 'flashIt' );
	if( !updateDiv || null == updateDiv ) return false;
	
	var tmpSpinner = new Element( 'img', {
		'id': 'tmp_spinner',
		'src': path + 'images/spinner.gif',
		'border': 0,
		'styles': {
			'width': '16px',
			'height': '16px',
			'margin': '0'
		}
	} );
	
	if( $('tmp_spinner') ) $('tmp_spinner').destroy();
	$( 'legislation' ).setStyle( 'display', 'none' );
	updateDiv.empty();
	updateDiv.adopt( tmpSpinner );
	
	if( param == 'usa' ) param = 'us';
	
	switch( param ) {
		case 'de': // germany
		case 'pt': // portugal
		case 'no': // norway
		case 'mt': // malta 
		case 'cl': // chile
		case 'us': // usa 
		case 'fr': // france 
		case 'gr': // greece
		case 'ch': // swiz
		case 'se': // sweden
		case 'ie': // ireland
		case 'nl': // netherlands
		case 'cz': // czech
		case 'au': // australia
		case 'nz': // new zealand
		case 'uk': // united kingdom
		case 'at': // austria
		case 'dk': // denmark
		case 'pl': // poland
		case 'es': // spain
		case 'be': // belgium
		case 'it': // italy
		case 'hr': // croatia
		case 'ru': // russia
		case 'lk': // sri lanka
		case 'sk': // slovakia
		case 'slo': // slovenia
		case 'fi': // finland
		case 'il': // israel
		case 'hu': // hungary
		case 'bg': // bulgaria
			//do something
			
			var request = new Request.JSON({
				url:		path,
				data: 		'api=flashGet&lang=' + param,
				method:		'get',
				onSuccess:	function(resp) {
					
					if( resp.result == true && resp.list ) {
						
						updateDiv.empty();
						
						$( 'linkLegislation' ).set( 'href', $( 'staticLegislation' ).get( 'html' ) + param );
						$( 'legislation' ).setStyle( 'display', 'block' );
						
						for( i = 0; i < resp.list.length; i++ ) {
							updateDiv.adopt( new Element( 'p' ).adopt( 
								new Element( 'a', {
									html: resp.list[i].title,
									href: resp.list[i].url
								})
							));
						}
						
					} else {
						// no matches
						updateDiv.empty();
						if( resp.nomatch && resp.nomatch.length ) {
							updateDiv.set( 'html', resp.nomatch );
						}
					}
				}
			}).get();
	
			break;
		case 'collegues':
			// handle collegues
			
			var request = new Request.JSON({
				url:		path,
				data: 		'api=flashGet&lang=' + param,
				method:		'get',
				onSuccess:	function(resp) {
					
					if( resp.result == true && resp.list ) {
						
						updateDiv.empty();
						
						if( !resp.collegues || resp.collegues != true ) {
							$( 'linkLegislation' ).set( 'href', $( 'staticLegislation' ).get( 'html' ) + param );
							$( 'legislation' ).setStyle( 'display', 'block' );
						}
						
						for( i = 0; i < resp.list.length; i++ ) {
							updateDiv.adopt( new Element( 'p' ).adopt( 
									new Element( 'a', {
										html: resp.list[i].title,
										href: resp.list[i].url
								})
							));
						}
					} else {
						// no matches
						updateDiv.empty();
						if( resp.nomatch && resp.nomatch.length ) {
							updateDiv.set( 'html', resp.nomatch );
						}
					}
				}
			}).get();
			
			break;
		default: {
			updateDiv.empty();
			break;
		}
	}
}

