	/* Faking rollovers for LIs in IE6 */

/*if ($.browser.msie) {

	$(document).ready(function(){
		$('li:has(ul)','#menu').hover(
			function () {
				$(this).addClass('hover');
			},
			function () {
				$(this).removeClass('hover');
			}
		);
	});

}*/



$(document).ready(function(){

	// Starting menu

	if ($.browser.msie) {
	
		$('#menu ul').eq(0).superfish({
			hoverClass:    'sfHover',
			pathClass:     'hover',
			pathLevels:    1,
			delay:         800,
			animation:     {},
			speed:         'normal',
			autoArrows:    false,
			dropShadows:   false,
			disableHI:     true,
			onInit:        function(){
				
			},
			onBeforeShow:  function(){
				$('#menu ul ul').css({
					display:'block',
					visibility:'visible'
				});
			}
		}); 
	
	} else {
	
		$('#menu ul').eq(0).superfish({
			hoverClass:    'sfHover',
			pathClass:     'hover',
			pathLevels:    1,
			delay:         800,
			animation:     {opacity:'1'},
			speed:         'normal',
			autoArrows:    false,
			dropShadows:   false,
			disableHI:     true,
			onInit:        function(){
				
			},
			onBeforeShow:  function(){
				$(this).css({
					display:'block',
					opacity:'0'
				});
			}
		}); 
	
	}

	/* Starting carousel */
	
	$('#quotes').innerfade({
		/*speed: 1000,
		timeout: 6000,
		type: 'sequence',
		containerheight:'230px'*/
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '230px',
		containerwidth: '600px',
		animationSpeed: 5000,
		animationtype: 'fade'
	});

	$('#quotes').css({
		position:'absolute',
		right:'0px'
	});

	// Mechanism to show/hide bios/abstracts

	if (page_id == 'program') {

		$(".options a").click(function(){
			var root = this.parentNode.parentNode.parentNode.parentNode.parentNode;
			if ( $(this).hasClass("show-bio") ) {
				if($(root).find(".bio").is(":hidden")){
					$(this).text("Hide bio");
					if ( $.browser.safari ) {
	 					$(root).find(".bio").addClass("show");
	 				}
	 				else {
	 					$(root).find(".bio").show('fast');
	 				}
					return false;
				}
				else{
					$(this).text("Show bio");
					if ( $.browser.safari ) {
	 					$(root).find(".bio").removeClass("show");
	 				}
	 				else {
	 					$(root).find(".bio").hide('slow');
	 				}
					return false;
				}
			}
			if ( $(this).hasClass("show-abstract") ) {
				if($(root).find(".abstract").is(":hidden")){
					$(this).text("Hide abstract");
					if ( $.browser.safari ) {
	 					$(root).find(".abstract").addClass("show");
	 				}
	 				else {
	 					$(root).find(".abstract").show('fast');
	 				}
					return false;
				}
				else{
					$(this).text("Show abstract");
					if ( $.browser.safari ) {
	 					$(root).find(".abstract").removeClass("show");
	 				}
	 				else {
	 					$(root).find(".abstract").hide('fast');
	 				}
					return false;
				}
			}
		});

	}

	// Stuff needed at Registration page

	if (page_id == 'registration') {

		// select handlers
		$('#payment-type').change(function (i) {
			if (this.options[this.selectedIndex].value == 'pp') {
				$('#credit-card-options').hide();
				$('#paypal-options').show();
			} else {
				$('#credit-card-options').show();
				$('#paypal-options').hide();
			}
		}).change();
		$('#number-of-attendees').change(function (i) {
			var attendees = parseInt(this.options[this.selectedIndex].value);
			for (var j = 1;j<attendees+1;j++) {
				$('#ad'+j).show();
			}
			for (var j = attendees+1;j<11;j++) {
				$('#ad'+j).hide();
			}
		}).change();

        // populate attendee 1 info from purchaser
        var attendee_name_changed = false;
        $('#first-name').change(function () {
            if(!attendee_name_changed) {
                $('#ad1-first-name').val(this.value);
            }
        });
        $('#ad1-first-name').change(function () {
            attendee_name_changed = true;
        });
        $('#last-name').change(function () {
            if(!attendee_name_changed) {
                $('#ad1-last-name').val(this.value);
            }
        });
        $('#ad1-last-name').change(function () {
            attendee_name_changed = true;
        });
        $('#email').change(function () {
            if(!attendee_name_changed) {
                $('#ad1-email').val(this.value);
            }
        });
        $('#ad1-email').change(function () {
            attendee_name_changed = true;
        });

		//tooltips
		$('a#cvv2').tooltip({
			track: true,
			bodyHandler: function() {
				return $('#cvv-definition').html();
			},
			showURL: false
		});

		// validate form
		$("#registration-form").validate({
			errorElement: "div",
			errorClass: "error-msg",
			errorContainer: $("#error-msg"),
			errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
			},
			highlight: function(element, errorClass) {
				$(element).parent().addClass('error');
			},
			unhighlight: function(element, errorClass) {
				$(element).parent().removeClass('error');
			},
			rules: {
                                'registration_type': {
                                  required: true
                                },
                                'reg[email]': {
                                        equalTo:'#email2'
                                },
                                'email2': {
                                        required:true,
                                        email:true
                                },
				'reg[first_name]': {
					required:true
				},
				'reg[last_name]': {
					required:true
				},
				'cc[name]': {
					required: function(element) {
						var sel = $('#payment-type').get(0);
						return sel.options[sel.selectedIndex].value == 'cc';
					}
				},
				'cc[number]': {
					required: function(element) {
						var sel = $('#payment-type').get(0);
						return sel.options[sel.selectedIndex].value == 'cc';
					}
				},
				'cc[csc]': {
					required: function(element) {
						var sel = $('#payment-type').get(0);
						return sel.options[sel.selectedIndex].value == 'cc';
					}
				}
			}

		});

	}
	
	// Profile 
	
	if (page_id == 'community_profile' || page_id == 'community_profile_edit') {
		$('.main-info a,.col-4 #gravatar1').eq(0).tooltip({
			track: true,
			bodyHandler: function() {
				return $('#gravatar-exp').html();
			},
			showURL: false
		});
	}
	
	
	// Profile edit page
	
	if (page_id == 'community_profile_edit') {
		// validate form
		$("#profile-edit-form").validate({
			errorElement: "div",
			errorClass: "error-msg",
			errorContainer: $("#error-msg"),
			errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
			},
			highlight: function(element, errorClass) {
				$(element).parent().addClass('error');
			},
			unhighlight: function(element, errorClass) {
				$(element).parent().removeClass('error');
			},
			rules: {
				'first-name': {
					required:true,
					email:true
				},
				'last-name': {
					required:true
				}
			}

		});
	}
	
	// Community section -- pass select screen

	if (page_id == 'community_profile_pass') {
		// validate form
		$("#select-pass").validate({
			errorElement: "div",
			errorClass: "error-msg",
			errorContainer: $("#error-msg"),
			errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
			},
			highlight: function(element, errorClass) {
				$(element).parent().addClass('error');
			},
			unhighlight: function(element, errorClass) {
				$(element).parent().removeClass('error');
			},
			rules: {
				'password': {
					required:true
				},
				'c-password': {
					required:true,
					equalTo: "#password"
				}
			}

		});
	}
	
	// Banners functions
	
	if (page_id == 'media_banners_and_buttons' || page_id == 'media_spread') {
		$('.banners-gallery a.trigger, ul.stw-banners li a').click(function(e) {
			$('#'+this.id+'-code').dialog({
				buttons: { 
					"Cancel": function() { 
							$(this).dialog("close"); 
					} 
				} ,
				title: 'Copy the code and paste it into your website',
				modal: true,
				overlay:{
					background:'#000',
					opacity:0.8
				},
				width:513,
				resizable:false,
				draggable:false,
				height:200
			});
			return false;
		});
	}
	
	// Spread the Word
	
	if (page_id == 'media_spread') {
		// validate form
		$("#spread-the-word").validate({
			errorElement: "div",
			errorClass: "error-msg",
			errorContainer: $("#error-msg"),
			errorPlacement: function(error, element) {
				error.appendTo( element.parent() );
			},
			highlight: function(element, errorClass) {
				$(element).parent().addClass('error');
			},
			unhighlight: function(element, errorClass) {
				$(element).parent().removeClass('error');
			},
			rules: {
				'from': {
					required:true,
					email:true
				},
				'to': {
					required:true
				},
				'message': {
					required:true
				}
				
			}
		});
	}
	
	// Home profile images rollovers
	
	if (page_id == 'home') {
	
		/*$('div.profiles li a').eq(0).fancybox({ 
			'hideOnContentClick':true,
			'zoomSpeedIn': 300, 
			'zoomSpeedOut': 100, 
			'overlayShow': true ,
			'overlayOpacity': 0.8
		}); 
		
		$('div.profiles li a img').eq(1).mouseover(function(){
			this.src='/images/content/home/profiles_over.jpg';
		}).mouseout(function(){
			this.src='/images/content/home/profiles_kurzweil.jpg';
		});
		$('div.profiles li a img').eq(2).mouseover(function(){
			this.src='/images/content/home/profiles_over.jpg';
		}).mouseout(function(){
			this.src='/images/content/home/profiles_diamandis.jpg';
		});*/
	}
	
	// images
	
	if (page_id == 'media_images') {
		$('ul.images li a').fancybox({ 
			'hideOnContentClick':true,
			'zoomSpeedIn': 300, 
			'zoomSpeedOut': 100, 
			'overlayShow': true ,
			'overlayOpacity': 0.8
		}); 
	}

});
