// JavaScript Document
$(document).ready(function(){

    $.each($.browser, function(k, val){
        if (k == 'msie' && $.browser.version == '7.0') {
            $(document).find('head').append('<link rel="stylesheet" type="text/css" href="../Data/style/ie7.css" media="all" />');
            $(document).find('.more').css({
                'position': 'relative',
                'left': '5px'
            });
            
            $.fn.lh_fix = function(n){
                return this.each(function(){
                    var lh = $(this).css('line-height');
                    var new_lh = parseInt(lh) + n;
                    $(this).css({
                        'line-height': new_lh + 'px',
                        'vertical-align': 'middle'
                    });
                });
            }
            $.fn.lh_fix_min = function(n){
                return this.each(function(){
                    var lh = $(this).css('line-height');
                    var new_lh = parseInt(lh) - n;
                    $(this).css({
                        'line-height': new_lh + 'px',
                        'vertical-align': 'middle'
                    });
                });
            }
            $('#nav').find('.text-shadow').lh_fix(4);
            
            
        }
    });
    $.fn.short_btn_effect = function(i){
        this.each(function(){
        
            var $that_a = $(this).find('a');
            var btn_width = $that_a.width();
            $that_a.not('.admin_ok').addClass('short');
            var btn_short = $that_a.width();
            $(this).hover(function(){
            
                $that_a.stop().animate({
                    width: btn_width
                }, '200').removeClass('short');
                
            }, function(){
            
                $that_a.stop().animate({
                    width: btn_short
                }, '200');
                
            });
            if ($.browser.msie) {
                $(this).mousedown(function(){
                    $(this).addClass('actived');
                }).mouseup(function(){
                    $(this).removeClass('actived')
                });
            }
        });
    }
    $.fn.check_btn_effect_needs = function(i){
        return this.each(function(){
            var w = $(this).find('a').width();
            if (w == 0) {
                return;
            }
            else 
                if (w !== 0) {
                    $(this).short_btn_effect();
                }
        });
    }
    $('.btn_fn_admin').each(function(){
        $(this).check_btn_effect_needs();
    });
    $('.news-post').each(function(){
        var $delete_news = $(this).find('.delete_news');
        var $news_set = $(this).find('.news_set');
        var $sets_inner = $(this).find('.sets_inner');
        var $news_post = $(this);
        var sets_inner_width;
        sets_inner_width = $sets_inner.width();
        $sets_inner.hide();
        $news_set.css({
            'cursor': 'pointer'
        });
        $delete_news.hide();
        $news_post.filter('.admin_mode').hover(function(e){
            $delete_news.show();
            $news_set.hover(function(){
                $sets_inner.stop().animate({
                    width: sets_inner_width
                }, '200');
            }, function(){
                $sets_inner.stop().animate({
                    width: 0
                }, '200');
            });
        }, function(){
            $delete_news.hide();
        });
    });
    
    $.fn.commentPost_fx = function(flag_cm_reply){
        //alert(flag_cm_reply);

        var $cm_post = $(this);
        var $cm_set = $cm_post.find('.cm_set');
        var $cm_reply = $cm_post.find('.cm_reply');

        var $delete_cm, $parent_delete_cm;
        var $sets_inner, $parent_sets_inner;
        
        $delete_cm = $cm_post.find('.delete_cm');
   
        $cm_reply.find('.delete_cm').addClass('reply_delete_cm');
        $cm_post.find('.delete_cm').not('.reply_delete_cm').addClass('parent_delete_cm');
        
        $parent_delete_cm = $cm_post.find('.parent_delete_cm');
        $parent_sets_inner = $parent_delete_cm.find('.sets_inner');

        var pW = $parent_sets_inner.width();

        $('.commentPost .cm_reply').each(function(){
          $(this).cm_reply_fx();
        });
                
        $cm_set.css('cursor','pointer');
        $parent_sets_inner.hide();
        $parent_delete_cm.hide();

        $cm_post.hover(function(){
          $parent_delete_cm.show();

          $cm_set = $parent_delete_cm.find('.cm_set');
          
          $cm_set.hover(function(){
            $parent_sets_inner.stop().animate({
              'width': pW
            }, '200');
          },function(){
            $parent_sets_inner.stop().animate({
              'width': 0
            }, '200');
          });       
        },function(){
          $parent_delete_cm.hide();
        });
        
    }
    
    $.fn.cm_reply_fx = function() {
      //alert('cm_reply');
      var $this = $(this);
      var $reply_delete_cm = $this.find('.reply_delete_cm');
      var $cm_set = $reply_delete_cm.find('.cm_set');
      var $reply_sets_inner = $reply_delete_cm.find('.sets_inner');
      
      var iW = $reply_sets_inner.width();
      $reply_sets_inner.css('visibility','hidden');
      $reply_delete_cm.css('visibility','hidden');
      
      $this.hover(function(){
      $reply_sets_inner.css('visibility','visible');
      $reply_delete_cm.css('visibility','visible');
      $reply_sets_inner.css('width',0);
      
      //alert('cm_reply');
      $reply_delete_cm.show();
      
        $cm_set.hover(function(){
        //alert('cm_reply');
          $reply_sets_inner.stop().animate({
            'width': iW
          }, '200');
        },function(){
          $reply_sets_inner.stop().animate({
            'width': 0
          }, '200');
        });
               
      },function(){
        $reply_delete_cm.hide();
      });
        
    }

    $('#comment-detail').find('.detail-section').addClass('commentPost');
    
    $('.cm_reply').parents('.commentPost').addClass('has_cm_reply');
    
    $('.commentPost').each(function(){
      var flag_cm_reply = $(this).hasClass('has_cm_reply') ? true : false; 
      	$(this).commentPost_fx(flag_cm_reply);
    });
    
    $('.msg_contn').each(function(){
      $(this).commentPost_fx();
    });
        
    $.fn.cm_reply_last = function(post) {
    	var post = $(this);
    	var replies = post.find('.cm_reply');
    	replies.last().addClass('last_reply');
    }
    
    $('.comment-content_inner').each(function(){
    	$(this).cm_reply_last();
    });
    
    $('.cm_reply_more').click(function(){
    	var href = $(this).attr('rel');
    	document.location.href=href+'#comment-detail';
    	$('#control-tab').tabs('select', 1);
    });
    
    
    
    $(".book-rate1 img").mouseenter(function(e){
        var obj = $(this).parent('.rate');
        var comment = $(this).parents('li.rate-item').find('.book-rate-comment');
        var new_rate = $(this).attr('title');
        if (new_rate) {
            obj.attr('class', 'rate rate-' + (new_rate));
            comment.attr('class', 'book-rate-comment comment-rate-' + new_rate);
        }
    }).click(function(e){
        var obj = $(this).parent('.rate');
        var new_rate = $(this).attr('title');
        obj.attr('rel', new_rate);
    });
    $(".book-rate1").mouseleave(function(e){
        var obj = $(this).find('.rate');
        var comment = $(this).parents('li.rate-item').find('.book-rate-comment');
        var rate = obj.attr('rel');
        if (rate) {
            obj.attr('class', 'rate rate-' + (rate));
            comment.attr('class', 'book-rate-comment comment-rate-' + rate);
        }
    });
    $('html').height('auto');
    $('body').height('auto');
    if ($(window).height() > $('body').height()) {
        $("#content").animate({
            'min-height': $(document).height() - $('#header').height() - $('#footer').height()
        }, 100).height('100%');
    }
    //DD_roundies.addRule('.button', '5px');
    if ($.browser.msie) {
        var elem = $('#container');
        if (elem.css('position') == 'absolute') {
            var offset = $('#header .wrapper').offset();
            elem.css('right', offset.left).css('z-index', 20000);
        }
    }
    
    bk_shelf_deco();
    
    $(document).find('body').append('<div class="notice_bar_contn "><p class="notice_bar">修改已自動儲存</p></div>');
    
    var $notice_bar = $('.notice_bar_contn');
    
    $.fn.notice_hide_fadeOut = function(){
        $notice_bar.animate({
            opacity: 'hide',
            top: '-31px'
        }, 500);
    }
    $.fn.notice_show_fadeIn = function(){
        $notice_bar.animate({
            opacity: 'show',
            top: '0'
        }, 500).delay(2000);
    }
    $('.notice_bar_contn').hide();
    
    $('.button').click(function(){
        $.fn.notice_show_fadeIn();
        $.fn.notice_hide_fadeOut();
    });
    
    
    
    
    $.fn.open_search_box = function(){
        $('.search_btn_on_contn').addClass('act');
        $('#search-box').show();
        $('#search').focus();
    }
    $.fn.close_search_box = function(){
        $('.search_btn_on_contn').removeClass('act');
        $('#search-box').hide();
    }
    
    $('#search-box').hide();
    
    $('.search_btn_on_contn').hover(function(){
        $.fn.open_search_box();
    }, function(){
        $('#top').mousedown(function(){
            $.fn.close_search_box();
        });
        $('#content-wrapper').mousedown(function(){
            $.fn.close_search_box();
        });
        $('#footer').mousedown(function(){
            $.fn.close_search_box();
        });
    });
    
    $('#search-box .search_btn').click(function(){
        $.fn.close_search_box();
    });
    
    
    
    
    
    
    // cyril edited at 0303 //
    $('.e_cover_wrap').hover(function(){
        $(this).addClass('hoving');
    }, function(){
        $(this).removeClass('hoving');
    });
    
    $('.book-display').click(function(){
        var linkto = $(this).find('a').attr('href');
        window.location = linkto;
    });
    
    $('.book-display').live('mouseover', function(){
		var thistitle = $(this).find('a').attr('title');
		//$(this).attr('title', thistitle);
		$(this).find('img').attr('title', thistitle);
		//$(this).attr('title', thistitle);		
	});
    
    
    // cyril edited at 0304 //
    $('.e-book').click(function(){
        var linkto = $(this).find('.e_cover_wrap').find('a').attr('href');
        window.location = linkto;
    });
    
    $('.books .ebook .book').click(function(){
        var linkto = $(this).find('.e_cover_wrap').find('a').attr('href');
        window.location = linkto;
    });
    
    $('.comment.ebook .book').click(function(){
        var linkto = $(this).find('.e_cover_wrap').find('a').attr('href');
        window.location = linkto;
    });
    
    // e-reso panel file title 0627 //
    $('.e-reso_files p').each(function(){
    	var $this_a = $(this).find('a');
    	var this_title = $(this).find('.real_file_name').text();
    	$this_a.attr('title',this_title);
    });
    
    // e-reso panel file title 0627 //
    $('.e-reso_download li').each(function(){
    	var $this_a = $(this).find('a');
    	var this_title = $(this).find('.real_file_name').text();
    	$this_a.attr('title',this_title);
    });
    
    
    // defined placeholder function for legacy browser [using fn.placeholder]
    $.fn.placeholder = function(o) {
        var defaults = {
            css_class: 'placeholder'
        };
        var options = $.extend(defaults, o);
        var testinput = document.createElement('input');
        $.extend($.support, {
            placeholder: !! ('placeholder' in testinput)
        });

        $(this).each(function() {
            if ($.support.placeholder) {
                return false;
            }

            if ($(this).attr('placeholder') !== undefined) {
                var should_value = $(this).attr('placeholder');
                var cur_value = $(this).attr('value');
                $(this).attr('autocomplete', 'off');

                if (should_value === cur_value) {
                    $(this).addClass(options.css_class);
                }
                if (cur_value === '') {
                    $(this).addClass(options.css_class);
                    $(this).val(should_value);
                }
                // for password placeholder
                if ($(this).attr('type') === 'password') {
                    var placeholder_val = $(this).attr('placeholder');
                    var pw_holder = $('<span />', {
                        text: placeholder_val,
                        'class': options.css_class,
                        css: {
                            position: 'absolute',
                            fontFamily: '"Segoe UI", sans-serif',
                            background: 'transparent',
                            cursor: 'text',
                            border: '0 none',
                            top: $(this).position().top,
                            left: $(this).position().left,
                            lineHeight: $(this).height() + 3 + 'px',
                            paddingLeft: parseFloat($(this).css('paddingLeft')) + 2 + 'px'
                        }
                    }).insertAfter(this);

                    $(this).val('').addClass(options.css_class);
                    pw_holder.click(function() {
                        pw_holder.hide();
                        $(this).prev('input[type="password"]').focus().addClass('typing').removeClass('placeholder');
                    });

                    $(this).focusin(function() {
                        if ($(this).hasClass(options.css_class)) {
                            pw_holder.hide();
                            $(this).removeClass(options.css_class);
                        }
                    });
                    $(this).focusout(function() {
                        if ($(this).val() === '') {
                            pw_holder.show();
                            $(this).val('').addClass(options.css_class);
                        }
                    });
                }
                // for text field placeholder
                if ($(this).attr('type') !== 'password') {
                    $(this).focusin(function() {
                        var placeholder_val = $(this).attr('placeholder');
                        if (placeholder_val === $(this).val()) {
                            $(this).val('').removeClass(options.css_class);
                        }
                    });
                    $(this).focusout(function() {
                        var placeholder_val = $(this).attr('placeholder');
                        $(this).removeClass(options.css_class);
                        if ($(this).val() === '' && $(this).val() !== placeholder_val) {
                            $(this).val(placeholder_val).removeClass(options.css_class);
                        }
                        if ($(this).val() === placeholder_val) {
                            $(this).addClass(options.css_class);
                        }
                    });
                }
            }
            $(this).parents('form').submit(function() {
                $(this).find('[placeholder]').each(function() {
                    var input = $(this);
                    if (input.val() === input.attr('placeholder')) {
                        input.val('');
                    }
                });
            });
        });
    };

    $('.placeholder').placeholder();
    
    // input typing color
    $('.placeholder').each(function() {
        var $input = $(this);
        $input.focusin(function() {
            $input.addClass('typing');
        });
        $input.focusout(function() {
            $input.removeClass('typing');
        });
    });
    
    // resizable init
    $('.resizable').TextAreaResizer();

    
});


