$(document).ready(function(){ 
    
    function stripeMe(){
    $(".stripeMe tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
    $(".stripeMe tr:odd").addClass("alt");
    }
    
    function message(data, container){
        $(container + ' .message').empty().append(data).slideDown(1000);
    }
    
    function openViewer(){
        $('#main').animate({height:'900px'},1000, "easeOutQuint");
        return false;
    }
    
    function closeViewer(){
        $('#main').animate({height:'400px'},1000, "easeOutQuint");
        return false;
    }
    
    $('#tab').toggle(
        function(){
            $('#login-register').animate({top:0},1000, "easeOutQuint", function(){
                $('#tab').empty().html('Close &uarr;');
            });
        },
        function(){
            $('#login-register').animate({top:'-400px'},1000, "easeOutQuint", function(){
                $('#tab').empty().html('Login|Register &darr;');
            });
        }
    );
    
    $('.login-reg').live('click', function(){
        $('#tab').trigger('click');
        $('.qtip').trigger('mouseout');
    })
    
    //stuff for AJAX loading
    //script for loading recipes by category, perhaps I can generalize this even more for all functions
    $('#categories a').click(function(){
        var $category = $(this).attr('id');
        $('#thumb-strip').fadeOut(300)
            .load(
            'getThumbs.php',            
            {'category': $category      //data
            },  
            function(data) {            //callback
                $(this).hide().fadeIn(300);
            }
            );
    });
    
    $('#ask-superchef').click(function(){
        var $category = 'askSuperchef';
        $('#thumb-strip').fadeOut(300)
            .load(
            'getAskSuperchef.php',            
            {'category': $category      //data
            },  
            function(data) {            //callback
                $(this).hide().fadeIn(300);
            }
            );
    });

    //script for cooking school thumbs
    //
    $('#cookingSchool').click(function(){
        var $category = 'cookingSchool';
        $('#thumb-strip').fadeOut(300)
            .load(
            'getCookingSchool.php',
            {'category': $category      //data
            },  
            function(data) {            //callback
                $(this).hide().fadeIn(300);
            }
            );
    });
    
    //TODO keep working on this
    function loadRecipeText(videoclip){
        $.get(
            'getCosts.php',
            {'recipe': videoclip},
            function(data){
                $('#result').empty();
                $('#result').append(data);
                stripeMe();
            }
        );
    }   
    
    $('#slides').cycle({
        fx: 'fade',
        timeout: 5000,
        speed: 1000,
        prev: '.left-arrow',
        next: '.right-arrow'
    });
        
   $.localScroll({
        lazy:true,
        target:'#main',
        axis:'y',
        hash:true,
        easing:'easeInOutQuad',
        duration:500,
        onAfter:function(anchor){
            if (window.location.hash == '#videos' || window.location.hash == '#text-module' ){
                openViewer();
            }
            else{
                closeViewer();
            }
        }
    });
    //register new memebers
    /*
     TODOS
     need to add success/failure feedback with .ajax
     Silver success message: 'you will receive a confirmation email, please verify, yadda yadda'
     Gold success message: 'you will go to checkout page and be back shortly'
     receive cookie to change interface if logged in?
    */
    function forgotPassword(){
        var forgotPasswordData = $('#forgotPassword form').serialize();
        $.post(
            'forgotPassword.php',
            forgotPasswordData,
            function(data){
                var container = '#login';
                message(data, container);
            }
        )
    }
    
    function inputPassword(){
        var newPasswordData = $('#newPassword').serialize();
        $.post(
            'inputPassword.php',
            newPasswordData,
            function(data){
                var container = '';
                message(data, container);
            }
        );
    }
    
    function upgradeMe(){
        var upgradeFormData = $('.upgradeForm form').serialize();
        $.post(
            'upgrade.php',
            upgradeFormData,
            function(data){
                var container = '#login';
                message(data, container);
            }
        )
    }
    
    function registerSubmit(){
        var regFormData = $('#register form').serialize();
        //disable all submit buttons
        $.post(
            'register.php',
            regFormData,
            function(data){
                var container = '#register';
                message(data, container);
                //update the interface here to tell user to checkout, upgrade later and/or verify account
                //update for errors too
            }
        );
    }
    
    //login
    function loginSubmit(){
        var loginData = $('#loginForm').serialize();
        //disable all submit buttons
        $.post(
            'login.php',
            loginData,
            function(data){
                var container = '#login';
                message(data, container);
                //TODO update interface to tell user they can logout or upgrade anytime
                //TODO update for errors and such too
            }
        )
    }
    
    $('.upgrade').click(function(){
        $('.upgradeForm').slideDown(1000);
        return false;
    });
    
    $('.cancelGold').click(function(){
        $('#cancelGold').slideDown(1000);
        return false;
    });
    
    $('.forgotPassword').click(function(){
        $('#forgotPassword').slideDown(1000);
        return false;
    });
    
    $('.goBack').click(function(){
        $(this).parent().slideUp(300);
        return false;
    });
    
    $('.logout').live('click', function(){
        $.post(
            'logout.php',
            function(data){
            }
        );
        $('#tab').trigger('click');
        $('.homeLink').trigger('click');
        return false;
    });
    
    //reset the login form after an error
    $('.resetForm').live('click', function(){
        $('.message').slideUp();
        $('.required').val('');
    });

    //login validation
    $('#newPassword').validate({
        submitHandler: inputPassword
    });
    
    $('#loginForm').validate({
        submitHandler: loginSubmit
    });
    
    $('#register form').validate({
        submitHandler: registerSubmit
    });
    
    $('.upgradeForm form').validate({
        submitHandler: upgradeMe
    });
    
    $('#forgotPassword form').validate({
        submitHandler: forgotPassword
    });
    
        //stuff to do on page load
    if (window.location.hash == '#videos' || window.location.hash == '#text-module'){
        openViewer();
    }

    if (window.location.hash == '#thumb-chooser' || window.location.hash == '#slideshow'){
        closeViewer();
    }
    
    $('#thumb-window, #thumb-window-gallery').serialScroll({
                items:'.thumb',
		prev:'.rSlider',
		next:'.lSlider',
                step:-1,
		offset:-20, //when scrolling to photo, stop 230 before reaching it (from the left)
		duration:500,
                lazy:true,
		stop:true,
		lock:true,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart' //use easing
	});
    
    $('.thumb[title]').live('mouseover', function(){
        $(this).qtip({
            style: { name: 'light', tip: true },
            hide: { when: 'mouseout', fixed: true }, //to mouseover the tooltip
            position: {
                corner: {
                   target: 'topMiddle',
                   tooltip: 'bottomLeft'
                }
            },
            style: { 
                width: 370,
                padding: 15,
                border: {
                   width: 7,
                   radius: 4
                },
                tip: { size: { x: 12, y: 18 },
                corner: 'bottomLeft'
                }
            }
        });
    });
    
     //TODO change this when the cookie is hashed
    $('.go').live('click', function(){
        $('#tab').trigger('click');
        $('.homeLink').trigger('click');
    });
        
    $('.video_link').live('click', function(){
        var videoclip = $(this).attr('id');
        loadRecipeText(videoclip);
        var videoclipPath = 'videos/' + videoclip + '.flv';
        var player = flowplayer('player', {src:'flash/flowplayer-3.2.2.swf', wmode:'opaque'});
        player.play(videoclipPath);
        return false;
    });
    
    $('.pause').click(function(){
        if ($('#player_api').length > 0){
            flowplayer().pause();
        }
        });
    
    $('#main-navigation').children().click(function(){
        if ($('#player_api').length > 0){
            flowplayer().pause();
        }
        });
    
    $('#cancelUpgrade').click(function(){
	$('.upgradeForm').slideUp();
	return false;
    })
    
    $('#gallery .thumb a').colorbox();
    
});


