/* ------------------------------------------------------------------ */
/* Application scripts */
/* ------------------------------------------------------------------ */

function go(url) {
	window.location = url;
}

function iframe_get(iFrameName) {  
	var IFrame = document.getElementById(iFrameName);
	if (!IFrame.contentWindow.document.body) return '';
	var content = IFrame.contentWindow.document.body.innerHTML;       
	return content;
}

function iframe_clear(id) {
	var obj = document.getElementById(id);
	if (obj.contentWindow.document.body) {
		obj.contentWindow.document.body.innerHTML='';
	}
}

$(document).ready(function() {
	$(".suggest_city").autocomplete('/ajax/suggest_city', {delay: 100, minChars: 2, cacheLength: 10, width: 200});
});

/* ------------------------------------------------------------------ */
/* Favorites */
/* ------------------------------------------------------------------ */

function favorite_add(company_id) {
	$.get('/ajax/favorite_add/' + company_id, {}, function(response) {
		if (response.result == '1') {
			alert('Added!');
			$('#star' + company_id).addClass('star-selected');
		}
		else alert(response.error);
	}, 'json');
}

function favorite_delete(company_id) {

}

/* ------------------------------------------------------------------ */
/* Portfolio Works */
/* ------------------------------------------------------------------ */

function work_delete(work_id) {
	if (confirm('Are you sure?')) {
		$.post('/ajax/delete_company_work/' + work_id, {}, function(resp) {
			if (resp.result == '1') {
				$('#work' + work_id).fadeOut();
			}
			else {
				alert(resp.error);
			}
		}, 'json');
	}
}

function work_edit(work_id) {
	$('#work_edit_button_' + work_id).hide();
	$('#work_edit_processing_' + work_id).show();


	params = {
		title: $('#work_edit_title_' + work_id).val(),
		description: $('#work_edit_description_' + work_id).val()
	};

	$.post('/ajax/edit_company_work/' + work_id, params, function(resp) {
		if (resp.result == '1') {
			$('#work_form_' + work_id).slideToggle();
			$('#work_title_' + work_id).html(params['title']);
			$('#work_description_' + work_id).html(params['description']);

			setTimeout("$('#work" + work_id + "').effect('highlight', {}, 1000);", 500);
		}
		else {
			alert('Sorry, cant update information! Error: ' + resp.error);
		}
	}, 'json');

	$('#work_edit_button_' + work_id).show();
	$('#work_edit_processing_' + work_id).hide();
}

/* ------------------------------------------------------------------ */
/* Upload & Delete Portfolio Images */
/* ------------------------------------------------------------------ */

function portfolio_delete_image(work_id, image_id) {
	id = work_id + '_' + image_id;
	$('#input_' + id).hide();
	$('#loading_' + id).show();
	$('#form_' + id).hide();
	$.get('/ajax/delete_portfolio_image', {work: work_id, id: image_id}, function(resp) {
		$('#input_' + id).show();
		$('#loading_' + id).hide();
		$('#form_' + id).show();

		if (resp.result == '1') {
			$('#result_' + id + ' > img').attr('src', '');
			$('#result_' + id).hide();
		}
	}, 'json');
}

function portfolio_upload_start(work_id, image_id) {
	id = work_id + '_' + image_id;
	$('#input_' + id).hide();
	$('#loading_' + id).show();
	$('#form_' + id).hide();
	$('#upload_form_' + id).submit();
	portfolio_upload_wait(work_id, image_id);
}

function portfolio_upload_wait(work_id, image_id) {
	id = work_id + '_' + image_id;
	content = iframe_get("upload_" + id);
	if (!content) {
		setTimeout("portfolio_upload_wait('" + work_id + "', '" + image_id + "');", 200);
		return;
	}
	else iframe_clear("upload_" + id);

	obj = eval('(' + content + ')');
	if (obj.result) {
		$('#result_' + id + ' > img').attr('src', obj.image.medium);
		$('#result_' + id).show();
		$('#loading_' + id).hide();
		$('#input_' + id).val('').show();
	}
	else {
		$('#result_' + id).hide();
		$('#loading_' + id).hide();
		$('#input_' + id).val('');
		$('#form_' + id).show();
		alert('Cant process the file');
	}
}


/* ------------------------------------------------------------------ */
/* Company Logo */
/* ------------------------------------------------------------------ */

function logo_upload_start() {
	$('#logo_loading').show();
	$('#logo_file').hide();
	$('#logo_form').submit();
	logo_upload_wait();
}

