(function($){	
	Cufon.replace('ul#menu li a', {hover: true, fontFamily: "PillSemiBd"});
	Cufon.replace('div#socialWebsites h3, div.leftBoxContent h3', {fontFamily: "PillSemiBd"});
	Cufon.replace('div.homeBox h2', {fontFamily: "PillLight"});
	Cufon.replace('div.homeBox h2 span', {fontFamily: "PillBold"});
	Cufon.replace('a.orangeBgBtn span, a.sectionBtn span', {hover: true, fontFamily: "PillRegular"});
	Cufon.replace('a#facebook span, a#hyves span, a#twitter span, a#envelope span, a#youtube span', {hover: true, fontFamily: "PillOblique"});
	
	$(function(){
		
		$("input[rel='"+$('#selectedCourse').val()+"']").attr('checked', true);
		
		$('div#languages a#selectedLang').bind('click', function(){
			$('div#languages ul').toggle();
			return false;
		});	
		$("a[rel='gallery']").colorbox({slideshow:true, height:"95%"});

		// profile tabs
		var currentTab = window.location.hash || '#personal';
		$('ul#profile li a[href="'+currentTab+'"]').parent().addClass('active');
		$(currentTab, '#profileForm').show();
		
		$('ul#profile li a').bind('click', function(){
			$('ul#profile li a[href="'+currentTab+'"]').parent().removeClass('active');
			$(currentTab, '#profileForm').hide();
		
			$(this).parent().addClass('active');
			currentTab = $(this).attr('href');
			$(currentTab, '#profileForm').show();	
		});
		
		$('input[name=use_billing_address]').bind('change', copyBillingInfo).trigger('change');
		
		$('a#copyAddress').bind('click', function(){
			$('textarea[name=custom_location]').val($('p#fixedAddress').text());			
			return false;
		});
		
		$('a.scheduleRemove').bind('click', function(){
			$(this).parent().parent().remove();
			return false;
		});
		
		$('a#addSchedule').bind('click', function(){
			var key = 'new_'+parseInt(Math.random()*1000, 10);
			$('table#schedule tbody').append('<tr><td><input type="text" class="calendarField textfield" size="14" name="schedule_start['+key+']" id="'+key+'" /></td><td><input type="text" class="calendarField textfield" size="14" name="schedule_end['+key+']"  id="end'+key+'" /></td><td><a href="#" class="scheduleRemove button"><span>Remove</span></a></td></tr>');
			
			$('#rightSide input.calendarField').each(
				function(){
					calId = $(this).attr('id');
					Calendar.setup({
						trigger    : calId,
						inputField : calId,
						dateFormat : "%Y-%m-%d %H:%M",
						showTime: 24
					});
				}
			);
			
			removeItem();
			return false;		
		});
		
		$('#rightSide input.calendarField').each(
			function(){
				calId = $(this).attr('id');
				Calendar.setup({
					trigger    : calId,
					inputField : calId,
					dateFormat : "%Y-%m-%d %H:%M",
					showTime: 24
				});
			}
		);
		
		$('a#showSchedule').bind('click', function(){
			$('table#courseDetails tr#scheduleRow').toggle();
			return false;	
		});
						
	});
	
	var copyBillingInfo = function(e) {
		if ($(this).attr('checked') === true) {
			$('input[name=accountcourse_address_line_1]').val($('input[name=accountbilling_address_line_1]').val());
			$('input[name=accountcourse_address_line_2]').val($('input[name=accountbilling_address_line_2]').val());
			$('input[name=accountcourse_postcode]').val($('input[name=accountbilling_postcode]').val());
			$('input[name=accountcourse_city]').val($('input[name=accountbilling_city]').val());
			$('select[name=accountcourse_country]').val($('select[name=accountbilling_country]').val());
		}
	}
	
	var removeItem = function(){
		$('a.scheduleRemove').bind('click', function(){
			$(this).parent().parent().remove();
			return false;
		});
	}
		
	EYE.newsletter = function() {
		var keyDown = function(ev) {
			if (ev.keyCode == 13) {
				submit();
				return false;
			}
		};
		var sending = false;
		var submit = function() {
			var el = $('#nwsl_email');
			var val = el.val();
			if (val != '' && sending == false) {
				$('#emailUpdates').css('opacity', .3);
				sending = true;
				$.ajax({
					complete: complete,
					data: {
						plugin: 'newsletter',
						email: val
					},
					success: success,
					url: '/ajaxserver.php',
					type: 'POST'
				});
			} else {
				el.focus();
			}
			return false;
		};
		var complete = function() {
			$('#emailUpdates').css('opacity', 1);
			sending == false;
		};
		var success = function(resp) {
			var responseErros = $('errorResponse', resp);
			$('#emailUpdates').find('em').remove();
			if (responseErros.size() > 0) {
				$('#nwsl_email')
					.after('<em>'+responseErros.text()+'</em>');
			} else {
				$('#emailUpdates')
					.find('*').hide().end()
					.append('<p>'+$('success', resp).text()+'</p>');
			}
		};
		return {
			init: function() {
				$('#nwsl_email').bind('keydown', keyDown);
				$('#nwsl_submit').bind('click', submit);
				$('a#envelope').toggle(
					function(){
						$('#emailUpdates').fadeIn();
						this.blur();
					},
					function(){
						$('#emailUpdates').fadeOut();
						this.blur();
					}
				);
			}
		};
	}();
	EYE.register(EYE.newsletter.init, 'init');
	
	EYE.course = function() {
		var indx;
		var initialCourse, initialType, changed = false;
		var emptyCourse = {
			level: 1,
			classes: 1,
			title: '',
			age: '',
			duration: ''
		};
		var changeType = function() {
			var selectedOpt = $(this).find('option:selected');
			indx = $.trim(selectedOpt.text().toLowerCase());
			var courses = EYE.predefinedCourses[indx];
			var html = [];
			for (var i=0, cnt=courses.length; i < cnt; i++) {
				courses[i].key = i;
				html.push(tmpl('courseTemplate', courses[i] ));
			}
			$('#predefinedCourses tbody')
				.find('tr').remove().end()
				.append(html.join(''));
			if (!changed && initialCourse) {
				if (initialType == indx) {
					$('#predefinedCourses input[value='+initialCourse+']').attr('checked', true);
				}
			}
			$('#predefinedCourses').trigger('click');
		};
		var checkSelection = function() {
			var tableEl = $(this);
			var selectedCourse = tableEl.find('input:checked');
			if (selectedCourse.size()) {
				tableEl.find('tr').removeClass('selected');
				selectedCourse
					.closest('tr').addClass('selected')
					.prev().addClass('selected');
				var courseIndx = parseInt(selectedCourse.closest('tr').attr('rel'), 10);
				setValues(EYE.predefinedCourses[indx][courseIndx]);
				if (indx != initialType || courseIndx != initialCourse) {
					changed = true;
				}
			} else {
				setValues(emptyCourse);
			}
		};
		var setValues = function(course) {
			$('select[name=custom_course_level]').val(course.level-1);
			$('select[name=custom_course_lenght]').val(course.classes-1);
			$('input[name=title]').val(course.title);
			$('input[name=custom_age_group]').val(course.age);
			$('input[name=custom_class_duration]').val(course.duration);
			$('input[name=selected_course]').val(indx+'|'+course.key);
		};
		return {
			'init': function () {
				if (document.getElementById('courseForm')) {
					$('select[name=custom_course_type]')
						.bind('change', changeType)
						.trigger('change');
					$('#predefinedCourses').bind('click', checkSelection);
				}
			}
		};
	}();
	EYE.register(EYE.course.init, 'init');
})(jQuery);
