/* 
 *  This file is part of the LikePHP package.
 *  Abendas <abendas@163.com; rushairer@163.com>
 * 
 *  For the full copyright and license information, please view the LICENSE
 *  file that was distributed with this source code.
 */
var Album = new Object();
Album.init = function(){
    $('#createAlbumBox').dialog({
        autoOpen: false,
        width: 400,
        minHeight: 300,
        bgiframe:true,
        resizable:false,
        draggable: false,
        modal: true,
        buttons :{
            '取消': function() {
                $(this).dialog('close');
            },
            '创建': function() {
                Album.submitAlbumForm();
            }
        }
    });
    $('#createalbum_link').click(Album.showCreateAlbumForm);
    $('#editalbum_link, ul#icons li').hover(
        function() {
            $(this).addClass('ui-state-hover');
        },
        function() {
            $(this).removeClass('ui-state-hover');
        }
        );
}

Album.checkAlbumForm = function(){
    var result = !Album.checkAlbumName($('#album_name').val())
    || !Album.checkAlbumCategoryId($('#album_category_id').val())
    || !Album.checkAlbumDescription($('#album_description').val());
    return !result;
}
Album.checkEditAlbumForm = function(){
    var result = !Album.checkAlbumName($('#edit_album_name').val())
    || !Album.checkAlbumCategoryId($('#edit_album_category_id').val())
    || !Album.checkAlbumDescription($('#edit_album_description').val());
    return !result;
}
Album.checkWords = function(e,shower,max){
    var len = e.value.length;
    if(len>max){
        len = "<font color='red'>"+len+"</font>";
    }
    $('#'+shower).html('已经写了'+len+'字');
}
Album.checkAlbumName = function(name){
    if(name.length<1 || name.length>20){
        alert('名称字数限制为 1 至 20 个字.');
        return false;
    }
    return true;
}
Album.checkAlbumCategoryId = function(category_id){
    if(parseInt(category_id) <=0){
        alert('请选择分类!');
        return false;
    }
    return true;
}
Album.checkAlbumDescription = function(description){
    if(description.length<10 || description.length>200){
        alert('描述字数限制为 10 至 200 个字.');
        return false;
    }
    return true;
}
Album.submitAlbumForm = function(){
    var checker = Album.checkAlbumForm();
    if( checker){
        $('#createAlbumForm')[0].submit();
        return true;
    }else{
        return false;
    }
}
Album.showCreateAlbumForm = function(){
    $('#createAlbumBox').dialog('open');
    return false;
}
Album.uploadShowPhoto = function(id,url){
    $('#photoinfo_'+id).show();
    $('#photoinfo_'+id+' .albumthumbpic').html('<img src="'+url+'"/>');
    $('#photoinfo_'+id+' input[type=text]').val(Album.getPhotoName(url));
}
Album.getPhotoName = function(url){
    var tmp = url.split('\\');
    return tmp[tmp.length-1].split('.')[0];
}
Album.initUpload = function(){
    var pnum = 6;
    for(var i = 1; i<pnum+1;i++){
        var photo = $('#photo_'+i);
        photo.attr('alt',i);
        photo.change(function(){
            Album.uploadShowPhoto(this.alt, this.value);
        });
    }
}
Album.submitPhotoForm = function(id){
    $('#photoform_'+id)[0].submit();
}
Album.showPhoto = function(id){
    var url = $('#photo_'+id).val();
    var html = "<img src='/attachments/"+url+"' alt='"+id+"'/>";
    $('#bigphoto').html(html)[0].focus();
}
Album.moveListUl = function(per,direction,length){
    var ListUl = $('#ListUl');
    var ml = ListUl.css('margin-left');
    ml = parseInt( ml.split('px')[0]);
    ml = ml + per*direction;
    min = -length + 184 *4;
 
    if(ml<=0 && ml>=min){
        //ListUl.css('margin-left',ml+'px');
        ListUl.animate({'marginLeft':ml+'px'},'fast');
    }
}
Album.gotoAlbum= function(id){
    document.location.href = '?action=show&id='+id;
}
Album.editAlbum= function(id){
    document.location.href = '?action=edit&id='+id;
}
