$(function () {
	var is_ie = $.browser.msie;
	$('.sidebar li>a,.sidebar dl a').tipsy({gravity: 'e'});
	$('.articleflow a').tipsy({gravity: 's'});
	$('.ad a').tipsy({gravity: 'e'});
	$('ul.down li a').tipsy({gravity: 'w'});

	$('#donation').mouseover(function(){
		$(this).addClass("inner");
		//$(this).removeClass("outer");
	}).mouseout(function(){
		//$(this).addClass("outer");
		$(this).removeClass("inner");
	}).tipsy({gravity: 'e'});
	
	$('#proc').bind("ajaxSend", function(){
		getcenter('#proc');
		$(this).html(" connecting ……");
		$(this).show();
	}).bind("ajaxComplete", function(){
		getcenter('#proc');
		$(this).html('handle end ……').show().fadeOut(1000);
	});
});
var onalert = null;



function getcenter(e){
	size = getPageSize();
	pos = getScrollTop();
	vleft = parseInt((size[0] - $(e).width())/2 + pos[0]);
	vtop = parseInt((size[1] - $(e).height())/2 + pos[1]);
	$(e).css({left:vleft + 'px',top:vtop + 'px'});
}
function getScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (typeof(document.documentElement) != "undefined" && typeof(document.documentElement.scrollTop) != "undefined" || typeof(document.documentElement.scrollLeft) != "undefined" ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (typeof(document.body) != "undefined") {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop);
	return arrayPageScroll;
}
function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = new Array(w,h);
	return arrayPageSize;
}

function pmchecknew() {
	$('#pmnotice').load(Rurl+'/pm/?checknewpm=' + Math.random());
}

function dheader(url,time){
	time = time ? time*1000 : 0;
	setTimeout("window.location.href = '" + url + "'; ",time);
}
function derror(data,text){
	text = '错误信息';
	$.prompt(data,{
		title: '<img src="'+IMGDIR+'/window/error.png" /> '+text
	});
}
function _input(e,id){
	return id ? $('input[name="'+ e +'"]',$('#'+id)) : $('input[name="'+ e +'"]');
}
function checkall(e,className){
	$('.checkbox',$('#'+className)).attr('checked',$(e).attr('checked'));
}
function changedisplay(){
	$.post(Rurl+'/user/display/',function(data){
		location.reload();
	});
}
function redirect(url) {
	window.location.replace(url);
}


/* *
 * 添加商品到收藏夹
 */
function addfavorite(id)
{
	$.post(Rurl+'/user/favorite/',{id:id,'favoritesubmit':'yes'},function(data){
		$.prompt(data,{
			buttons:{},
			title: prompt_title('user','Message')
		});
	});
}
function report(obj,id) {
    var div = getDownDiv(obj,'600',380);
	$.post(Rurl+'/user/report/',{id:id},function(data){
        $('#append_parent').html(div.html(data));
	});
}
function contact_user(uid)
{
	$.post(Rurl+'/user/contact_user/',{id:uid},function(data){
		$.prompt(data,{
			buttons:{},
			title: prompt_title('user','收藏家信息'),
			callback: function(v,m,f){
				if(!v) return false;
			}
		});
	});
}
function show_prompt(id,title,img){
	var e = $('#'+id);
	var data = e.html();
	title = title ? title:'提示信息';
	img = img ? img:'user';
	$.prompt(data,{
		buttons:{'关闭':false},
		title: prompt_title(img,title)
	});
}
var btn_cancel = {取消:false};
var btn_submit = {提交:true, 取消:false};

function addgoods(formID)
{
	if(!checkform()){return false;}
	var imgNum = _input('gallery[]','galleryPreview').length;
	var stick = $('input[name="stick"]:checked').val();
	var publish = $('input[name="publish"]:checked').val();
	$.get(Rurl+'/user/addgoods/',{imgNum:imgNum,stick:stick,publish:publish,t:new Date()},function(data){
		$.prompt(data,{
			buttons:{},
			title: prompt_title('user','提交清单'),
			callback: function(v,m,f){
				if(v){
					$('#'+formID).submit();
				}
			}
		});
	});
}
/* stick publish sale*/
function clicksubmit(id,action){
	$.post(Rurl+'/user/'+action+'/',{id:id},function(data){
		$.prompt(data,{
			buttons:{},
			title: prompt_title('user','提交清单')
		});
	});
}


function prompt_timeout(text,time){
	if(!time){
		time = 1;
	}
	$.prompt(text,{
		buttons:{},
		loaded:function(){
			setTimeout(function() {
				$.prompt.close();
			}, time*1000);
		}
	});
}

