jQuery(document).ready(function() {
//图片延迟
    jQuery(".entry lastest img, .entry img, #tab_content_follow img").lazyload({
        //placeholder : "http://www.madcn.com/madcg/images/grey.gif",
		placeholder : "images/animation_loading_small.gif",
        effect      : "fadeIn"
    }); 
//灯箱
/*	jQuery('p.image a').fancybox({
		'overlayShow'	: true,
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000',
		'titleShow'	: 'false',
		'titlePosition' : 'inside',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	}).css({ cursor: "url(template/madcn/img/zoomin.cur), pointer" });*/

	jQuery(".info a[href$='.jpg'], .info a[href$='.jpeg'], .info a[href$='.png'], .info a[href$='.gif'], .info a[href$='.JPG'], .info a[href$='.JPEG'], .info a[href$='.PNG'], .info a[href$='.GIF']").fancybox({
		'overlayShow'	: true,
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000',
		'titleShow'			: true,
		'cyclic'			: true,
		'titlePosition' : 'inside',
        'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) { 
									if(currentArray.length > 1)
										return (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '   ' + title : '');
							  },
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	}).css({ cursor: "url(template/madcn/img/zoomin.png), pointer" }).each (function(i){
		var galleriesid = $(this).parents(".entry").attr("id");
		$(this).attr("rel", galleriesid);
	});
	jQuery('.l_plus').fancybox({
		'type'				: 'iframe',
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'overlayShow'	: true,
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000',
		'titleShow'	: 'false',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});

	var vhost = location.hostname;
	var externalurl = jQuery(".info a[href^='http://']:not([href*='" + vhost + "'])[rel$=nofollow], .info a[href^='https://']:not([href*='" + vhost + "'])[rel$=nofollow]");
	externalurl.each(function() {
		if(!jQuery(this).children().is('img')) {
			jQuery(this).prepend("<img src=http://www.google.com/s2/favicons?domain="+this.href.replace(/^(http:\/\/[^\/]+).*$/, '$1').replace( 'http://', '' )+" class=gfavicons />");
		}

		$(this).powerFloat({
			//给后面加上 .jpg ，令插件认同此为图片。
			target: '<img class="float_ajax_image" src="http://open.thumbshots.org/image.aspx?url=' + $(this).attr("href") + '.jpg" width="120" height="90"><p class="float_title">' + $(this).attr("title") + '</p>',
			targetMode: "remind",
			offsets: {x:20, y:-5}
		});
	});
	jQuery('#web_app a').each(function() {
		$(this).powerFloat({
			//给后面加上 .jpg ，令插件认同此为图片。
			target: '<img class="float_ajax_image" src="http://open.thumbshots.org/image.aspx?url=' + $(this).attr("href") + '.jpg" width="120" height="90"><p class="float_title">' + $(this).attr("title") + '</p>',
			targetMode: "remind",
			offsets: {x:-20, y:-5}
		}).removeAttr('title');
	});
		

//隐藏或者隐藏层

	jQuery(".text_mail").css("display", "none");
	jQuery(".text_qq").css("display", "none");
	jQuery(".text_web").css("display", "none");

    jQuery(".visitor_email_s").toggle(function() {
		jQuery(".text_mail").css("display", "inline");
		jQuery(this).next(".text_mail").focus();
    }, function() {
		jQuery(".text_mail").css("display", "none");
    });
	 jQuery(".visitor_qq_s").toggle(function() {
		jQuery(".text_qq").css("display", "inline");
		jQuery(this).next(".text_qq").focus();
    }, function() {
		jQuery(".text_qq").css("display", "none");
    });
	  jQuery(".visitor_web_s").toggle(function() {
		jQuery(".text_web").css("display", "inline");
		jQuery(this).next(".text_web").focus();
    }, function() {
		jQuery(".text_web").css("display", "none");
    });
	
	jQuery(".visitor_email_s, .visitor_qq_s, .visitor_web_s").mouseover(function() {
		jQuery(this).css("cursor", "pointer");
    });
	
	jQuery(".text_name").focus(function() {
		if(jQuery(this).val()=='无名氏')jQuery(this).attr("value", "");
    });
	jQuery(".text_name").blur(function() {
		if(jQuery(this).val()=='')jQuery(this).attr("value", "无名氏");
    });
	jQuery(".text_name").change(function() {
		jQuery(".text_name").attr("value", jQuery(this).val());
    });
	jQuery(".text_mail").change(function() {
		jQuery(".text_mail").attr("value", jQuery(this).val());
    });
	jQuery(".text_qq").change(function() {
		jQuery(".text_qq").attr("value", jQuery(this).val());
    });
	jQuery(".text_web").change(function() {
		jQuery(".text_web").attr("value", jQuery(this).val());
    });
	
//	jQuery(".text_name, .text_mail, .text_qq, .text_web").focus(function() {
//		$(this).css("border-bottom", "1px #FF9F40 solid");
//    });
//	jQuery(".text_name, .text_mail, .text_qq, .text_web").blur(function() {
//		$(this).css("border-bottom", "1px #CCC solid");
//    });

	
	
	jQuery("#t_con").focus(function() {
		if(jQuery(this).val()=='多个搜索关键词用空格隔开')jQuery(this).attr("value", "");
    });
	jQuery("#t_con").blur(function() {
		if(jQuery(this).val()=='')jQuery(this).attr("value", "多个搜索关键词用空格隔开");
    });

//收起过长的quote框，并可通过点击展开
	var quote_c = 1;
	if( location.search.indexOf('?') == 0 ) {
		if( location.search.split('?')[1].split('&')[0].split('=')[1] == 'view' ) quote_c = 0;
	}
	if( quote_c == 1 ) {
		jQuery(".quote").each(function(){
			if (jQuery(this).height()>50) {//单行文字用 17
				jQuery(this).addClass('quote_min');
				jQuery(this).attr('title', '内容并未显示完全，点击展开详细内容');
			}
		});
		jQuery('.quote_min').click(function () {
				jQuery(this).removeClass('quote_min');
				jQuery(this).removeAttr('title');
		});
	}

//控制文内图片大小
	jQuery(".info img").each(function(){
		if( jQuery(this).width() > 500){
			jQuery(this).width(500);
		}
//		if( $(this).height() >= 300){
//			$(this).height(300);
//		}
	});
	
//插入文本框指定位置 开始
(function($){ 
$.fn.extend({ 
insertAtCaret: function(myValue){ 
var $t=$(this)[0]; 
if (document.selection) { 
this.focus(); 
sel = document.selection.createRange(); 
sel.text = myValue; 
this.focus(); 
} 
else 
if ($t.selectionStart || $t.selectionStart == '0') { 
var startPos = $t.selectionStart; 
var endPos = $t.selectionEnd; 
var scrollTop = $t.scrollTop; 
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length); 
this.focus(); 
$t.selectionStart = startPos + myValue.length; 
$t.selectionEnd = startPos + myValue.length; 
$t.scrollTop = scrollTop; 
} 
else { 
this.value += myValue; 
this.focus(); 
} 
} 
}) 
})(jQuery); 
//插入文本框指定位置 结束

