
/** add click functions on load.
 */
$j(function () {
	$j('#choose_plan input:checked').click();
	$j('#gym_info .options a, #club_search_options .options a').click(function() {
		$j(this).next().toggle();
		if ($j(this).find('span').html() == '+') {
			$j(this).find('span').html('-');
		}
		else {
			$j(this).find('span').html('+');
		}
	});

	$j('#sample_background').click(function () {
		$j('#sample_background, #sample_plan').fadeOut(650);
	});
	$j('#update_info_form').submit(function (e) {
		submit_club_correction();
		e.preventDefault();
	});
	$j('#action').val('');
	$j('#search_map_cont a.close').click(function (e) {
		e.preventDefault();
		$j('#overlay').hide();
		$j('#search_map_cont').hide('normal').css({left: 0, top: 0}).removeClass('modal');
	});
	$j('#update_info_cont a.close').click(function (e) {
		e.preventDefault();
		$j('#overlay').hide();
		$j('#update_info_cont').hide('normal').css({left: 0, top: 0}).removeClass('modal');
	});
	$j('#overlay').click(function (e) {
		e.preventDefault();
		$j(this).hide();
		$j('#content_container .modal').hide('normal').css({left: 0, top: 0}).removeClass('modal');
	});
	
	$j('#club_search .b_main_small, #club_search .b_green').click(function () {
		do_club_search();
	});
	$j('#club_search :text').keypress(function (e) {
		if (e.keyCode == '13') do_club_search();
	});
});

function do_club_search() {
	$j('#club_search_form').submit();
}

/** show a different province form element depending on the country selected.
 * @param value
 */
function show_provinces(value, id_prefix, input_prefix) {
	if ($j('#'+ id_prefix +'provinces_'+value).length) {
		var input = "<select name=\""+ input_prefix +"province\">"+ $j('#'+ id_prefix +'provinces_'+value).html() +"</select>";
	}
	else {
		var input = "<input name=\""+ input_prefix +"province\" value=\""+ $j('#'+ id_prefix +'province_input').val() +"\">";
	}
	$j('#'+ id_prefix +'province_cont').html(input);
}

/** show/hide content dependant on the selected plan.
 */
function show_plan_info(plan, plan_id, price, per_month, status) {
	$j('#plan_name').html(plan);
	if (price != 0) $j('#plan_price').html(' ($'+ price + per_month +')');
	else $j('#plan_price').html('');

	$j('#status').val(status);
	$j('#silver_info').hide();
	$j('#gold_info').hide();
	
	switch (plan_id) {
	case 1:
		$j('#silver_info').show();
		break;
	case 2:
		$j('#silver_info').show();
		$j('#gold_info').show();
		break;
	}
}

/** check if an order can be submitted (does an upgrade need a price update)
 * @return bool
 */
function check_order() {
	$j.ajax({
		type: "POST",
		url: "payment.php",
		dataType: "html",
		data: "a=check",
		success: function(result) {
			if (result != '') {
				alert(result);
			}
			else {
				document.forms.payment_form.submit();
			}
		},
		error: function() {
			document.forms.payment_form.submit();
		}
	});
}

/** submit the remove clubs form.
 * @param form_id
 * @param no_clubs_message
 */
function remove_clubs(form_id, no_clubs_message) {
	if ($j('#'+ form_id +' input:checkbox:checked').length > 0) {
		$j('#action').val('r');
		$j('#'+ form_id)[0].submit();
	}
	else {
		alert(no_clubs_message);
	}
}

var last_plan = '';

/** toggle the visibility of the sample layout.
 */
function toggle_sample(plan, anchor) {
	if (plan == 'silver') {
		$j('#silver_cont').show();
		$j('#gold_cont').hide();
	}
	else if (plan == 'gold') {
		$j('#silver_cont').show();
		$j('#gold_cont').show();
	}
	else {
		$j('#silver_cont').hide();
		$j('#gold_cont').hide();
	}
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
	}
	
	var windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	var pos = $j(anchor).parent().parent().offset();
	var top = pos.top;

	$j('#sample_plan').css({
		top: top + 5,
		left: 37
	});
	$j('#sample_background').css({
		height: pageHeight
	});
	$j('#sample_background, #sample_plan').fadeIn(550);
	last_plan = plan;
}

/** show a popup map of the specified gym for the specified location.
 * @param latitude
 * @param longitude
 */
function show_map(latitude, longitude, anchor) {
	var top = $j(anchor).parents('.search_item').position().top;
	var left = $j(anchor).position().left;
	$j('#search_map_cont').show().animate({
		left: left - 4 - $j('#search_map_cont').outerWidth(),
		top: top + 35
	}, 'normal').addClass('modal');

	show_overlay();
	
	if ($j('#search_map_cont').css('display') == 'block') {
		if (GBrowserIsCompatible()) {
			if (!map) {
				map = new google.maps.Map2(document.getElementById("search_map"));
				map.addControl(new GSmallZoomControl3D());
			}
			map.clearOverlays();
			map.setCenter(new google.maps.LatLng(latitude, longitude), 14);
			map.addOverlay(new GMarker(map.getCenter()));
		}
	}
}

/** show a form to correct a gym's address information.
 * @param club_id
 */
