/**
 * Contains javascript functions specific to the portfolio page.
 */
var Portfolio = {

	save : function(form) {
		// this is just for form validation
		if (form['title'].value == '')
		{
			alert('Title is required');
			return false;
		}
		else if (form['description'].value == '')
		{
			alert('Description is required');
			return false;
		}
	},

	edit : function(id) {

		if (id == '' || id == 0) {
			alert('Please click the home you would like to edit.');
			return false;
		}

		var form = $('portfolioForm');
		if (form)
		{
			if (form['id'].value == '') {
				Portfolio.loadHome(id);
			}
		}

		$('portfolioDesc').hide();
		$('portfolioEdit').show();
	},

	cancel : function() {
		$('portfolioEdit').hide();
		$('portfolioDesc').show();
	},

	remove : function(id) {
		
		if (id == '' || id == 0) {
			alert('Please click the home you would like to remove.');
			return false;
		}

		new Ajax.Request('portfolio_ajax.php?action=delete&id='+id, {
			  method: 'get',
			  onSuccess: function(transport) {
				var id = transport.responseText;
				if (id > 0) {
					Portfolio.loadHome(-1);
					$('home'+id).remove();
				} else {
					alert("An error occurred while trying to delete the data. Please try again later.");
				}
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to delete the data. Please try again later.");
			  }
			});
	},

	add_new : function() {

		var form = $('portfolioForm');
		form['description'].value = '';
		form['address'].value = '';
		form['title'].value = '';
		form['url'].value = '';
		form['floorplan'].value = '';
		form['id'].value = '';

		$('portfolioDesc').hide();
		$('portfolioEdit').show();
	},

	loadHome : function(id) {
		new Ajax.Request('portfolio_ajax.php?action=get_data&id='+id, {
			  method: 'get',
			  onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				//$('portfolioMain').update(json['img']);
				//$('portfolioDesc').update(json['body']);
				// now take all the properties and set up the view
				var id = json['id'];

				$('pid').value = id;
				
				$('largeImage').src = json['image'];
				$('ptitle').innerHTML = json['title'];
				$('paddress').innerHTML = json['address'];
				$('pdesc').innerHTML = json['description'];

				if (json['url'] != '') {
					$('urldiv').innerHTML = '<a href="' + json['url'] + '" target="_blank" id="purl">click here for Virtual Tour</a>';
				} else {
					$('urldiv').innerHTML = '';
				}

				if (json['floorplan'] != '') {
					$('floorplandiv').innerHTML = '<a href="' + json['floorplan'] + '" target="_blank" id="fpurl">Floor Plan</a>';
				} else {
					$('floorplandiv').innerHTML = '';
				}

				var form = $('portfolioForm');
				if (form)
				{
					form['id'].value = json['id'];
					form['title'].value = json['title'];
					form['address'].value = json['address'];
					form['description'].value = json['description'];
					form['url'].value = json['url'];
					//$('portfolioid').value = id;
				}
			  },
			  onFailure: function(transport) {
				  alert("An error occurred while trying to get the data. Please try again later.");
			  }
			});
	},

	change_order : function (id, select_box) {
		var pos = select_box.selectedIndex;

		new Ajax.Request('portfolio_ajax.php', {
			method: 'post',
			parameters: {action : 'change_order', res_id : id, position : pos},
			onSuccess: function (transport) {
				if (parseInt(transport.responseText) >= 0) {
					window.location = "portfolio.php";
				} else {
					alert('Error changing portfolio order');
				}
			}
		});
	},

/** testing functions **/
	startUpload : function() {
		$('f1_upload_process').show();
		return true;
	},

	stopUpload : function(flag) {
		$('f1_upload_process').hide();
		if (flag == true) {
			$('result').update('Saved');
		} else {
			$('result').update('Failed to Save');
		}
		$('result').show();

		$('portfolioEdit').hide();
		$('portfolioDesc').show();
	}

};
