$(document).ready(function(){

	date_options = { showOn: "both", buttonImage: "/images/calendar.gif", buttonImageOnly: true, dateFormat: "dd/mm/yy" }
	$("#startdate").datepicker(date_options);
	$("#enddate").datepicker(date_options);
	$("#start_date").datepicker(date_options);
	$("#end_date").datepicker(date_options);
	
	/*$("#start_date").live('change',function(){
		
		var start_date_value = $("#start_date").val();
		var end_date_value = $("#end_date").val();
		
		var start_date_array = start_date_value.split('/');
		
		if(end_date_value.length == 0 && start_date_value.length > 0){
			
			var start_date_array = start_date_value.split('/');
			var date = new Date(start_date_array[1] + '/' + start_date_array[0] + '/' + start_date_array[2]);
			var date6MonthsFromNow = new Date(date).add(6).month();
			
			$("#end_date").val(date6MonthsFromNow.toString("dd/MM/yyyy"));

		}
			
	});*/	
	
   // Set search type to hidden element.
   $("div.searchbox-inner a").click(function() {

		// Get the item we clicked.
		clicked_item = $(this).attr('id').replace("-active","");

		// Set the hidden value.
		entry_type = $(this).html();
		$("#entry-type").val(entry_type.toLowerCase().capitalize());

		// Swap the id to the active one, should use cllass ideally.
		$("div.searchbox-inner a").each(function() {

			$(this).attr('id',$(this).attr('id').replace("-active",""));

		});

		$(this).attr('id',clicked_item + "-active");

		// Disable relevent items.

		if(entry_type == "EVENT") {
			reset_search();
		}

		if(entry_type == "ACTIVITY") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("img.ui-datepicker-trigger").hide();
		}

		if(entry_type == "CLUB") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}

		if(entry_type == "TEAM") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("input[name=disbaled_access]").hide();
			$("input[name=child_friendly]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}
		
		if(entry_type == "GYM") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}
		
		if(entry_type == "VENUE") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}

		if(entry_type == "ASSOCIATION") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}

		if(entry_type == "ORGANISER") {
			reset_search();
			$("input#startdate").hide();
			$("input#enddate").hide();
			$("input[name=participation]").hide();
			$("input[name=spectator]").hide();
			$("input[name=social]").hide();
			$("select[name=intensity]").hide();
			$("select[name=comedy]").hide();
			$("select[name=cost]").hide();
			$("img.ui-datepicker-trigger").hide();
		}

		return false;
   });

   // Entry Participatio validatio
   $("input#participation_event").change(function() {
   		if($(this).attr('checked') == true) {
   			$("input#social_event").attr('disabled','true');
   		} else if($("input#spectator_event").attr('checked') == true) {
   			$("input#social_event").attr('disabled','true');
   		} else {
   			$("input#participation_event").attr('disabled','');
   			$("input#spectator_event").attr('disabled','');
   			$("input#social_event").attr('disabled','');
   		}
   });

   $("input#spectator_event").change(function() {
   		if($(this).attr('checked') == true) {
   			$("input#social_event").attr('disabled','true');
   		} else if($("input#participation_event").attr('checked') == true) {
   			$("input#social_event").attr('disabled','true');
   		} else {
   			$("input#participation_event").attr('disabled','');
   			$("input#spectator_event").attr('disabled','');
   			$("input#social_event").attr('disabled','');
   		}
   });


   // Set the entry type as the one selected
   entry_type_selected = $("#entry-type").val();
   $("div.searchbox-inner a").each(function() {

   		id = $(this).attr('id').replace("-active","");
   		$(this).attr('id',id);

   		name = $(this).html().toLowerCase().capitalize()
   		name_id = $(this).attr('id')

   		if(entry_type_selected == name){
   			$(this).attr('id',name_id + "-active");
   			$(this).click(); // CG: force the form to update, hiding/showing fields etc
   		}
   });

   $("input#social_event").change(function() {
   		if($(this).attr('checked') == true) {
   			$("input#participation_event").attr('disabled','true');
   			$("input#spectator_event").attr('disabled','true');
   		} else {
   			$("input#participation_event").attr('disabled','');
   			$("input#spectator_event").attr('disabled','');
   			$("input#social_event").attr('disabled','');
   		}
   });


   // Entry swicther on homepage.
   $("a#upcoming-tab").click(function() {

		// Show the tab.
		$("#upcoming-results").show();
		$("#recent-results").hide();
		$("#recent-entries-filter").hide();
		$("#upcoming-entries-filter").show();

		// Style the llink.
		$("a#upcoming-tab").addClass("active-entries");
		$("a#recent-tab").removeClass("active-entries");

		return false;

   });

   $("a#recent-tab").click(function() {

		$("#upcoming-results").hide();
		$("#recent-results").show();
		$("#recent-entries-filter").show();
		$("#upcoming-entries-filter").hide();

		$("a#upcoming-tab").removeClass("active-entries");
		$("a#recent-tab").addClass("active-entries");

		return false;

   });

   $("#link-entry").fancybox({
		'width'				: 965,
		'height'			: 600,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe',
		'onClosed': function() {
   			parent.location.reload(true);	
		} 
	});
	
	$("#sport-select").fancybox({
		'width'				: 950,
		'height'			: 600,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});
	
	$("#image-select-link").fancybox({
		'width'				: 952,
		'height'			: 550,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});
	
	 $("#link-entry-bottom").fancybox({
		'width'				: 965,
		'height'			: 600,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe',
		'onClosed': function() {
   			parent.location.reload(true);	
		} 
	});
	
	 $("#edit-updates").fancybox({
		'width'				: 420,
		'height'			: 350,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe',
		'onClosed': function() {
   			parent.location.reload(true);	
		} 
	});

	$("#add-photos").fancybox({
		'width'				: 950,
		'height'			: 600,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe',
		'onClosed': function() {
   			parent.location.reload(true);	
		} 
	});

	$("#add-videos").fancybox({
		'width'				: 420,
		'height'			: 350,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe',
		'onClosed': function() {
   			parent.location.reload(true);	
		} 
	});

	$("#email-admin").fancybox({
		'width'				: 420,
		'height'			: 550,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});

	$(".email-admin-btn").fancybox({
		'width'				: 420,
		'height'			: 430,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});

	$(".email-users").fancybox({
		'width'				: 420,
		'height'			: 395,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});

	$(".users-involved").fancybox({
		'width'				: 420,
		'height'			: 350,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'padding' : 0,
		'margin' : 0,
		'showCloseButton' : false,
		'type'				: 'iframe'
	});

   //$("#photos div.photos-thumb a img").fancybox();
   $("#photos div.photos-thumb a.img ").attr('rel', 'gallery').fancybox();

   // Show hide toggle.
   $("span.hidesection").click(function() {

		$(this).parent().next("div").toggle();

		if($(this).html() == "Hide") {
			$(this).html("Show");
		} else {
			$(this).html("Hide");
		}

   });
  
   //Active/Archive entries
   $('#active-entries-link').live('click',function(event){
		$('div.archive-entry-results').slideUp("slow", function() {
			 
			$('div.active-entry-results').slideDown("slow"); 
			$('#archive-entries-link').removeClass('active');
			$('#active-entries-link').addClass('active');
			 
			$('#archive-filter').hide();
			$('#active-filter').show();
		});
		event.preventDefault();
	});
	
	 $('#archive-entries-link').live('click',function(event){
		$('div.active-entry-results').slideUp("slow", function() {
			
			$('div.archive-entry-results').slideDown("slow"); 
			 $('#active-entries-link').removeClass('active');
			 $('#archive-entries-link').addClass('active');
			 
			 $('#active-filter').hide();	 
			 $('#archive-filter').show();
			 
		});
		event.preventDefault();
	});
	
   //Saved Active/Archive entries
   $('#saved-active-entries-link').live('click',function(event){
		$('div.saved-archive-entry-results').slideUp("slow", function() {
			 
			$('div.saved-active-entry-results').slideDown("slow"); 
			$('#saved-archive-entries-link').removeClass('active');
			$('#saved-active-entries-link').addClass('active');
			 
			$('#saved-archive-filter').hide();	 
			$('#saved-active-filter').fadeIn();
		});
		event.preventDefault();
	});
	
	 $('#saved-archive-entries-link').live('click',function(event){
		$('div.saved-active-entry-results').slideUp("slow", function() {
			
			 $('div.saved-archive-entry-results').slideDown("slow"); 
			 $('#saved-active-entries-link').removeClass('active');
			 $('#saved-archive-entries-link').addClass('active');
			 
			 $('#saved-active-filter').hide();
			 $('#saved-archive-filter').show(); 
		});
		event.preventDefault();
	});


   $("span.popout").live('click', function() {
		if($("div.options-popout").is(':visible')){
			$(this).parent().find("div.options-popout").hide();
		} else {
			$(this).parent().find("div.options-popout").show();
		}
   });

   // Profile page gs.
   $("select#your-entries-filter").change(function() {
	
		if($("#filter_user_id").val()) {
			filter_url = "/profiles/filter_your_entries/" + $(this).val() + "/"+ $("#filter_user_id").val();
		} else {
			filter_url = "/profiles/filter_your_entries/" + $(this).val();
		}
		//console.log(filter_url);
		$("div.added-entries div.active-entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("div.added-entries div.active-entry-results").load(filter_url);
   });

   $("select#saved-entries-filter").change(function() {
		filter_url = "/profiles/filter_saved_entries/" + $(this).val();
		//if($("#filter_user_id").val()) {
		//	filter_url = "/profiles/filter_saved_entries/" + $(this).val() + "/"+ $("#filter_user_id").val();
		//} else {
		//	filter_url = "/profiles/filter_saved_entries/" + $(this).val();
		//}
		$("div.saved-entries div.saved-active-entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("div.saved-entries div.saved-active-entry-results").load(filter_url);
   });

   $("select#archive-your-entries-filter").change(function() {
		//filter_url = "/profiles/filter_archive_your_entries/" + $(this).val();
		if($("#filter_user_id").val()) {
			filter_url = "/profiles/filter_archive_your_entries/" + $(this).val() + "/"+ $("#filter_user_id").val();
		} else {
			filter_url = "/profiles/filter_archive_your_entries/" + $(this).val();
		}
		$("div.added-entries div.archive-entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("div.added-entries div.archive-entry-results").load(filter_url);
   });

   $("select#archive-saved-entries-filter").change(function() {
		filter_url = "/profiles/filter_archive_saved_entries/" + $(this).val();
		$("div.bookmark-archived-entries div.saved-archive-entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("div.bookmark-archived-entries div.saved-archive-entry-results").load(filter_url);
   });

   $("select#linked-filter").change(function() {
		filter_url = "/entries/filter_links/" + $("#entry_id").val() + "/" + $(this).val();
		$("#linked_entries_panel div.entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("#linked_entries_panel div.entry-results").load(filter_url);
   });
   
   //Bookmarks filter
   $("input#bookmark-filter-button").live('click',function() {
		filter_url = "/bookmarks/" + $('#bookmark-entry-id').val() + "/false/" + $('#bookmark-search-filter').val();
		$("div.bookmark-archived-entries div.saved-archive-entry-results").html('<div class="loader"><img src="/images/loader.gif" /></div>');
		$("div.bookmark-archived-entries div.saved-archive-entry-results").load(filter_url);
   });
   
   
   //entry tooltip
   $('#add-entry').click(function(){
   		$('#add-entry-tool-tip').fadeIn();
   });
   $('#create-auto-link').click(function(){
   		$('#add-entry-tool-tip').fadeIn();
   });
   $('#close-tool-tip').click(function(){
   		$('#add-entry-tool-tip').fadeOut();
   });
   
   $('a.remove').click(function(){
	   	
		var answer = confirm("You have chosen to delete this entry, are you sure?")
		
		if(answer)
			return true;
		else
			return false;
		
   });



    // Popup for not loggedin users.
	// Must be placed after fancy box code, otherwise it will overwite it.
   $(".not-logged-in").click(function() {
		alert("You must be logged in to use this function");
		return false;
   });

   $(".confirm-bookmark-activity").click(function() {
   		answer = confirm("You are about to bookmark this activity. Are you sure you wish to do this?");
   		if(!answer) {
   			return false;
   		}
   });


   // Membership Popups
   $(".confirm-membership").click(function() {

		message = ""

		if(!$(".confirm-membership").hasClass('un-confirmed-membership') && !$(".confirm-membership").hasClass('confirmed-membership')) {
			message = "Notification will be sent to the administrator of this entry to confirm you are a member, do you wish to continue?"
		}

		if($(".confirm-bookmark-club").hasClass('confirmed-bookmark')) {
			message = "You have chosen to move to the ‘I am member’ list. This will send a notification to the administrator of the entry to confirm you are a member. Do you wish to continue?"
		}

		if(message != "") {
   			answer = confirm(message);
   			if(!answer) {
   				return false;
   			}
   		}
   });

   $(".confirm-bookmark-club").click(function() {

   		message = ""

   		if(!$(".confirm-bookmark-club").hasClass('confirmed-bookmark')) {
   			message = "You are about to bookmark this club. Are you sure you wish to do this?"
   		}

   		if($(".confirm-membership").hasClass('un-confirmed-membership')) {
   			message = "This will cancel your ‘I am a member’ request’ Do you wish to continue?"
   		}

   		if($(".confirm-membership").hasClass('confirmed-membership')) {
   			message = "You have chosen to move to the ‘save entry’ list this will remove your from the ‘member’ list, do you wish to continue?"
   		}



   		if(message != "") {
   			answer = confirm(message);
   			if(!answer) {
   				return false;
   			}
   		}
   });


   $(".confirm-bookmark-team").click(function() {

   		message = ""

   		if(!$(".confirm-bookmark-team").hasClass('confirmed-bookmark')) {
   			message = "You are about to bookmark this team. Are you sure you wish to do this?"
   		}

   		if($(".confirm-membership").hasClass('un-confirmed-membership')) {
   			message = "This will cancel your ‘I am a member’ request’ Do you wish to continue?"
   		}

   		if($(".confirm-membership").hasClass('confirmed-membership')) {
   			message = "You have chosen to move to the ‘save entry’ list this will remove your from the ‘member’ list, do you wish to continue?"
   		}

   		if(message != "") {
   			answer = confirm(message);
   			if(!answer) {
   				return false;
   			}
   		}
   });

   $(".confirm-bookmark-venue").click(function() {

   		message = ""

   		if(!$(".confirm-bookmark-venue").hasClass('confirmed-bookmark')) {
   			message = "You are about to bookmark this venue. Are you sure you wish to do this?"
   		}

   		if(message != "") {
   			answer = confirm(message);
   			if(!answer) {
   				return false;
   			}
   		}
   });

});

// Helper functions.

String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

function reset_search() {
	$("input[name=participation]").show();
	$("input[name=spectator]").show();
	$("input[name=social]").show();
	$("input[name=disbaled_access]").show();
	$("input[name=child_friendly]").show();
	$("select[name=intensity]").show();
	$("select[name=comedy]").show();
	$("input#startdate").show();
	$("input#enddate").show();
	$("img.ui-datepicker-trigger").show();
	$("select[name=intensity]").show();
	$("select[name=comedy]").show();
	$("select[name=cost]").show();
}

function push_sports(sports) {
	$.fancybox.close();
	sports_string = sports.join(", ");
	$("#sports").val(sports_string);
	
	
	$(".sports-display-element").empty();
	for (var i = 0; i < sports.length; i++) {
		$(".sports-display-element").append("<span>" + sports[i]  + "</span>");
	}
	if(sports.length == 0) {
		$(".sports-display-element").append("<span>Click to select a sport</span>");
	}
	
}

function pull_sports() {
	return $("#sports").val();
}

function push_image(image) {
	$.fancybox.close();
	
	url = image[0];
	id = image[1];
	
	$(".selected-image img").attr("src", url);
	$("input#gallery_image_path").val(url);
	$("#gallery_id").val(id);
}
