var login_v = false;
var login_name = '';
function add_work(){
    var req = new Request.HTML({
        method: 'post',
        url: 'process.php',
        data: 'in_link=' + document.getElementById('in_link').value + '&in_cat=' + document.getElementById('in_cat').value + '&in_alt=' + document.getElementById('in_alt').value + '&in_pre=' + document.getElementById('in_pre').value + '&in_photo=' + document.getElementById('in_photo').checked + '&subaddW=1',
        onComplete: function(response){
            if (response[0].data == true) {
                alert('success');
                $('abs').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
                $('overlay').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
                document.getElementById('in_link').value = '';
                document.getElementById('in_cat').value = '';
                document.getElementById('in_alt').value = '';
                document.getElementById('in_pre').value = '';
                document.getElementById('in_photo').checked = 'checked';
                ref_work();
            }
        }
    }).send('in_link=' + document.getElementById('in_link').value + '&in_cat=' + document.getElementById('in_cat').value + '&in_alt=' + document.getElementById('in_alt').value + '&in_pre=' + document.getElementById('in_pre').value + '&in_photo=' + document.getElementById('in_photo').checked + '&subaddW=1');
    return false;
}

function ref_work(){
    var req = new Request.HTML({
        method: 'post',
        url: 'ref_works.php',
        data: '',
        onComplete: function(response){
            document.getElementById('portfolio-left').innerHTML = response[0].parentNode.innerHTML;
            var fileref = document.createElement('script');
            fileref.setAttribute("type", "text/javascript");
            fileref.setAttribute("src", "ref_nav.js");
            document.getElementsByTagName("head")[0].appendChild(fileref)
            var fileref = document.createElement('script');
            fileref.setAttribute("type", "text/javascript");
            fileref.setAttribute("src", "slimbox.js");
            document.getElementsByTagName("head")[0].appendChild(fileref)
        }
    }).send();
}

function login(){
    var req = new Request.HTML({
        method: 'post',
        url: 'process.php',
        data: 'resu=' + document.getElementById('in_user').value + '&wordpass=' + document.getElementById('in_pw').value + '&sublogin=1',
        onComplete: function(response){
            if (response[0].data == 0) {
                alert('success');
                $('abs').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
                $('overlay').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
                login_name = document.getElementById('in_user').value;
                document.getElementById('in_user').value = '';
                document.getElementById('in_pw').value = '';
                login_v = true;
            }
        }
    }).send('resu=' + document.getElementById('in_user').value + '&wordpass=' + document.getElementById('in_pw').value + '&sublogin=1');
    return false;
}

function logged(){
    if (login_name != '') {
        var req = new Request.HTML({
            method: 'post',
            url: 'process.php',
            data: 'resu=' + login_name + '&sublogged=1',
            onComplete: function(response){
                if (response[0].data == 0) {
                    return true;
                }
            }
        }).send('resu=' + login_name + '&sublogged=1');
    }
    else {
        return false;
    }
}

