﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
var regEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
var feature_description = [
'<ul><li>到你平时常去的地方报到</li><li>在你周边的地点报到并作虚拟消费可你为你赚取点数</li><li>到不同的地方报到提升你的等级</li><li>快速提升等级让你可以购买更多处房产和提升他们的等级</li></ul>',
'<ul><li>买下你现实生活中最喜欢的地方</li><li>当其他玩家在你的房产报到时向他们收取房租</li><li>升级及你的房产来收取更多的房租</li><li>房产的市值会随着地点的人气上下浮动</li></ul>',
'<ul><li>到不同地点报到可以获得丰富的物品奖励</li><li>物品帮助你在随后的报道中获取更多的点数</li><li>需要钱的时候还可以把物品卖掉套换现金</li></ul>',
'<ul><li>快邀请你的好友来一起玩16fun</li><li>参观好友的房产</li><li>发送邮件、现金和物品给好友</li><li>与好友比一比看谁更有房产投资眼光</li></ul>'];
var feature_img = ['/Content/images/feature_checkin_img.png', '/Content/images/feature_property_img.png', '/Content/images/feature_item_img.png', '/Content/images/feature_community_img.png'];

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

var currentPos;
var current;
var ready = true;

function setup() {
    jQuery.preLoadImages('/Content/images/feature_item_over.jpg', '/Content/images/feature_checkin_over.jpg', '/Content/images/feature_property_over.jpg', '/Content/images/feature_community_over.jpg', '/Content/images/feature_item_img.png', '/Content/images/feature_checkin_img.png', '/Content/images/feature_property_img.png', '/Content/images/feature_community_img.png', '/Content/images/guide_over.jpg', '/Content/images/aboutus_over.jpg', '/Content/images/terms_over.jpg', '/Content/images/privacy_over.jpg', '/Content/images/contactus_over.jpg', '/Content/images/joinus_over.jpg', '/Content/images/login_over.jpg', '/Content/images/register_over.jpg', '/Content/images/reply_over.png', '/Content/images/reply_disable.png', '/Content/images/submit_over.png', '/Content/images/textbox_active.jpg', '/Content/images/textbox_short_active.jpg', '/Content/images/register_over.png', '/Content/images/download_button_over.png', '/Content/images/sinat_over.png', '/Content/images/hiring_over.png');
    //Set indicator points to home
    currentPos = 0;
    //Attach mouseover and mouseout function
    $('#home').mouseover(function() { move_indicator(0); }).mouseout(function() { move_indicator() });
    $('#news').mouseover(function() { move_indicator(1); }).mouseout(function() { move_indicator() });
    $('#feedback').mouseover(function() { move_indicator(2); }).mouseout(function() { move_indicator() });

    $('#username').focus(function() {
        if ($(this).val() == $(this)[0].title) {
            $(this).removeClass("defaultText");
            $(this).val("");
        }
    });

    $('#username').blur(function() {
        if ($(this).val() == "") {
            $(this).addClass("defaultText");
            $(this).val($(this)[0].title);
        }
    });

    $('#passwordText').focus(function() {
        $(this).addClass("hidden");
        $('#password').removeClass('hidden');
        $('#password').focus();
    });

    $('#password').blur(function() {
        if ($(this).val() == "") {
            $('#passwordText').removeClass('hidden');
            $(this).addClass('hidden');
        }
    });
    $('#username').blur();
    UserLogin();
}

function move_indicator(target) {
    var distance;
    var item;
    if (target == undefined) {
        item = $('#mainmenu li')[currentPos];
    } else {
        item = $('#mainmenu li')[target];
    }
    var itemLeft = $(item).attr('offsetLeft');
    var itemWidth = $(item).attr('offsetWidth');
    var indicatorWidth = $('#indicator').attr('offsetWidth');
    var indicator = $('#indicator');
    indicator.stop();
    indicator.animate({ 'left': itemLeft + itemWidth / 2 - indicatorWidth / 2 }, 700, 'easeOutQuint');
}

