
var newsletter = {
	
	signUp : function( _email , _name ){
		
		var _form = _email.closest('form');
		
		if( _name.val() == '' || _name.val() == _name.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg a nevét!' ).show();
			return;
		}
		else if( _email.val() == '' || _email.val() == _email.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg e-mail címét!' ).show();
			return;
		}
		
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_newsletter',
				method: 'add',
				email: _email.val(),
				name: _name.val()
			},
			function( response ){
				
				if( response.error == 0 ) {
					window.location.href = response.redirect_url;
				}
				else {
					_form.find('.box-syserror').html( response.msg ).show();
				}
				
			},
			'json'
		);
		
	}
	
};

var callmeback = {
	
	send : function( _name , _phone ){
		
		var _form = _name.closest('form');
			
		if( _name.val() == '' || _name.val() == _name.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg a nevét!' ).show();
			return;
		}
		else if( _phone.val() == '' || _phone.val() == _phone.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg a telefonszámát!' ).show();
			return;
		}
			
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_callmeback',
				method: 'send',
				email: _name.val(),
				name: _phone.val()
			},
			function( response ){
				
				if( response.error == 1 ) {
					_form.find('.box-syserror').html( response.msg ).show();
				}
				else {
					_form.find('.box-syssuccess').html( response.msg ).show();
				}
				
			},
			'json'
		);
	}
	
};

var vote = {
	
	save : function( _form ){
		
		var _vote_id = _form.find('input[name="vote_id"]');
		var _voteoption_id = _form.find('input[type="radio"]:checked');
		
		
		if( _voteoption_id.length == 1 ) {
			if( _vote_id.attr('voted') == '1' ) {
				_form.find('.box-syserror').html('Ön már egyszer szavazott.').show();
				return;
			}

			$.post(
				base_url + 'ajax.php',
				{
					module: 'public_vote',
					method: 'save',
					vote_id: _vote_id.val(),
					voteoption_id: _voteoption_id.val()
				},
				function( response ){
					if( response.error == 0 ) {
						_form.closest('.box-content').html( response.html );
					}
				},
				'json'
			);
		}
		else {
			_form.find('.box-syserror').html( 'Kérem, adjon meg egy választ!' ).show();
		}
		
	},
	
	showResult : function( voteID ) {
				
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_vote',
				method: 'getResult',
				vote_id: voteID
			},
			function( response ){
				$('#sidebox-vote').html( response.html );
				/*window.location.href = '#top';*/
			},
			'json'
		);
		
	}
	
};

var productContact = {
	
	send : function( _form ){
		
		var _name = _form.find('input[name="name"]');
		var _email = _form.find('input[name="email"]');
		var _contactperson_id = _form.find('input[name="contactperson_id"]');
		var _product_id = _form.find('input[name="product_id"]');
		var _subject = _form.find('input[name="subject"]');
		var _message = _form.find('textarea[name="message"]');
		
		if( _name.val() == '' || _name.val() == _name.get(0).defaultValue ) {
			//alert( 'Kérem adja meg a nevét!' );
			$('#box-contactperson').find('.box-syserror').html( 'Kérem adja meg a nevét!' ).show();
			return;
		}
		else if( _email.val() == '' || _email.val() == _email.get(0).defaultValue ) {
			//alert( 'Kérem adja meg e-mail címét!' );
			$('#box-contactperson').find('.box-syserror').html( 'Kérem adja meg az e-mail címét!' ).show();
			return;
		}
		else if( _subject.val() == '' || _subject.val() == _subject.get(0).defaultValue ) {
			//alert( 'Kérem adjon meg tárgyat!' );
			$('#box-contactperson').find('.box-syserror').html( 'Kérem adjon meg tárgyat!' ).show();
			return;
		}
		else if( _message.val() == '' || _message.val() == _message.get(0).defaultValue ) {
			//alert( 'Kérem adjon meg tárgyat!' );
			$('#box-contactperson').find('.box-syserror').html( 'Kérem adjon meg üzenetet!' ).show();
			return;
		}
			
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_productContact',
				method: 'sendMail',
				name: _name.val(),
				email: _email.val(),
				contactperson_id: _contactperson_id.val(),
				product_id: _product_id.val(),
				subject: _subject.val(),
				message: _message.val()
			},
			function( response ){
								
				if( response.error == 1 ) {
					$('#box-contactperson').find('.box-syserror').html( response.msg ).show();
				}
				else {
					window.location.href = response.redirect_url;
				}
				
			},
			'json'
		);
	}
	
};

