//プロフィールパネルのイベントなど（リファクタリング対象）

var chara_list_height;
var chara_loader_height;
var back_list_height;
var back_loader_height;
var book_list_height;
var book_loader_height;

var chara_detail_open_flag;
var chara_detail_comp_flag;
var back_detail_open_flag;
var back_detail_comp_flag;
var book_detail_open_flag;
var book_detail_comp_flag;

function initProfChara() {
    $('div#face_image p.image_block').each(function(){
        if ($(this).children('img.thumb_image').attr('alt') == 'none') return;
        var full_img_selecter = $(this).children('img.full_image');
        var width = full_img_selecter.width();
        var height = full_img_selecter.height();
        var aspect = width/height;
        if (height > limit) {
            height = height * (limit / height);
            width = height * aspect;
        }
        full_img_selecter.height(height + 'px')
                         .width(width + 'px')
                         .css("left", (-width) + "px");
    });
}

function initProfBack() {
    var preview_selecter = $('div#prof_back_preview');
    var limit_w = preview_selecter.width();
    var limit_h = preview_selecter.height();
    $('div#prof_back_preview img').each(function(){
        var w = $(this).width();
        var h = $(this).height();
        if (limit_w/w <= limit_h/h) {
            var top = (limit_h - limit_w/w * h)/2;
            $(this).width(limit_w+'px').height((limit_w/w * h)+'px').css('top', top+'px');
        } else {
            var right = (limit_w - limit_h/h * h)/2;
            $(this).width((limit_h/h * w)+'px').height(limit_w+'px').css('top', right+'px');
        }
    });
}

function profileSubmitCallback(target, data) {
	
	switch (target) {
		case 'name':
			$('#prof_user_data input[name="profile.update.name"]').val(data);
	        $('#own').html(data);
			break;
			
		case 'chara_name':
            $('#prof_user_data input[name="profile.update.chara_name"]').val(data);
            $('#own').html(data);
			break;
			
		case 'free_text':
            $('#prof_user_data textarea[name="profile.update.free_text"]').val(data);
			break;
			
		case 'chara_text':
            $('#prof_user_data textarea[name="profile.update.chara_text"]').val(data);
            break;
	}

}

function showLikeWindow(userId, target_id, target_type, elem) {
	if ($('#set_like_block:visible').size() > 0) {
		$('#set_like_block').slideUp("fast", function(){
										$(this).remove();
										$(elem).attr('class', 'btnclosed');
									});
	} else {
		$.post('/like/index',
	           {'user_id'     : userId,
			    'target_id'   : target_id,
				'target_type' : target_type},
	            function(data){
					switch(target_type) {
						case 'user':
						$('#like_user_set_block').html(data);
						break;
						
						case 'book':
                        $('#like_book_set_block').html(data);
                        break;
						
						case 'chara':
                        $('#like_chara_set_block').html(data);
                        break;
						
						case 'back':
                        $('#like_back_set_block').html(data);
                        break;
					}
	                $(elem).parents('.panel_content').css('height', 'auto');
					$(elem).attr('class', 'btnopen');
	            });
	}
}

function showListWindow(userId, target_id, target_type, page_num, panel) {
    if (typeof(page_num) == 'undefined' && $('#third_frame:visible').size() > 0) {
        $('#third_frame').hide("fast");
    } else {
        $.post('/like/list',
               {'user_id'     : userId,
                'target_id'   : target_id,
				'target_type' : target_type,
				'page_num'    : page_num},
                function(data){
					if (panel == 'pop' || $('#pop_window:visible').size() > 0) {
						$('#pop_frame').html(data);
                        showPopWindow();
					} else {
						if ($('#second_frame:visible').size() > 0) {
							$('#third_frame').css('top', $('#second_frame').css('top'));
						}
						$('#third_frame').html(data).show("fast");
					}
                });
    }
}