//div随页面滚动 开始
	var offset = jQuery("#tab").offset();
	var topPadding = 15;
	jQuery(window).scroll(function() {
		if (jQuery(window).scrollTop() > offset.top) {
			jQuery("#tab").stop().animate({
				marginTop: jQuery(window).scrollTop() - offset.top + topPadding
			});
		} else {
			jQuery("#tab").stop().animate({
				marginTop: 0
			});
		};
	});
//div随页面滚动 结束


 	jQuery(".flashbox").fancybox({
		'type'				: 'iframe',
		'width'				: '80%',
		'height'			: '80%',
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'overlayShow'	: true,
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000',
		'titleShow'	: 'false',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});


  	jQuery("#jiathis").fancybox({
		'type'				: 'iframe',
		//'width'				: '70',
		//'height'			: '75',
		//'autoDimensions'	: false,
		'overlayShow'	: true,
		'overlayOpacity'	: 0.7,
		'overlayColor'		: '#000',
		'titleShow'	: 'false',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	
	

	jQuery(".reply_control").hide();
 	jQuery(".detail_reply").mouseover( function(){
		jQuery(this).children(".reply_control").show();
	} );
 	jQuery(".detail_reply").mouseout( function(){
		jQuery(this).children(".reply_control").hide();
	} );
	
	jQuery(".marktip").hide();
 	jQuery(".entry-meta-t").mouseover( function(){
		jQuery(this).find(".marktip").show();
	} );
 	jQuery(".entry-meta-t").mouseout( function(){
		jQuery(this).find(".marktip").hide();
	} );
	
	
//设置文章标记 开始
	(function($){ 
		$.fn.extend({ 
			entrymark: function(entryid){ 
				$.post(BASE_URL + 'httprequest.php?act=markfav', 'id=' + entryid, function (data) {
					if (data == '1') {
						$('#markfav_' + entryid).css({'color': '#FFA60B', 'font-size': '15px'});
					} else {
						$('#markfav_' + entryid).css({'color': '#8E8E8E', 'font-size': '15px'});
					}
				});
			}
		}) 
	})(jQuery); 
//设置文章标记 结束
//文章投票 开始
	(function($){ 
		$.fn.extend({ 
			evote: function(entryid, vote){ 
				$.post(BASE_URL + 'httprequest.php?act=evote', { 'id': entryid, 'vote': vote }, function (data) {
					var obj = jQuery.parseJSON( data );
					if (obj.vote == '1') {
						$('#evote_' + entryid + ' small:first').html('(' + obj.count + ')');
						$('#evote_' + entryid + ' span:first').css({'color': '#16D214', 'font-size': '15px'}).unwrap();
						$('#evote_' + entryid + ' span:last').css({'color': '#929B9A', 'font-size': '10px'}).unwrap();
					} else {
						$('#evote_' + entryid + ' small:last').html('(' + obj.count + ')');
						$('#evote_' + entryid + ' span:last').css({'color': '#FF3B3B', 'font-size': '15px'}).unwrap();
						$('#evote_' + entryid + ' span:first').css({'color': '#929B9A', 'font-size': '10px'}).unwrap();
					}
					$('#evote_' + entryid).attr("title","你对此消息投过票了").addClass('like');
				});
			}
		}) 
	})(jQuery); 
//文章投票 结束


	$('.input_message').xheditor({
		tools:'Cut,Copy,Paste,Pastetext,|,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,|,Align,List,Outdent,Indent,Link,Unlink,Img,Flash,|,Source,Preview,Fullscreen,About',skin:'default',
		showBlocktag:true,internalScript:false,internalStyle:false,width:620,height:200,forcePtag:false,
		shortcuts:{'ctrl+enter':function(){$('#entry_post').submit();}}
	});
	
	
	$("#posta").toggle(
		function () {
			$('#message').xheditor({
				tools:'Pastetext,|,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,SelectAll,Removeformat,|,Outdent,Indent,Link,Unlink,Img,Flash,|,Source,Preview,Fullscreen,About',skin:'default',
				showBlocktag:true,internalScript:false,internalStyle:false,width:620,height:200,sourceMode:true,forcePtag:false,
				shortcuts:{'ctrl+enter':function(){$('#entry_post').submit();}},
				beforeSetSource:ubb2html,beforeGetSource:html2ubb
			});
		},
	function () {
			$('#message').xheditor(false);
		}
	);
	
	
/* 		$('.ecard1').powerFloat({
			width: "120px",
			target: ["<a href='?act=u&amp;id=1'>只看孙老四在说啥</a>", "<a href='http://www.madcn.com/p/'>孙老四的西洋镜儿</a>", "<a href='http://www.madcn.com/'>狂人秀吧</a>", "<a href='##'>更多 >></a>"],
			targetMode: "list",
		}); */


	$('#message').bind('mousedown mouseup keydown keyup change keypress', function () {
		$('#message_counter').html( parseInt($(this).val().length) );
	});
	
	$('.content[id^="reply_message_"]').bind('mousedown mouseup keydown keyup change keypress', function () {
		$(this).siblings('.postCounterTips').children('span').html( 256 - parseInt($(this).val().length) );
	});


});
