function getId(element){
	id = element.getAttribute("id");
	id = id.substring( 4, id.length);
	return id;
}

function activateMeetingplanItems(){

	$('leaders_notes').select('a.js_hide')[0].observe("click", function(event){
		hideLeadersNotes();
		Event.stop(event);
	});

	$('leaders_notes').select('a.js_show')[0].observe("click", function(event){
		showLeadersNotes();
		Event.stop(event);
	});


	$('leaders_notes').select('a.js_addremovenotes')[0].observe("click", function(event){
	
		// If currently displayed then hide ELSE display
		if ( $('leaders_notes_notes').readAttribute("class") == "notes" ){

			continueevent = confirm(commonSubTexts_json.confirm_removenotes);

			if(continueevent == true) {
				$('leaders_notes_notes').writeAttribute("class", "notes_hidden");
				$('leaders_notes_notes').setAttribute("class", "notes_hidden");
				$('leaders_notes_notes_textarea').setValue("");
				meetingplans_json.leaders_notes.notes = $F('leaders_notes_notes_textarea');
				updateSessionJSON();
			}

		} else {

			$('leaders_notes_notes').writeAttribute("class", "notes");
			$('leaders_notes_notes').setAttribute("class", "notes");

		}

		Event.stop(event);
	});


	// If there are make suggestion off links
	if ( $('leaders_notes').select('a.js_makesuggestion_off')[0] ){
		$('leaders_notes').select('a.js_makesuggestion_off')[0].observe("click", function(event){
	
			alert(commonSubTexts_json.msg_featureforsubscribersonly);
	
			Event.stop(event);
		});
	}



	$$('.meetingplan_item', '.meetingplan_item_hide').each( function(currentItem){
	
		var currentId = getId(currentItem);

		currentItem.select('a.js_hide').each( function(currentHyperlink){
			currentHyperlink.observe("click", function(event){
				hideItem(currentItem);
				event.stop();
			});
	
		});
		
		currentItem.select('a.js_addremovenotes').each( function(currentHyperlink){
	
			currentHyperlink.observe("click", function(event){

				// If currently displayed then hide ELSE display
				if ( $('notes' + currentId).readAttribute("class") == "notes" ){

					continueevent = confirm(commonSubTexts_json.confirm_removenotes);

					if(continueevent == true) {
						$('notes' + currentId).writeAttribute("class", "notes_hidden");
						$('notes' + currentId).setAttribute("class", "notes_hidden");
						$('notes_textarea' + currentId).setValue("");
						meetingplans_json.items[currentId].notes = $F('notes_textarea' + currentId);
						updateSessionJSON();
					}

				} else {

					$('notes' + currentId).writeAttribute("class", "notes");
					$('notes' + currentId).setAttribute("class", "notes");

				}

				event.stop();

			});
	
		});

		currentItem.select('a.js_makesuggestion_off').each( function(currentHyperlink){
	
			currentHyperlink.observe("click", function(event){

				alert(commonSubTexts_json.msg_featureforsubscribersonly);
				event.stop();

			});
	
		});
	
		currentItem.select('a.js_show').each( function(currentHyperlink){
	
			currentHyperlink.observe("click", function(event){
				showItem(currentItem);
				event.stop();
			});
	
		});

		if ( $('notes_textarea' + currentId) ){
			$('notes_textarea' + currentId).observe("blur", function(event){
				meetingplans_json.items[currentId].notes = $F('notes_textarea' + currentId);
				updateSessionJSON();
			});
		}

		if ( $('leaders_notes_notes_textarea') ){
			$('leaders_notes_notes_textarea').observe("blur", function(event){
				meetingplans_json.leaders_notes.notes = $F('leaders_notes_notes_textarea');
				updateSessionJSON();
			});
		}
	
		// Catch all hyperlinks to /modalvideos/ and cause to open in modal window instead
		currentItem.select('a').each( function(element){
			if ( element.readAttribute("href") ){
	
				if ( element.readAttribute("href").toString().include("/modalvideos/") ){
					makeLinkVideoModal(element);
				}
	
			}
		});


	});

	makeLinksModal($$('a.js_makesuggestion'));

	updateMeetingDuration();

}

