if (!Clip) var Clip = {};
Clip.Album = {};

Clip.Album.List = Class.create();

Clip.Album.List.prototype = {
    page : 1,
    perpage : 20,
    sort : 'desc',
    mode : 1,
    pars : null,
    initialize: function() {
    },
    select : function(id) {
        var category_input = document.createElement('input');
        category_input.type = 'hidden';
        category_input.name = 'category';
        category_input.value = id;
        $('form').appendChild(category_input);
        $('form').submit();
    },
    get : function() {
    },
    dummy: null
}

var album_list = new Clip.Album.List();

Clip.Album.Upload = Class.create();

Clip.Album.Upload.prototype = {
    initialize: function(){
        
    }

}

album = new Clip.Album.Upload();

Clip.Album.Uploader = Class.create();

Clip.Album.Uploader.prototype = {
    id: $A(),
    image_id: $H(),
    jsr: null,
    host: 'album.shonan-clip.jp',
    callback: 'uploader.checked',
    initialize: function() {
         new PeriodicalExecuter( this.check.bind(this), 3 );
    },
    clear: function(){
        this.id = $A();
        image_id = $H();
        jsr = null;
    },
    submit: function(form_id){
        for(var i=1;i<=10;i++){
            if($('dummy_title' + i).value){
                var title = document.createElement('input');
                title.type = 'hidden';
                title.name = 'title' + i;
                title.value = $('dummy_title' + i).value;
                $(form_id).appendChild(title);
            }
            if($(i + '_photo').value || this.id[i - 1]){
                var photo = document.createElement('input');
                photo.type = 'hidden';
                photo.name = 'photo';
                photo.value = i;
                $(form_id).appendChild(photo);
            }
        }
        $(form_id).submit();
    },
    setUUID: function(idx, id, img) {
        //        this.id.push(id);
        this.id[idx] = id;
        this.image_id[id] = img;
    },
    createCache: function(id) {
        
        this.jsr = new JSONscriptRequest(
                         'http://' + this.host + '/rest/upload/create/?callback=uploader.createdCache&id=' + this.id[id]
        );
        this.jsr.buildScriptTag();
        this.jsr.addScriptTag();
        /*
        new Ajax.Request(
                         '/rest/upload/create/?id=' + this.id[id], {  method: 'get', onSuccess: this.checked.bind(this) }
                         );
        */
    },
    //    createedCache: function(request){
    createdCache: function(json){
        this.jsr.removeScriptTag();
    },
    check: function() {
        var list = this.id.join(',');
        if(list){
            this.jsr = new JSONscriptRequest(
                                             'http://' + this.host +'/rest/upload/ping/?callback=' + this.callback + '&id=' + list
                                             );
            this.jsr.buildScriptTag();
            this.jsr.addScriptTag();
            //            new Ajax.Request(
            //                 '/rest/upload/ping?id=' + list,
            //                 { method: 'get', onSuccess: this.checked.bind(this) }
            //                 );
        }
    },
    checkedMovie: function(json){
        var results = json.result;
        if(($('movie') && $('movie').value) || ($('music') && $('music').value)){
            if(results[0].status == 'success'){
                $('form').submit();
            }
        }else{
            var file = document.createElement('input');
            file.type = "hidden";
            file.name = "file_error";
            file.value = 1;
            $('form').appendChild(file);
            $('form').submit();
        }
        this.jsr.removeScriptTag();
    },
    //    checked: function(request) {
    //    var json = eval('('+request.responseText+')');
    checked: function(json){
        var results = json.result;
        if(results){
            for(var i=0; i<results.length; i++){
                if(results[i].status == 'success' && this.image_id[results[i].id]) {
                    if($(this.image_id[results[i].id])){
                        $(this.image_id[results[i].id]).src = results[i].file + '?' + new Date().getTime();
                    }
                }
            }
        }
        this.jsr.removeScriptTag();
    },
    edit: function(id) {
        $(id).style.display = '';
    },
    del: function(id, suffix) {
        this.jsr = new JSONscriptRequest(
                                         'http://' + this.host + '/rest/upload/delete/' + this.id[id - 1] + '?callback=uploader.deled'
                                         );
        this.jsr.buildScriptTag();
        this.jsr.addScriptTag();
        //        new Ajax.Request( '/rest/upload/delete/' + this.id[id - 1], { method: 'get' } );
        this.id[id - 1] = null;
        id = id + suffix;
        $(id).style.display = '';
        $('edit_' + id).style.display = 'none';
        $('img_' + id).src = '/image/not/not_album_s.gif';
    },
    deled: function(json){
    }
};




