$('document').ready( function() {
	var baseUrl = $('span[config-id=baseUrl]').text();
	var clicked = false;
	
	$(this).everyTime( 5000, 'update', function() {
		$.get( baseUrl+'live/ajaxlivebox', function(data) {	
			$( '#onLiveBox' ).remove();
			$( '#head' ).append( data );
		});
	});
	
	$('#write-to-newsletter').click( function() {
		if( clicked == false ) {
			$( this ).val('');
		}
		clicked = true;
	});
	
	$.datepicker.setDefaults( $.datepicker.regional[''] );
	$('.datepicker').datepicker( {
		dateFormat: 'dd-mm-yy',
		changeMonth: true,
		changeYear: true,
		yearRange: "1910:2011"
	} );
//	$('.datepicker').datepicker( 'option', $.datepicker.regional[$('.datepicker').attr('locale')] );
	
	$('#write-to-newsletter-btn').click( function() {
		var email = $('#write-to-newsletter').val();
		if( validateEmail(email) ) {
			$.get( baseUrl+'index/ajaxwritenewsletter/email/'+email, function(data) {
				var response = eval( "("+data+")" );
				
				$( '#newsletter p' ).html( response.content );
				if( response.status == true ) {
					$( '#write-to-newsletter, #newsletter p' ).addClass( 'success' );
				}
				else {
					$( '#write-to-newsletter, #newsletter p' ).addClass( 'error' );
				}
			});
		}
		else {
			$('#write-to-newsletter').addClass( 'error' );
		}
	});
	
	$( '#phones #systems' ).change( function() {
		var system = $(this).val();
		var brand  = $('#phones #brands').val();
		
		$( '#phones li' ).css( 'display', 'none' );
		if( system > 0 ) {
			$( '#phones li' ).each( function() {
				if( $(this).attr('system-id') == system ) {
					if( brand > 0 ) {
						if( $(this).attr('brand-id')==brand ) {
							$(this).css( 'display', '' );
						}
					}
					else {
						$(this).css( 'display', '' );
					}
				}
			});
		}
		else {
			if( brand > 0 ) {
				$( '#phones li[brand-id='+brand+']' ).css( 'display', '' );
			}
			else {
				$( '#phones li' ).css( 'display', '' );
			}
		}
	});
	
	$( '#phones #brands' ).change( function() {
		var brand  = $(this).val();
		var system = $('#phones #systems').val();
		
		$( '#phones li' ).css( 'display', 'none' );
		if( brand > 0 ) {
			$( '#phones li' ).each( function() {
				if( $(this).attr('brand-id') == brand ) {
					if( system > 0 ) {
						if( $(this).attr('system-id')==system ) {
							$(this).css( 'display', '' );
						}
					}
					else {
						$(this).css( 'display', '' );
					}
				}
			});
		}
		else {
			if( system > 0 ) {
				$( '#phones li[system-id='+system+']' ).css( 'display', '' );
			}
			else {
				$( '#phones li' ).css( 'display', '' );
			}
		}
	});
	
	$('.tab > div.bs').buttonset();
	
	var cache = {};
	$( '#search input[type=text]' ).autocomplete({
		minLength: 2,
		source: function(request,response) {
			var term = request.term;
			if( term in cache ) {
				response( cache[term] );
				return;
			}
			
			$.getJSON( baseUrl+'index/ajaxsearch', request, function(data) {
				cache[term] = data;
				response( data );
			});
		},
		select: function( event, ui ) {
			$( '#search input[type=text]' ).val( ui.item.value );
			document.form_search.submit();
		}
	});
	
	enable_adverb_slider( 10000, "adverbBox", 'slide' );
	enable_adverb_slider( 8000, "adverbBoxTop", 'fade' );
	enable_adverb_slider( 8000, "adverbBoxBottom", 'fold' );
});

function validateEmail( elementValue ) {  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  

	return emailPattern.test( elementValue );  
}

function enable_adverb_slider( _time, _id, _effect ) {
	var advIdx = 1;
	var id = "#"+_id;
	var num = $( id+' li' ).length;
	if( num < 2 ) {
		return;
	}
	
	$( this ).everyTime( _time, function() {
		$( id+' li' ).last().hide( _effect, 1800, function() {
			$( id+' ul' ).first().prepend( "<li>"+$(this).html()+"</li>" );
			$( id+' li' ).last().remove();
			
			if( advIdx == num ) {
				advIdx = 0;
			}
				
			advIdx++;
		});
	});
}