function hideLeadersNotes(element) {
	$('leaders_notes').writeAttribute("class", "leaders_notes_hide");
	$('leaders_notes').setAttribute("class", "leaders_notes_hide");

	meetingplans_json.leaders_notes.display = 0;
	updateSessionJSON();

	$('leaders_notes').scrollTo();
}

function showLeadersNotes(element) {
	$('leaders_notes').writeAttribute("class", "leaders_notes");
	$('leaders_notes').setAttribute("class", "leaders_notes");

	meetingplans_json.leaders_notes.display = 1;
	updateSessionJSON();
}

function hideItem(element) {
	element.writeAttribute("class", "meetingplan_item_hide");
	element.setAttribute("class", "meetingplan_item_hide");

	element.select('div.agegroup').each( function(agegroup_element){
		agegroup_element.setStyle('height: 10px;');
	});

	meetingplans_json.items[getId(element)].display = 0;
	updateSessionJSON();

	element.scrollTo();
}

function showItem(element) {
	element.writeAttribute("class", "meetingplan_item");	
	element.setAttribute("class", "meetingplan_item");

	element.select('div.agegroup').each( function(agegroup_element){
		agegroup_element.setStyle('height: 50px;');
	});

	meetingplans_json.items[getId(element)].display = 1;
	updateSessionJSON();
}


function activateChangeOrderBehaviour(){
	Position.includeScrollOffsets = true;
	Sortable.create("changeorder_draggables", {
		scroll: "changeorder_draggables_window"
	});
	
	if($('js_savechanges')){
		$('js_savechanges').observe("click", function(event){

			var count = 0;
			$('changeorder_draggables_window').select('li.draggable_item').each( function(item){
				id = item.readAttribute("id").substring( 14, item.readAttribute("id").length);
				meetingplans_json.item_order[count] = id;
				count++;
			});
			updateSessionJSON(true);
			Control.Modal.close();

			Event.stop(event);
		});
	}
}


var additem_id = 0;

function activateAddActivitiesBehaviour(){

	additem_id = 0;

	$('addactivities').select('a').each( function(element){

		element.observe('click', function(event){
			$('addactivities_preview').update('');
			new Ajax.Updater('addactivities_preview', element.readAttribute("href"), {
				evalScripts: true,
				onComplete: function(){

					// Catch all hyperlinks to /modalvideos/ and cause to open in modal window instead
					$('addactivities_preview').select('a').each( function(video_hyperlink){
						if ( video_hyperlink.readAttribute("href") ){
				
							if ( video_hyperlink.readAttribute("href").toString().include("/modalvideos/") ){

								video_hyperlink.observe('click', function(event){
									$('addactivities_preview').update('');
									new Ajax.Updater('addactivities_preview', video_hyperlink.readAttribute("href"));
									Event.stop(event);
								});

							}
				
						}
					});
					
				}
			});
			Event.stop(event);
		});

	});

	$('js_addtoplan').observe("click", function(event){

		// If add item isn't yet set then do nothing
		if ( additem_id == 0 ){
			return null;
		}

		// Check if current activity to add is already in MP
		var b_used = false;
		meetingplans_json.item_order.each( function(item_id){
			if ( item_id == additem_id ){
				b_used = true;
			}
		});
		
		// If activity not already in MP then add to JSON and then update ELSE give warning
		if ( !b_used ){

			meetingplans_json.item_order.push(additem_id);
	
			meetingplans_json.items[additem_id] = {"notes": "", "display": "1", "agegroup": additem_agegroup, "duration": additem_duration };
	
			updateSessionJSON(true);
			Control.Modal.close();

		} else {

			alert(commonSubTexts_json.msg_activityalreadyinmp);

		}

	});

}

function updateSessionJSON(b_reloadOnComplete){

	var settings = Object.toJSON( meetingplans_json );
	var params = { settings: settings, id: meetingplans_json.id, cmp: meetingplans_json.cmp };

	new Ajax.Request('/data/energize/scripts/meetingplan_ajax_setsession.php', {
		method: 'post',
		parameters: params,
		onComplete: function(){

			updateMeetingStatus();
			updateMeetingDuration();

			if ( b_reloadOnComplete ){
				updateMeetingPlan();
			}
		}
	});
}


