$(document).ready(function() {

	//Leadership Form
	$('form.students').validate({
		errorContainer: $('form.students div.error'),
		errorPlacement: function(error, element) {},
		meta: 'validate',
		rules: {
			full_name: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
            phone: {
				required: true
			},
			university: {
				required: true
			},
			city: {
				required: true
			}
		}
	});

	//Leadership Form
	$('form.leadership').validate({
		errorContainer: $('form.leadership div.error'),
		errorPlacement: function(error, element) {},
		meta: 'validate',
		rules: {
			full_name: {
				required: true
			},
			position: {
				required: true
			},
			company: {
				required: true
			},
			email: {
				required: true,
				email: true
			}
		}
	});
    
    //Wireless CTF Form
	$('form.wireless-ctf').validate({
		errorContainer: $('form.wireless-ctf div.error'),
		errorPlacement: function(error, element) {},
		meta: 'validate',
		rules: {
			full_name: {
				required: true
			},
			country: {
				required: true
			},
			city: {
				required: true
			},
			email: {
				required: true,
				email: true
			}
		}
	});
    var options = { 
        success: showResponse,
		resetForm: true
    };
	function showResponse(responseText, statusText, xhr, $form) {
		if(responseText != 'sent') {
			alert('There were technical issues submitting your request, please try again at a later time.');
		} else {
			alert('Thank you for the application. We will contact you as soon as possible.');		
		}
	}
   $('form.leadership, form.wireless-ctf, form.students').ajaxForm(options);
   
	//Cleanup
	$('ul.periods li').addClass('clearfix');
	//Events
	$('div.schedule ul.tabs a').click(function() {
		$('div.schedule ul.tabs li').removeClass('selected');
		$(this).parent().addClass('selected');
		$('div.schedule').find('div.timetable').hide();
		$('div.schedule').find('#' + $(this).attr('rel')).show(); 
	});
	$('a.tab-launcher').click(function() {
		$('ul.tabs li').removeClass('selected');
		$('ul.tabs li.' + $(this).attr('rel')).addClass('selected');
		$('div.schedule').find('div.timetable').hide();
		$('div.schedule').find('#' + $(this).attr('rel')).show();
		$('html, body').animate({ scrollTop: ($('ul.tabs li.' + $(this).attr('rel')).offset().top - 15) }, { duration: 'slow', easing: 'swing'});		
	});
	$('div.lightbox a.close, div#overlay').click(function() {
        window.location.hash = '#!/';
		$('div.lightbox').fadeOut();
		$('div#overlay').fadeOut();
	});
	$('ul#navigation a, a.register, a.all-speakers, a.summit-apply, li.summit, .lightbox-launcher, a.speaker').live('click', function() {
		if($(this).attr('rel')) {
            $('div.lightbox').hide();
			$('div#overlay').show();
			$('#' + $(this).attr('rel')).show();
		}
	});
	//Next bio
	$('div.bio a.next').click(function() {
		$(this).parents('div.bio').hide();
		if($(this).parents('div.bio').next().length == 0) {
			$('div.bio:first-child').fadeIn(200);
		} else {
			$(this).parents('div.bio').next().fadeIn(200);
		}
	});
	//Previous bio
	$('div.bio a.previous').click(function() {
		$(this).parents('div.bio').hide();
		if($(this).parents('div.bio').prev().length == 0) {
			$('div.bio:last-child').fadeIn(200);
		} else {
			$(this).parents('div.bio').prev().fadeIn(200);
		}
	});

    //Speaker's Marquee
    marqueeInit({
        uniqueid: 'marquee',
        style: {
            'height': '30px',
            'width': '900px'
        },
        inc: 10,
        mouse: 'cursor driven',
        moveatleast: 1,
        neutral: 300,
        savedirection: true
    });
    
    //Sticky categories
	$('div#legend').stickySidebar({
		timer: 400,
        easing: 'easeInOutBack'
	});
    
    //Workshop expansion
    $('span.switch').click(function() {
    
        var description = $(this).closest('div.description');
        var full = description.find('div.full');
        var summary = description.find('div.summary');
        var button = $(this);
        
        if(summary.is(':visible')) {
            summary.hide();
            full.fadeIn('slow');
            button.text('close');
        } else {
            full.hide();
            summary.fadeIn('slow');
            button.text('details');
        }
        
    });
    
    //Subtabs
    $('ul.subtabs a').click(function() {
        var subtabs = $(this).closest('ul.subtabs');
		subtabs.find('li').removeClass('selected');
		$(this).closest('li').addClass('selected');
        
        //Time height fix
        var timeHeightFix = function() {
            $('div.time').each(function() {
                var period = $(this).closest('ul.periods > li');
                var time = period.find('div.time');
                var content = period.find('div.description-wrapper');
                if(time.hasClass('terminate')) {
                    time.height(content.innerHeight() - 35);
                } else {
                    time.height(content.innerHeight() + 15);
                }
            });
        }
        
        //Awww yea, ravioli hacks like a boss!
        //TODO: Make this more abstract
        if($(this).attr('rel') == 'keynotes') {
            $('#talks').hide();
            $('#talks-2').hide();
            $('#keynotes').show();
            $('div#conference div.next.to-talks-2').hide();
            $('div#conference div.next.to-talks').show();
        } else if($(this).attr('rel') == 'talks') {
            $('#keynotes').hide();
            $('#talks-2').hide();
            $('#talks').show();
            $('div#conference div.next.to-talks').hide();
            $('div#conference div.next.to-talks-2').show();
            timeHeightFix();
        } else if($(this).attr('rel') == 'talks-2') {
            $('#keynotes').hide();
            $('#talks').hide();
            $('#talks-2').show();
            $('div#conference div.next.to-talks').hide();
            $('div#conference div.next.to-talks-2').hide();
            timeHeightFix();
        } else if($(this).attr('rel') == 'one-day-workshops') {
            $('#two-day-workshops').hide();
            $('#one-day-workshops').show();
        } else if($(this).attr('rel') == 'two-day-workshops') {
            $('#one-day-workshops').hide();
            $('#two-day-workshops').show();
        } else if($(this).attr('rel') == 'wwiv') {
            $('#wireless-ctf').hide();
            $('#wwiv').show();
        } else if($(this).attr('rel') == 'wireless-ctf') {
            $('#wwiv').hide();
            $('#wireless-ctf').show();
        }
    });
    
    //Conference Next
    //TODO: clean up this cheese
    $('div#conference div.next').click(function() {
        var destination = $(this).find('a').attr('rel');
        function smoothTo(tabname) {
            $('html, body').animate({
                    scrollTop: ($('div#conference ul.subtabs li.' + tabname).offset().top - 15) 
                }, 
                { 
                    duration: 'slow', easing: 'swing'
                }
            );
        }
        if(destination == 'talks') {
            smoothTo('talks');
            $('a[rel="talks"]:first').trigger('click');
        } else if(destination == 'talks-2') {
            smoothTo('talks-2');
            $('a[rel="talks-2"]:first').trigger('click');
        }
    });
 
    //Cheesy hash navigation
    //TODO fix Parent to child routing!
    function filterHash(value){
        return $('<div/>').text(value.replace(/[^a-z0-9-]+/ig, '')).html();
    }
    if(window.location.hash) {
        var hash = filterHash(window.location.hash.substring(3));
        console.log(hash);  
        if(hash == 'workshops/one-day') {
            hash = 'one-day-workshops';
            $('a[rel="workshops"]:first').trigger('click');
        } else if(hash == 'workshops/two-day') {
            hash = 'two-day-workshops';
            $('a[rel="workshops"]:first').trigger('click');
        } else if(hash == 'events' || hash == 'events/wwiv') {
            hash = 'wwiv';
            $('a[rel="events"]:first').trigger('click');
        } else if(hash == 'events/wireless-ctf') {
            hash = 'wireless-ctf';
            $('a[rel="events"]:first').trigger('click');
        }
        var matched = $('a[rel="' + hash + '"]:first');
        if(matched.length > 0) {
            matched.trigger('click');
        }
    }
    
    //Totop Button
    $().UItoTop({ easingType: 'easeOutQuart' });
    
});
