$(document).ready(function(){
	$('#feedback .submit_button').click(function(e){
		e.preventDefault();
		$form = $(this).parents('form');
		$form.find('.error').remove();
		var email_filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		var formIsValid = true;
		
		var type = 'none';
		
		if ($form.find('textarea').val() != 'Feedback' && $form.find('textarea').val() != '') {
			type = 'feedback';
		}
		if ($form.find('.subscribe:checked').length>0) {
			if (type == 'feedback') {
				type = 'both';
			} else {
				type ='subscription';
			}
		}
		
		$form.find('.input input').each(function(){
			$(this).parents('.input').find('.error').remove();
			
			if($(this).hasClass('required') && ($(this).val() == '')){
				$(this).parents('.input').append('<div class="error">This field is required</div>');
				formIsValid = false;
			}
			if($(this).is('#email') && (!email_filter.test($(this).val()))){
				$(this).parents('.input').append('<div class="error">You should type a valid email</div>');
				formIsValid = false;
			}
		});
		
		if(type=='none') {
			$form.append('<div class="error">You have to leave feedback or check the box if you want to subscribe first.</div>');
			return false;
			
		}
		
		if(!formIsValid){
			return false;
		}
		
		if (type =='both' || type=='subscription') {
			$.post ('thanks-subscription.htm', $form.serialize(), function(data,status){
					var txt='Something goes wrong';
					if(status=='success'){
						txt = '<span class="feed-res">Thank you for subscription!</span>';
						
					}
					if (type!='both') {
							$form.html(txt);
					}
				}, 'html');
		}
		
		if (type =='both' || type=='feedback') {
			$.post ('thanks-feedback.htm', $form.serialize(), function(data,status){
					var txt='Something goes wrong';
					if(status=='success'){
						txt = '<span class="feed-res">Thank you for feedback!</span>';
					}
					if (type!='both') {
							$form.html(txt);
					} else {
						$form.html('<span class="feed-res">Thank you for feedback and subscription!</span>');
					}
					
				}, 'html');
			}
		});
		
	$('#poll .submit_button').click(function(z){
		
		z.preventDefault;
		
		$form = $(this).parents('form');
		var questionId = 'poll_side';
		var answerIds = [
			'vote_0',
			'vote_1'
		];
		var questionNames = [
			'Every day',
			'Once a week'
		];
		var currAnswer = $form.find('input:checked').attr('id');
		var currData = {
			question:questionId,
			answer:currAnswer
		};
		
		$.post('polls-vote.php', currData, function(data, textStatus) {
			
			if (textStatus == 'success') {
				var maxValue=0,result = [];
				for (i=0; i < answerIds.length; i++) {
					var val = data[answerIds[i]], name = questionNames[i];
					if(val>maxValue){maxValue = val};
					result[i] = '<li class="result"><span class="pr-name">'+name+'<span class="pr-bar" style="width:'+val/maxValue*100+'%"></span></span><span class="pr-val">'+val+'</span></li>';
				};
				result = result.join('');
				$form.find('.poll_radio').html(result);
			}
			
		}, 'json');
		return false;
	});
	/* Google Analytics goals */
	$('a[href^=casino/]').attr('target', '_blank');/*.click(function(){
		var href = $(this).attr('href');
		if (typeof(_gaq)!='undefined' && href.indexOf('//casino/')==-1) {
			trackPageview('click/'+href.replace(/http:\/\/[^\/]+\/[^\/]+/i, ''));
		}
		return true;
	});*/
});

$(window).load(function(){
	$('.img_onpage').each(function(){
		var thisAlign = $(this).attr('align');
		var thisText = $(this).attr('title');
		var thisWidth = $(this).width();
		
		$(this)
				.wrap('<div class="imgwrap imgwrap_'+thisAlign+'"></div>')
				.attr('align', '');
										   
		if ($(this).attr('title')) {
				$(this).after('<div class="img_title" style="width:'+thisWidth+'px">'+thisText+'</div>');
		}
	});
});
