var logos = new Array();
var logos_dim = new Array();
var loading_uploads_changeable = true;

var thermos_logo1, thermos_logo2, thermos_logo3;

function reset_thermos_upload_logos() {
	logos.length = 0;
	logos_dim.length = 0;
	
	loading_uploads_changeable = true;
	
	$('#upload_logos_next').css('opacity', 0.3);
	$('#upload_logos_skip').css('opacity', 1.0);
	
	$('#logo1').val('');
	$('#logo2').val('');
	$('#logo3').val('');
	
	$('#step_thermos_upload_logos_loading').slideUp(400);
	$('#step_thermos_upload_logos_error').slideUp(400);
}

function thermos_check_upload() {
	var no_logos = 0;
	
	if(thermos == 'thermos1'){
		no_logos = 1;
	}else if(thermos == 'thermos2'){
		no_logos = 2;
	}else if(thermos == 'thermos3'){
		no_logos = 1;
	}else if(thermos == 'thermos4'){
		no_logos = 1;
	}else if(thermos == 'thermos5'){
		no_logos = 3;
	}
	
	if(
		(
			($('#logo1').val() != '' && no_logos == 1) ||
			(($('#logo1').val() != '' || $('#logo2').val() != '') && no_logos == 2) ||
			(($('#logo1').val() != '' || $('#logo2').val() != '' || $('#logo3').val() != '') && no_logos == 3)
		) && loading_uploads_changeable
		){
		
		$('#upload_logos_next').stop().animate({'opacity': 1.0}, 400);
	}
}

function thermos_upload_fail() {
	if($('.current_step').attr('id') == 'step_thermos_upload_logos'){
		if(loading_uploads_changeable){
			var new_height = 0;
			
			new_height += parseInt($('.step_header', $('#step_thermos_upload_logos')).height());
			new_height += parseInt($('.step_header', $('#step_thermos_upload_logos')).css('margin-top'));
			new_height += parseInt($('.step_header', $('#step_thermos_upload_logos')).css('margin-bottom'));
			
			new_height += parseInt($('.step_content', $('#step_thermos_upload_logos')).height());
			new_height += parseInt($('.step_content', $('#step_thermos_upload_logos')).css('margin-top'));
			new_height += parseInt($('.step_content', $('#step_thermos_upload_logos')).css('margin-bottom'));
			
			$('#step_thermos_upload_logos').animate({'height': new_height}, 400);
			
			$('#step_thermos_upload_logos_loading').slideUp(400);
			$('#step_thermos_upload_logos_error').delay(500).slideDown(400);
			
			$('#upload_logos_next').stop().delay(900).animate({'opacity': 1.0}, 400);
			$('#upload_logos_skip').stop().delay(900).animate({'opacity': 1.0}, 400);
		}else{
			setTimeout('thermos_upload_fail()', 200);
		}
	}
}

function thermos_upload_load_next() {
	var temp_next = true;
	
	if(thermos_logo1 != undefined){
		logos_dim[0] = new Array();
		logos_dim[0]['w'] = thermos_logo1.width;
		logos_dim[0]['h'] = thermos_logo1.height;
		
		if(logos_dim[0]['w'] == undefined || logos_dim[0]['h'] == undefined || logos_dim[0]['w'] <= 0 || logos_dim[0]['h'] <= 0){
			temp_next = false;
		}
	}
	
	if(thermos_logo2 != undefined){
		logos_dim[1] = new Array();
		logos_dim[1]['w'] = thermos_logo2.width;
		logos_dim[1]['h'] = thermos_logo2.height;
		
		if(logos_dim[1]['w'] == undefined || logos_dim[1]['h'] == undefined || logos_dim[1]['w'] <= 0 || logos_dim[1]['h'] <= 0){
			temp_next = false;
		}
	}
	
	if(thermos_logo3 != undefined){
		logos_dim[2] = new Array();
		logos_dim[2]['w'] = thermos_logo3.width;
		logos_dim[2]['h'] = thermos_logo3.height;
		
		if(logos_dim[2]['w'] == undefined || logos_dim[2]['h'] == undefined || logos_dim[2]['w'] <= 0 || logos_dim[2]['h'] <= 0){
			temp_next = false;
		}
	}
	
	if(temp_next){
		$('#step_thermos_upload_logos_loading').slideUp(400);
		
		setTimeout('show_next()', 400);
	}else{
		setTimeout('thermos_upload_load_next()', 400);
	}
}

