$(document).ready(function() {
	/* ================= add photos page ================= */
	window.AddPhotos = {
		init : function() {
			var canRun = $('body').is('#add-photos');
			if (canRun) {
				
				this.attachHendlers();
			}
			return this;
		},
		FLICKR_SUBMIT_ACTION : '/add-photos/flickr-submit/',
		HQ23_SUBMIT_ACTION : '/add-photos/23hq-submit/',
		FACEBOOK_SUBMIT_ACTION : '/add-photos/facebook-submit/',
		type : 'none',
		attachHendlers : function() {

			/* flickr */

			$('form #source-flickr').click(function() {
				AddPhotos.type = 'flickr';
				
				AddPhotos.showModalForm();
				
				return false;
			});

			/* 23 */
			$('form #source-23').click(function() {
				AddPhotos.type = 'hq23';
				AddPhotos.showModalForm();
				return false;
			});
			$('form #source-facebook').click(function() {

				AddPhotos.type = 'facebook';
				AddPhotos.showModalForm();
				return false;
			});

		},
		showModalForm : function() {
			
			var fieldset = ModalUtils.retrieveHtmlTemplateById("modal-process");
			ModalUtils.replaceIdAttributes(fieldset);
			
			ModalUtils.createAndResizeModal(fieldset);
			
			AddPhotos.attachModalHandlers(fieldset);

		},
		showDirectHq23SignupModalForm : function() {
			var fieldset = ModalUtils.retrieveHtmlTemplateById("modal-process");
			ModalUtils.replaceIdAttributes(fieldset);
			ModalUtils.createAndResizeModal(fieldset);
			AddPhotos.attachDirectHq23SigupHandlers(fieldset);

		},
		/* =============== ADDING FLICKR MODALS ============== */
		attachModalHandlers : function(modalForm) {
			$('#modal-process .control.primary a').click(function() {
				if (AddPhotos.type == 'flickr') {
					$('form#add_photos_form').attr('action', AddPhotos.FLICKR_SUBMIT_ACTION);
				}
				if (AddPhotos.type == 'hq23') {
					$('form#add_photos_form').attr('action', AddPhotos.HQ23_SUBMIT_ACTION);
				}
				if (AddPhotos.type == 'facebook') {
					$('form#add_photos_form').attr('action', AddPhotos.FACEBOOK_SUBMIT_ACTION);
				}
				
				$('form#add_photos_form').submit();
				return false;
			});
		},
		attachDirectHq23SigupHandlers : function(modalForm) {
			$('#modal-process .control.primary a').click(function() {
				window.location.href = "/facebook/callback/";
				return false;
			});
		}
	}.init();
});
