var thermos_crop_pos = new Array();
var logos_crop = new Array();
var thermos_loading_crop_changeable = true;

function reset_thermos_crop_logos() {
	thermos_crop_pos.length = 0;
	logos_crop.length = 0;
	
	thermos_loading_crop_changeable = true;
	
	$('#thermos_crop_container').html('');
	
	$('#crop_logos_next').css('opacity', 0.3);
	$('#crop_logos_skip').css('opacity', 1.0);
	
	$('#step_thermos_crop_logos_loading').slideUp(400);
	$('#step_thermos_crop_logos_error').slideUp(400);
}

function thermos_update_coords1(c) {
	if(c.x >= 0 && c.y >= 0 && c.w > 0 && c.h > 0){
		thermos_crop_pos[0] = c;
	}
	
	thermos_check_crop();
}

function thermos_update_coords2(c) {
	if(c.x >= 0 && c.y >= 0 && c.w > 0 && c.h > 0){
		thermos_crop_pos[1] = c;
	}
	
	thermos_check_crop();
}

function thermos_update_coords3(c) {
	if(c.x >= 0 && c.y >= 0 && c.w > 0 && c.h > 0){
		thermos_crop_pos[2] = c;
	}
	
	thermos_check_crop();
}

function thermos_check_crop() {
	var show_thermos_crop = false;
	
	if(logos[0] != undefined && thermos_crop_pos[0] != undefined && thermos_crop_pos[0]['x'] >= 0 && thermos_crop_pos[0]['y'] >= 0 && thermos_crop_pos[0]['w'] > 0 && thermos_crop_pos[0]['h'] > 0){
		show_thermos_crop = true;
	}
	
	if(logos[1] != undefined && thermos_crop_pos[1] != undefined && thermos_crop_pos[1]['x'] >= 0 && thermos_crop_pos[1]['y'] >= 0 && thermos_crop_pos[1]['w'] > 0 && thermos_crop_pos[1]['h'] > 0){
		show_thermos_crop = true;
	}
	
	if(logos[2] != undefined && thermos_crop_pos[2] != undefined && thermos_crop_pos[2]['x'] >= 0 && thermos_crop_pos[2]['y'] >= 0 && thermos_crop_pos[2]['w'] > 0 && thermos_crop_pos[2]['h'] > 0){
		show_thermos_crop = true;
	}
	
	if(show_thermos_crop){
		$('#crop_logos_next').stop().animate({'opacity': 1.0}, 400);
	}else{
		$('#crop_logos_next').stop().animate({'opacity': 0.3}, 400);
	}
}

function thermos_crop_fail() {
	if($('.current_step').attr('id') == 'step_thermos_crop_logos'){
		if(thermos_loading_crop_changeable){
			var new_height = 0;
			
			new_height += parseInt($('.step_header', $('#step_thermos_crop_logos')).height());
			new_height += parseInt($('.step_header', $('#step_thermos_crop_logos')).css('margin-top'));
			new_height += parseInt($('.step_header', $('#step_thermos_crop_logos')).css('margin-bottom'));
			
			new_height += parseInt($('.step_content', $('#step_thermos_crop_logos')).height());
			new_height += parseInt($('.step_content', $('#step_thermos_crop_logos')).css('margin-top'));
			new_height += parseInt($('.step_content', $('#step_thermos_crop_logos')).css('margin-bottom'));
			
			$('#step_thermos_crop_logos').animate({'height': new_height}, 400);
			
			$('#step_thermos_crop_logos_loading').slideUp(400);
			$('#step_thermos_crop_logos_error').delay(500).slideDown(400);
			
			$('#crop_logos_next').stop().delay(900).animate({'opacity': 1.0}, 400);
			$('#crop_logos_skip').stop().delay(900).animate({'opacity': 1.0}, 400);
		}else{
			setTimeout('thermos_crop_fail()', 200);
		}
	}
}

function thermos_crop_success(data) {
	if($('.current_step').attr('id') == 'step_thermos_crop_logos'){
		if(thermos_loading_crop_changeable){
			if(data.image1 != undefined){
				logos_crop[0] = data.image1;
			}
			
			if(data.image2 != undefined){
				logos_crop[1] = data.image2;
			}
			
			if(data.image3 != undefined){
				logos_crop[2] = data.image3;
			}
			
			$('#step_thermos_crop_logos_loading').slideUp(400);
			
			setTimeout('show_next()', 400);
		}else{
			setTimeout(function() { thermos_crop_success(data); }, 200);
		}
	}
}

function array_length(arr) {
	var length = 0;
	
	for(val in arr){
		length++;
	}
	
	return length;
}