function thermos_upload_success(images) {
	if($('.current_step').attr('id') == 'step_thermos_upload_logos'){
		if(loading_uploads_changeable){
			if(images[0] != undefined){
				logos[0] = images[0].name;
				
				thermos_logo1 = new Image;
				thermos_logo1.src = 'upl/' + logos[0];
			}
			
			if(images[1] != undefined){
				logos[1] = images[1].name;
				
				thermos_logo2 = new Image;
				thermos_logo2.src = 'upl/' + logos[1];
			}
			
			if(images[2] != undefined){
				logos[2] = images[2].name;
				
				thermos_logo3 = new Image;
				thermos_logo3.src = 'upl/' + logos[2];
			}
			
			thermos_upload_load_next()
		}else{
			setTimeout(function() { thermos_upload_success(images); }, 200);
		}
	}
}

function thermos_upload_logos() {
	$('#ajax-temp').remove();
	
	var iframe = document.createElement('iframe');
	
	iframe.setAttribute('id', 'ajax-temp');
	iframe.setAttribute('name', 'ajax-temp');
	iframe.setAttribute('style', 'display: none;');
	
	iframe.setAttribute('width', '0');
	iframe.setAttribute('height', '0');
	
	document.upload_form2.parentNode.appendChild(iframe);
	
	window.frames['ajax-temp'].name = 'ajax-temp';
	
	document.upload_form2.setAttribute('target', 'ajax-temp');
	document.upload_form2.setAttribute('action', 'ajax_upload_logo.php');
	document.upload_form2.setAttribute('method', 'post');
	document.upload_form2.setAttribute('enctype', 'multipart/form-data');
	document.upload_form2.setAttribute('encoding', 'multipart/form-data');
	
	document.upload_form2.submit();
}

function start_before_thermos_upload_logos() {
	thermos_check_upload();
	
	var no_logos = 0;
	
	if(thermos == 'thermos1'){
		no_logos = 1;
		
		$('#logos_placement img').attr('src', 'img/thermoses/thermos1_placement.jpg');
	}else if(thermos == 'thermos2'){
		no_logos = 2;
		
		$('#logos_placement img').attr('src', 'img/thermoses/thermos2_placement.jpg');
	}else if(thermos == 'thermos3'){
		no_logos = 1;
		
		$('#logos_placement img').attr('src', 'img/thermoses/thermos3_placement.jpg');
	}else if(thermos == 'thermos4'){
		no_logos = 1;
		
		$('#logos_placement img').attr('src', 'img/thermoses/thermos4_placement.jpg');
	}else if(thermos == 'thermos5'){
		no_logos = 3;
		
		$('#logos_placement img').attr('src', 'img/thermoses/thermos5_placement.jpg');
	}
	
	if(no_logos == 1){
		$('#upload_logo1').show();
		$('#upload_logo2').hide();
		$('#upload_logo3').hide();
	}else if(no_logos == 2){
		$('#upload_logo1').show();
		$('#upload_logo2').show();
		$('#upload_logo3').hide();
	}else if(no_logos == 3){
		$('#upload_logo1').show();
		$('#upload_logo2').show();
		$('#upload_logo3').show();
	}
}

$('document').ready(
	function() {
		$('#logo1').val('');
		$('#logo2').val('');
		$('#logo3').val('');
		
		$('input[name=logo1]').change(
			function() {
				thermos_check_upload();
			}
		);
		
		$('input[name=logo2]').change(
			function() {
				thermos_check_upload();
			}
		);
		
		$('input[name=logo3]').change(
			function() {
				thermos_check_upload();
			}
		);
		
		$('#upload_logos_next').click(
			function() {
				if($(this).css('opacity') == 1){
					$('#step_thermos_upload_logos').animate({'height': 40}, 400);
					
					$('#upload_logos_next').stop().animate({'opacity': 0.3}, 400);
					$('#upload_logos_skip').stop().animate({'opacity': 0.3}, 400);
					
					loading_uploads_changeable = false;
					
					if($('#step_thermos_upload_logos_error').css('display') != 'none'){
						$('#step_thermos_upload_logos_error').slideUp(400);
						$('#step_thermos_upload_logos_loading').delay(500).slideDown(400);
						
						loading_upload_timer = setTimeout('loading_uploads_changeable = true', 1300);
					}else{
						$('#step_thermos_upload_logos_loading').slideDown(400);
						
						loading_upload_timer = setTimeout('loading_uploads_changeable = true', 800);
					}
					
					thermos_upload_logos();
				}
			}
		);
		
		$('.step_header_back', $('#step_thermos_upload_logos')).click(
			function() {
				reset_steps('step_thermos_upload_logos');
			}
		);
	}
);

