$(document).ready(function(){ 
    $(".external").attr("target","_blank");
    //$('hr').replaceWith('<hr class="hr" />')
});

var changeTitleT;

// start infinite loop for title changing
function changeTitleStart(timeout){
    changeTitle(1);
    changeTitleT = setTimeout("changeTitleStart("+timeout+")", timeout);
}

function changeTitleStop(){
    clearTimeout(changeTitleT);
}

// changing titles on homepage
function changeTitle(shiftVal){
    var id = $('.hpTitles span.actual').attr('id');
    var titlesCnt = $('.hpTitles .title').length;
    
    if(!id){
        var title = $('.hpTitles #0');
        title.addClass('actual');
        $('.hpTitles h2').html(title.text());
        $('.hpTitles #readHow').attr('href', title.attr('rel'));
        return;
    }
    
    $('.hpTitles #'+id).removeClass('actual');
    $('.hpTitles h2,.hpTitles #readHow').fadeOut(700, function(){
        var newId = (parseInt(id)+parseInt(shiftVal)+titlesCnt)%titlesCnt;
        var title = $('.hpTitles #'+newId);
        $('.hpTitles h2').html(title.text());
        $('.hpTitles #readHow').attr('href', title.attr('rel'));
        title.addClass('actual');
        $('.hpTitles h2,.hpTitles #readHow').fadeIn(700);
    });
}

// changing images in gallery
function changeImage(obj, shiftVal, categoryId){
    var navigation = $(obj).parent().html();
    $('#'+categoryId+' .navigation img').unwrap();
    var actualPhotoId = parseInt($('#photoId-'+categoryId).text());
    var actualPhoto = $('#thumb-'+categoryId+'-'+actualPhotoId);
    var photosCnt = parseInt($('#photoCnt-'+categoryId).text());
    var newPhotoId = (actualPhotoId-1+parseInt(shiftVal)+photosCnt)%photosCnt+1;
    var timeout = 500;
    var photoWidth = actualPhoto.outerWidth(true);
    var categoryWidth = photoWidth*photosCnt;
    var thumbs = $('#'+categoryId+' .thumbs');
    
    thumbs.css('width', categoryWidth);
    
    if(shiftVal>0){
        if (Math.abs(parseInt(thumbs.css('margin-left')))+photoWidth == categoryWidth){
            photoWidth = (-1)*(categoryWidth-photoWidth);
        }
        photoWidth = photoWidth*(-1);
    }else{
        if(parseInt(thumbs.css('margin-left')) == 0){
            photoWidth = (-1)*(categoryWidth-photoWidth);
        }
    }
    thumbs.animate({marginLeft: '+='+photoWidth+'px'}, timeout, function(){
        $('#'+categoryId+' .navigation').html(navigation);
        $('#photoId-'+categoryId).html(newPhotoId);
    });
}

// change page in gallery
function changePage(obj, rowId){
    $('.pagination a').removeClass('active');
    $(obj).addClass('active');
    $('.row').hide(0, function(){
        $('#row'+rowId).show();
    });
}

// show and hide description of team member
function animateTeamDescription(categoryId, obj){
    var description = obj.parent().siblings('.description');
    
    if(obj.attr('class') == 'close'){
        description.hide();
        obj.removeClass().addClass('open');
    }else{
        description.show();
        obj.removeClass().addClass('close');
    }
    
    //var descriptions = $('#'+categoryId+' #'+rowId+' .description');
    
    //if($('#'+categoryId+' #'+rowId+' .close').attr('class')){
        //descriptions.hide();
        //$('#'+categoryId+' #'+rowId+' a.close').removeClass().addClass('open');
    //}else{
        //descriptions.show();
        //$('#'+categoryId+' #'+rowId+' a.open').removeClass().addClass('close');
    //}
}

function setHeight(cols){
    var max_height = 0;
    $.each(cols, function(ii, col){if(max_height < $(col).height()){max_height = $(col).height()}});
    $(cols).css('height', max_height);
}

// show and hide product description
function animateDescription(obj){
    var cols = $(obj).parents('tr').find('.category');
    var div = $(obj).parent().children('.description');
    var logo = $(obj).parents('div.product').children('img.logo');
    
    if($(obj).hasClass('close')){
        $(cols).css('height', '');
        div.hide();
        logo.show();
        $(obj).removeClass().addClass('open');

        if (location.hash){
            location.hash = "#all";
        }

    }else{
        $(cols).css('height', '');
        div.show();
        logo.hide();
        $(obj).removeClass().addClass('close');

        if (obj.rel){
            location.hash = '#'+obj.rel;
        }
    }
    
    setHeight(cols);
}

