// ------------- functions for all the pages -------------

// set the footer bar to the bottom
function setFooterToBottom() {
    
    if($('body').height() < $(window).height()) {
        $("#footer").css("position","absolute");
    } else {
        $("#footer").css("position","relative");
    }
}

// mouseover thumb post
function mouseoverThumbPost() {
    $('.post_box img').mouseover(function() {
        $(this).stop(true,true).fadeTo('slow',0.5);
        $(this).stop(true,true).fadeTo('slow',1);    
    });
    $('input[name=submit].submit').mouseover(function(){
        $(this).stop(true,true).fadeTo('slow',0.5);
        $(this).stop(true,true).fadeTo('slow',1);     
    });
}
// on click thumb go To
function onClickThumb() {
    $('.post_box').click(function(){
        //alert($('a').parents($(this)).attr('href'));
    })
}

// if the textarea for the comment is empty show the label
function showLabelComment() {
    if($('textarea[name=comment]').val() != '') {
        $('#labelComment').hide();
    } else {
        $('#labelComment').show();
    }   
    
    $('textarea[name=comment]').keyup(function(){
        if($(this).val() != '') {
            $('#labelComment').hide();
        } else {
            $('#labelComment').show();
        }
    });
}


// ------------- functions for homepage -------------

// set the all 3 columns at the same height
function equalHeight(group) {
    var tallest = 0;
    var rows = Math.ceil($('.hc_column').size() / 3);
    $(group).each(function() {
        var thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    
    $(group).height(tallest);
    $('.home_content').height((tallest+110)*rows);
}

// check to see if the current page is home or a category
function check_class() {
    var current_class = '';
    $('.menu_top li').each(function(){
        if($(this).hasClass('current_page_item')) {
            current_class = 'current_page_item';    
        } else if ( $(this).hasClass('current-cat') ) {
            current_class = 'current-cat'; 
        }    
    });
    return current_class;
}

// check the category selected
function check_category() {
    var cat_id = $('#cat_id').val();
    if($('.cat-item-'+cat_id+'').hasClass('current-cat') == false ) {
        $('.cat-item-'+cat_id+'').addClass('current-cat');
    }
}

// init the pink bar to the current page for the first refresh
function iniPinkBarMenu() {
    //set the width of the current page pink bar
    var init_firstli = 0;
    
    current_class = check_class();
    
    if($('.menu_top li:first').hasClass('current_page_item') || current_class == '') {
        init_firstli = -5;
    }
    
    var init_width = $('.menu_top li.'+current_class+' a').width()-init_firstli+30;
    var init_left = $('.menu_top li.'+current_class+' a').position().left+init_firstli-15;
    $(".menu_pink_bar").css({'left':init_left, 'width':init_width});      
}

// move the pink bar on mouseover
function mouseoverMenu() {
    $(".menu_top li a").mouseover(function() {
        var init_firstli = 0;
        
        if($('.menu_top li:first').hasClass('current_page_item') || $(this).parents('.menu_top li').index('li') == 0 || current_class == '') {
            init_firstli = -5;
        }
        var init_width = $(this).width()-init_firstli+30;
        var next_left = $(this).position().left+init_firstli-15;  
        $(".menu_pink_bar").stop(true,true).animate({left:next_left, width:init_width},500);   
    });    
}

// get the pink bar back to the current page on mouseout
function iniPinkBarMenuOut() {
    var init_firstli = 0;
    current_class = check_class();
    
    if($('.menu_top li:first').hasClass('current_page_item') || current_class == '') {
        init_firstli = -5;
    }

    var init_width = $('.menu_top li.'+current_class+' a').width()-init_firstli+30;
    var init_left = $('.menu_top li.'+current_class+' a').position().left+init_firstli-15;
    $(".menu_pink_bar").animate({left:init_left, width:init_width},500);      
}
function mouseoutMenu() {
    $(".menu_top").mouseleave(function(){
        iniPinkBarMenuOut();        
    });
}

// FOOTER BAR: set the color of the bottom bar && set the width of the middle widget
function setColorMWFB() {
    total_column = $('.hc_column').size();
    total_rows = Math.ceil(total_column / 3);
    for(i=0; i<total_column; i++) {
        // first column
        if(i%3==0) {
            $('.hc_bottom_bar:eq('+i+')').addClass('hcbb_color4');        
        }
        // second column
        if(i%3==1) {
            $('.hc_bottom_bar:eq('+i+')').addClass('hcbb_body_color');  
            $('.hc_column:eq('+i+')').addClass('hcc_big');  
        }
        // third column 
        if(i%3==2) {
            $('.hc_bottom_bar:eq('+i+')').addClass('hcbb_color5');     
        }
    }
}

// set light box for pictures that are in the content
function setLightBox() {
    $(".post_content a").lightBox({
        imageLoading: 'wp-content/themes/fashionable/images/lightbox-ico-loading.gif',
        imageBtnClose: 'wp-content/themes/fashionable/images/lightbox-btn-close.gif',
        imageBtnPrev: 'wp-content/themes/fashionable/images/lightbox-btn-prev.gif',
        imageBtnNext: 'wp-content/themes/fashionable/images/lightbox-btn-next.gif'
    }); 
}

// set the number of columns on the single page
function setLayout() {
    var numColumns = $('#layout').val();
    if(numColumns == 1) {
        $('.first_column').css({'width':'944px'});
        $('.first_column').fadeIn(500);
    } else if(numColumns == 2) {
        $('.first_column').css({'width':'693px'});
        $('.first_column').fadeIn(500);
        $('.second_column').fadeIn(500);
    } else {
        $('.first_column').fadeIn(500);
        $('.second_column').fadeIn(500);    
        $('.third_column').fadeIn(500);    
    }
}

// ------------- ON LOAD -------------
$(document).ready(function(){
    
    //setFooterToBottom();
    mouseoverThumbPost();
    onClickThumb();
    
    // menu mouseover
    $(window).bind('load',function(){
        check_category();
        iniPinkBarMenu();
        mouseoverMenu();  
        mouseoutMenu();         
    });
    setColorMWFB();
    equalHeight('.hc_column');
    
    showLabelComment();
    
    setLightBox();
    
    setLayout();
     
});