function setup_home_index() {
    currentPos = 0;
    move_indicator();
    show_feature(0);
    $('#indicator').removeClass('invisible');
    $("#main_wrapper").width("100%");
    $('#checkin').click(function() {
        show_feature(0);
    });
    $('#property').click(function() {
        show_feature(1);
    });
    $('#item').click(function() {
        show_feature(2);
    });
    $('#community').click(function() {
        show_feature(3);
    });
}

function setup_news_index() {
    currentPos = 1;
    move_indicator();
    $('#indicator').removeClass('invisible');
}

function setup_feedback_index() {
    currentPos = 2;
    move_indicator();
    $('#indicator').removeClass('invisible');
}

function show_feature(idx) {
    //Reset all other buttons.
    var items = $('#feature .menu').find('li');
    for (var i = 0; i < items.length; i++) {
        var item = $(items[i]).find('a');
        $(item).css('background-image', $(item).css('background-image').replace('_over.jpg', '.jpg'));
    }
    $($(items[idx]).find('a')).css('background-image', $($(items[idx]).find('a')).css('background-image').replace('.jpg', '_over.jpg'));
    $($('#featureImg').find('img')).attr('src', feature_img[idx]);
}

function show_guide(guideLink, idx) {
    var item = $(guideLink).siblings('div.guideitem_content');

    var items = $(item[idx]).children('p');
    var height = 0;
    for (var i = 0; i < items.length; i++) {
        height += $(items[i]).attr('clientHeight');
    }
    if ($(item[idx]).css('height') == '0px') {
        $(item[idx]).animate({ 'height': height, 'padding-top': 5, 'padding-bottom': 5 }, 500, 'easeOutQuint');
    } else {
        $(item[idx]).animate({ 'height': 0, 'padding-top': 0, 'padding-bottom': 0 }, 500, 'easeOutQuint');
    }
}

var current_pos = 0;
var currentaction = 0;
var defaultpic = "../../Content/images/userpic_large_default.png";
function ScrollTip() {
    var positions = [{ 'left': '160px', 'top': '0px' }, { 'left': '220px', 'top': '60px' }, { 'left': '270px', 'top': '90px' }, { 'left': '300px', 'top': '0px' }, { 'left': '371px', 'top': '85px' }, { 'left': '342px', 'top': '105px' }, { 'left': '475', 'top': '110px' }, { 'left': '531px', 'top': '55px' }, { 'left': '559px', 'top': '113px' }, { 'left': '673px', 'top': '120px'}];
    var IndexJsonlength = IndexJson.length;
    if (currentaction >= IndexJsonlength) {
        currentaction = 0;
    }
    if (current_pos >= positions.length) {
        current_pos = 0;
    }
    if (IndexJsonlength > 0) {
        $("#UserTip").html("");
        $("#UserTip").hide();
        if (current_pos >= 5) {
            $("#UserTip").css("background", "transparent url(../../Content/images/update_right_bg.png) no-repeat scroll center");
        }
        else {
            $("#UserTip").css("background", "transparent url(../../Content/images/update_left_bg.png) no-repeat scroll center");
        }
        $("#UserTip").css("position", "relative");
        $("#UserTip").css("top", positions[current_pos].top).css("left", positions[current_pos].left);
        $("#UserTip").show().html("<div class=\"tipuserphoto\"><a href=\"/User/Profile/" + IndexJson[currentaction].UserID + "\" target=\"_blank\" title=\"" + IndexJson[currentaction].Name + "\"><img src=\"" + (IndexJson[currentaction].UserPhoto.length > 0 ? IndexJson[currentaction].UserPhoto : defaultpic) +
        "\" /></a></div><div class=\"tipbox\"><div><span>" + IndexJson[currentaction].Date + "</span><a href=\"/User/Profile/" + IndexJson[currentaction].UserID + "\" target=\"_blank\" title=\"" + IndexJson[currentaction].Name + "\">" + (IndexJson[currentaction].Name.length > 8 ? IndexJson[currentaction].Name.substr(0, 8) + "..." : IndexJson[currentaction].Name) + "</a></div>" +
        "<div class=\"tipcontent\">" + IndexJson[currentaction].Value + "</div></div>");
    }
    current_pos++;
    currentaction++;
}

