/*CORE.register('debug.dummy').load(
			'https://getfirebug.com/firebug-lite-beta.js','text/javascript',
			function(){
				console.log && console.log('Console Test');	
			},
 'ASYNC');*/
			




CORE.register('site.handler').define({
	self : window.CORE,
	
	registerSiteModul : function(){
		var that = this.self;
		
		this.sys.Event.bind('dom.ready', function(){
			if(!!that.site){
				switch(that.site){
					case 'arbeiten' :
						CORE.modules['site.arbeiten'].start();
					break;
					case 'kontakt' :
						CORE.modules['site.kontakt'].start();
					break;
				}
			}
		});
	}
}).registerSiteModul();


CORE.register('site.arbeiten').define({			
			textBar : false,
			filters : [],
			start : function(){
					var that = this;
					
					
					//precache closing cross
					var img = new Image();
					img.src = '/images/cross.png';
					
					if( window.location.hash ){
						this.filterByHash(	window.location.hash );
					}
										
					// Filter Function					
					$('nav.sub li').hover(function(){							
							$(this).addClass('prohibe');							
							$(this).parent().find('li').not('.prohibe').stop().animate({ opacity: 0.8},200, function(){ $(this).parent().find('li').removeClass('prohibe')}); 	    
					}, function(){
							$(this).parent().find('li').stop().animate({ opacity: 1 }, 200);	
					});
					
					$('nav.sub li').bind('click', CORE.modules['site.arbeiten'].filter );
			},
			
			keyMap : {
				'website' : 'Wir bieten ihnen das komplett Paket, Konzept, Design, Umsetzung und Hosting - alles aus einer Hand. Der Einsatz neuster Technologien gibt uns die M&ouml;glichkeit, ein modernes und zukunftsweisende Webdesign anzubieten.',
				'app' : 'Bis zum Produkt launch bleibt diese Kategorie leer',
				'print' : 'Wir gestalten alles von Ihrem Firmen Corporate Design bis zur Visitenkarte. Gerne gestalten wir auch ihre Anzeigen, Flyer und Plakate f&uuml;r Sie.',
				'identity' : ''				
			},
			
			filterByHash : function( hash ){
					var cat = hash.split('/'), found = [];
										
					$.each( cat , function(i,val){
							if( CORE.utils.array.contains(['website','print','identity','app'],val)) found.push(val);
					});
					
					if( found.length > 0 ){
						this.filter(0, found, true);
					}
			},
			
			unfilter : function(){
					var self = CORE.modules['site.arbeiten'];
				
					CORE.utils.array.remove( $(this).parent().attr('data-content'), self.filters);
						
					if( self.filters.length === 1 ){
								self.showDescription( self.filters[0], $('#work_sampels'));
								$('#work_sampels').animate({ width: '450px' });
								$('#filter').text('Filtered by');								
					} else {
								$('#filter').text('Filter by');
								$('#description').animate({ marginLeft: '-1050px'}, 500);
								$('#work_sampels').animate({ width: '100%' });
					}
					
					self.filterItems('unfilter');
					self.hash(self.filters);
					
					var li = $(this).parent().removeClass('active');
					$(this).remove();
					
					window.setTimeout( function(){
						li.bind('click',  CORE.modules['site.arbeiten'].filter );
					},13);
			},
			
			hash : function( newHash ){					
					window.location.hash = '#!/' + newHash.join('/');
			},
			
			filter : function( event, cat, hacky ){
					var category = cat || $(this).attr('data-content'),
						element = $('#work_sampels').addClass('three').parent(),
						self = CORE.modules['site.arbeiten'],
						thisObj = this;
							
							if( hacky ){
								self.filters = category;
							} else {
							   	self.filters.push(category);
							}
							
							self.hash(self.filters);
													
							if( self.filters.length === 1 ){
								self.showDescription( category, element );
								$('#work_sampels').animate({ width: '450px' });
								$('#filter').text('Filtered by');								
							} else {
								$('#description').animate({ marginLeft: '-1050px' }, 500);
								$('#work_sampels').animate({ width: '100%' });
							}
							
							if(!hacky){	
								var closing = $('<img />',{
											src : '/images/cross.png',
											click : CORE.modules['site.arbeiten'].unfilter,
											class : 'filterClose'
								});
								$(this).append(closing);							
								$(this).addClass('active');					
								$(this).unbind('click');
							} else {
								$.each( category , function(i,val){
									var closing = $('<img />',{
											src : '/images/cross.png',
											click : CORE.modules['site.arbeiten'].unfilter,
											class : 'filterClose'
										});
									var elem = $('#f_' + val );
									
									elem.append(closing);									
									elem.addClass('active');
									window.setTimeout( function(){	elem.unbind('click');},13);								
								});
							}
							
							self.filterItems.call(self,'filter');

			},
			
			filterItems : function(by){
					var that = this;
						li = $('#work_sampels');
				
					if( this.filters.length === 0){
						li.find('li').show();			
					} else {
					
						li.find('li').each(function(){									
										if( CORE.utils.array.contains(that.filters , $(this).attr('data-type'))){
											$(this).fadeIn(500);
										} else {
											$(this).fadeOut(500);
										}									
						});		
					}
			},
				
			
			showDescription : function(category){
				$('#description').html('<h4>'+ category +'</h4><p>' + this.keyMap[category] +'</p>').animate({ marginLeft: '0px'});
			}
});