Clip.Album.Setting = Class.create();

Clip.Album.Setting.prototype = {
    initialize: function(){
        
    },
    get_email: function(){
        $('email_for_mobile').innerHTML = "";
        var url = '/rest/album/generate_email/?format=json&'+(new Date().getTime());
        new Ajax.Request(
                         url, { method: 'get', onSuccess: this.got_email.bind(this) }
                         );
    },
    got_email: function(request){
        var json =  eval("("+request.responseText+")");
        $('email_for_mobile').innerHTML = json.email_for_mobile;
    }
}
var setting = new Clip.Album.Setting();


Clip.Album.Photos = Class.create();


Clip.Album.Photos.prototype = {
    initialize: function() {
        this.page = 1;
        this.perpage = 20; //4;
        this.sort = 'desc';
        this.imp_sort = 'desc';
        this.favorite_sort = 'dsc';
        this.sort_type = '';
        this.sort_flag = '';
        this.mode = 1;
        this.submit = 1;
        this.pars = '';
        this.genre = '';
        this.af = 0;
        //this.rest_query = null;
        this.rest_query = this.loadhash();
        this.hash = null;
        this.myalbum = 0;
        map = undefined;
    },
    loadhash: function() {
        var hash = document.location.hash.replace(/^#+/, '').replace(/\//g, '&');
        if(this.hash != hash){
            this.hash = hash;
            return hash;
        }
    },

    updatehash: function() {
        bbb.change( this.rest_query.replace(/&/g, '/') );
    },

    updatecallback: function() {
        var hash = this.loadhash();
        this.rest_query = hash;
        if(this.rest_query){
            this.remake_form();

            var config = hash.parseQuery();
            this.page = config.page;
            this.perpage = config.perpage;
            $('sort_imp1').src = '/image/common/sort_memo1.gif';
            $('sort_favorite1').src = '/image/common/sort_favorite1.gif';
            $('sort_new1').src = '/image/common/sort_new1.gif';
            this.sort_type = config.imp_sort ? 'imp' : config.favorite_sort ? 'favorite' : '';
            if(this.sort_type == 'imp'){
                this.imp_sort = config.imp_sort == 'desc' ? 'asc' : 'desc';
            }else if(this.sort_type == 'favorite'){
                this.favorite_sort = config.favorite_sort == 'desc' ? 'asc' : 'desc';
            }else{
                this.sort = config.sort ? '' : 'desc';
            }
            this.sort_flag = config.sort_flag;
            this.mode = config.mode;
            this.next = config.next;
            this.prev = config.prev;
            this.changeSort(this.sort_type);
            this.setMenu();

            this.search_data();
        }
    },

    remake_form: function() {

    },
    select: function(id){
        $('form').category.value=id;
        var sgON = document.getElementsByClassName('sgON');
        sgON.each( function(l) { l.className = '' } );
        $('category_'+id).className = 'sgON';
        this.submit = 1;
        this.search();
    },
    setMenu: function(){
        /*
        if(this.sort){
            $('sort_new1').src='/image/common/sort_new1.gif';
        }else{
            $('sort_new1').src='/image/common/sort_new2.gif'
        }
        */
        if($('sort_box1')){
        $('sort_box1').src = '/image/common/sort_box1.gif';
        //$('sort_box2').src = '/image/common/sort_box2.gif';
        //$('sort_box3').src = '/image/common/sort_box3.gif';
        $('sort_box4').src = '/image/common/sort_box4.gif';
        if(this.mode == 2){
            $('sort_box2').src = '/image/common/sort_box2_on.gif';
        }else if(this.mode == 3){
            $('sort_box3').src = '/image/common/sort_box3_on.gif';
        }else if(this.mode == 4){
            $('sort_box4').src = '/image/common/sort_box4_on.gif';
        }else{
            $('sort_box1').src = '/image/common/sort_box1_on.gif';
        }
        }
        if( $('sort_view_4')){
            if((this.mode == 3) || (this.mode == 4)){
                $('sort_view_4').style.display = '';
                $('sort_view_100').style.display = 'none';
                if(this.perpage == 4){
                    $('sort_view_20').src = '/image/common/sort_view_20.gif';
                    $('sort_view_4').src = '/image/common/sort_view_4_on.gif';
                }else{
                    $('sort_view_20').src = '/image/common/sort_view_20_on.gif';
                    $('sort_view_4').src = '/image/common/sort_view_4.gif';
                }
            }else{
                $('sort_view_4').style.display = 'none';
                $('sort_view_100').style.display = '';
                if(this.perpage == 100){
                    $('sort_view_20').src = '/image/common/sort_view_20.gif';
                    $('sort_view_100').src = '/image/common/sort_view_100_on.gif';
                }else{
                    $('sort_view_20').src = '/image/common/sort_view_20_on.gif';
                    $('sort_view_100').src = '/image/common/sort_view_100.gif';
                }
            }
        }
    },
        swapSortImage: function( sort_type ) {
        if(sort_type == 'imp') {
            if(this.sort_type == 'imp'){
                if(this.imp_sort == 'desc'){
                    $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
                }else{
                    $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
                }
            }else{
                $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
            }
        }else if(sort_type == 'favorite'){
            if(this.sort_type == 'favorite'){
                if(this.favorite_sort == 'desc'){
                    $('sort_favorite1').src = '/image/common/sort_favorite2_on.gif';
                }else{
                    $('sort_favorite1').src = '/image/common/sort_favorite1_on.gif';
                }
            }else{
                $('sort_favorite1').src = '/image/common/sort_favorite1_on.gif';
            }
        }else{
            if(this.sort_type){
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
            }else{
                if(this.sort){
                    $('sort_new1').src = '/image/common/sort_new2_on.gif';
                }else{
                    $('sort_new1').src = '/image/common/sort_new1_on.gif';
                }
            }
        }
    },
    swapSortImageRestore: function( sort_type) {

        if( this.sort_flag != 1 ){
            if(sort_type == 'imp') {
                if(this.sort_type == 'imp' ){
                    if(this.imp_sort == 'desc'){
                        $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
                    }else{
                        $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
                    }
                }else{
                        $('sort_imp1').src = '/image/common/sort_memo1.gif';
                }
            }else if(sort_type == 'favorite'){
                if(this.sort_type == 'favorite' ){
                    if(this.favorite_sort == 'desc'){
                        $('sort_favorite1').src = '/image/common/sort_favorite1_on.gif';
                    }else{
                        $('sort_favorite1').src = '/image/common/sort_favorite2_on.gif';
                    }
                }else{
                    $('sort_favorite1').src = '/image/common/sort_favorite1.gif';
                }
            }else{
                if(this.sort_type == 'imp' || this.sort_type == 'favorite'){
                    $('sort_new1').src = '/image/common/sort_new1.gif';
                }else{
                    if(this.sort == 'desc'){
                        $('sort_new1').src = '/image/common/sort_new1_on.gif';
                    }else{
                        $('sort_new1').src = '/image/common/sort_new2_on.gif';
                    }
                }
            }
        }else{
            this.sort_flag = 0;
        }
    },
    changeSort: function(sort_type){
        this.sort_flag = 1;
        if(sort_type == 'imp') {
            if(this.sort_type == 'imp'){
                if(this.imp_sort == 'desc'){
                    this.imp_sort='asc';
                    $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
                }else{
                    this.imp_sort='desc';
                    $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
                }
            }else{
                this.imp_sort='desc';
                $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
                this.sort='desc';
                $('sort_new1').src = '/image/common/sort_new1.gif';
                this.favorite_sort='desc';
                $('sort_favorite1').src = '/image/common/sort_favorite1.gif';
            }
        }else if(sort_type == 'favorite'){
            if(this.sort_type == 'favorite'){
                if(this.favorite_sort == 'desc'){
                    this.favorite_sort='asc';
                    $('sort_favorite1').src = '/image/common/sort_favorite2_on.gif';
                }else{
                    this.favorite_sort='desc';
                    $('sort_favorite1').src = '/image/common/sort_favorite1_on.gif';
                }
            }else{
                this.imp_sort='desc';
                $('sort_imp1').src = '/image/common/sort_memo1.gif';
                this.favorite_sort='desc';
                $('sort_favorite1').src = '/image/common/sort_favorite1_on.gif';
                this.sort='desc';
                $('sort_new1').src = '/image/common/sort_new1.gif';
            }
        }else{
            if(this.sort_type){
                this.imp_sort='desc';
                $('sort_imp1').src = '/image/common/sort_memo1.gif';
                this.favorite_sort='desc';
                $('sort_favorite1').src = '/image/common/sort_favorite1.gif';
                this.sort='desc';
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
            }else{
                if(this.sort){
                    this.sort='';
                    $('sort_new1').src = '/image/common/sort_new2_on.gif';
                }else{
                    this.sort='desc';
                    $('sort_new1').src = '/image/common/sort_new1_on.gif';
                }
            }
        }
        this.sort_type = sort_type;

        this.submit = 0;
        this.search();
    },
    /*
    swapSortImage: function( sort_type ) {
        if(sort_type == 'imp') {
            if(this.imp_sort == 'desc'){
                $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
            }else{
                $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
            }
        }else{
            if(this.sort == 'desc'){
                $('sort_new1').src = '/image/common/sort_new2_on.gif';
            }else{
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
            }
        }
    },
    swapSortImageRestore: function( sort_type) {
        if( this.sort_flag != 1 ){
            if(sort_type == 'imp') {
                if(this.sort_type == 'imp' ){
                    if(this.imp_sort == 'desc'){
                        $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
                    }else{
                        $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
                    }
                }else{
                    if(this.imp_sort == 'desc'){
                        $('sort_imp1').src = '/image/common/sort_memo1.gif';
                }else{
                        $('sort_imp1').src = '/image/common/sort_memo2.gif';
                    }
                }
            }else{
                if(this.sort_type == 'imp'){
                    if(this.sort == 'desc'){
                        $('sort_new1').src = '/image/common/sort_new1.gif';
                    }else{
                        $('sort_new1').src = '/image/common/sort_new2.gif';
                    }
                }else{
                    if(this.sort == 'desc'){
                        $('sort_new1').src = '/image/common/sort_new1_on.gif';
                    }else{
                        $('sort_new1').src = '/image/common/sort_new2_on.gif';
                    }
                }
            }
        }else{
            this.sort_flag = 0;
        }
    },
    changeSort: function( sort_type ){
        this.af = 0;
        this.sort_flag = 1;
        if(sort_type == 'imp'){
            if(this.imp_sort == 'desc'){
                this.imp_sort='asc';
                $('sort_imp1').src = '/image/common/sort_memo2_on.gif';
            }else{
                this.imp_sort='desc';
                $('sort_imp1').src = '/image/common/sort_memo1_on.gif';
            }
            if(this.sort){
                $('sort_new1').src = '/image/common/sort_new1.gif';
            }else{
                $('sort_new1').src = '/image/common/sort_new2.gif';
            }
            this.sort_type  = 'imp';
        }else{
            if(this.sort){
                this.sort='';
                $('sort_new1').src = '/image/common/sort_new2_on.gif';
            }else{
                this.sort='desc';
                $('sort_new1').src = '/image/common/sort_new1_on.gif';
            }
            if(this.imp_sort == 'desc'){
                $('sort_imp1').src = '/image/common/sort_memo1.gif';
            }else{
                $('sort_imp1').src = '/image/common/sort_memo2.gif';

            }
            this.sort_type  = '';
        }
        this.submit = 0;
        this.search();
    },
    */
    changeMode: function(n){
        /*
        if(n > 2 && this.mode < 3){
            if(this.perpage == 20){
                pp = 4;
            }else{
                pp = 20;
            }
        }else if(n < 3 && this.mode > 2){
            if(this.perpage == 20){
                pp = 100;
            }else{
                pp = 20;
            }
        }else{
            pp = this.perpage;
        }
        */
        pp = 20;
        this.page =  Math.floor(((this.page - 1) * this.perpage + 1)/pp) + 1;
        this.perpage = pp;
        this.mode = n;
        this.submit = 0;
        this.search();
    },
    swapModeRestore: function(n){
        if(this.mode != n){
            MM_swapImgRestore();
        }
    },
    changePerpage: function(n){
        this.page =  Math.floor(((this.page - 1) * this.perpage + 1)/n) + 1;
        this.perpage = n;
        this.submit = 0;
        this.search();
    },
    swapPerpageRestore: function(n){
        if(this.perpage != n){
            MM_swapImgRestore();
        }
    },
    next_page: function() {
        this.page = $('form').next.value;
        if(this.page){
            this.submit = 0;
            this.search();
        }
    },
    previous_page: function() {
        this.page = $('form').prev.value;
        if(this.page){
            this.submit = 0;
            this.search();
        }
    },
    search: function() {
        if(this.submit == 1){
            this.page = 1;
            this.pars = Form.serialize('form');
        }
        this.submit = 1;
        if(this.sort_type == 'imp'){
            sort = "imp_sort=" + this.imp_sort;
        }else if(this.sort_type == 'favorite'){
            sort = "favorite_sort=" + this.favorite_sort;
        }else{
            sort = "sort=" + this.sort;
        }
        this.setMenu();
        if(this.myalbum == 1){
            this.perpage = 30;
        }
        //this.rest_query = this.rest_query ? this.rest_query : 'perpage=' + this.perpage + '&page=' + this.page + '&' + sort + '&mode=' + this.mode + '&' + this.pars;
        this.rest_query = 'perpage=' + this.perpage + '&page=' + this.page + '&' + sort + '&mode=' + this.mode + '&' + this.pars;
        if(this.myalbum == 1 && !this.rest_query.match(/myalbum=1/)){
            this.rest_query = this.rest_query + "&myalbum=1"
        }
        this.updatehash();
    },
    search_data: function(){
        if (this.mode != 4) {
            if ($('map')) {
              Element.hide($('map'));
              Element.hide($('gMAPzoom2'));
            }
            var url = '/rest/photos/?format=json&' + this.rest_query + '&' +new Date().getTime();
            if (this.admin_category) {
                url = url + '&admin_category=' + this.admin_category;
            }
            this.rest_query = null;

            if (this.is_jsr) {
                this.jsr = new JSONscriptRequest(
                    'http://' + this.host + url + '&callback=album_photos.searched'
                );
                this.jsr.buildScriptTag();
                this.jsr.addScriptTag();
            } else {
                new Ajax.Request(
                      url, { method: 'get', onSuccess: function(request, json){ this.searched(request) }.bind(this) }
                    );
            }
        } else {
            Element.show($("map"));
            Element.show($('gMAPzoom2'));
            $('result_list').innerHTML = '';
            if (map == undefined) {
                var lat = 35.32066664311905;
                var lng = 139.55283522605896;
                var zoom = 6;
                map =  new Clip.Map.RenderNearMap(lat, lng, zoom);
                map.category = 'album';
                map.renderNearIcons();
            }
            $('navi1').innerHTML = '';
            $('navi2').innerHTML = '';
            $('arrow_next1').style.display = 'none';
            $('arrow_next2').style.display = 'none';
            $('arrow_next1_none').style.display = '';
            $('arrow_next2_none').style.display = '';
            $('arrow_prev1').style.display = 'none';
            $('arrow_prev2').style.display = 'none';
            $('arrow_prev1_none').style.display = '';
            $('arrow_prev2_none').style.display = '';
        }
    },
    searched: function(request) {
        var json = this.is_jsr ? request : eval("(" + request.responseText+")");
        $('result_list').innerHTML = '';
        if(json.result){
            Jemplate.process( 'photos.tt', json, $('result_list'));
            $('navi1').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('navi2').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('form').prev.value = json.pager.previous_page;
            $('form').next.value = json.pager.next_page;
        }else{
            Jemplate.process( 'photos.tt', json, $('result_list'));
            $('navi1').innerHTML = '0-0(0件中)';
            $('navi2').innerHTML = '0-0(0件中)';
            $('form').prev.value = '';
            $('form').next.value = '';
        }
        if(!json.pager.next_page){
            $('arrow_next1').style.display = 'none';
            $('arrow_next2').style.display = 'none';
            $('arrow_next1_none').style.display = '';
            $('arrow_next2_none').style.display = '';
        }else{
            $('arrow_next1').style.display = '';
            $('arrow_next2').style.display = '';
            $('arrow_next1_none').style.display = 'none';
            $('arrow_next2_none').style.display = 'none';
        }
        if(!json.pager.previous_page){
            $('arrow_prev1').style.display = 'none';
            $('arrow_prev2').style.display = 'none';
            $('arrow_prev1_none').style.display = '';
            $('arrow_prev2_none').style.display = '';
        }else{
            $('arrow_prev1').style.display = '';
            $('arrow_prev2').style.display = '';
            $('arrow_prev1_none').style.display = 'none';
            $('arrow_prev2_none').style.display = 'none';
        }
        if (this.is_jsr) {
            this.jsr.removeScriptTag();
        }
    },
    del : function(id) {
        new Ajax.Request(
                         '/rest/photos/delete?format=json', {  method: 'post',
                                                               parameters: "id=" + id,
                                                               onSuccess: function( request, json ){ this.deled(request) }.bind(this) }
                         );
    },
    deled : function(request){
        var json = eval("(" + request.responseText+")");
        if( json.status == 'success' ){
            location.reload();
        }
    },
    surf_movie_mode : function() {
        this.admin_category = 1;
        this.is_jsr = true;
    }
}

var album_photos = new Clip.Album.Photos();


Clip.Album.Edit = Class.create();

Clip.Album.Edit.prototype ={
    target_id : null,
    target_uuid: null,
    initialize: function() {
    },
    edit: function(id, target, event) {
        //var id = id;
        var x = Event.pointerX(event);
        var y = Event.pointerY(event);
        x -= 30;
        y -= 655;
        if ( navigator.userAgent.match(/MSIE/) ) {
            x += 40;
            y += 25;

            // xxx
            var clientwidth = document.body.clientWidth;
            var border = 940;
            if (clientwidth > border) {
                var gap = clientwidth - border;
                x -= gap / 2;
            }
        }
        else if ( window.opera || navigator.userAgent.match(/Windows/) ) {
            x += 0;
        }

        this.target_id = id;

        $('message_editbox').style.left = x + 'px';
        $('message_editbox').style.top = y + 'px';
        $('message_editbox').style.display = '';
        //this.openDialog();
    },

    dummy : null
}

var album_editor = new Clip.Album.Edit();
Clip.Album.Select = Class.create();

Clip.Album.Select.prototype = {
    y: 0,
    current_id: null,
    page: 1,
    perpage: 20,
    next: null,
    perv: null,
    uploader: null,
    session_type: 'profile',
    filename: 'filename',
    item_id: null,
    path: null,
    table_name: null,
    table_id: null,
    initialize: function(){
        this.y = 0;
    },
    reset: function(){
        this.page = 1;
        this.form_id = null;
        this.form_data = null;
    },
    close: function(){

        if(this.form_id && this.session_type){
            var lb = new Lightbox({ url: this.form_url });
            lb.callback = function(){
                Clip.Util.Form.unserialize( this.form_id, this.form_data );
                window.scrollTo(0, this.y) }.bind(this);
            lb.show();
        }else{
            var select = document.getElementsByTagName('select');
            for(var i=0; i<select.length;i++){
                select[i].style.visibility = '';
            }
            window.scrollTo(0,this.y);
            new Lightbox().hide();
        }
    },
    uploadData: function() {
        var pars = Form.serialize('form_photo');
        pars = pars + "&uuid=" + $('uuid').value + '&photo_title=' + $('photo_title').value ;
        new Ajax.Request(
                         '/rest/album/photo_upload?format=json',
                         {  method: 'post',
                            parameters: pars,
                                 onSuccess: function( request, json ){ this.uploadedData(request) }.bind(this) }
                         );
    },
    uploadedData: function(request) {
        var json = eval("(" + request.responseText+")");
        if(!json.error_msg){
            this.setImage(json.uuid, json.path ? json.path : $('img_1_photo').src);
        }else{
            var error_div = document.getElementsByClassName('err');
            for(var i=0;i<error_div.length;i++){
                error_div[i].innerHTML = '';
                error_div[i].style.display = 'none';
            }
            $H(json.error_msg).each(function(k){
                    var id = "error_" + k[0];
                    $(id).innerHTML = '<p>' + k[1]+ '</p>';
                    $(id).style.display = ''
                });
        }
    },
    upload: function( current_id ) {
        if(this.form_id){
            this.form_data = Form.serialize(this.form_id);
        }
        if( current_id ){
            this.current_id = current_id;
        }
        this.y = this.getScroll();
        window.scrollTo(0,0)
        var select = document.getElementsByTagName('select');
        for(var i=0; i<select.length;i++){
            select[i].style.visibility = 'hidden';
        }
        new Lightbox({ url: '/album/photo_upload?' + new Date().getTime()}).show();
    },
    getList: function( current_id, file_type ) {
        if( current_id ){
            this.page = 1;
            if(this.form_id){
                this.form_data = Form.serialize(this.form_id);
            }
            this.current_id = current_id;
            this.y = this.getScroll();
        }

        window.scrollTo(0,0)
        var select = document.getElementsByTagName('select');
        for(var i=0; i<select.length;i++){
            select[i].style.visibility = 'hidden';
        }
        if( file_type == 'movies' ){
            new Lightbox({ url: '/album/movie_select?' + new Date().getTime()}).show();
        }else{
            new Lightbox({ url: '/album/photo_select?' + new Date().getTime()}).show();
        }
    },
    getScroll: function() {
        var yScroll;

        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        }
        else if ( document.documentElement && document.documentElement.scrollTop ) { // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
        }
        else if (document.body) { // all other Explorers
            yScroll = document.body.scrollTop;
        }

        return yScroll;
    },
    next_page: function(file_type) {
        this.page = this.next;
        if(this.page){
            this.submit = 0;
            this.get(file_type);
        }
    },
    previous_page: function(file_type) {
        this.page = this.prev;
        if(this.page){
            this.submit = 0;
            this.get(file_type);
        }
    },

    get: function(file_type) {
        if(this.submit == 1){
            this.pars = Form.serialize('form');
        }
        new Ajax.Request(
                         '/rest/album/photo_select?format=json&' + this.pars + '&perpage=' + this.perpage + '&page=' + this.page + '&file_type=' + file_type,
                         {  method: 'post',
                            onSuccess: function( request, json ){ this.got(request) }.bind(this) }
                         );
    },
    got: function(request) {
        var json = eval("(" + request.responseText+")");
        $('photo_list').innerHTML = '';
        if(json.result){
            Jemplate.process( 'photo_select.tt', json, $('photo_list') );
            $('navi1').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            $('navi2').innerHTML = json.pager.first+'-'+json.pager.last+'('+json.pager.total_entries+'件中)';
            this.prev = json.pager.previous_page;
            this.next = json.pager.next_page;
        }
        if(!json.pager.next_page){
            Jemplate.process( 'photo_select.tt', json, $('photo_list') );
            $('photo_arrow_next1').style.display = 'none';
            $('photo_arrow_next2').style.display = 'none';
            $('photo_arrow_next1_none').style.display = '';
            $('photo_arrow_next2_none').style.display = '';
        }else{
            $('photo_arrow_next1').style.display = '';
            $('photo_arrow_next2').style.display = '';
            $('photo_arrow_next1_none').style.display = 'none';
            $('photo_arrow_next2_none').style.display = 'none';
        }
        if(!json.pager.previous_page){
            $('photo_arrow_prev1').style.display = 'none';
            $('photo_arrow_prev2').style.display = 'none';
            $('photo_arrow_prev1_none').style.display = '';
            $('photo_arrow_prev2_none').style.display = '';
        }else{
            $('photo_arrow_prev1').style.display = '';
            $('photo_arrow_prev2').style.display = '';
            $('photo_arrow_prev1_none').style.display = 'none';
            $('photo_arrow_prev2_none').style.display = 'none';
        }
        var ids = $A();
        var anchors = $A( $('photo_list').getElementsByTagName('a') );
        anchors.each( function(a){
                var href_array = a.href.split('/');
                var id = href_array.pop();
                ids.push(id);
                a.href = "#";
                a.onclick = function(){ new Lightbox({ url: '/album/photo_detail?id=' + id + '&' + new Date().getTime()}).show(); }.bind(this);
            }.bind(this));
        var imgs = $A( $('photo_list').getElementsByTagName('img') );
        var n = 0;
        imgs.each( function(i){
                if( i.name == 'edit' ){
                    var id = ids[n];
                    var src = this.current_id.match(/^movie/) ? $('img_' + ids[n]).src : $('img_' + ids[n]).src.replace('small', 'master');
                    i.onclick = function(){ this.setImage(id, src); return false; }.bind(this);
                    n = n + 2;
                }
            }.bind(this))
    },
    setImage: function(id, path) {
        this.uploader.clear();
        this.path = path;
        new Ajax.Request(
                         '/rest/album/photo_set?format=json&id=' + id + '&key=' + this.current_id + '&type=' + this.session_type + '&filename=' + this.filename,
                         {  method: 'post',
                            onSuccess: function( request, json ){ this.setImageSuccess(request) }.bind(this) }
                         );
    },
    setImageSuccess : function(request) {
        var json = eval("(" + request.responseText+")");
        //        if(json.clip_path && $('photo1_clip_div')){
        //    $('photo1_clip_div').style.display = '';
        //    $('img_photo1_clip').src = json.clip_path;
        //}
        if(this.form_id && this.session_type){
            var lb = new Lightbox({ url: this.form_url });
            lb.callback = function(){
                $('img_' + this.current_id).src = this.path  + '?' + new Date().getTime();
                $('img_' + this.current_id).width = json.width;
                $('img_' + this.current_id).height = json.height;
                $('edit_' + this.current_id).style.display = '';
                $('album_' + this.current_id).style.display = 'none';
                if(this.current_id.match(/^movie/)){
                    $(this.current_id).value = json.movie_uuid;
                }
                Clip.Util.Form.unserialize( this.form_id, this.form_data );
                window.scrollTo(0, this.y) }.bind(this);
            lb.show();
        }else{
            if(this.current_id.match(/^movie/)){
                $(this.current_id).value = json.movie_uuid;
            }else{
                var cid = this.current_id.match(/\d+/);
                if($('dummy_caption' + cid) && $('photo_title')){
                    $('dummy_caption' + cid).value = $('photo_title').value;
                }
            }
            $('img_' + this.current_id).src = this.path + '?' + new Date().getTime();
            if(json.width && json.height){
                $('img_' + this.current_id).width = json.width;
                $('img_' + this.current_id).height = json.height;
            }
            $('edit_' + this.current_id).style.display = '';
            $('album_' + this.current_id).style.display = 'none';
            this.close();
        }


    },
    setUploader : function(uploader) {
        this.uploader = uploader;
    },
    dummy : null
};


