var sPredefined = '';
var sContest = getParam('is_contest');
var sCardType = 'mastercard';

$(document).ready(function() {
	bindSteps();
	
	//	Thumbnails, second page
	$('#imageCategory').change(function() {
		changeImageGroup($(this).val());
	});
	$('.thumbnails img').click(function() {
		$(this).parent().parent().find('img.active').removeClass('active');
		$(this).addClass('active');
		sPredefined = $(this).attr('file');
	});
	
	//	Checkbox "I agree", first page
	$('#iagree').click(function() {
		validateFirstPage();
	});
	
	$('#cardType').change(function() {
		$('#cardtype').val($(this).val());
		sCardType = $(this).val();
	});
	
	$('#userimage').change(function() {
		validateSecondPage();
	});
	$('#iknow').click(function() {
		validateSecondPage();
	});
	
	if(bFileSizeSmallWrong || bFileSizeBigWrong || bFileExtWrong){
		showErrorOnSecondPage();
	}
	
	if(sContest){
		$('#is_contest').val(sContest);
	}
	
	if(getParam('is_contest')){
		$('#imageCategory').attr('disabled', 'disabled');
		$('#docTitle').html('Создание работы на&#160;конкурс дизайна банковской карты');
	}
	
	validateFirstPage();
	validateSecondPage();
});

function bindSteps(){
	$('#nextStep1').click(function() {
		if(!$(this).hasClass('disabled')){
			showStep(2);
		}
		return false;
	});
	
	$('#prevStep2').click(function() {
		showStep(1);
		return false;
	});
	
	$('#nextStep2').click(function() {
		if(!$(this).hasClass('disabled')){
			if(sPredefined != '' && sCardType != ''){
				$(this).attr('href', '/retail/cards/design/demo_edit/?predefined=' + sPredefined + '&cardtype=' + sCardType + '&is_contest=' + sContest);
			}else{
				$('#imageUpload').submit();
				return false;
			}
		}
	});
}

function showStep( iStep ){
	for(var i=1; i<=2; i++){
		$('#step' + i).hide();
	}
	$('#step' + iStep).show();
}

function changeImageGroup( sName ){
	$('.thumbnails:visible').hide();
	if(sName == 'own'){
		$('#images').hide();
		$('#own').show();
		sPredefined = '';
	}else{
		$('#' + sName + '_group').show();
		$('#images').show();
		$('#own').hide();
		sPredefined = $('#' + sName + '_group img:first').attr('file');
	}
	
	validateSecondPage();
}

function validateFirstPage(){
//	if($('#iagree').attr('checked')){
		$('#nextStep1').removeClass('disabled');
// 	}else{
// 		$('#nextStep1').addClass('disabled');
// 	}
}

function validateSecondPage(){
	if(sPredefined != '' && sCardType != ''){
		$('#nextStep2').removeClass('disabled');
	}else{
		//	Validate input:image
		
		if($('#iknow:checked').length == 0 || $('input[name="userimage"]').val() == '' || !checkFilesExtLocal('userimage','.jpg.jpeg')){
			$('#nextStep2').addClass('disabled');
		}else{
			$('#nextStep2').removeClass('disabled');
		}
	}
}

function checkFilesExtLocal(fieldName, extensions){
	field = $('input[name=' + fieldName + ']');
	valueLength = field.val().toLowerCase().length;
	lengthToTrim = valueLength - 4;
	fieldValue = field.val().toLowerCase();
	fExtension = fieldValue.substr(lengthToTrim);

	if(extensions.match(fExtension)) {
		return(1);
	} else {
		return(0);
	}
}

//	Show error about unsupported image format or incorrect image size
function showErrorOnSecondPage(){
	$('#iagree').attr('checked', 'checked');
	$('#nextStep1').removeClass('disabled');
	showStep(2);
	if(bFileExtWrong){
		$('#error_wrongformat').show();
	}
	if(bFileSizeSmallWrong){
		$('#error_smallimage').show();
	}
	if(bFileSizeBigWrong){
		$('#error_bigimage').show();
	}
}