// cyril edit 0506 bk_shelf_deco effects //
function bk_shelf_deco() {
    var $e_book_cover = $('.ebook .book');
    $e_book_cover.find('img').wrap('<div class="e_cover" />');
    $e_book_cover.find('.e_cover').before('<div class="e_cover_top" />');
    $e_book_cover.find('.e_cover_top').append('<img src="../Data/images/bookshelf/ebook_bg_big_01.png" />');
    $e_book_cover.append('<div class="e_cover_tail" />');
    $e_book_cover.find('.e_cover_tail').append('<img src="../Data/images/bookshelf/ebook_bg_big_03.png" />');
    $e_book_cover.wrapInner('<div class="e_cover_wrap" />');

    var $e_reso_bk_cover = $('.book-shelf-slider .e-reso .book-cover');
    $e_reso_bk_cover.wrap('<div class="e-reso_img" />');
    $e_reso_bk_cover.parents('.e-reso_img').append('<img src="../Data/images/flag_e_resources.png" class="flag_e-reso" />');

    // e-reso book flag effect //

    var $e_reso_cover = $('.e-reso .book-cover');
    $e_reso_cover.wrap('<div class="e-reso_img" />');

    if ($e_reso_cover.parents('.e-reso').hasClass('comment')) {
        $e_reso_cover.parents('.e-reso_img').append('<img src="../Data/images/flag_e_resources.png" class="flag_e-reso" />');
    }

}

