$(function(){
    $.extend({
        showDialog:function(loadPage,loadingimg){
            $("body").append("<div id='bgDiv'></div><div id='loadbgDiv' style='position:absolute !important; position:absolute; z-index:1001; text-align:center;top:40%; display:none;'></div>");

			dHeight = $(document).height()+"px";
			dWidth = $(document).width();
			dWidth = (dWidth-600)/2;
			$("#bgDiv").css({'position':'absolute','left':'0','top':'0px','z-index':'1000','background':'#000000','filter':'alpha(opacity=40)','opacity':'0.4','width':'100%','height':dHeight});
            $("#loadbgDiv").css("left",dWidth);
			if($.browser.msie){
				$("#loadbgDiv").css("top",document.body.scrollTop+150);
			}else{
				$("#loadbgDiv").css("top",$(document).height()-document.body.scrollHeight+150);
			}
			$.ajax({
                url:loadPage,
                beforeSend:function(xmlhttprequest){
                    $("#loadbgDiv").append("loading...");
                },
                success:function(data,status){
                    $("#loadbgDiv").load(loadPage);
					$("#loadbgDiv").fadeIn(1500);

                },
                error:function(xmlhttprequest,error){
                    $("#loadbgDiv").html(error);
                }
            });
        },
        hideDialog:function()
        {
            $("#loadbgDiv").fadeOut(1500,function(){
				$("#bgDiv").remove();
			});
        }
    });
});

$(function(){
    $.extend({
        showDialog:function(loadPage,loadingimg){
            $("body").append("<div id='bgDiv'></div><div id='loadbgDiv' style='position:absolute !important; position:absolute; z-index:1001; text-align:center;top:40%; display:none;'></div>");

			dHeight = $(document).height()+"px";
			dWidth = $(document).width();
			dWidth = (dWidth-600)/2;
			$("#bgDiv").css({'position':'absolute','left':'0','top':'0px','z-index':'1000','background':'#000000','filter':'alpha(opacity=40)','opacity':'0.4','width':'100%','height':dHeight});
            $("#loadbgDiv").css("left",dWidth);
			if($.browser.msie){
				$("#loadbgDiv").css("top",document.body.scrollTop+150);
			}else{
				$("#loadbgDiv").css("top",$(document).height()-document.body.scrollHeight+150);
			}
			$.ajax({
                url:loadPage,
                beforeSend:function(xmlhttprequest){
                    $("#loadbgDiv").append("loading...");
                },
                success:function(data,status){
                    $("#loadbgDiv").load(loadPage);
					$("#loadbgDiv").fadeIn(1500);

                },
                error:function(xmlhttprequest,error){
                    $("#loadbgDiv").html(error);
                }
            });
        },
        hideDialog:function()
        {
            $("#loadbgDiv").fadeOut(1500,function(){
				$("#bgDiv").remove();
			});
        }
    });
});

$(function(){
    $.extend({
        showFlash:function(){
			loadPage = "includes/preroll.php?random="+Math.random();
            $("body").append("<div id='bgDiv'></div><div id='loadbgDiv' style='position:absolute !important; position:absolute; z-index:1001; text-align:center;top:40%; display:none;'></div>");

			dHeight = $(document).height()+"px";
			dWidth = $(document).width();
			dWidth = (dWidth-800)/2;
			$("#bgDiv").css({'position':'absolute','left':'0','top':'0px','z-index':'1000','background':'#000000','filter':'alpha(opacity=40)','opacity':'0.4','width':'100%','height':dHeight});
            $("#loadbgDiv").css("left",dWidth);
			if($.browser.msie){
				$("#loadbgDiv").css("top","100px");
			}else{
				$("#loadbgDiv").css("top","100px");
			}
			//alert($(document).height());
			/*$.ajax({
                url:loadPage,
				cache:false,
                beforeSend:function(xmlhttprequest){
                    $("#loadbgDiv").append("loading...");
                },
                success:function(data,status){
                    $("#loadbgDiv").load(loadPage);
					$("#loadbgDiv").fadeIn(1500);

                },
                error:function(xmlhttprequest,error){
                    $("#loadbgDiv").html(error);
                }
            });*/

			$("#loadbgDiv").load(loadPage);
			$("#loadbgDiv").fadeIn(1500);
        },
        hideFlash:function()
        {
            $("#loadbgDiv").fadeOut(1500,function(){
				$("#bgDiv").remove();
			});
        }
    });
});

function cookie(name, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            var path = options.path ? '; path=' + options.path : '';
            var domain = options.domain ? '; domain=' + options.domain : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
};


