/**
 * home - handle home, login and landing page related functionality 
 * 
 * @author: Chanel Munezero <chanel.munezero@escapemg.com>
 */
(function(){
    if(!window.gs) {
        return;
    }
    
    var home = window.gs.home = {
        // default options/settings
        searchTimeout: null,
        searchTimeoutWait: 300,
        
        empty: null
    };
    
    home.init = function()
    {
        // login input focus, blur
        $('form.login input').each(function() {
            var init = this.name.toUpperCase();
            $(this).focus(function() {
                $(this).parent().addClass('active');
                if(this.value==init) {
                    this.value = '';
                }
            }).blur(function() {
                $(this).parent().removeClass('active');
                if(this.value=='') {
                    this.value = init;
                }
            });
        });
        // login button toggle
        $("#loginClick").click(function(){
            debug('login button click', this);
            return false;
        });
        // initialize form handler
        //home.handleForm();
        $('#searchForm').submit(function(event) {
            // return false if same info
            if($('#searchInput').val()=='' || $('#searchInput').val()=="Your Band's Name") {
                return false;
            }
        });
    };

    home.loginClick = function()
    {
        var addClass, remClass, openHeight=125, closeHeight=5;
        if($('#loginWrapper').hasClass('closed')) {
            addClass = 'open';
            remClass = 'closed';
            $('#loginWrapper').css('height',closeHeight).addClass(addClass).removeClass(remClass).animate({height:openHeight});
        } else {
            addClass = 'closed';
            remClass = 'open';
            $('#loginWrapper').css('height',openHeight).animate({height:closeHeight}, function() {
                $(this).addClass(addClass).removeClass(remClass);
            });
        }
        return false;
    }
    home.showArtistNameTooltip = function() {
        debug('show tooltip');
        var tooltip = '#tooltip';
        if(!$(tooltip).is(':visible')) {
            $(tooltip).fadeIn('fast');
        }
        return false;
    }
    home.closeArtistNameTooltip = function() {
        debug('hide tooltip');
        var tooltip = '#tooltip';
        if($(tooltip).is(':visible')) {
            $(tooltip).fadeOut('fast');
        }
        return false;
    }
    
    home.doArtistSearch = function(obj) {
        debug('do artist search');
        clearTimeout(home.searchTimeout);
        home.searchTimeout = setTimeout(function() {
            debug('artist search timeout, do real search now');
            if(gs.search.lock) {
                gs.home.doArtistSearch(obj);
            }
            gs.search.doCatalogSearch(obj);
        }, home.searchTimeoutWait);
    }
    home.autocompleteItemClick = function(obj) {
        debug('autocomplete item click', obj);
        var li = $(obj).siblings('.hide').children('ul').children('li').get(0);
        debug('li id:', li, li.id);
        if($('#selected-'+li.id).length) {
            debug('curr obj already exists', li);
            return false;
        }
        var newObj = li.cloneNode(true);
        newObj.id = 'selected-'+li.id;
        $(newObj).find('input').attr('name', 'artists[]');
        $('#artistList').append(newObj).show();
        return false;
    }
    home.addArtistName = function(obj) {
        clearTimeout(home.searchTimeout);
        $('#autocomplete').hide();
        debug('add artist name button click', obj);
        var name = $(obj).siblings('.inputWrapper').children('.search').get(0);
        debug('input name', name);
        if(!name.value) {
            debug('no name value');
            return false;
        }
        if($('#selected-'+name.value).length) {
            debug('curr obj already exists', li);
            return false;
        }
        var li = $('#artistList li.hide').clone(true);
        li.removeClass('hide').attr('id','selected-'+name.value).find('span').html(name.value);
        li.find('input').attr('value', name.value).attr('name', 'artists[]');
        $('#artistList').append(li).show();

        return false;
    }

    $('.file').change(function() {
        debug($(this).attr('value'));
        $(this).siblings('.inputWrapper').children().val($(this).attr('value'));
    });

    // auto init
    home.init();        
})();

function sliderInterval() {
    // do sliders next
    var width, first;
    var numSliders = $('#slider ul').length;
    var sliders = $('#slider ul').each(function(i) {
        width = width || $(this).width();
        //debug('start width: ',width);
        first = first || this;
        $(this).animate({left: -width+'px'}, function() {
            $(this).css('left','0px'); 
            if(i==0) {
                $(this).parent().append($(first).remove()); 
            }
        });
    });
}
function testimonials() {
    // do testimonial block first
    var container = $('#testimonials');
    var first = $('li:visible', container);
    var next = $('li:hidden:first', container);
    //debug($('li:visible', container),$('li:hidden:first', container));
    $(container).append(first);
    $(first).fadeOut(function() {
        $(next).fadeIn();
    });
}
function timer_1() {
    // prevent unnecessary timeouts
    if(!$('#slider ul').length) { return; }

    sliderInterval()
    si_1 = setTimeout('timer_1()', wait_1);
}
function timer_2() {
    // prevent unnecessary timeouts
    if(!$('#testimonials').length) { return; }

    testimonials();
    si_2 = setTimeout('timer_2()', wait_2);
}

var wait_1 = 7000;
var si_1 = setTimeout('timer_1();', wait_1);
var wait_2 = 10000;
var si_2 = setTimeout('timer_2();', wait_2);