// resizable
(function($){var textarea,staticOffset;var iLastMousePos=0;var iMin=32;var grip;$.fn.TextAreaResizer=function(){return this.each(function(){textarea=$(this).addClass('processed'),staticOffset=null;$(this).wrap('<div class="resizable-textarea"><span></span></div>').parent().append($('<div class="grippie"></div>').bind("mousedown",{el:this},startDrag));var grippie=$('div.grippie',$(this).parent())[0];grippie.style.marginRight=(grippie.offsetWidth-$(this)[0].offsetWidth)+'px'})};function startDrag(e){textarea=$(e.data.el);textarea.blur();iLastMousePos=mousePosition(e).y;staticOffset=textarea.height()-iLastMousePos;textarea.css('opacity',0.25);$(document).mousemove(performDrag).mouseup(endDrag);return false}function performDrag(e){var iThisMousePos=mousePosition(e).y;var iMousePos=staticOffset+iThisMousePos;if(iLastMousePos>=(iThisMousePos)){iMousePos-=5}iLastMousePos=iThisMousePos;iMousePos=Math.max(iMin,iMousePos);textarea.height(iMousePos+'px');if(iMousePos<iMin){endDrag(e)}return false}function endDrag(e){$(document).unbind('mousemove',performDrag).unbind('mouseup',endDrag);textarea.css('opacity',1);textarea.focus();textarea=null;staticOffset=null;iLastMousePos=0}function mousePosition(e){return{x:e.clientX+document.documentElement.scrollLeft,y:e.clientY+document.documentElement.scrollTop}}})(jQuery);