var contact = {
	
	send : function( _form ){
				
		var _name = _form.find('input[name="name"]');
		var _email = _form.find('input[name="email"]');
		var _message = _form.find('textarea');
		
		if( _name.val() == '' || _name.val() == _name.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg a nevét!' ).show();
			return;
		}
		else if( _email.val() == '' || _email.val() == _email.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem adja meg e-mail címét!' ).show();
			return;
		}
		else if( _message.val() == '' || _message.val() == _message.get(0).defaultValue ) {
			_form.find('.box-syserror').html( 'Kérem töltse ki az Üzenetek mezőt!' ).show();
			return;
		}
			
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_contact',
				method: 'sendMail',
				name: _name.val(),
				email: _email.val(),
				message: _message.val()
			},
			function( response ){
								
				if( response.error == 1 ) {
					_form.find('.box-syserror').html( response.msg ).show();
				}
				else {
					window.location.href = response.redirect_url;
				}
				
			},
			'json'
		);
	}
	
};

var request = {
	
	send : function( _form , _event ){
				
		var _name = _form.find('input[name="name"]');
		var _email = _form.find('input[name="email"]');
		
		if( _name.val() == '' || _name.val() == _name.get(0).defaultValue ) {
			$('.box-main-request').find('.box-syserror').html( 'Kérem adja meg a nevét!' ).show();
			$('body').get(0).scrollTop = 0;
			return;
		}
		else if( _email.val() == '' || _email.val() == _email.get(0).defaultValue ) {
			$('.box-main-request').find('.box-syserror').html( 'Kérem adja meg e-mail címét!' ).show();
			$('body').get(0).scrollTop = 0;
			return;
		}
		else if( $('.productCheckbox:checked').length < 1 ) {
			$('.box-main-request').find('.box-syserror').html( 'Kérem válasszon termékeink közül!' ).show();
			$('body').get(0).scrollTop = 0;
			return;
		}
			
		$.post(
			base_url + 'ajax.php',
			{
				module: 'public_request',
				method: 'sendMail',
				email: _email.val()
			},
			function( response ){
								
				if( response.error == 1 ) {
					$('.box-main-request').find('.box-syserror').html( response.msg ).show();
					$('body').get(0).scrollTop = 0;
					return;
				}
				else {
					_form.submit();
				}
				
			},
			'json'
		);
	}
	
};

var slideshow = {
	slides : [],
	actual : 0,
	
	next: function(){
				
		$('.slide:eq('+parseInt(slideshow.actual)+')').fadeToggle(1200,function(){
			
			slideshow.actual--;

			if( slideshow.actual == 0 ) {
				slideshow.actual = slideshow.slides.length;
				$('.slide').show();
			}
			
		});
				
	}
}

var slideshow_timer;

$(function() {
	
	/* Input elemek tartalmának kezelése */
	$('input[type="text"]').focus( function(){
		if( $(this).val() == $(this).get(0).defaultValue ) {
			$(this).val('');
		}
	} ).blur( function(){
		if( $(this).val() == '' ) {
			$(this).val( $(this).get(0).defaultValue );
		}
	} );
	/* -- */


	/* SLIDESHOW */
	/*
	$('.scrollable').scrollable( {
		circular: true , 
		onSeek: function( event , item ){
			$('.slide .box-slideContent').fadeIn( 1000 );
		}
	} );

	$('.left').click( function( ev ){
		window.clearInterval( window.slideshow_timer );
		ev.preventDefault();
		slideshow.prev();
	} );
	*/

	$('.right').click( function( ev ){
		window.clearInterval( window.slideshow_timer );
		ev.preventDefault();
		slideshow.next();
	} );
	
	window.slideshow_timer = window.setInterval( function() {
		slideshow.next();
	} , 7000 );

	if( $('#box-main').height() > $('#box-sidebar').height() ) {
		$('.sidebar').height( $('#box-box-main').height() );
	}
	else {
		$('#box-main').height( $('#box-sidebar').height() );
	}
	/* -- SLIDESHOW -- */
	
	/* NEWSLETTER */
	$('form.newsletterForm').submit( function(ev){
		
		ev.preventDefault();
		var _email = $(this).find('input[name="email"]');
		var _name = $(this).find('input[name="name"]');
				
		newsletter.signUp( _email , _name );
		
	} );
	/* -- NEWSLETTER -- */
	
	/* CALLMEBACK */
	$('form.callmebackForm').submit( function(ev){
		
		ev.preventDefault();
		var _name = $(this).find('input[name="name"]');
		var _phone = $(this).find('input[name="phone"]');
				
		callmeback.send( _name , _phone );
		
	} );
	/* -- CALLMEBACK -- */
	
	
	/* SZAVAZÁS */
	$('form.voteForm').submit( function(ev){
		
		ev.preventDefault();
		vote.save( $(this) );
		
	} );
	/* -- SZAVAZÁS -- */
	
	/* Levél a kapcsolattartónak */
	$('form.productContactForm').submit( function(ev){
		
		ev.preventDefault();
		productContact.send( $(this) );
		
	} );
	/* --  -- */
	
	/* Levél általános */
	$('form.contactForm').submit( function(ev){
		
		ev.preventDefault();
		contact.send( $(this) );
		
	} );
	/* --  -- */
	
	/* Ajánlatkérés */
	$('form.requestForm input[type="submit"]').click( function(ev){
		
		ev.preventDefault();
		request.send( $(this).closest('form') , ev );
		
	} );
	/* --  -- */

});