CORE.register('site.kontakt').define({
		start: function(){
			var that = this;
			$('#kontaktSubmit').bind('click' , function(e){
				e.preventDefault();											
				that.validate();										
			});
		},
		
		validate : function(){
			var data =	{
					name : {
						element : $('#kontaktName'),
						rule : 'minlength',
						param : 5,
					},
					email : {
						element : $('#kontaktMail'),
						rule : 'email',
					},
					message : {
						element: $('#kontaktMessage'),
						rule : 'minlength',
						param : 30,
					},
			}
				
			var validationError = this.check(data);
			
			if( validationError ){
				//remove previous error
				$('.errorMessage').remove();
				$.each( data , function(i,item){
					$(item.element).removeClass('formError');
				});
				
				$.each( data , function(i,item){
					if(item.error){
						$(item.element).addClass('formError').parent().find('label').append('<span class="errorMessage">' +  item.error + '</span>');
						$('.errorMessage').fadeIn(500);
					}
				});
			} else {
				
			}
	
		},
		
		check : function( formData ){
			var validationError = false, that = this;
			
			$.each( formData , function (i,item){		
				item.value = $(item.element).val();
				
				switch(item.rule){
					case 'minlength' : 
						if(!that.minlength(item.value,item.param)){
							formData[i].error = that.errorDescrition[ item.rule ].replace(/\%\i\%/, item.param );
							validationError = true;
						}
					break;
					case 'maxlength' : 
						if(!that.maxlength( item.value, item.param )){
							formData[i].error = that.errorDescrition[ item.rule ].replace(/\%\i\%/, item.param);
							validationError = true;
						}
					break;
					case 'email' :
						if(!that.email(item.value)){
							formData[i].error = that.errorDescrition[ item.rule ].replace(/\%\i\%/, item.param);
							validationError = true;
						}
					break;
				}
			});
			
			return validationError;
		},
				
		minlength: function(value, param) {
			return CORE.utils.string.trim(value).length > param;
		},
		
		
		maxlength: function(value, param) {
			return CORE.utils.string.trim(value).length < param;
		},
		
		email: function(value) {
			// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
			return  /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
		},
		
		errorDescrition : {
			'minlength' : 'Bitte mehr als %i% Buchstaben angeben',
			'maxlength' : 'Ihre Nachricht ist zu lang, maximal %i% Zeichen',
			'email' : 'Bitte richtige Email Adresse angeben'
		}

});