function confirm_submit(msg,formID){
	var target_items = $('input:checked',$('#'+formID));
	if(target_items.length == 0){
		alert('0 item!');
		return false;
	}
	$.prompt(msg,{
		buttons:{Submit:true, Cancel:false},
		callback: function(v,m,f){
			if(v){
				$('#' + formID).submit();
			}
		}
	});
}
function submitcheck(formId,action){
	var target_items = $('input:checked',$('#'+formId));
	if(target_items.length == 0){
		alert('none item!');
		return false;
	}
	var target_Array = target_items.serializeArray();
    var url = $('#'+formId).attr('action');
	$.post(url+'&'+action+'submit=yes',target_Array,function(data){
		$.prompt(data,{
			buttons:{},
			title: prompt_title('user','执行确认？'),
			callback: function(v,m,f){
				if(v){
					
                    $.prompt(data,{title: prompt_title('user','Member CPanel Info'),callback: confirm_reload });
				}
			}
		});
	});
}



function getDownDiv(obj,divwidth,movepos,id){
	movepos = movepos?movepos:0;
	var div = $(document.createElement('div'));
	var offset = $(obj).offset();
	id = id ? id : 'movablelayer';
	div.attr('id',id);
	divwidth && div.css('width',divwidth+'px');
	div.css({'left':offset.left - movepos + 'px',
		'top':offset.top + obj.offsetHeight + 4 + 'px'});
	return div;
}
function copylink()
{
	var href = document.location.href;
	clipboardData.setData('Text', href);
	alert(href + " copied to Clipboard");
}
function pmwin(action, param , script) {
	if(script == null){
		script = 'pm';
	}
	/*if(uid == 0){
		alert('please login!');return false;
	}*/
	if(action == 'open') {
		var clientWidth = document.body.clientWidth;
		var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
		var ucwidth = 800;
		var ucheight = clientHeight * 0.9;

		if(!$('#uclayer').is(':visible')) {
			div = $(document.createElement('div'));
			div.attr('id','uclayer');

			div.css(
				{width:ucwidth + 'px',
				height:ucheight + 'px',
				position:'absolute',
				'z-index':'999',
				left:((clientWidth - ucwidth) / 2) + 'px',
				top:((clientHeight - ucheight) / 2 + scrollTop) + 'px'});
	//alert();
			div.show();
			div.html('<div style="width: 800px; background: #666666; margin: 5px auto; text-align: left">' +
				'<div style="width: 800px; height: ' + ucheight + 'px; padding: 1px; background: #FFFFFF; border: 1px solid #7597B8; position: relative; left: -6px; top: -3px">' +
				'<div onmouseover="pmwindrag(this)" style="cursor: move; position: relative; left: 0px; top: 0px; width: 800px; height: 30px; margin-bottom: -30px;"></div>' +
				'<a href="###" onclick="pmwin(\'close\')" style="position: absolute; right: 20px; top: 15px">[×]</a>' +
				'<div id="pmwinmask" style="margin-top: 30px; position: absolute; width: 100%; height: 100%; display: none"></div><iframe id="pmframe" name="pmframe" style="width:' + ucwidth + 'px;height:100%" allowTransparency="true" frameborder="0"></iframe></div></div>');

			$('#append_parent').html(div);
		}

		if(!param) {
			pmframe.location = Rurl+'/pm/';
		} else {
			pmframe.location = Rurl+'/'+script+ '.php?' + param;
		}
	} else if(action == 'close') {
		$('#uclayer').hide();
	}
}
function pmwindrag(e){
	$.getScript(Rurl+'/includes/javascript/jquery.easydrag.js', function(){
		$("#uclayer").easydrag(true);
	});
}


function Confirm_item(el,action,itemid,txt) {
	$.prompt(txt,{
        title: prompt_title('user','Member CPanel Info'),
		buttons:{Submit:true, Cancel:false},
		callback: function(v,m,f){
			if(v){
                var url = $('#' + el).attr('action');
                items = itemid?{'itemid':itemid}:'';
                $.post(url+'&'+action+'submit=yes',items,function(data){
                    //$.prompt(data,{ callback: mycallbackfunc });
                    //$.prompt(data,{show:'slideDown',title: prompt_title('user','Member CPanel Info'),submit:function(v,m){ v && location.reload();}});

                    $.prompt(data,{ show:'slideDown',title: prompt_title('user','Member CPanel Info'),callback:function(v,m){ 
                        location.reload();
                    }});

                });
			}
		}
	});
}
function confirm_reload(v,m,f) {
    v && location.reload();
}
function SubmitConfirm(msg,formID){
	$.prompt(msg,{
		buttons:{Submit:true, Cancel:false},
		callback: function(v,m,f){
			if(v){
				$('#' + formID).submit();
			}
		}
	});
}