$(document).ready(function(){
    
    // nascrolovanie na miniaturu vybraneho setu v zozname
    $('#scroll_down').click(function () {
        $('html, body').animate({scrollTop:$('.model-item.active').position().top});
    });
    
    $(".select").selectbox();
    setFade();
    setPager();
    
    $('a.list-item-click').click(function(){
        selectItem($(this));
        return false;
    });
    
    $('#modelTitle img').load(function () {
        $('#modelTitle').removeClass('ajaxPreLoadMin');
        $(this).fadeIn('slow');
    });
    
    $('#search_form').submit(function () {
        var model_id = $('#searchModel').val();
        if(model_id) {
            location.href = '/model/detail/id/'+model_id+'/';
            return false;
        }
        getModelList(1, null);
        return false;
    });
    
    $('#prev_model').click(function () {
        if($(this).attr('rel')) {
            selectItem($(".model-item[rel='"+$(this).attr('rel')+"'] a.list-item-click"));
        } else {
            if($('#pagerTop .prev').length){
                getModelList($('#pagerTop .prev').attr('rel'),'last');
            } else {
                report('Newest model does not exist','ERROR');
            }
        }
        return false;
    });
    $('#next_model').click(function () {
        if($(this).attr('rel')) {
            selectItem($(".model-item[rel='"+$(this).attr('rel')+"'] a.list-item-click"));
        } else {
            if($('#pagerTop .next').length){
                getModelList($('#pagerTop .next').attr('rel'),'first');
            } else {
                report('Next model does not exist','ERROR');
            }
        }
        return false;
    });
})

function setFade() {
    $("div.model-item[rel='"+$('#model_detail').attr('rel')+"'] img").fadeTo(0,0.5);
    $("div.model-item[rel='"+$('#model_detail').attr('rel')+"']").addClass('active');
    
    $('div.model-item img').hover(function () {
        $(this).fadeTo(150,0.5);
    },function () {
        if(!$(this).parent().parent().hasClass('active')){
            $(this).fadeTo(150,1);
        }
    });
    
    var isInList = false;
    
    var itemIds = $('div.model-item a').each(function () {
        if($(this).parent().hasClass('active')){
            var itemIds = $(this).attr('rel').split(':');
            var currId = itemIds[0]; //object.attr('rel');
            var prevId = itemIds[1];
            var nextId = itemIds[2];
            $('#next_model').attr('rel',nextId);
            $('#prev_model').attr('rel',prevId);
            isInList = true;
        }
    });
    
    if(!isInList){
        if($('div.model-item').length){
            var itemIds = $('div.model-item:eq(0) a').attr('rel').split(':');
            $('#next_model').attr('rel',itemIds[0]);
            $('#prev_model').attr('rel',itemIds[0]);
        }
    }
}

function selectItem(object) {
    $('div.model-item img').fadeTo(0,1).parent().parent().removeClass('active');
    object.parent().addClass('active');
    var itemIds = object.attr('rel').split(':');
    
    var currId = itemIds[0]; //object.attr('rel');
    var prevId = itemIds[1];
    var nextId = itemIds[2];
    
    $('#next_model').attr('rel',nextId);
    $('#prev_model').attr('rel',prevId);   
    $('html, body').animate({scrollTop:0});
    $("#" + object.parent().attr('id') + " img").fadeTo(0, 0.5);
    
    showModel(currId);
    
}

function showModel(modelId) {
    $('#modelTitle').addClass('ajaxPreLoadMin');
    $('#modelTitle img').fadeOut('slow');
    
    $.getJSON('/model/get-model-detail/id/' + modelId, function (json) {
        $('#model_name').html(json.Model.name);
        $('#model_nationality').html(json.Model.nationality);
        $('#model_height').html(json.Model.height);
        $('#model_weight').html(json.Model.weight);
        $('#model_brasize').html(json.Model.brasize);
        $('#model_hair_color').html(json.Model.hair_color);
        $('#model_photosets').html(json.Model.photosets.length ? json.Model.photosets.length : 'no sets');
        $('#model_films').html(json.Model.films.length ? json.Model.films.length : 'no movies');
        $('#model_profile').html(json.Model.profile);
        $('#model_title').attr('src',json.Model.title_photo).attr('alt',json.Model.name).attr('title',json.Model.name);
        
        $('#model_detail').attr('rel',json.Model.model_id).attr('href', '/model/detail/id/' + json.Model.model_id);
    });
}


function getModelList(page, chooseModel) {
    $('#listSection').fadeTo(150,0.2);
    
    if(chooseModel == 'first' || chooseModel == 'last'){
        $('#modelTitle').addClass('ajaxPreLoadMin');
        $('#modelTitle img').fadeOut('slow');
    }
    
    $.ajax({
         url: '/model/get-model-list?page=' + page
        ,type: "GET"
        ,data: getSearchData()
        ,success: function (ret) {
            
            $('#listSection').html(ret);
            setPager();
            $('#listSection').fadeTo(0,1);
            $('a.list-item-click').click(function(){
                selectItem($(this));
                return false;
            });
            setFade();
            
            if($(".model-item").length){
                
                $('#prev_page1').attr('rel',(parseInt(page,10) - parseInt(1,10)));
                $('#next_page1').attr('rel',(parseInt(page,10) + parseInt(1,10)));
                
                if(chooseModel == 'first'){
                    selectItem($(".model-item:first a.list-item-click"));
                }
                if(chooseModel == 'last'){
                    selectItem($(".model-item:last a.list-item-click"));
                }
            } else {
                $('#next_model').attr('rel','');
                $('#prev_model').attr('rel','');
                $('#model_name').html('');
                $('#model_nationality').html('');
                $('#model_height').html('');
                $('#model_weight').html('');
                $('#model_brasize').html('');
                $('#model_hair_color').html('');
                $('#model_photosets').html('');
                $('#model_films').html('');
                $('#model_profile').html('');
                $('#model_detail').attr('');
            }
        }
    });
}

function getSearchData() {
    var searchModel      = $('#searchModel').val();
    var searchHair       = $('#searchHair').val();
    var searchBrasize    = $('#searchBrasize').val();
    var searchExclusivity= $('#searchExclusivity').val();
    
    return "searchModel="+searchModel+"&searchHair="+searchHair+"&searchBrasize="+searchBrasize+"&searchExclusivity="+searchExclusivity;
}

function setPager() {
    $('#pagerTop').html($('#pagerBottom').html());
    
    $('#pagerTop .prev').click(function () {
        if($(this).attr('rel') > 0){
            getModelList($(this).attr('rel'));
        } else {
            report('Newest models does not exist','ERROR');
        }
        return false;
    });
    
    $('#pagerTop .next').click(function () {
        getModelList($(this).attr('rel'));
        return false;
    });
    
    $('#pagerBottom .prev').click(function () {
        if($(this).attr('rel') > 0){
            getModelList($(this).attr('rel'));
        } else {
            report('NNewest models does not exist','ERROR');
        }
        return false;
    });
    
    $('#pagerBottom .next').click(function () {
        getModelList($(this).attr('rel'));
        return false;
    });
}
