$(document).ready(function(){
	var sendingMail = false;
    //var scrollOn = true;
    //$.address.change(function(){ scrollOn=true; });
    //$(window).scroll(function(){
        //if(scrollOn){
            //if($(window).scrollTop() == $(document).height() - $(window).height()){
                //rows = Math.ceil($('ul#works li').size()/4);
                //$('img#loading').toggle();
                //setTimeout(function(){
                    //loadMore();
                    //$('img#loading').toggle();
                //}, 1000); 
            //}
        //}
    //});
    $('.wrapper').live('click', function(){
        $('#searchform:visible').toggle();
        $('#loginform:visible').toggle();
    });
    $('ul#works li').live('mouseover', function(){
        $('.play', this).fadeIn(0);
        $('.content', this).animate({backgroundColor: '#1d1d1e'}, 0);
    });
    $('ul#works li').live('mouseleave', function(){
        $('.play', this).fadeOut(0);
        $('.content', this).animate({backgroundColor: 'transparent'}, 0);
    });
    $('.hoverable').live('mouseover', function(){
        $('.tags', this).fadeIn(0);
        if($('.t1').length != 0 || $('.t2').length != 0)
            $('.play', this).fadeIn(0);
        $('.content', this).fadeIn(0);
    });
    $('.hoverable').live('mouseleave', function(){
        if($('.t1').length != 0 || $('.t2').length != 0){
            $('.content', this).fadeOut(0);
            $('.tags', this).fadeOut(0);
            $('.play', this).fadeOut(0);
        } 
    });
    $('span#info').live('click', function(){
        $('#share-block:visible').toggle();
        $('#info-block').toggle();
        videoOpacity();
    });
    $('span#heart').live('click', function(){
        hideHeart();
    });
    $('#share span').live('mouseover', function(){
        id = $(this).attr('id');
        $('#roll-'+id).toggle();
    });
    $('#share span').live('mouseleave', function(){
        id = $(this).attr('id');
        $('#roll-'+id).toggle();
    });
    $('.info-close').live('click', function(evt){
        $('#info-block:visible').hide();
        $('#share-block:visible').hide();
    });
    //function loadMore(){
        //rows = Math.ceil($('ul#works li').size()/4);
        //height = $('.expandable').height();
        //newSize = calculateHeight(rows);
        //$('.expandable').css('height', newSize+'px');
        ////check again to see if we must change the button
        //newSize = calculateHeight(rows);
        //if(newSize == height){
            //$('#loadmore').toggle();
            //$('#backtotop').toggle();
            //scrollOn = false;
        //}
    //}
    //function calculateHeight(){
        //rows = Math.ceil($('ul#works li').size()/4);
        //height = $('.expandable').height();
        //fullHeight = 195*rows;
        //toShow = fullHeight - height;
        //toShow = (toShow > (195*3)) ? (195*3) : toShow;
        //return height + toShow;
    //}
    //$('#loadmore').live('click', function(){
        //rows = $(this).attr('rel');
        //loadMore(rows);
    //});
    //$('#backtotop').live('click', function(){
        //$(window).scrollTop(0);
    //});
    $('#share-block form').live('submit', function(evt){
        evt.preventDefault();
        if(!sendingMail){
            sendingMail = true;
            setTimeout('hideHeart();', 3000);
            $('#submit-heart').val('Sending...');
            $.ajax({
                url: $(this).attr('action'),
                type: 'POST',
                data: ({
                    to: $('input#to').val(),
                    email: $('input#from').val(),
                    body: $('input#body').val(),
                    subject: $('input#subject').val()
                }),
                success: function(data){
                    sendingMail = false;
                    $('#submit-heart').val('Send');
                    $('#mail-status').text(data);
                    $('input#to').val('');
                    $('input#from').val('');

                }
            });
        }
    });
    $('.t2').livequery(function(){ $('#footer').removeClass('border'); }, function(){});
    $('.t1').livequery(function(){ $('#footer').removeClass('border'); }, function(){});
    $('#loadmore').livequery(function(){ if($('ul#works li').size() <= 12) $(this).toggle(); }, function(){});

    $('#top-controls span#login').live('click', function(){
        $('#searchform:visible').toggle();
        $('#loginform').toggle();
    });
    $('#top-controls span#search').live('click', function(){
        $('#loginform:visible').toggle();
        $('#searchform').toggle();
    });
    $('#searchform form').live('submit', function(evt){
        evt.preventDefault();
        request = $('input', this).val();
        $('input', this).val('searching...');
        $('input', this).attr('disable', true);
        $.ajax({
            url: 'tags.php',
            type: 'POST',
            data: ({ q: request }),
            success: function(data){
                window.location = '#search/'+data;
            }
        });
        $('input', this).val('');
        $('input', this).attr('disable', false);
    });
    //Gallery
    $('#controller span').live('click', function(){
        id = $('img', this).attr('id');
        sactive = $('#controller span.active');
        sactive.toggleClass('active');
        $(this).toggleClass('active');
        active = $('#gallery img.active');
        active.fadeOut('fast').toggleClass('active');
        $('#gallery img#image-'+id).fadeIn('fast').toggleClass('active');
    });
    var scrollCounts = 1000;
    var scrollSet = false;
    var scrolls = 0;
    $('#buttons #btn-next').live('click', function(){
        if(!scrollSet){
            scrollCounts = $('#count-scrolls').text(); 
            scrollSet = true;
        }
        if(scrolls < scrollCounts){
            $('.container').css('width', ($('.container').children('span').length * $('.container').children('span').width())+'px');
            $('#controller .viewport').scrollTo('+=782px', 300, {axis:'x'});
            scrolls++;
        }
    });
    $('#buttons #btn-prev').live('click', function(){
        if(!scrollSet){
            scrollCounts = $('#count-scrolls').text(); 
            scrollSet = true;
        }
        if(scrolls > 0 ){
            $('.container').css('width', ($('.container').children('span').length * $('.container').children('span').width())+'px');
            $('#controller .viewport').scrollTo('-=782px', 300, {axis:'x'});
            scrolls--;
        }
    });
    $('.default-value').each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if(this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if(this.value == '') {
                this.value = default_value;
            }
        });
    });
    
});
function hideHeart(){
    $('#info-block:visible').toggle();
    $('#share-block').toggle();
    videoOpacity();
}
function videoOpacity(){
//    if($('#info-block').is(':visible') || $('#share-block').is(':visible')){
//        $('#video iframe').hide();
//        $('#video img').show()
//    }
//    else{
//        $('#video iframe').show();
//        $('#video img').hide()
//    }
}