function addLike(userId, icon, target_id, target_type) {
	var like_elem = $('#set_like_block').parent().siblings('.tab02_btn_like');
	
	$.post('/like/add',
	       {'user_id'     : userId,
		    'icon'        : icon,
			'target_id'   : target_id,
			'target_type' : target_type,
			'name'        : $('#set_like_block input:radio[name="name"]:checked').val(),
			'text'        : $('#set_like_block input:text').val()},
		   function(data){
               $('<span></span>').attr('class', 'like_finish').appendTo(like_elem);
               setTimeout(function(){
                    $(like_elem).children('.like_finish').fadeOut('fast', function(){
                        $(this).remove();
                    });
               }, 1000);
		   });
		   
	$('#set_like_block').hide("fast", function(){
										 $(this).remove();
										 like_elem.children('.btnopen').attr('class', 'btnclosed');
									  });
}

function deleteLike(user_id) {
	var obj = new Object();
	var count = 0;
	$('#like_list_block_content .like_list_checkbox input:checked').each(function(){
		var temp = $(this).attr('name').split('_');
		obj[temp[0]] = obj[temp[0]] ? obj[temp[0]] + ',' + temp[1] : temp[1];
		count ++;
	});
	if (count == 0) return false;
	obj['user_id'] = user_id;
	obj['target_id'] = null;
	obj['target_type'] = 'all';
	obj['page_num'] = '1';
	
	$.post('/like/delete', obj,
	       function(data){
                if (data == '' || pageRedirect(data)) return;
				$('#third_frame').html(data);
		   });
	return false;
}

function likeUserLink(user_id, name) {
	flash.userInfo(new Array(user_id, '0', name));
}

ajax.profileBefore = function(data, user) {
	
    $('#mylist_ctrlpanel').hide('fast');
	
	if (user) {
        second_frame_pointer_y = $(user).offset().top;
    }
	
    //該当ユーザの隣に表示するよう調整
    hideSecondFrame('', true);
    
    $('#second_frame').html(secondFrameLoadHtml());
    				  
//	var pos = getSecondFramePos('#second_frame', second_frame_pointer_y);
//	
//	$('#second_frame').css('top', pos + 'px');
	
	showSecondFrame('fast', true);
	
}

ajax.profileComplete = function(data) {
	
    ajaxSubmit();
    
	if (info_type == 'book') {
        $('div#user_prof').children('div').each(function () {
            if ($(this).attr('id') == 'prof_book_data') {
                $('div#prof_book_data_list').hide();
            } else {
                $(this).children(".panel_content, .panel_footer").hide();
				$(this).children(".panel_title_on").attr('class', 'panel_title_off');
            }
        });
    } else if (info_type == 'chara') {
        $('div#user_prof').children('div').each(function () {
            if ($(this).attr('id') == 'prof_chara_data') {
                $('div#prof_chara_data_list').hide();
            } else {
                $(this).children(".panel_content, .panel_footer").hide();
				$(this).children(".panel_title_on").attr('class', 'panel_title_off');
            }
        });
    } else if (info_type == 'back') {
        $('div#user_prof').children('div').each(function () {
            if ($(this).attr('id') == 'prof_back_data') {
                $('div#prof_back_data_list').hide();
            } else {
                $(this).children(".panel_content, .panel_footer").hide();
				$(this).children(".panel_title_on").attr('class', 'panel_title_off');
            }
        });
    } else if (info_type == 'book_only') {
        $('div#user_prof').children('div').each(function () {
			if ($(this).attr('id') == 'prof_book_data') {
				$('div#prof_book_data_list').hide();
				$(this).children(".panel_title_on").attr('class', 'panel_title_noevent_on');
			} else {
				$(this).children(".panel_content, .panel_footer").hide();
                $(this).children(".panel_title_on").attr('class', 'panel_title_noevent_off');
			}
        });
    } else if (info_type == 'chara_only') {
		$('div#user_prof').children('div').each(function () {
            if ($(this).attr('id') == 'prof_chara_data') {
                $('div#prof_chara_data_list').hide();
                $(this).children(".panel_title_on").attr('class', 'panel_title_noevent_on');
            } else {
                $(this).children(".panel_content, .panel_footer").hide();
                $(this).children(".panel_title_on").attr('class', 'panel_title_noevent_off');
            }
        });
	} else if (info_type == 'back_only') {
        $('div#user_prof').children('div').each(function () {
            if ($(this).attr('id') == 'prof_back_data') {
                $('div#prof_back_data_list').hide();
                $(this).children(".panel_title_on").attr('class', 'panel_title_noevent_on');
            } else {
                $(this).children(".panel_content, .panel_footer").hide();
                $(this).children(".panel_title_on").attr('class', 'panel_title_noevent_off');
            }
        });
    }
	
}