Clip.Album.impressionSubmit = function(){
    if(!$('disable') || $('disable').value != 1){
        if($('dummy_caption1')){
            $('form').caption1.value =  $('dummy_caption1').value;
            $('form').caption2.value =  $('dummy_caption2').value;
            $('form').caption3.value =  $('dummy_caption3').value;
        }
        $('form').submit();
    }
}

Clip.Album.moviePlayer = function(uuid, select, id, width, height) {
    var id = id ? id : '';
    var width  = width  ? width  : '460';
    var height = height ? height : '365';
    $('player' + id).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="shonan_clip_movizo' + id + '" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/swf/shonan_clip_movizo.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="wmode" value="transparent"><param name="FlashVars" value="id='+ uuid +'&select='+ select +'" /><embed src="/swf/shonan_clip_movizo.swf" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="shonan_clip_movizo" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="id='+ uuid +'&select='+ select+'" wmode="transparent" /></object>';

}
Clip.Album.soundPlayer = function(uuid, select, id, width, height) {
    var id = id ? id : '';
    var width  = width  ? width  : '460';
    var height = height ? height : '365';
    $('player' + id).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="shonan_clip_sound' + id + '" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/swf/shonan_clip_sound.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="wmode" value="transparent"><param name="FlashVars" value="id='+uuid+'&select='+select+'&soundbgswf=test_movie.swf" /><embed src="/swf/shonan_clip_sound.swf" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="shonan_clip_sound" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id='+uuid+'&select='+select+'&soundbgswf=test_movie.swf" wmode="transparent" /></object>'

}
