function doEnter()
{
    this.set('morph', {
        duration:150
    });
    this.morph({
        'opacity': 1
    });
}

function doLeave()
{
    this.set('morph', {
        duration:500
    });
    this.morph({
        'opacity': 0.6
    });
}

function checkSize() {

    var size = document.getSize();

    if ($('gallery')) {
        var nx, ny, ndelta;

        var gsx = $('gallery_smalls').getParent().getParent().offsetHeight;

        ny = size.y-gsx-187;

        

        nx = size.x > 1100 ? 1100 : (size.x < 1000 ? 1000 : size.x) ;

        ndelta = 1100

        $('main').setStyle('height', size.y-41);

        //$('gallery_smalls').setStyle('padding-top', ny+25);


        
        
        $('gal_next').setStyle('margin-left', nx-40);
        $('gal_next').setStyle('margin-top', (ny/2)-(62/2));
        $('gal_prev').setStyle('margin-top', (ny/2)-(62/2));
        $('gal_prev').setStyle('margin-left', '10px');
        
        $('photos').getElements('li').each(function(el)
        {
            el.setStyle('width', nx);
            el.setStyle('height', ny);

            el.getElement('td').setStyle('height', ny);
            el.getElement('td').setStyle('width', nx-100);
            el.getElement('table').setStyle('width', nx);
            el.getElement('table').setStyle('height', ny);
            el.getElement('img').setStyle('height', ny);

            el.getElement('img').setStyle('max-height', el.getElement('img').getProperty('height')+'px');

            el.getElement('img').setStyle('max-width', el.getElement('img').getProperty('width')+'px');
            el.getElement('img').removeProperty('width');
            el.getElement('img').removeProperty('height');

            
        });

        if (gal_items) {
        var w = gal_items[cur_item].getStyle('width');
        gal_c.setStyles({'margin-left': '-'+parseInt(w)*cur_item+'px'});
        }
    }
    
    if ($('wide_content')) {
        if (size.x < 1100) {
            $('wide_content').addClass('narrow');
        } else {
            $('wide_content').removeClass('narrow');
        }
    }
    
    var size = document.getSize();

    

//    $('contacts').setStyle('height', size.y);
//    $('contacts').getElement('table').setStyle('height', size.y);
        
        
}

var cur_item = 0;
var gal_items;
var gal_c;
var last_item;
var gal_smals;

function galNext()
{
    if (cur_item == gal_items.length-1)
    {
            return;
    }
    last_item = cur_item;
    cur_item++;
    
    if (cur_item == gal_items.length-1) {
        $('gal_next').morph({'display': 'none'});
    }
    updateGal();
}


var currentAnchor;

function checkAnchor(){
    if(currentAnchor != document.location.hash){
        currentAnchor = document.location.hash;
        var index = currentAnchor.substring(1);
        if (index == 0) {
            index = 1;
        }
        doClickImg(index-1);
    }
}

function updateGal(){

    $('gal_prev').setAttribute('href', '#'+(cur_item-1+1));
    $('gal_next').setAttribute('href', '#'+(cur_item+1+1));

    if (cur_item > 0) {
        $('gal_prev').morph({'display': 'block'});
    }

    if (cur_item < gal_items.length-1) {
        $('gal_next').morph({'display': 'block'});
    }

    var w = gal_items[cur_item].getStyle('width');
    gal_c.morph({'margin-left': '-'+parseInt(w)*cur_item+'px'});
    gal_items[cur_item].morph({'opacity':1})
    
    gal_items[last_item].morph({'opacity':0.5});


    gal_smals[last_item].morph({'opacity': '0.6'});
    gal_smals[last_item].addEvent('mouseenter', doEnterImg);
    gal_smals[last_item].addEvent('mouseleave', doLeaveImg);
    //gal_smals[last_item].addEvent('click', doClickImg);

    gal_smals[cur_item].morph({'opacity': '1'});
    gal_smals[cur_item].removeEvent('mouseenter', doEnterImg);
    gal_smals[cur_item].removeEvent('mouseleave', doLeaveImg);
    //gal_smals[cur_item].removeEvent('click', doClickImg);
}

function galPrev()
{
    if (cur_item == 0)
    {
            return;
    }
    last_item = cur_item;
    cur_item--;
    
    if (cur_item == 0) {
        $('gal_prev').setStyles({'display': 'none'});
    } 
    updateGal();
}

function doEnterImg()
{
    if (cur_item == this.ind) return
    this.set('morph', {
        duration:150
    });
    this.morph({
        'opacity': 1
    });

}

function doLeaveImg()
{
    if (cur_item == this.ind) return
    this.set('morph', {
        duration:500
    });
    this.morph({
        'opacity': 0.6
    });
}

function doClickImg(ind)
{
    if (cur_item == ind) return;
    last_item = cur_item;
    cur_item = ind;
    $('gal_prev').morph({'display': 'none'});
    $('gal_next').morph({'display': 'none'});
    updateGal();
}

function workWithGal()
{

    cur_item = 0;
    last_item = -1;

    setInterval("checkAnchor()", 100);

    gal_smals = $$('#gallery_smalls li img') ;
    $$('#gallery_smalls li img').each(function(el, i){
        el.ind = i;
        el.getParent().setAttribute('href', '#'+(i+1));
        el.setStyle('opacity', '0.6');
        el.addEvent('mouseenter', doEnterImg);
        el.addEvent('mouseleave', doLeaveImg);
        //el.addEvent('click', doClickImg);
    }); 


    $('gal_prev').morph({'display': 'none'});
    $('gal_next').setStyle('opacity', 0.5);
    $('gal_prev').setStyle('opacity', 0.5);

    $('gal_next').addEvent('mouseenter', function(){
        this.morph({'opacity':1});
    });
    $('gal_next').addEvent('mouseleave', function(){
        this.morph({'opacity':0.5});
    });

    $('gal_prev').addEvent('mouseenter', function(){
        this.morph({'opacity':1});
    });
    $('gal_prev').addEvent('mouseleave', function(){
        this.morph({'opacity':0.5});
    });

    gal_items = $('gallery').getElements('li');

    gal_items.each(function(el){
       el.setStyle('opacity', 0.5)
    });

    gal_items[0].setStyles({'opacity':1})

    gal_c = $('gallery').getElements('ul');
        gal_smals[cur_item].morph({'opacity': '1'});
    gal_smals[cur_item].removeEvent('mouseenter');
    gal_smals[cur_item].removeEvent('mouseleave');
    gal_smals[cur_item].removeEvent('click');

    updateGal();
}

window.addEvent('domready', function() {

    
    
    window.addEvent('resize', function(){
         
        checkSize();
    });


    checkSize();

    if ($('gallery')) {
         workWithGal();
    }

        

    $$('#works li img').each(function(el){        
        el.setStyle('opacity', '0.6');        
        el.addEvent('mouseenter', doEnter);        
        el.addEvent('mouseleave', doLeave);        
    });        
    
    var mySmoothScroll = new Fx.SmoothScroll({
        links: '.smooth',
        wheelStops: false
    });

});

function hideContacts() {
    
    $('contacts').morph({
        'opacity': 0
    });
}

function showContacts() {
    
    $('contacts').setStyles({
        opacity:'0', 
        display:'block'
    });
    
    checkSize();
   
    $('contacts').morph({
        'opacity': 1
    });
    
}