function CheckReplyContent(a) {
    var val = $(a).val();
    var length = val.length;
    if (140 - length < 0) {
        $(a).next().css("color", "red");
        $(a).next().html("已经超出" + (length - 140) + "个字符");
    } else {
        $(a).next().html("您还可以输入" + (140 - length) + "个字符");
    }
}

function SendReview(a, loginstate) {
    data = { 'msg': $(a).parent().prev().prev()[0].value };
    if (data['msg'].length == 0) {
        $(a).parent().prev().prev()[0].focus();
        alert('回复没有内容！');
        return false;
    }
    if (loginstate == "False") {
        if ($(".newsnotlogin span:eq(0) input[type=text]:eq(0)").val() == "") {
            $(".newsnotlogin span:eq(0) input[type=text]:eq(0)").focus();
            alert("您没有登录，请填写您的姓名！");
            return false;
        }
    }
    if (data['msg'].length > 140) {
        alert('回复内容最大长度允许为140个字符！');
        return false;
    }
    return true;
}

function UserLogin() {
    var FullUrl = window.location.pathname.toLowerCase();
    var CurrentUrl = FullUrl.substr(FullUrl.lastIndexOf("/") + 1);
    var UserID = 0;
    if (CurrentUrl == "logon") {
        $("#login").css("display", "none");
    } else {
        $.post('/User/AjaxUserLogin', null,
	function(data) {
	    if (data['loginstate']) {
	        $("#login").css("display", "none");
	        $("#LoginSuccess").css("display", "block");
	        $("#LoginSuccess span:eq(1)").css("font-size", "16px").css("color", "#fff").html(data["NickName"]);
	        $("#LoginSuccess span:eq(2) a:eq(0)").attr("href", "/User/Profile/" + data["UserID"]).html("个人资料");
	        $("#LoginSuccess span:eq(2) a:eq(1)").css("margin-left", "20px");
	        UserID = data["UserID"];
	    }
	}, 'json');
    }
    return UserID;
}

function Login() {
    var Email = $("#username").val();
    var Password = $("#password").val();
    if ($("#username").val() == "邮箱" || $("#username").val()=="") {
        $("#username").focus();
        alert("请填写邮箱！");
        return;
    }
    if (Password.length <= 0) {
        alert("请填写密码！");
        $("#passwordText").addClass("hidden");
        $('#password').removeClass('hidden');
        $('#password').focus();
        return;
    }
    var data = { UserEmail: Email, UserPassword: Password };
    $.post('/User/AjaxLogin', data,
	function(data) {
	    if (data['loginstate'] == 1) {
	        var userID = UserLogin();
	        window.location.href = "/User/Profile/" + data['UserID'];
	    } else if (data['loginstate'] == 0) {
	        alert("登录失败！\n该用户不存在！");
	        window.top.location.href = "/User/Logon";
	    } else if (data['loginstate'] == -1) {
	        alert("登录失败！\n密码错误！");
	        window.top.location.href = "/User/Logon";
	    }
	}, 'json');
}
/*------------Show Google static map--------------*/
function ShowMap(Latitude, Longitude) {
    var mapUrl = "http://ditu.google.cn/staticmap?center=" + Latitude + "," + Longitude + "&zoom=14&format=jpg&markers=" + Latitude + "," + Longitude + ",reda&maptype=roadmap&size=480x350&key=ABQIAAAAz_Cf-zIAx6ucu1lfJCVMsRRRLxlmhSKIUJmu0lfuhZs9Pk8FaxTo9W79S9KtX9qjipRspTgb9SpuTg&sensor=false"
    $("#alterbg").css("display", "block").css("height", $(document).height());
    $("#map").html("<img alt=\"map\" src=\"" + mapUrl + "\" />");
    var left = (($(window).width() - $("#mapbox").width()) > 0 ? ($(window).width() - $("#mapbox").width()) : ($("#mapbox").width() - $(window).width())) / 2;
    var top = (($(window).height() - $("#mapbox").height()) > 0 ? ($(window).height() - $("#mapbox").height()) : ($("#mapbox").height() - $(window).scrollTop())) / 2 + document.documentElement.scrollTop;
    $("#mapbox").css("left", left);
    $("#mapbox").css("top", top);
    $("#mapbox").show();
}
function ColseMap() {
    $("#alterbg,#mapbox,#userphotobox").hide();
}