window.addEvent('domready', function(){
    var menu_start = false;
    new Asset.images(pre_images, {
        onProgress: function(c, i){
            this.setProperty('id', pre_ids[i]);
            this.inject('galStore');
        },
        onComplete: function(){
        }
    })
    
    var myFx = new Fx.Morph('galImage', {
        link: 'chain'
    }, {
        duration: 150,
        transition: Fx.Transitions.Sine.easeOut
    });
    function showPic(name, id){
        myFx.cancel();
        myFx.start({
            'opacity': 0
        }).chain(function(){
            if (name.getElement('img')) {
                name.getElement('img').inject('galStore');
            }
            document.getElementById(id).inject(name);
            myFx.start({
                'opacity': 1
            });
        });
    }
    // Adds the ability to fade for all navigation links
    $$('#nav img').addEvents({
        mouseenter: function(){
            this.tween('opacity', .5);
        },
        mouseleave: function(){
            this.tween('opacity', 1);
        }
    });
    $$('#menu_start').addEvents({
        mouseenter: function(e){
            if (!menu_start) {
                e.stop();
                var morphColor = this.getStyle('color');
                this.set('morph', {
                    duration: 200
                });
                this.morph({
                    'background-color': morphColor
                });
            }
        },
        mouseleave: function(e){
            if (!menu_start) {
                e.stop();
                this.set('morph', {
                    duration: 'long'
                });
                this.morph({
                    'background-color': '#fefefe'
                });
            }
        },
        'click': function(e){
            e.stop();
            if (!login_v) {
                $('abs').morph({
                    'visibility': 'visible',
                    'opacity': 1
                });
                $('overlay').morph({
                    'visibility': 'visible',
                    'opacity': 0.8
                });
                $('intro').morph({
                    'display': 'none'
                });
                $('login').morph({
                    'display': 'block'
                });
            }
            else {
                $$('#menu').set('morph', {
                    duration: 350
                });
                if (menu_start) {
                    menu_start = false;
                    $$('#menu').morph({
                        'visibility': 'hidden',
                        'opacity': 0
                    });
                }
                else {
                    menu_start = true;
                    $$('#menu').morph({
                        'visibility': 'visibile',
                        'opacity': 1
                    });
                }
                
            }
        }
    });
    $('add_p').addEvents({
        'click': function(e){
            e.stop();
            $('abs').morph({
                'visibility': 'visible',
                'opacity': 1
            });
            $('overlay').morph({
                'visibility': 'visible',
                'opacity': 0.8
            }); 
			$('intro').morph({
                    'display': 'block'
                });
                $('login').morph({
                    'display': 'none'
                });
        }
    });
    $('middle').addEvents({
        'click': function(e){
            if (e.target == this) {
                e.stop();
                $('abs').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
                $('overlay').morph({
                    'visibility': 'hidden',
                    'opacity': 0
                });
				$('intro').morph({
                    'display': 'none'
                });
                $('login').morph({
                    'display': 'none'
                });
            }
        }
    });
    $$('.pic-div').addEvents({
        mouseenter: function(e){
            e.stop();
            var morphColor = this.getStyle('color');
            this.set('morph', {
                duration: 200
            });
            this.morph({
                'background-color': morphColor,
                'border-color': morphColor
            });
            var source = this.getProperty('rel');
            if (!($('galImage').getElementById(source))) 
                showPic($('galImage'), source);
        },
        mouseleave: function(e){
            e.stop();
            this.set('morph', {
                duration: 'long'
            });
            this.morph({
                'background-color': '#ffffff',
                'border-color': '#aaaaaa'
            });
        }
    });
    
    //creating the slide effect for the about me content
    var aboutMeFx = new Fx.Slide('aboutMe-page', {
        mode: 'horizontal',
        onComplete: function(){
            //attempting to hide other contents' wrappers when the current content slides out.
            if (arguments[0].getStyle('margin-left') < '0px') {
                $('aboutMe-wrapper').setStyle('display', 'none');
            }
        }
    }).hide();
    
    //creating the slide effect for the portfolio content
    var portFx = new Fx.Slide('portfolio-page', {
        mode: 'horizontal',
        onComplete: function(){
            //attempting to hide other contents' wrappers when the current content slides out.
            if (arguments[0].getStyle('margin-left') < '0px') {
                $('portfolio-wrapper').setStyle('display', 'none');
            }
        }
    }).hide();
    
    //creating the slide effect for the links content
    var linkFx = new Fx.Slide('links-page', {
        mode: 'horizontal',
        onComplete: function(){
            //attempting to hide other contents' wrappers when the current content slides out.
            if (arguments[0].getStyle('margin-left') < '0px') {
                $('links-wrapper').setStyle('display', 'none');
            }
        }
    }).hide();
    
    //creating the slide effect for the links content
    var contFx = new Fx.Slide('contact-page', {
        mode: 'horizontal',
        onComplete: function(){
            //attempting to hide other contents' wrappers when the current content slides out.
            if (arguments[0].getStyle('margin-left') < '0px') {
                $('contact-wrapper').setStyle('display', 'none');
            }
        }
    }).hide();
    
    //adding events for when the user clicks About Me. Events include sliding the 
    $('aboutMe-link').addEvent('click', function(e){
        e.stop();
        $('aboutMe-wrapper').setStyle('display', 'inline');
        $('portfolio-wrapper').setStyle('display', 'inline');
        $('links-wrapper').setStyle('display', 'inline');
        $('contact-wrapper').setStyle('display', 'inline');
        $('aboutMe-wrapper').setStyle('z-index', 1);
        $('portfolio-wrapper').setStyle('z-index', 0);
        $('links-wrapper').setStyle('z-index', 0);
        $('contact-wrapper').setStyle('z-index', 0);
        $('name').fade('out');
        $$('.name2').morph({
            opacity: 1
        });
        portFx.slideOut();
        aboutMeFx.slideIn();
        linkFx.slideOut();
        contFx.slideOut();
        $('blue-line').morph({
            left: 597
        });
        $('brown-line').morph({
            left: 15
        });
        $('red-line').morph({
            left: 10
        });
        $('orange-line').morph({
            left: 5
        });
        $('yellow-line').morph({
            left: 0
        });
    });
    $('portfolio-link').addEvent('click', function(e){
        e.stop();
        $('aboutMe-wrapper').setStyle('display', 'inline');
        $('portfolio-wrapper').setStyle('display', 'inline');
        $('links-wrapper').setStyle('display', 'inline');
        $('contact-wrapper').setStyle('display', 'inline');
        $('aboutMe-wrapper').setStyle('z-index', 0);
        $('portfolio-wrapper').setStyle('z-index', 1);
        $('links-wrapper').setStyle('z-index', 0);
        $('contact-wrapper').setStyle('z-index', 0);
        $('name').fade('out');
        $$('.name2').morph({
            opacity: 1
        });
        aboutMeFx.slideOut();
        portFx.slideIn();
        linkFx.slideOut();
        contFx.slideOut();
        $('blue-line').morph({
            left: 597
        });
        $('brown-line').morph({
            left: 592
        });
        $('red-line').morph({
            left: 10
        });
        $('orange-line').morph({
            left: 5
        });
        $('yellow-line').morph({
            left: 0
        });
    });
    $('links-link').addEvent('click', function(e){
        e.stop();
        $('aboutMe-wrapper').setStyle('display', 'inline');
        $('portfolio-wrapper').setStyle('display', 'inline');
        $('links-wrapper').setStyle('display', 'inline');
        $('contact-wrapper').setStyle('display', 'inline');
        $('aboutMe-wrapper').setStyle('z-index', 0);
        $('portfolio-wrapper').setStyle('z-index', 0);
        $('links-wrapper').setStyle('z-index', 1);
        $('contact-wrapper').setStyle('z-index', 0);
        $('name').fade('out');
        $$('.name2').morph({
            opacity: 1
        });
        aboutMeFx.slideOut();
        portFx.slideOut();
        linkFx.slideIn();
        contFx.slideOut();
        $('blue-line').morph({
            left: 597
        });
        $('brown-line').morph({
            left: 592
        });
        $('red-line').morph({
            left: 587
        });
        $('orange-line').morph({
            left: 582
        });
        $('yellow-line').morph({
            left: 0
        });
    });
    $('contact-link').addEvent('click', function(e){
        e.stop();
        $('aboutMe-wrapper').setStyle('display', 'inline');
        $('portfolio-wrapper').setStyle('display', 'inline');
        $('links-wrapper').setStyle('display', 'inline');
        $('contact-wrapper').setStyle('display', 'inline');
        $('aboutMe-wrapper').setStyle('z-index', 0);
        $('portfolio-wrapper').setStyle('z-index', 0);
        $('links-wrapper').setStyle('z-index', 0);
        $('contact-wrapper').setStyle('z-index', 1);
        $('name').fade('out');
        $$('.name2').morph({
            opacity: 1
        });
        aboutMeFx.slideOut();
        portFx.slideOut();
        linkFx.slideOut();
        contFx.slideIn();
        $('blue-line').morph({
            left: 597
        });
        $('brown-line').morph({
            left: 592
        });
        $('red-line').morph({
            left: 587
        });
        $('orange-line').morph({
            left: 582
        });
        $('yellow-line').morph({
            left: 577
        });
    });
    $('name-link').addEvent('click', function(e){
        e.stop();
        $('aboutMe-wrapper').setStyle('display', 'inline');
        $('portfolio-wrapper').setStyle('display', 'inline');
        $('links-wrapper').setStyle('display', 'inline');
        $('contact-wrapper').setStyle('display', 'inline');
        $('aboutMe-wrapper').setStyle('z-index', 0);
        $('portfolio-wrapper').setStyle('z-index', 0);
        $('links-wrapper').setStyle('z-index', 0);
        $('contact-wrapper').setStyle('z-index', 0);
        $('name').fade('in');
        $$('.name2').morph({
            opacity: 0
        });
        aboutMeFx.slideOut();
        portFx.slideOut();
        linkFx.slideOut();
        contFx.slideOut();
        $('blue-line').morph({
            left: 20
        });
        $('brown-line').morph({
            left: 15
        });
        $('red-line').morph({
            left: 10
        });
        $('orange-line').morph({
            left: 5
        });
        $('yellow-line').morph({
            left: 0
        });
    });
});
