$(document).ready(function(){

    /* Admin search box */
    if($('.search-box').length){
        if($('.search-box input#query').val() == ''){
            $('.search-box').hide();
        }
    }

    $('.actions a.admin-search').click(function(){
        $('.search-box').toggle();
        return false;
    });

    /* Popup */
    $('a.pop').click(function(){
        window.open($(this).attr('href'), 'PagePop');

        return false;
    });

    /* Oval Button */
    $('a.ovalbutton, a.ovalbutton-blue, a.ovalbutton-green, a.ovalbutton-orange, a.ovalbutton-red').wrapInner('<span></span');

    /* Sortable */
    $(".sortable tbody").sortable({ handle: 'tr' , items: 'tr'});

    /* Flash message sort hiding after n seconds */
    if($('#flashMessageSort').length){
        $('#flashMessageSort').animate({opacity: 1.0}, 2000).hide('fast');
    }

    /* Product Image */
    $('.product-images .thumb-list a').click(function(){
        var productImage = $('.product-images .thumb img');
        var productImageLink = $('.product-images .thumb a');
        var src = $(this).attr('href');
        var filename = '/files/images/max/' + $(this).children('img').attr('alt');

        productImage.attr('src', src);
        productImageLink.attr('href', filename);

        return false;
    });

    /* States ajax request */
    /* Only preload for add (profile id not exists) */
    if($('#ProfileId').length == 0 && $('#ProfileCountryId').length){
        // Initial Load
        $('#ProfileCountryId').ready(function(){

            // Showing please wait on state select
            $('#ProfileStateId').empty().attr('disabled', 'disabled');
            var newOption = '<option value="0">Please Wait...</option>';
            $('#ProfileStateId').append(newOption);

            // Get the country id
            var countryId = $('#ProfileCountryId option:selected').val();

            // Ajax request to get states list
            $.ajax({
                type: "GET",
                dataType: 'json',
                url: '/states/getlist/' + countryId,
                success: function(data){
                    $('#ProfileStateId').empty();

                    $.each(data, function(index, item){
                        var newOption = '<option value="'+item.State.id+'">'+item.State.name+'</option>';
                        $('#ProfileStateId').append(newOption);
                    })

                    $('#ProfileStateId').removeAttr('disabled');
                },
                error: function(){
                    $('#ProfileStateId').empty();
                    var newOption = '<option value="0">No State Available</option>';
                    $('#ProfileStateId').append(newOption);
                }
            });
        });
    }

    /* Load state on change */
    if($('#ProfileCountryId').length){
        $('#ProfileCountryId').change(function(){

            // Showing please wait on state select
            $('#ProfileStateId').empty().attr('disabled', 'disabled');
            var newOption = '<option value="0">Please Wait...</option>';
            $('#ProfileStateId').append(newOption);

            // Get the country id
            var countryId = $('#ProfileCountryId option:selected').val();

            // Ajax request to get states list
            $.ajax({
                type: "GET",
                dataType: 'json',
                url: '/states/getlist/' + countryId,
                success: function(data){
                    $('#ProfileStateId').empty();

                    $.each(data, function(index, item){
                        var newOption = '<option value="'+item.State.id+'">'+item.State.name+'</option>';
                        $('#ProfileStateId').append(newOption);
                    })

                    $('#ProfileStateId').removeAttr('disabled');
                },
                error: function(){
                    $('#ProfileStateId').empty();
                    var newOption = '<option value="0">No State Available</option>';
                    $('#ProfileStateId').append(newOption);
                }
            });
        });
    }
});
