/*
*----------Client login lightbox, validation and ajax call to load the requested client gallery
*/

(function($) {

    var that, portLink,
    initialize, bindEvents, validateCL, checkWidth, blurMe, menuUp, menuDown;
            
    /* Initilization function */        
    initialize = function () {
        bindEvents();
    };//end:function
            
    /* Bind Events here */
    bindEvents = function() {
        // bind to the #login button's click event
        $('#clientLoginLink').bind('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            
            var element = $('#clientLoginContainer');
            
            if($('html').hasClass('ipad')) {
                element = $('#clientLoginiPad');
            }
            
            $.colorbox({
                inline: true,
                href: element,
                opacity: '0.7',
                onComplete: function() {
                    $('#clientLoginContainer').find('#proofPassCode').focus();
                }
            });
        });
        
        $('#clientLoginContainer #cancel').bind('click', function(e) {
            $.colorbox.close();
            $('#proofPassCode').val('');
        });
        
        $('#clientLoginContainer #login').bind('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            $('#proofsLogin').submit();
        });
        
        // bind to the form's sumbit event
        $('#proofsLogin').bind('#login', function() {
            $(this).ajaxSubmit({
                url: '/clients-area/getProofs.php',
                dataType: 'json',
                beforeSubmit: validateCL,
                success: function(data) {
                    if(data.type == 'success') {
                        $('#proofPassCode').removeClass('loading').val('');
                        $('#pageTitle h1').html('clients<span>.area</span>');
                        $('#mainNav a.active').removeClass('active');
                        $('#portfolioNav a.active').removeClass('active');
    					$('#container').attr('class','clients');
                        $.colorbox.close();
                        $('#content-inner').html('<iframe src="http://www.alexbussa.com/clients-area/' + data.message + '/index.html" name="proofsDisplay" scrolling="no" style="width:960px;height:640px;border:0px;" frameborder="0"></iframe>');   
                    } else {
                        $('#proofPassCode').removeClass('loading')
                            .addClass('error');
                    }
                },
                complete: function(xhr, status) {
                    if(status == 'error') {
                        console.log('Completion Error');
                    }
                }
            });
            return false; // <-- important!
        });
        
        // bind to the window's load event
        $(window).bind('load', function() {
            checkWidth();
        });
        
        // bind to the window's resize event
        var ie7 = (document.all && !window.opera && window.XMLHttpRequest);
        if (!ie7) {
            $(window).bind('resize', function() {
                checkWidth();
            });
        }
    
        // bind to the document's mousedown event
        $(document).bind('mousedown', function() {
            blurMe();
        });
        
        // bind to the hover hover event of the portfolio a
        $('#portfolioNav a').bind('mouseover', function() {
            if ($(this).attr('class') != 'active') {
                portLink = $(this);
                menuUp(portLink);
            }
        });
        
        // bind to the hover hover event of the portfolio a
        $('#portfolioNav a').bind('mouseout', function() {
            if ($(this).attr('class') != 'active') {
                portLink = $(this);
                menuDown(portLink);
            }
        });
        
    };//end: function
            
    validateCL = function() {
        // start form validation set to valid
        var isFilled = true;
        
        // check to see if #proofPassCode field is empty
        if ($('#proofPassCode').val() == '') {
            $('#proofPassCode').addClass('error');
            isFilled = false;
        }
        else {
            $('#proofPassCode').removeClass('error')
                .addClass('loading');
        }
        
        return isFilled;
    };//end:function
    
    // check width of window and add class if less than 1280px
    checkWidth = function() {
        //if ($(window).width() <= 1280) {
        //    $('html').addClass('lt1280');
        //}
        //else {
        //    $('html').removeClass('lt1280');
        //}
    };//end:function
    
    // blur all clicked a and input[type=button]
    blurMe = function() {
        $('a').focus(function() {
            $(this).blur();
        });
        $('input[type=button]').focus(function() {
            $(this).blur();
        });
    };//end:function
    
    // animate portfolio menu a
    menuUp = function(portLink) {
        portLink.animate({
            paddingTop: '0px'
        }, 50, function() {}
    )};//end: function
    
    // animate portfolio menu a
    menuDown = function(portLink) {
    portLink.animate({
            paddingTop: '10px'
        }, 400, function() {}
    )};//end: function
    
    $(document).ready(function() {
        initialize();
    });

})(jQuery);