function crop_logos() {
	var data = {};
	
	if(thermos_crop_pos[0] != undefined && thermos_crop_pos[0]['x'] >= 0 && thermos_crop_pos[0]['y'] >= 0 && thermos_crop_pos[0]['w'] > 0 && thermos_crop_pos[0]['h'] > 0){
		data.pos_x1 = thermos_crop_pos[0]['x'];
		data.pos_y1 = thermos_crop_pos[0]['y'];
		data.pos_w1 = thermos_crop_pos[0]['w'];
		data.pos_h1 = thermos_crop_pos[0]['h'];
		data.logo1 = logos[0];
	}
	
	if(thermos_crop_pos[1] != undefined && thermos_crop_pos[1]['x'] >= 0 && thermos_crop_pos[1]['y'] >= 0 && thermos_crop_pos[1]['w'] > 0 && thermos_crop_pos[1]['h'] > 0){
		data.pos_x2 = thermos_crop_pos[1]['x'];
		data.pos_y2 = thermos_crop_pos[1]['y'];
		data.pos_w2 = thermos_crop_pos[1]['w'];
		data.pos_h2 = thermos_crop_pos[1]['h'];
		data.logo2 = logos[1];
	}
	
	if(thermos_crop_pos[2] != undefined && thermos_crop_pos[2]['x'] >= 0 && thermos_crop_pos[2]['y'] >= 0 && thermos_crop_pos[2]['w'] > 0 && thermos_crop_pos[2]['h'] > 0){
		data.pos_x3 = thermos_crop_pos[2]['x'];
		data.pos_y3 = thermos_crop_pos[2]['y'];
		data.pos_w3 = thermos_crop_pos[2]['w'];
		data.pos_h3 = thermos_crop_pos[2]['h'];
		data.logo3 = logos[2];
	}
	
	if(array_length(data) > 0){
		$.ajax({
				'url': 'ajax_crop_logo.php',
				'type': 'POST',
				'data': data,
				'success': function(data) {
					if(data.image1 != undefined || data.image2 != undefined || data.image3 != undefined){
						thermos_crop_success(data);
					}else{
						thermos_crop_fail();
					}
				},
				'dataType': 'json',
				'error': thermos_crop_fail
		});
	}else{
		thermos_crop_fail();
	}
}

function start_before_thermos_crop_logos() {
	$('#thermos_crop_container').html('');
	
	if(logos[0] != undefined){
		$('#thermos_crop_container').append('<div id="crop1_container"><IMG SRC="upl/' + logos[0] + '" ID="crop1" /></div>');
	}
	
	if(logos[1] != undefined){
		$('#thermos_crop_container').append('<div id="crop2_container"><IMG SRC="upl/' + logos[1] + '" ID="crop2" /></div>');
	}
	
	if(logos[2] != undefined){
		$('#thermos_crop_container').append('<div id="crop3_container"><IMG SRC="upl/' + logos[2] + '" ID="crop3" /></div>');
	}
	
	
	if(logos[0] != undefined){
		$('#crop1').css('width', logos_dim[0]['w']);
		$('#crop1').css('height', logos_dim[0]['h']);
		
		$('#crop1_container').css('width', logos_dim[0]['w']);
		$('#crop1_container').css('height', logos_dim[0]['h']);
	}
	
	if(logos[1] != undefined){
		$('#crop2').css('width', logos_dim[1]['w']);
		$('#crop2').css('height', logos_dim[1]['h']);
		
		$('#crop2_container').css('width', logos_dim[1]['w']);
		$('#crop2_container').css('height', logos_dim[1]['h']);
	}
	
	if(logos[2] != undefined){
		$('#crop3').css('width', logos_dim[2]['w']);
		$('#crop3').css('height', logos_dim[2]['h']);
		
		$('#crop3_container').css('width', logos_dim[2]['w']);
		$('#crop3_container').css('height', logos_dim[2]['h']);
	}
	
	$('#thermos_crop_container').fadeIn(400);
}

function start_after_thermos_crop_logos() {
	if(logos[0] != undefined){
		$('#crop1').Jcrop({onSelect: thermos_update_coords1});
	}
	
	if(logos[1] != undefined){
		$('#crop2').Jcrop({onSelect: thermos_update_coords2});
	}
	
	if(logos[2] != undefined){
		$('#crop3').Jcrop({onSelect: thermos_update_coords3});
	}
	
	//$('#jcrop-holder IMG').css('position', 'relative');
}

function end_thermos_crop_logos() {
	$('#thermos_crop_container').html('');
}

$('document').ready(
	function() {
		$('#crop_logos_next').click(
			function() {
				if($(this).css('opacity') == 1){
					$('#thermos_crop_container').html('');
					
					$('#step_thermos_crop_logos').animate({'height': 40}, 400);
					
					$('#crop_logos_next').stop().animate({'opacity': 0.3}, 400);
					$('#crop_logos_skip').stop().animate({'opacity': 0.3}, 400);
					
					thermos_loading_crop_changeable = false;
					
					if($('#step_thermos_crop_logos_error').css('display') != 'none'){
						$('#step_thermos_crop_logos_error').slideUp(400);
						$('#step_thermos_crop_logos_loading').delay(500).slideDown(400);
						
						setTimeout('thermos_loading_crop_changeable = true', 1300);
					}else{
						$('#step_thermos_crop_logos_loading').slideDown(400);
						
						setTimeout('thermos_loading_crop_changeable = true', 800);
					}
					
					crop_logos();
				}
			}
		);
		
		$('#crop_logos_skip').click(
			function() {
				show_next();
			}
		);
		
		$('.step_header_back', $('#step_thermos_crop_logos')).click(
			function() {
				reset_steps('step_thermos_crop_logos');
			}
		);
	}
);