JQ = $;
function add_favorite(obj,type_id,refid) {
	var x = obj.offsetLeft;
	var y = obj.offsetTop;
	while(obj=obj.offsetParent)
	{
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}

	JQ.ajax({
			url : "includes/ajax.php",
			type : "get",
			data : "op=addfavorite&type="+ type_id +"&refid="+refid,
			cache : false,
			dataType : "html",
			success : function(msg){
				if(msg == "true")
				{
					JQ("#common-tip").html("Add success!");
					y = y + 20;
					JQ("#common-tip").css("left",x);
					JQ("#common-tip").css("top",y);
					JQ("#common-tip").show();
					setTimeout('close_common_tip()',2000);
				}else
				{
					alert(msg);
				}
			}
		})
}

function close_common_tip()
{
	JQ("#common-tip").hide();
}


function set_char_list()
{
	ret = "";
	for(i=_charstart;i<_charstart+5;i++)
	{
		ret += "<a href=\""+_charurl+String.fromCharCode(i)+"\">"+String.fromCharCode(i)+"</a>"
	}
	$("#charlist").html(ret);
}

function prev_char()
{
	if(_charstart>65){
		_charstart--;
	}else{
		return;
	}
	set_char_list();
}

function next_char()
{
	if(_charstart<86){
		_charstart++;
		//alert(_charstart);
	}else{
		return;
	}
	set_char_list();
}

function show_search_type(obj)
{
	obj.value = "";
	var x = obj.offsetLeft;
	var y = obj.offsetTop;
	while(obj=obj.offsetParent)
	{
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}
	x += 60;
	y += 22;
	$("#favorite-tip").css("left",x);
	if($.browser.msie)
	{
		$("#favorite-tip").css("top",y+5);
	}
	else
	{
		$("#favorite-tip").css("top",y);
	}
	$("#favorite-tip").show();
}

function close_search_tip()
{
	$("#favorite-tip").hide();
}

function view_note_all(noteid)
{
	$.ajax({
			url : "includes/ajax.php",
			type : "get",
			data : "op=getnote&noteid="+ noteid,
			cache : false,
			dataType : "html",
			success : function(msg){
				if(msg.length>0)
				{
					$("#note_"+noteid).html(msg);
				}else
				{
					alert("Error");
				}
			}
		})
}

function show_profile()
{
	$("#updateform").show();
}
var up_username_check = true;
var up_email_check = true;
var up_seccode_check = false;
function up_check_username(obj)
{
	if(obj.value != ""){
		$.ajax({
			url : "includes/ajax.php",
			type : "get",
			data : "op=checkuser&action=username&username="+obj.value,
			cache : false,
			dataType : "html",
			success : function(msg){
				if(msg == "false")
				{
					$("#username-error").html("");
					up_username_check = true;
					return;
				}else
				{
					$("#username-error").html("***Username already exists!***");
					up_username_check = false;
				}
			}
		})
	} else
	{
		up_username_check = true;
	}
}

function up_check_email(obj)
{
	if(obj.value != ""){
		$.ajax({
			url : "includes/ajax.php",
			type : "get",
			data : "op=checkuser&action=email&email="+obj.value,
			cache : false,
			dataType : "html",
			success : function(msg){
				if(msg == "false")
				{
					$("#email-error").html("");
					up_email_check = true;
					return;
				}else
				{
					$("#email-error").html("***Email already exists!***");
					up_email_check = false;
				}
			}
		})
	}else
	{
		up_email_check = true;
	}
}

function up_check_seccode(obj)
{
	$.ajax({
		url : "includes/ajax.php",
		type : "get",
		data : "op=validatecode&action=register&code="+obj.value,
		cache : false,
		async : false,
		dataType : "html",
		success : function(msg){
			if(msg == "true")
			{
				$("#seccode-error").html("");
				up_seccode_check = true;
				return;
			}else
			{
				$("#seccode-error").html("***Validatecode is incorrect!***");
				up_seccode_check = false;
			}
		}
	})
}

function update_userinfo()
{

	if(!up_username_check)
	{
		alert("Username is incorrect!");
		return;
	}
	if(!up_email_check)
	{
		alert("Password is incorrect!");
		return;
	}
	if($("#password").attr("value") != $("#repassword").attr("value"))
	{

		alert("Resubmit Password is not equal to Password");
		return;
	}

	if(!up_seccode_check)
	{
		alert("Validatecode is incorrect!")
		return;
	}
	document.forms['upform'].submit();
}