ajax.charaListBefore = function () {
    $('#panel_chara_select').hide('fast');
}

ajax.charaListComplete = function () {
    var top = $('#prof_roleplay').offset().top - $('#second_frame').offset().top - 10;
//    $('#panel_chara_select').css('top', top + 'px');
    $('#panel_chara_select').css('top', top + 'px');
    $('#panel_chara_select').show('fast');
}

ajax.roleplayBefore = function () {
    $('#panel_chara_select').hide('fast');
}


ajax.profCharaDetailBefore = function() {
	chara_detail_comp_flag = false;
	chara_detail_open_flag = false;
	
	chara_list_height = $('div#prof_chara_data_list').height();
    $('div#prof_chara_data_list').hide();
	
    chara_loader_height = $('div#prof_chara_data_detail').html(charadetailLoadHtml).outerHeight();
	
	$('div#prof_chara_data_detail').slideDown('fast');
								   
	$('div#prof_chara_data div.panel_content').animate({'height': chara_loader_height + 'px'}, 'fast',
	                                           function(){
                                               
											        chara_detail_open_flag = true;
	                                           
											   }).queue(function () {
                                                    if (chara_detail_comp_flag && !chara_detail_open_flag) {
													   
														var limit = $('#prof_chara_data_detail').outerHeight();
														$(this).animate({'height': limit + 'px'}, 'fast')
														        .dequeue();
													} else {
														$(this).dequeue();
													}
									           });
}

ajax.profCharaDetailComplete = function() {

    chara_detail_comp_flag = true;
	
	if (chara_detail_open_flag) {
		var limit = $('div#prof_chara_data_detail').outerHeight();
		
        $('div#prof_chara_data div.panel_content').animate({'height': limit + 'px'}, 'fast');
	}
	
    
    $('div#prof_chara_data_detail p.image_block').mouseover(function(e) {
        var height = $(this).children('img.full_image').height();
        var y_zero = $('#prof_chara_data .tab_charav_left').position().top + 80;
        $(this).children('img.full_image').css("top", (y_zero-height/2) + "px").show();
        
    }).mouseout(function() {
        $(this).children('img.full_image').hide();
    });
    
    $('p#list_switch_chara').click(function (){
		
        $('div#prof_chara_data_detail').slideUp('fast');
        $('div#prof_chara_data div.panel_content').animate({'height': chara_list_height + 'px'}, 'fast', function(){
			$('div#prof_chara_data_list').show();
		});
    });
        
    $('div#prof_chara_data_detail img.full_image').bind('load', function(){
        var width = $(this).width();
        var height = $(this).height();
        var aspect = width/height;
		
        if (height > 500) {
            height = height * (500 / height);
            width = height * aspect;
        }
        $(this).height(height + 'px')
                .width(width + 'px')
                .css("left", (-width-20) + "px");
    });

}

ajax.profBackDetailBefore = function () {
	back_detail_comp_flag = false;
    back_detail_open_flag = false;
    
	back_list_height = $('div#prof_back_data_list').height();
    $('div#prof_back_data_list').hide();
    
    back_loader_height = $('div#prof_back_data_detail').html(backdetailLoadHtml).outerHeight();
    
    $('div#prof_back_data_detail').slideDown('fast');
                                   
    $('div#prof_back_data div.panel_content').animate({'height': back_loader_height + 'px'}, 'fast',
                                               function(){
                                               
                                                    back_detail_open_flag = true;
                                               
                                               }).queue(function () {
                                                            if (back_detail_comp_flag && !back_detail_open_flag) {
                                                               
                                                                var limit = $('#prof_back_data_detail').outerHeight();
                                                                $(this).animate({'height': limit + 'px'}, 'fast')
                                                                        .dequeue();
                                                            } else {
                                                                $(this).dequeue();
                                                            }
                                                        });
}