function ShowUserPhoto(userid, userbigphoto) {
    //当用户没有传头像或最大头像图片不存在时不触发显示
    if (userbigphoto == "" || $("#userphotobox img:eq(0)").attr("id")=="false") {
        return;
    }
    var left = (($(window).width() - $("#userphotobox").width()) > 0 ? ($(window).width() - $("#userphotobox").width()) : ($("#userphotobox").width() - $(window).width())) / 2;
    var top = (($(window).height() - $("#userphotobox").height()) > 0 ? ($(window).height() - $("#userphotobox").height()) : ($("#userphotobox").height() - $(window).scrollTop())) / 2 + document.documentElement.scrollTop;
    $("#userphotobox").css("left", left);
    $("#userphotobox").css("top", top);
    $("#alterbg").css("display", "block").css("height", $(document).height());
    $("#userphotobox").show();
}

/***********Confirm Or Reject Friend Apply************/
function TransactFriendApply(o, type, fid) {
    if (type == 1) {
        var data = { friendid: fid };
        $.post('/User/AjaxConfirmFriend', data,
	function(data) {
	    if (data['success'] == 0) {
	        alert("请先登录！");
	    } else if (data['success'] == 200) {
	    $(o).parent().parent().remove();
	        
	    }
	}, 'json');
    } else if (type == 0) {
        var data = { friendid: fid };
        $.post('/User/AjaxRejectFriend', data,
	function(data) {
	    if (data['success'] == 0) {
	        alert("请先登录！");
	    } else if (data['success'] == 200) {
	        $(o).parent().parent().remove();
	    }
	}, 'json');
    }
}

function AddFriend(a, page, fid) {
    var data = { friendid: fid };
    $.post('/User/AjaxAddFriend', data,
	function(data) {
	    if (data['success'] == 0) {
	        alert("请先登录！");
	    } else if (data['success'] == 200) {
	        alert("您的好友请求已经发送，请等待好友的确认！");
	        if (page == "profile") {
	            $(a).removeClass("addfriend").addClass("addfriendoff").removeAttr('onclick').attr("title", "您的好友请求已经发送，请等待好友确认！");
	        } else if (page == "friend") {
	            $(a).removeAttr("onclick").attr("src", "../../Content/images/add_disable.jpg").attr("title", "您的好友请求已经发送，请等待好友确认！");
	        }
	    }
	}, 'json');
}

function setupFriendSearch() {
    if ($("#key").val() == "") {
        $("#key").val("姓名，昵称或电子邮箱");
    }
    $("#key").focus(function() {
        if ($("#key").val() == "姓名，昵称或电子邮箱") {
            $("#key").val("");
        }
        $(this).parent().removeClass('searchinput').addClass('searchinput_active');
    });
    $("#key").blur(function() {
        if ($("#key").val() == "") {
            $("#key").val("姓名，昵称或电子邮箱");
        }
        $(this).parent().removeClass('searchinput_active').addClass('searchinput');
    });
    $("#searchfriend").click(function() {
        if ($("#key").val() == "" || $("#key").val() == "姓名，昵称或电子邮箱") {
            alert("请输入搜索信息！");
            $("#key").focus();
            return false;
        }
        return true;
    });
}

