jQuery.extend(weibo.plugin, {
	image:function(element, options){
	   
	    
	}
});

var stopUploadPic = 0;
jQuery.extend(weibo.plugin.image, {
	html:'<input type="text" style="width:230px;height:20px;" id="online_img" value="" name="online_img">'+
	'<input type="button" style="width:130px;" value="采集" id="shangchuan" onclick="weibo.plugin.image.upload1()">'+
	'<div id="upload_selectpic"><div class="btn_green2">'+
	'<form action="'+U("weibo/plugins/before_publish")+'" enctype="multipart/form-data" method="post" id="uploadpic">'+
	'<input type="hidden" name="plugin_id" value="1"><a class="gp_btn_c"><input type="file" hidefoucs="true" name="pic" onchange="weibo.plugin.image.upload(this)"></a>'+
	'</form></div></div><div class="alC pt10 pb10 f14px" id="upload_loading" style="display:none"><img src="'+ _THEME_+'/images/icon_waiting.gif" width="20" class="alM"> 正在上传中...<br /><a class="btn_w mt10" href="javascript:void(0)" onclick="$(\'div .talkPop\').remove();weibo.plugin.image.stopAjax();">取消上传</a></div>',
	click:function(options){
		if (1 != $('div.talkPop').data('type')) {
			weibo.publish_type_box(1,this.html,options);
		}
	},
	upload1:function(){
		$("#shangchuan").val('获取中...');
               
                
		var img_url=$("#online_img").val();
		$.post( U("weibo/plugins/before_publish_input") ,{imgurl:img_url,plugin_id:1},function(txt){
			 txt = eval( '(' + txt + ')' );
			      if(txt.boolen==1){
						var img = new Image;
						img.src = txt.picurl;
						img.onload = function(){
							if( this.width>190 || this.height>190 ){
								var style;
								if( this.height >  this.width ){
									style = "height:190px;width:"+this.width*(190/this.height)+"px";
								}else{
									style = "width:190px;height:"+this.height*(190/this.width)+"px";
								}
								
								var html = '<div class="indeximg"><img src="'+txt.picurl+'" style="'+style+'"><input id="pickpic" name="publish_type_data" type="hidden" style="width:86%" value='+txt.type_data+' /></div>'+
								'<div class="mymargin"><input type="text" style="width:230px;height:20px;" id="online_img" value="http://" name="online_img">&nbsp;&nbsp;'+
	'<input type="button" class="btn_b" value="采集" id="shangchuan" onclick="weibo.plugin.image.upload1()"></div>';
								 
							}else{
								var html = '<div class="indeximg"><img src="'+txt.picurl+'"><input id="pickpic" name="publish_type_data" type="hidden" style="width:86%" value='+txt.type_data+' /></div>'+
								'<div class="mymargin"><input type="text" style="width:230px;height:20px;" id="online_img" value="http://" name="online_img">&nbsp;&nbsp;'+
	'<input type="button" class="btn_b" value="采集" id="shangchuan" onclick="weibo.plugin.image.upload1()"></div>';
								}
							if($('#content_publish').val()==''){
								$('#content_publish').val('');
							}
							$("#publish_type_content").html( html );
							$('div.talkPop').data('type', 1);
							$('#upload_loading').hide();
							$("#shangchuan").val('采集');
							$("#online_img").val('http://');
                                                        $('#online_img').show();
                                                        $('#shangchuan').show();
							$('#upload_selectpic').hide();
							weibo.checkInputLength('#content_publish',140);
						};
								
				  }else{
					alert( txt.message );
						$("#shangchuan").val('重新采集');
			      }
		});
	},
	upload:function(o){
		var allowext = ['jpg','jpeg','gif','png'];
		var ext = /\.[^\.]+$/.exec( $(o).val() );
		ext = ext.toString().replace('.','');
		if( jQuery.inArray( ext.toLowerCase() , allowext )==-1 ){
			alert('只允许上传jpg、jpeg、gif、png格式的图片');
			return false;
		}
		weibo.textareaStatus('off');
		$('#upload_selectpic').hide();
		$('#upload_loading').show();
		$('#weibo_close_handle').hide();
		var options = {
			    success: function(txt) {
				if(stopUploadPic==1){
					return false;
				}
			      txt = eval( '(' + txt + ')' );
			      if(txt.boolen==1){
						var img = new Image;
						img.src = txt.picurl;
						img.onload = function(){
							if( this.width>190 || this.height>190 ){
								var style;
								if( this.height >  this.width ){
									style = "height:190px;width:"+this.width*(190/this.height)+"px";
								}else{
									style = "width:190px;height:"+this.height*(190/this.width)+"px";
								}
								
							var html = '<div class="indeximg"><img src="'+txt.picurl+'" style="'+style+'"><input id="pickpic" name="publish_type_data" type="hidden" style="width:86%" value='+txt.type_data+' /></div>'+
								'<div id="upload_selectpic"><div class="btn_green2" >'+
	'<form action="'+U("weibo/plugins/before_publish")+'" enctype="multipart/form-data" method="post" id="uploadpic">'+
	'<input type="hidden" name="plugin_id" value="1"><a class="gp_btn_c"><input type="file" hidefoucs="true" name="pic" onchange="weibo.plugin.image.upload(this)"></a>'+
	'</form></div></div><div class="alC pt10 pb10 f14px" id="upload_loading" style="display:none"><img src="'+ _THEME_+'/images/icon_waiting.gif" width="20" class="alM"> 正在上传中...<br /><a class="btn_w mt10" href="javascript:void(0)" onclick="$(\'div .talkPop\').remove();weibo.plugin.image.stopAjax();">取消上传</a></div>';
								 
							}else{
								var html = '<div class="indeximg"><img src="'+txt.picurl+'"><input id="pickpic" name="publish_type_data" type="hidden" style="width:86%" value='+txt.type_data+' /></div>'+
								'<div id="upload_selectpic"><div class="btn_green2" href="javascript:void(0);" >'+
	'<form action="'+U("weibo/plugins/before_publish")+'" enctype="multipart/form-data" method="post" id="uploadpic">'+
	'<input type="hidden" name="plugin_id" value="1"><a class="gp_btn_c"><input type="file" hidefoucs="true" name="pic" onchange="weibo.plugin.image.upload(this)"></a>'+
	'</form></div><div class="alC pt10 pb10 f14px" id="upload_loading" style="display:none"><img src="'+ _THEME_+'/images/icon_waiting.gif" width="20" class="alM"> 正在上传中...<br /><a class="btn_w mt10" href="javascript:void(0)" onclick="$(\'div .talkPop\').remove();weibo.plugin.image.stopAjax();">取消上传</a></div>';			
								
								}
							if($('#content_publish').val()==''){
								$('#content_publish').val('');
							}
							$("#publish_type_content").html(html );
							$('#upload_loading').hide();
							$('#upload_selectpic').show();
						
							weibo.checkInputLength('#content_publish',140);
						};
								
				  }else{
					alert( txt.message );
                                        
                                        $('#upload_selectpic').show();
                                        
                                         
			      }
			    } 
			};
//			$('#publish_type_content').html('<div style="width:400px;text-align:center;height:150px;"><img src="__THEME__/images/icon_waiting.gif" width="30"></div>');
			var httpRespondHandle = $('#uploadpic').ajaxSubmit( options );
		    return false;
	},
	stopAjax:function(){
		stopUploadPic=1;
	}

});