function correct_club_info(club_id, user_id, anchor) {
	var club_user_id = $j('#club_'+ club_id +'_user_id').val();
	var left = $j(anchor).position().left + $j(anchor).innerWidth() + 15;
	var top = $j(anchor).parent().parent().position().top - 35;

	// if this is the club owner, redirect them to the edit page 
	if (club_user_id > 0 && user_id == club_user_id) {
		window.location.href = $j('#club_'+ club_id +'_edit_href').val();
		return;
	}
	// show update info form
	$j('#update_info_cont').show().animate({
		left: left,
		top: top
	}, 'normal').addClass('modal');
	show_overlay();
	
	$j('#club_name strong').html($j('#club_'+ club_id +'_name').val());
	$j('#club_id').val(club_id);
	$j('#club_address').val($j('#club_'+ club_id +'_address').val());
	$j('#club_city').val($j('#club_'+ club_id +'_city').val());
	$j('#club_country').val($j('#club_'+ club_id +'_country').val());
	$j('#club_postal_code').val($j('#club_'+ club_id +'_postal_code').val());
	$j('#club_phone_number').val($j('#club_'+ club_id +'_phone_number').val());
	show_provinces($j('#club_country').val(), 'club_', 'club_');
	$j('#club_province_cont [name=club_province]').val($j('#club_'+ club_id +'_province').val());
	$j('#club_description').val($j('#club_'+ club_id +'_description').val());
}

function submit_club_correction() {
	var valid = true;
	var data = '';
	var club_id = $j('#club_id').val();

	$j('#club_address').parent().removeClass('required');
	$j('#club_description').parent().removeClass('required');
	$j('#club_city').parent().removeClass('required');
	$j('#club_country').parent().removeClass('required');
	$j('#club_province_cont').parent().removeClass('required');
	$j('#club_postal_code').parent().removeClass('required');
	$j('#club_phone_number').parent().removeClass('required');
	$j('#verify_img').parent().removeClass('required');
	
	data = 'club_id=' + parseInt(club_id);
	
	// check all fields are filled in
	if ($j('#contact_name').val() == '') {
		$j('#contact_name').parent().addClass('required');
		valid = false;
	}
	else data += '&contact_name=' + $j('#contact_name').val();

	if ($j('#contact_email').val() == '') {
		$j('#contact_email').parent().addClass('required');
		valid = false;
	}
	else data += '&contact_email=' + $j('#contact_email').val();

	if ($j('#contact_phone_number').val() == '') {
		$j('#contact_phone_number').parent().addClass('required');
		valid = false;
	}
	else data += '&contact_phone_number=' + $j('#contact_phone_number').val();
	
	if ($j('#club_address').val() == '') {
		$j('#club_address').parent().addClass('required');
		valid = false;
	}
	else data += '&address=' + $j('#club_address').val();
	
	if ($j('#club_description').val() == '') {
		$j('#club_description').parent().addClass('required');
		valid = false;
	}
	else data += '&description=' + $j('#club_description').val();

	if ($j('#club_city').val() == '') {
		$j('#club_city').parent().addClass('required');
		valid = false;
	}
	else data += '&city=' + $j('#club_city').val();

	if ($j('#club_country').val() == '') {
		$j('#club_country').parent().addClass('required');
		valid = false;
	}
	else data += '&club_country=' + $j('#club_country').val();

	if ($j('#club_province_cont [name=club_province]').val() == '') {
		$j('#club_province_cont').parent().addClass('required');
		valid = false;
	}
	else data += '&club_province=' + $j('#club_province_cont [name=club_province]').val();

	if ($j('#club_postal_code').val() == '') {
		$j('#club_postal_code').parent().addClass('required');
		valid = false;
	}
	else data += '&postal_code=' + $j('#club_postal_code').val();

	if ($j('#club_phone_number').val() == '') {
		$j('#club_phone_number').parent().addClass('required');
		valid = false;
	}
	else data += '&phone_number=' + $j('#club_phone_number').val();

	if ($j('#verify_img').val() == '') {
		$j('#verify_img').parent().addClass('required');
		valid = false;
	}
	else data += '&verify_img=' + $j('#verify_img').val();
	
	if (valid) {
		$j('#club_'+ club_id +' .message').removeClass('required');
		$j.ajax({
			type: "POST",
			url: $j('#update_info_form').attr('action')+ "correction.php",
			dataType: "json",
			data: data,
			success: function(data) {
				if (data.success) {
					// show the success message
					$j('#club_'+ club_id +' .message').html(data.message).css({'border': '1px solid #4B8F08', 'color': '#4B8F08', 'margin-top': '5px', 'padding': '4px'}).show();
					// clear the link used
					$j('#club_'+ club_id +' .correct').replaceWith('');
				}
				else {
					// show the error message
					$j('#club_'+ club_id +' .message').html(data.message).addClass('required').css({'margin-top': '5px'}).show();
					var today = new Date();
					$j('#verify_img_cont img').attr('src', '/crypt_img.php?'+ today.getTime());
				}
			}
		});
		$j('#overlay').hide();
		$j('#update_info_cont').fadeOut(550);
	}	
}

/** return the cookie value for name passed in, if it's set. 
 * @param name
 */
function get_cookie(name) {
	var nameEQ = name + "=";
	var cookies = document.cookie.split(';');	
	for(var i=0; i < cookies.length; i++) {
		var cookie = cookies[i];
		// remove any leading spaces
		while (cookie.charAt(0) == ' ') {
			cookie = cookie.substring(1, cookie.length);
		}
		if (cookie.indexOf(nameEQ) == 0) {
			return cookie.substring(nameEQ.length, cookie.length);
		}
	}
	return null;
}