function preDeleteFriend(o) {
    $("span[accept]").hide();
    $("span[reject]").hide();
    $("span[delete]").addClass("u_setbutton").show();
    $(o).parent().hide();
    $(o).parent().prev().show().css("display", "block").addClass("u_setbutton");
    $(o).parent().prev().prev().show().addClass("u_setbutton");
}
function cacelDeleteFriend() {
    $("span[accept]").hide();
    $("span[reject]").hide();
    $("span[delete]").addClass("u_setbutton").show();
}
function DeleteFriendFriend(fid) {
    var data = { friendid: fid };
    $.post('/User/AjaxDeleteFriend', data,
	function(data) {
	    if (data['success'] == 0) {
	        alert("请先登录！");
	    } else if (data['success'] == 200) {
	        window.location.href = window.location.href;
	    }
	}, 'json');
}

function setupusersettinginputbg() {
    $("input[usersetting]").blur(function() { $(this).parent().removeClass("u_ssinput_active").addClass("u_ssinput"); }).focus(function() { $(this).parent().removeClass("u_ssinput").addClass("u_ssinput_active"); });
}


function setupUserDetailEdit() {
    $("#editdetail").click(function() {
        var errorcount = 0;
        if ($("#Name").val() == "") {
            $("#Name").parent().parent().next().html("请填写昵称");
            errorcount++;
        } else {
            if ($("#Name").val().length > 100) {
                $("#Name").parent().parent().next().html("昵称最长为100个字符");
                errorcount++;
            }
        }
        if ($("#City").val().length > 8) {
            $("#City").parent().parent().next().html("来源最长为8个字符");
            errorcount++;
        }
        if ($("#Telehphone").val().length > 50) {
            $("#Telehphone").parent().parent().next().html("电话最长长度为50个字符");
            errorcount++;
        }
        return errorcount == 0;
    });
}

function setupUserPassworEdit() {
    $("#editpassword").click(function() {
        var errorcount = 0;
        if ($("#OldPassword").val() == "") {
            $("#OldPassword").parent().parent().next().html("请填写旧密码！");
            errorcount++;
        }
        if ($("#Password").val().length == "") {
            $("#Password").parent().parent().next().html("请填写密码！");
            errorcount++;
        } else {
            if ($("#Password").val().length > 100) {
                $("#Password").parent().parent().next().html("密码最长为8个字符！");
                errorcount++;
            }
        }
        if ($("#ConfirmPassword").val() == "") {
            $("#ConfirmPassword").parent().parent().next().html("请填写确认密码！");
            errorcount++;
        } else {
            if ($("#ConfirmPassword").val() != $("#Password").val()) {
                $("#ConfirmPassword").parent().parent().next().html("两次密码填写不一致！");
                errorcount++;
            }
        }
        return errorcount == 0;
    });
}

function setupUserEmailEdit() {
    $("#editemail").click(function() {
        var errorcount = 0;
        if ($("#Email").val().length <= 0) {
            $("#Email").parent().parent().next().html("请填写邮箱！");
            errorcount++;
        } else {
            if ($("#Email").val().length > 100) {
                $("#Email").parent().parent().next().html("邮箱最长为100个字符！");
                errorcount++;
            } else {
                if (!regEmail.test($("#Email").val())) {
                    $("#Email").parent().parent().next().html("请填写正确的邮箱！");
                    errorcount++;
                }
            }
        }
        return errorcount == 0;
    });

    $("#Email").blur(function() {
        var data = { UserEmail: $("#Email").val() };
        $.post('/User/RegisterValidateEmail', data,
	    function(data) {
	        if (data['success']) {
	            $("#Email").val("");
	            $("#Email").parent().parent().next().html("该邮箱已经被注册！");
	        }
	    }, 'json');
    });
}