//切换图片
function switchPic(id,type,picurl){
	if( type=='close' ){
		$("#pic_show_"+id).hide();
		$("#pic_mini_show_"+id).show();
	}else{
		
		if( $("#pic_show_"+id).find('.imgSmall').attr('src')==''){
			$("#pic_mini_show_"+id).find('.loadimg').show();
			var img = new Image;
			img.src = picurl+'?time='+new Date();
			img.onload = function(){
				if( this.width>460 ){
					$("#pic_show_"+id).find('.imgSmall').css('width','460px');
				}
				$("#pic_show_"+id).find('.imgSmall').attr('src',this.src);
				$("#pic_mini_show_"+id).find('.loadimg').hide();
				$("#pic_show_"+id).show();
				$("#pic_mini_show_"+id).hide();	
			};
		}else{
			$("#pic_show_"+id).show();
			$("#pic_mini_show_"+id).hide();	
		}
	}
}

//旋转图片
function revolving(id,type){
	var img = $("#pic_show_"+id).find('.imgSmall');
	img.rotate(type);
}


$.fn.rotate = function(p){

	var img = $(this)[0],
		n = img.getAttribute('step');
	// 保存图片大小数据
	if (!this.data('width') && !$(this).data('height')) {
		this.data('width', img.width);
		this.data('height', img.height);
	};
	this.data('maxWidth',img.getAttribute('maxWidth'))

	if(n == null) n = 0;
	if(p == 'left'){
		(n == 0)? n = 3 : n--;
	}else if(p == 'right'){
		(n == 3) ? n = 0 : n++;
	};
	img.setAttribute('step', n);

	// IE浏览器使用滤镜旋转
	if(document.all) {
		if(this.data('height')>this.data('maxWidth') && (n==1 || n==3) ){
			if(!this.data('zoomheight')){
				this.data('zoomwidth',this.data('maxWidth'));
				this.data('zoomheight',(this.data('maxWidth')/this.data('height'))*this.data('width'));
			}
			img.height = this.data('zoomwidth');
			img.width  = this.data('zoomheight');
			
		}else{
			img.height = this.data('height');
			img.width  = this.data('width');
		}
		
		img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation='+ n +')';
		// IE8高度设置
		if ($.browser.version == 8) {
			switch(n){
				case 0:
					this.parent().height('');
					//this.height(this.data('height'));
					break;
				case 1:
					this.parent().height(this.data('width') + 10);
					//this.height(this.data('width'));
					break;
				case 2:
					this.parent().height('');
					//this.height(this.data('height'));
					break;
				case 3:
					this.parent().height(this.data('width') + 10);
					//this.height(this.data('width'));
					break;
			};
		};
	// 对现代浏览器写入HTML5的元素进行旋转： canvas
	}else{
		var c = this.next('canvas')[0];
		if(this.next('canvas').length == 0){
			this.css({'visibility': 'hidden', 'position': 'absolute'});
			c = document.createElement('canvas');
			c.setAttribute('class', 'maxImg canvas');
			img.parentNode.appendChild(c);
		}
		var canvasContext = c.getContext('2d');
		switch(n) {
			default :
			case 0 :
				img.setAttribute('height',this.data('height'));
				img.setAttribute('width',this.data('width'));
				c.setAttribute('width', img.width);
				c.setAttribute('height', img.height);
				canvasContext.rotate(0 * Math.PI / 180);
				canvasContext.drawImage(img, 0, 0);
				break;
			case 1 :
				if(img.height>this.data('maxWidth') ){
					h = this.data('maxWidth');
					w = (this.data('maxWidth')/img.height)*img.width;
				}else{
					h = this.data('height');
					w = this.data('width');
				}
				c.setAttribute('width', h);
				c.setAttribute('height', w);
				canvasContext.rotate(90 * Math.PI / 180);
				canvasContext.drawImage(img, 0, -h, w ,h );
				break;
			case 2 :
				img.setAttribute('height',this.data('height'));
				img.setAttribute('width',this.data('width'));
				c.setAttribute('width', img.width);
				c.setAttribute('height', img.height);
				canvasContext.rotate(180 * Math.PI / 180);
				canvasContext.drawImage(img, -img.width, -img.height);
				break;
			case 3 :
				if(img.height>this.data('maxWidth') ){
					h = this.data('maxWidth');
					w = (this.data('maxWidth')/img.height)*img.width;
				}else{
					h = this.data('height');
					w = this.data('width');
				}
				c.setAttribute('width', h);
				c.setAttribute('height', w);
				canvasContext.rotate(270 * Math.PI / 180);
				canvasContext.drawImage(img, -w, 0,w,h);
				break;
		};
	};
};