if ( devmode == 1 ){

	function saveSessionJSON(b_saveas){
	
		// Check if saving as or simply overwriting
		if ( b_saveas ) {
			save_prompt = prompt(commonSubTexts_json.msg_provideplantitle, meetingplan_title);
	
			if ( save_prompt ){
	
				meetingplans_json.cmp = Math.floor(Math.random()*999999999);

				var settings = Object.toJSON( meetingplans_json );
				var params = {title: save_prompt, settings: settings, id: meetingplans_json.id, new_cmp: meetingplans_json.cmp };
				
				new Ajax.Request('/data/energize/scripts/meetingplan_ajax_saveplan.php', {
					method: 'post', 
					parameters: params,
					onComplete: function(response){
						prompt(response.responseText,response.responseText);
						location.href = response.responseText;
					}
				});
				
			}
	
		} else {
			var settings = Object.toJSON( meetingplans_json );
			var params = {settings: settings, cmp: meetingplans_json.cmp };
	
			new Ajax.Request('/data/energize/scripts/meetingplan_ajax_saveplan.php', {
				method: 'post',
				parameters: params, 
				onComplete: function(response){
					alert(commonSubTexts_json.msg_plansaved);
					updateMeetingStatus();
				}
			});
		}
	
	}

} else {
	function saveSessionJSON(b_saveas){
	
		// Check if saving as or simply overwriting
		if ( b_saveas ) {
			save_prompt = prompt(commonSubTexts_json.msg_provideplantitle, meetingplan_title);
	
			if ( save_prompt ){
	
				meetingplans_json.cmp = Math.floor(Math.random()*999999999);

				var settings = Object.toJSON( meetingplans_json );
				var params = {title: save_prompt, settings: settings, id: meetingplans_json.id, new_cmp: meetingplans_json.cmp };
	
				new Ajax.Request('/data/energize/scripts/meetingplan_ajax_saveplan.php', {
					method: 'post',
					parameters: params,
					onComplete: function(response){
						location.href = response.responseText;
					}
				});
	
			}
	
		} else {
			var settings = Object.toJSON( meetingplans_json );
			var params = {settings: settings, cmp: meetingplans_json.cmp };
	
			new Ajax.Request('/data/energize/scripts/meetingplan_ajax_saveplan.php', {
				method: 'post',
				parameters: params,
				onComplete: function(response){
					alert(commonSubTexts_json.msg_plansaved);
					updateMeetingStatus();
				}
			});
		}
	
	}


}

function updateMeetingPlan(){
	// Load meeting plan items using current URL
	new Ajax.Updater('items', location.href, {
		evalScripts: true
	});
	
}

function updateMeetingStatus(){
	var params = { id: meetingplans_json.id, cmp: meetingplans_json.cmp };

	new Ajax.Updater('meetingplan_status', '/data/energize/scripts/meetingplan_ajax_status.php',{
		parameters: params
	});	
}


function updateMeetingDuration(){
	var i_totalDuration = 0;

	meetingplans_json.item_order.each( function(itemID) {
		if ( meetingplans_json.items[itemID].display == 1 ){
			//alert(meetingplans_json.items[itemID].duration);
			if ( meetingplans_json.items[itemID].duration ){
				i_totalDuration = parseInt(i_totalDuration) + parseInt(meetingplans_json.items[itemID].duration);
			}
		}
	});

	$('totalduration').update(i_totalDuration);
}