function logo_upload_wait() {
	content = iframe_get("logo_frame");
	if (!content) {
		setTimeout("logo_upload_wait();", 200);
		return;
	}
	else iframe_clear("logo_frame");

	$('#logo_loading').hide();
	$('#logo_file').show();
	$('#logo_input').val('');

	obj = eval('(' + content + ')');
	if (obj.result) {
		$('#img_logo_medium').attr('src', obj.image.medium);
	}
	else {
		alert('Cannot process file!');
	}
}

/* ------------------------------------------------------------------ */
/* User Avatar Upload */
/* ------------------------------------------------------------------ */

function avatar_upload_start() {
	$('#avatar_loading').show();
	$('#avatar_file').hide();
	$('#avatar_form').submit();
	avatar_upload_wait();
}

function avatar_upload_wait() {
	content = iframe_get("avatar_frame");
	if (!content) {
		setTimeout("avatar_upload_wait();", 200);
		return;
	}
	else iframe_clear("avatar_frame");

	$('#avatar_loading').hide();
	$('#avatar_file').show();
	$('#avatar_input').val('');

	var time = new Date;
	var unixtime_ms = time.getTime();
	var unixtime = parseInt(unixtime_ms / 1000);

	obj = eval('(' + content + ')');
	if (obj.result) {
		$('#img_avatar_medium').attr('src', obj.image.medium + '?' + unixtime);
	}
	else {
		alert('Cannot process file!');
	}
}

/* ------------------------------------------------------------------ */
/* Project Images */
/* ------------------------------------------------------------------ */

function project_post_upload_reset() {
	$('#upload_processing').hide();
	$('#upload_form').show();
	$('#upload_file').val('');
	$('#upload_form > textarea').val('');
	$('#upload_error').html('').hide();
}

function project_post_upload_image() {
	$('#upload_form').hide();
	$('#upload_processing').show();
	$.fancybox.resize();
	$('#upload_form').submit();
	project_post_upload_wait();
}

function project_post_upload_wait() {
	content = iframe_get('upload_frame');
	if (!content) {
		setTimeout("project_post_upload_wait();", 200);
		return;
	}
	else iframe_clear('upload_frame');
	obj = eval('(' + content + ')');
	if (obj.result == '1') {
		position = $('#upload_position').val();
		$('#upload_link' + position).html("<img width='70px' height='40px' src='" + obj.image.small + "' />");
		$.fancybox.close();
	}
	else {
		$('#upload_error').html(obj.error).show();
		$('#upload_processing').hide();
		$('#upload_form').show();
		$.fancybox.resize();
	}
}

/* ------------------------------------------------------------------ */
/* Popups */
/* ------------------------------------------------------------------ */

function question_submit(project_id) {
	$('#question_processing').show();
	$('#question_submit').hide();
	$('#question_result').hide();
	
	params = {
		action: 'send',
		project: project_id,
		message: $('#question_message').val()
	};
	
	$.post('/ajax/ask_question', params, function(resp) {
		$('#question_processing').hide();
		
		if (resp.result == '1') {
			$('#question_form').remove(); $('#question_sent').show();
			$.fancybox.resize();
		}
		else {
			$('#question_submit').show();
			$('#question_result').html(resp.error).show();
		}
	}, 'json');

}

function bid_send(project_id) {
	$('#bid_processing').show();
	$('#bid_submit').hide();
	$('#bid_result').hide();

	params = {
		project: project_id,
		value: $('#bid_value').val(),
		type: $('#bid_type').val(),
		timeframe: $('#bid_timeframe').val(),
		message: $('#bid_message').val()
	};

	$.post('/ajax/post_bid', params, function(resp) {
		if (resp.result == '1') {
			$('#bid_form').remove();
			$('#bid_sent').show();
			$.fancybox.resize();
		}
		else {
			$('#bid_processing').hide();
			$('#bid_submit').show();
			$('#bid_result').html(resp.error).show();
		}
	}, 'json');
}

function estimate_request_send(company_id) {
	$('#est_button').hide();
	$('#est_processing').show();

	params = {
		project: $('#est_project').val(),
		message: $('#est_message').val()
	};

	$.post('/ajax/request_estimate/' + company_id, params, function(resp) {
		if (resp) {
			$('#est_processing').hide();
			
			if (resp.result == '1') {
				$('#est_form').remove();
				$('#est_sent').show();
				$.fancybox.resize();
			}
			else {
				$('#est_button').show();
				$('#est_result').html(resp.error).show();
			}
		}
	}, 'json');
}

