/**
* Function to call the JavaScript Validation.
*
* @name 	load_form_new_article
* @access 	public
* @param 	var-type none
* @return 	none
* @author 	Joey Avino / Bart Jedrychowski
* @email	joey@amediacreative.com / bart@amediacreative.com
*/ 
function load_form_new_article()  {

	var check_list = new Array();
		check_list[0] = '.val_min_len_3';
		check_list[1] = '.val_min_len_20';
		check_list[2] = '.val_min_len_50';
		check_list[3] = '.val_file';

	var form = $('article_form');
	var callback = 'call_add_article_step2(obj)';
	init_val(check_list, form,callback);
	file('article_photo');
	submit_toggle();
	
}

/**
* Function to process new article.
*
* @name 	call_add_article_step2
* @param 	obj form
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function call_add_article_step2(obj) {

	var photo = $('uploaded_photo_name');
	if ( ! photo.get('value')) {

		var iframe_photo_input = frames['upl_photo'].document.forms[0].photo;
		var iframe_valdiv = iframe_photo_input.getParent().getParent();
		iframe_valdiv.addClass('val_error');
		return;

	}
	obj.set('send', { 

		onComplete: function(response) {
			var id_hide = obj.get('id');
 			var id_show = 'add_article_ajax_page_1';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id':id_show,'html':response, 'class':'global_input_wrapper'});
			ajax_page.inject(obj.getParent(), 'bottom');

			article_step2_edit(id_hide, id_show, 'add_article_step2_edit');
			load_form_new_article_step2();
			submit_toggle();
		}
	});

	obj.send();
	
}

/**
* Function to call the JavaScript Validation.
*
* @name 	load_form_new_article_step2
* @access 	public
* @param 	var-type none
* @return 	none
* @author 	Ever Daniel Barreto
* @email	e.barreto@amediacreative.com
*/ 
function load_form_new_article_step2()  {

	var check_list = new Array();
		check_list[0] = '.val_min_len_3';
		check_list[1] = '.val_min_len_50';
		check_list[2] = '.val_min_len_100';
		check_list[3] = '.val_file';

	var form = $('article_form_step3');
	var callback = 'call_add_article_step3(obj)';
	if (form) init_val(check_list, form, callback);
	submit_toggle();

	var options = {

		add_link: 'add_credit_link',
		event: 'click',
		id_input: 'article_credits',
		id_ol_list: 'article_credit_list',
		val_type: '.val_min_length_1',
		form: 'article_form',
		hlite_color: '#fff2b6',
		input_name: 'credits[]'
	
	};

	var multiadd_credits = new Multiadd(options);

	var options = {

		add_link: 'add_input_link',
		event: 'click',
		id_input: 'article_links',
		id_ol_list: 'article_links_list',
		val_type: '.val_url',
		form: 'article_form',
		hlite_color: '#fff2b6',
		input_name: 'links[]'
	
	};

	var multiadd_links = new Multiadd(options);


}


/**
* Function to process new article.
*
* @name 	call_add_article_step3
* @param 	obj form
* @return 	none
* @author 	Ever Daniel Barreto
* @email	e.barreto@amediacreative.com
*/ 
function call_add_article_step3(obj) {

	obj.set('send', { 

		onComplete: function(response) {
			var id_hide = obj.get('id');
 			var id_show = 'add_article_ajax_page_2';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id':id_show,'html':response, 'class':'global_input_wrapper'});
			ajax_page.inject(obj.getParent(), 'bottom');

			article_step2_edit(id_hide, id_show, 'add_article_step3_edit');
			article_step3(obj,id_show);
			submit_toggle();
		}
	});

	obj.send();
}