document.observe("dom:loaded", function() {


	/////////////////////////////////////////////////////////
	// ANY AREA
	/////////////////////////////////////////////////////////

	// Delete links
	if ( $$('a.ajax_deletelinks') ){

	    $$('a.ajax_deletelinks').each( function(currentElement){

			currentElement.observe('click', function(currentEvent){

				continuteevent = confirm(commonSubTexts_json.confirm_delete);

				if(continuteevent == true) {
					new Ajax.Request(currentElement.readAttribute('href'), {
						method: 'get',
						onComplete: function(){
							location.reload();
						}
				    });

				}

				Event.stop(currentEvent);
	
			});

	    });

	}


	// Meetingplan - Meeting plans list and scripting hover boxes
	if ( $$('ul.meetingplans_listing') ){
	
		$$('ul.meetingplans_listing').each( function(currentListing){

			currentListing.select('a.mp_title').each( function(currentLink){
				currentLink.observe('mouseover', function(event){
					currentLink.next('div').show();
				});
				currentLink.observe('mouseout', function(event){
					currentLink.next('div').hide();
				});
			});

			currentListing.select('div.meetingplan_info').each( function(currentDiv){
				currentDiv.hide();
			});

		});

	}


	/////////////////////////////////////////////////////////
	// MEETING PLAN SEARCH AREA
	/////////////////////////////////////////////////////////

	// Search form megathemes/themes behaviour
	if ($('mpsearchadvanced')) {

		$('megatheme').observe('change', function(event){

			// Now make a call to update theme options
  			new Ajax.Updater('theme' , '/data/energize/scripts/meetingplan_ajax_updatethemes.php', { 
				method: 'get',
				parameters: {megatheme_id: $F('megatheme')}
			});
		});

	}

	if ($('biblebook')) {
		$('biblebook').observe('change', function(event){

			// Now make a call to update theme options
  			new Ajax.Updater('biblechapter' , '/ajax/biblechapter', { 
				method: 'get',
				parameters: {biblebook: $F('biblebook')}
			});
		});
	}

	/////////////////////////////////////////////////////////
	// MEETING PLAN
	/////////////////////////////////////////////////////////

	if ( $('items') ){

		updateMeetingPlan();

		/////////////////////////////////////////////////////////
		// LOGGED IN
		/////////////////////////////////////////////////////////

		if ( $('meetingplan_options') ){
	
			elementFollowWindow( $('meetingplan_options') );

			makeLinksModal( $$('a.js_changeactivityorder') );
			makeLinksModal( $$('a.js_addactivities') );
		
			$$('a.js_savemeetingplan').each( function(element){
				element.observe("click", function(event){
					if ( meetingplans_json.cmp!= "" ){
						saveSessionJSON(false);
					} else {
						saveSessionJSON(true);
					}
					Event.stop(event);
				});
			});
		
			$$('a.js_savemeetingplanas').each( function(element){
				element.observe("click", function(event){
					saveSessionJSON(true);
					Event.stop(event);
				});
			});
		
			$$('a.js_tagplanasused').each( function(element){
				element.observe("click", function(event){
		
					continueevent = confirm(commonSubTexts_json.confirm_tagasused);
		
					if(continueevent == true) {
		
						new Ajax.Request(element.readAttribute("href"), {
							onComplete: function(){
								location.reload();
							}
						});
		
					}
		
					Event.stop(event);
				});
			});

			$$('a.js_untagplanasused').each( function(element){
				element.observe("click", function(event){
		
					continueevent = confirm(commonSubTexts_json.confirm_untagasused);
		
					if(continueevent == true) {
		
						new Ajax.Request(element.readAttribute("href"), {
							onComplete: function(){
								location.reload();
							}
						});
		
					}
		
					Event.stop(event);
				});
			});
		
			$$('a.js_resetmeetingplan').each( function(element){
				element.observe("click", function(event){
		
					continueevent = confirm(commonSubTexts_json.confirm_resetmptodefaults);
		
					if(continueevent == true) {
		
						new Ajax.Request(element.readAttribute("href"), {
							onComplete: function(){
								updateMeetingPlan();
								updateMeetingStatus();
							}
						});
		
					}
		
					Event.stop(event);
				});
			});
		
			$$('a.js_resetmeetingplantolastsave').each( function(element){
				element.observe("click", function(event){
		
					continueevent = confirm(commonSubTexts_json.confirm_resetmptolastsave);
		
					if(continueevent == true) {
		
						new Ajax.Request(element.readAttribute("href"), {
							onComplete: function(){
								updateMeetingPlan();
								updateMeetingStatus();
							}
						});
		
					}
		
					Event.stop(event);
				});
			});
	
		}

		/////////////////////////////////////////////////////////
		// LOGGED OUT
		/////////////////////////////////////////////////////////

		if ( $('meetingplan_options_off') ){
		
			elementFollowWindow( $('meetingplan_options_off') );
			
			$('meetingplan_options_off').select('a').each( function(element){
				element.observe("click", function(event){
		
					alert(commonSubTexts_json.msg_featureforsubscribersonly);
	
					Event.stop(event);
				});
			});
	
		}



	}

});