ajax.profBackDetailComplete = function () {
    back_detail_comp_flag = true;
    
    if (back_detail_open_flag) {
        var limit = $('div#prof_back_data_detail').outerHeight();
        
        $('div#prof_back_data div.panel_content').animate({'height': limit + 'px'}, 'fast');
    }
	
    $('div#prof_back_preview img').bind('load', function(){

        var width = $(this).width();
        var height = $(this).height();
        var aspect = width/height;
        if (height > back_loader_height) {
            height = height * (back_loader_height / height);
            width = height * aspect;
        }
        $(this).height(height + 'px')
                .width(width + 'px')
                .css("left", (-width-20) + "px");
    });
    
    $('div#back_image img.prof_back_thumb').mouseover(function(e){
        var id = $(this).attr('id').substr(16);
        var target_selecter = $('div#prof_back_preview img#prof_back_img_' + id);
        var height = target_selecter.height();
        var y_zero = $('div#prof_back_data_detail').offset().top;
        target_selecter.css("top", (e.pageY-y_zero-height/2) + "px").show();
    }).mouseout(function() {
        $('div#prof_back_preview img').hide();
    });
    
    $('p#list_switch_back').click(function (){
		
        $('div#prof_back_data_detail').slideUp('fast');
        $('div#prof_back_data div.panel_content').animate({'height': back_list_height + 'px'}, 'fast', function(){
			$('div#prof_back_data_list').show();
		});
    });
    
}

ajax.profBookDetailBefore = function () {
	book_detail_comp_flag = false;
    book_detail_open_flag = false;
	
	book_list_height = $('div#prof_book_data_list').height();
    $('div#prof_book_data_list').hide();
	
	book_loader_height = $('div#prof_book_data_detail').html(bookdetailLoadHtml).outerHeight();
    
    $('div#prof_book_data_detail').slideDown('fast');
                                   
    $('div#prof_book_data div.panel_content').animate({'height': book_loader_height + 'px'}, 'fast',
                                               function(){
                                               
                                                    book_detail_open_flag = true;
                                               
                                               }).queue(function () {
                                                    if (book_detail_comp_flag && !book_detail_open_flag) {
                                                       
                                                        var limit = $('#prof_book_data_detail').outerHeight();
                                                        $(this).animate({'height': limit + 'px'}, 'fast')
                                                                .dequeue();
                                                    } else {
                                                        $(this).dequeue();
                                                    }
                                               });
}

ajax.profBookDetailComplete = function () {
    book_detail_comp_flag = true;
    
    if (book_detail_open_flag) {
        var limit = $('div#prof_book_data_detail').outerHeight();
        
        $('div#prof_book_data div.panel_content').animate({'height': limit + 'px'}, 'fast');
    }
    
    $('p#list_switch_book').click(function (){
		
        $('div#prof_book_data_detail').slideUp('fast');
        $('div#prof_book_data div.panel_content').animate({'height': book_list_height + 'px'}, 'fast', function(){
			$('div#prof_book_data_list').show();
		});
    });
    
}

ajax.roleplayComplete = function () {
//    $('#second_frame').hide('fast');
	hideSecondFrame('fast');
    jQuery.post('/mylist/index/', {'html' : '1'},
                function(data){
                    pageRedirect(data);
                    $("#first_frame").html(data);
                    initMylist();
                }, 'html');
}

ajax.stoproleBefore = function () {
//    $('#second_frame').hide('fast');
	hideSecondFrame('fast');
}

ajax.stoproleComplete = function () {
    jQuery.post('/mylist/index/', {'html' : '1'},
                    function(data){
                        pageRedirect(data);
                        $("#first_frame").html(data);
                        initMylist();
                    }, 'html');
}

ajax.privatedataComplete = function(){
    showPopWindow();
}