/**
* Function to process new article - go back and edit.
*
* @name 	article_step2_edit
* @param 	str id_hide
* @param 	str id_show
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_step2_edit(h, s, btn_name) {

	var button = $(btn_name);
	if (button) {

		button.addEvent('click', function() {
						 
			$(s).dispose();
			$(h).setStyle('display','block');

		});
	}
}

/**
* Function to process new article - Submit
*
* @name 	article_step3
* @param 	obj form
* @param 	str id_show
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_step3(obj, s) {

	var button = $('add_article_step3');

	if (button) {
		
		button.addEvent('click', function() {

			var final_request = new Request({

				url: 'ajax',
				data: obj.toQueryString() + '&ajax_method=article.step3',

				onComplete: function(response) {

					$(s).dispose();	
					var ajax_page = new Element('div', {'id': s, 'html': response, 'class': 'global_input_wrapper'});
					ajax_page.inject(obj.getParent(), 'bottom');
					obj.reset();	
					//frames['upl_photo'].location.href = frames['upl_photo'].location.href;

				}
			});

			final_request.send();

		});
	}
}



/**
* Function to process edit article.
*
* @name 	call_edit_article_step2
* @param 	obj form
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*//* 
function call_edit_article_step2(obj) {

	var photo = $('uploaded_photo_name');
	if ( ! photo.get('value')) {

		var iframe_photo_input = frames['upl_photo'].document.forms[0].photo;
		var iframe_valdiv = iframe_photo_input.getParent().getParent();
		iframe_valdiv.addClass('val_error');
		return;

	}
	obj.set('send', { 

		onComplete: function(response) {
			var id_hide = obj.get('id');
 			var id_show = 'add_article_ajax_page';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id':id_show,'html':response, 'class':'global_input_wrapper'});
			ajax_page.inject(obj.getParent(), 'bottom');

			article_step2_edit(id_hide, id_show, 'add_article_step2_edit');
			article_step3(obj,id_show);
			submit_toggle();
		}
	});

	obj.send();
}
*/
/**
* Function to prepare the form for new articles 
*
* @name 	prepare_form
* @param 	str formid
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function prepare_form(formid) {

	var form = $(formid);
	if (form) {

		if (form.getStyle('display') == 'none')
			form.setStyle('display','block');
		
		if ($('article_credit_list')) $('article_credit_list').empty();
		if ($('article_links_list')) $('article_links_list').empty();
		
	}

	var ajax_page = $('add_article_ajax_page_1');
	if (ajax_page) ajax_page.dispose();
	
	ajax_page = $('add_article_ajax_page_2');
	if (ajax_page) ajax_page.dispose();

}

/**
* Function to attach the photo from iframe to the main form
*
* @name 	article_attach_photo
* @param 	str file_name
* @param 	str file_id
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_attach_photo(fl,fi) {

	var up = $('uploaded_photo_name');
	if (up)
		up.set('value',fl);
	var up = $('uploaded_photo_id');
	if (up)
		up.set('value',fi);

}


/**
* Function to remove the photo data from inputs
*
* @name 	article_remove_photo
* @param 	none
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_remove_photo() {

	var up = $('uploaded_photo_name');
	if (up)
		up.set('value','');
	var up = $('uploaded_photo_id');
	if (up)
		up.set('value','');

}

/*
|--------------------------------------------------------------------------
| Amedia Creative Community > Articles > Edit Article related functions
|--------------------------------------------------------------------------
|
*/

/**
* Function to call the JavaScript Validation for the Edit Article Lightbox Form
*
* @name 	load_form_edit_article
* @access 	public
* @param 	var-type none
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function load_form_edit_article()  {

	var check_list = new Array();
		check_list[0] = '.val_min_len_3';
		check_list[1] = '.val_min_len_20';
		check_list[2] = '.val_min_len_50';
		check_list[3] = '.val_file';

	var form = $('article_edit_form');
	var callback = 'call_edit_article_post(obj)';
	init_val(check_list, form, callback);
	file('article_photo_edit');
	submit_toggle();
	
	$$('.multiadd-item').addEvent('click', function(e) {

		this.erase('html');
		this.setStyle('display', 'none');

	});

	var options = {

		add_link: 'edit_credit_link',
		event: 'click',
		id_input: 'edit_article_credits',
		id_ol_list: 'edit_article_credit_list',
		val_type: '.val_min_length_1',
		form: 'edit_article_form',
		hlite_color: '#fff2b6',
		input_name: 'credits[]'
	
	};

	var multiadd_credits = new Multiadd(options);

	var options = {

		add_link: 'edit_input_link',
		event: 'click',
		id_input: 'edit_article_links',
		id_ol_list: 'edit_article_links_list',
		val_type: '.val_url',
		form: 'edit_article_form',
		hlite_color: '#fff2b6',
		input_name: 'links[]'
	
	};

	var multiadd_links = new Multiadd(options);


}

/**
* Function to process edit article.
*
* @name 	call_edit_article_post
* @param 	obj form
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function call_edit_article_post(form_object) {

	/*
	// Do we need to force the user to upload an image here? I think not :D
	var photo = $('uploaded_photo_name_edit');
	if (!photo.get('value')) {

		var iframe_photo_input = frames['upl_photo_edit'].document.forms[0].photo;
		var iframe_valdiv = iframe_photo_input.getParent().getParent();
		iframe_valdiv.addClass('val_error');
		return;

	}
	*/
	form_object.set('send', {
		onComplete: function(response) {
			
			var id_hide = form_object.get('id');
 			var id_show = 'edit_article_ajax_page';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id': id_show, 'html': response, 'class': 'global_input_wrapper' });
			ajax_page.inject(form_object.getParent(), 'bottom');
			article_edit_step2(id_hide, id_show);
			submit_toggle();
			
		}
	});
	form_object.send();

}

/**
* Function to process new article - go back and edit.
*
* @name 	article_edit_step2
* @param 	str id_hide
* @param 	str id_show
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_edit_step2(h,s) {

	var button = $('btn_edit_goback');
	if (button) {

		button.addEvent('click', function() {
						 
			$(s).dispose();
			$(h).setStyle('display','block');

		});
	}
}

/**
* Function to attach the photo from iframe to the edit form
*
* @name 	article_attach_photo_edit
* @param 	str file_name
* @param 	str file_id
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function article_attach_photo_edit(fl, fi) {

	var up = $('uploaded_photo_name_edit');
	if (up)
		up.set('value',fl);
	var up = $('uploaded_photo_id_edit');
	if (up)
		up.set('value',fi);

}


/**
* Function to remove the photo data from inputs
*
* @name 	article_remove_photo_edit
* @param 	none
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function article_remove_photo_edit() {

	var up = $('uploaded_photo_name_edit');
	if (up)
		up.set('value','');
	var up = $('uploaded_photo_id_edit');
	if (up)
		up.set('value','');

}





/*
|--------------------------------------------------------------------------
| Amedia Creative Community > Articles > Comment Article related functions
|--------------------------------------------------------------------------
|
*/

/**
* Function to call the JavaScript Validation for the Edit Article Lightbox Form
*
* @name 	load_form_edit_article
* @access 	public
* @param 	var-type none
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function load_form_comment_article()  {

	var check_list = new Array();
		check_list[0] = '.val_min_len_3';
		check_list[1] = '.val_min_len_50';
		check_list[2] = '.val_min_len_100';
		check_list[3] = '.val_file';

	var form = $('article_comment_form');
	var callback = 'call_comment_article(obj)';
	init_val(check_list, form, callback);
	submit_toggle();
	
}

/**
* Function to process comment article.
*
* @name 	call_comment_article
* @param 	obj form
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function call_comment_article(form_object) {

	form_object.set('send', {
		onComplete: function(response) {
			
			var id_hide = form_object.get('id');
 			var id_show = 'comment_article_ajax_page';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id': id_show, 'html': response, 'class': 'global_input_wrapper' });
			ajax_page.inject(form_object.getParent(), 'bottom');
			article_comment_step2(id_hide, id_show);
			submit_toggle();
			if ($('id_article'))
				update_comments_container($('id_article').get('value'), '/article/ajax_get_comments', 'comment_box');
			
		}
	});
	form_object.send();

}

/**
* Function to process comment article - go back and edit.
*
* @name 	article_comment_step2
* @param 	str id_hide
* @param 	str id_show
* @return 	none
* @author 	Bart Jedrychowski
* @email	bart@amediacreative.com
*/ 
function article_comment_step2(h,s) {

	var button = $('btn_comment_goback');
	if (button) {

		button.addEvent('click', function() {
						 
			$(s).dispose();
			$(h).setStyle('display','block');

		});
	}
}





/*
|--------------------------------------------------------------------------
| Amedia Creative Community > Articles > Send To a Friend related functions
|--------------------------------------------------------------------------
|
*/

/**
* Function to call the JavaScript Validation for the Send To a Friend Lightbox Form
*
* @name 	load_form_send_article
* @access 	public
* @param 	var-type none
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function load_form_send_article()  {

	var check_list = new Array();
		check_list[0] = '.val_min_len_3';

	var form = $('article_send_form');
	var callback = 'call_send_article_step2(obj)';
	init_val(check_list, form, callback);
	submit_toggle();
	
}

/**
* Function to process send article.
*
* @name 	call_send_article_step2
* @param 	obj form
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function call_send_article_step2(form_object) {

	form_object.set('send', {
		onComplete: function(response) {
			
			var id_hide = form_object.get('id');
 			var id_show = 'send_article_ajax_response';

			$(id_hide).setStyle('display','none');

			var ajax_page = new Element('div', {'id': id_show, 'html': response, 'class': 'global_input_wrapper' });
			ajax_page.inject(form_object.getParent(), 'top');
			article_send_edit(id_hide, id_show);

		}
	});
	form_object.send();

}

/**
* Function to process new article - go back and edit.
*
* @name 	article_send_edit
* @param 	str id_hide
* @param 	str id_show
* @return 	none
* @author 	Ever Daniel Barreto
* @email	ever@amediacreative.com
*/ 
function article_send_edit(hide_div, show_div) {

	var button = $('btn_send_goback');
	if (button) {

		button.addEvent('click', function() {
						 
			$(show_div).dispose();
			$(hide_div).setStyle('display','block');

		});
		
	}
}
