﻿var username;
var newFriend = false;
var Authenticated = false;
var timer;
var timer2;
var headerID = 0;
var myLangArray;

function preloader() {
/*
    var i = 0;
    var preLoad = new Array();
    images = new Array();
    images[0] = "./image/default/logout_00-1.png";
    images[1] = "./image/default/logout_01-1.png";
    images[2] = "./image/default/logout_02-1.png";
    images[3] = "./image/default/logout_03-1.png";
    images[4] = "./image/default/photo_up_01-1.png";
    images[5] = "./image/default/photo_up_02-1.png";
    images[6] = "./image/default/photo_up_03-1.png";
    images[7] = "./image/login_frame_buttons_04_on.png";
   
    for (i = 0; i <= 7; i++) {
        preLoad[i] = new Image();
        preLoad[i].src = images[i];
    }
    */
}
function imgsetting() {
$('.changecolor').add('.button22content').bind('mouseout', function(event) {
    $(this).css("color", "#FFFFCC");
    
    }).bind('mouseover', function(event) {
    $(this).css("color", "#F00");
    });
    
    
    $('.logout_00').bind('mouseout', function(event) {
    $(this).attr("src", "./image/default/logout_00.png");
    }).bind('mouseover', function(event) {
    $(this).attr("src", "./image/default/logout_00-1.png").css("cursor","pointer");
    });
    
    $('.login').bind('mouseout', function(event) {
    $(this).attr("src", "./image/default/login.png");
    }).bind('mouseover', function(event) {
    $(this).attr("src", "./image/default/login-1.png").css("cursor", "pointer");
    });
}

function keydown_click(event) {
    if (event.keyCode == 13) {
        OnClickLogin();
    }
    else if (event.keyCode == 9) {
    
        return false;
    }

}

function ie6check() {
    // alert($.browser.version);
   
    if ($.browser.msie && $.browser.version < 7)
        return true;
    else
        return false;
     
}
function getCookie(c_name)
{
    if (document.cookie.length > 0) {
       
     var c_list = document.cookie.split("\;");
	 for ( i in c_list )
	 {
	     var cook = c_list[i].split("=");
	     //alert(cook[0].length + "  " + c_name.length );
	     if (cook[0].indexOf(c_name) != -1) {
	         
		     return unescape(cook[1]);
		}
	} 
  }
  return "";
}
function checkLanguage() {
    var lang = "";
    var relang = "";
    var index = 0;
   
    if (getCookie("language").length < 1) {
        lang = window.navigator.userLanguage || window.navigator.language;
        relang = lang.toLowerCase();
        switch (relang) {
            case "zh-cn":
                //$("#tbody").load("minwt_zh-cn.html");
                index = 1;
                break;

            case "zh-tw":
                //$("#tbody").load("minwt_zh-tw.html");
                index = 1;
                break;

            default:
                // $("#tbody").load("minwt_zh-tw.html");
                index = 2;
                break;
        }
    }
    else {
        index = getCookie("language");
    }

    var cstr = "language=" + index;
    document.cookie = cstr;
     
    AspAjax.getLanguage(index,"pdefault");
}
function chg_lan() {
    var index = document.getElementById("languagechoice").selectedIndex;
    if (index == 0)
        return;
    AspAjax.getLanguage(index,"pdefault");
    var cstr = "language=" + index;
    document.cookie = cstr;
}
function setLanguage() {
    document.title = myLangArray[36];
    document.getElementById("edit").innerHTML = myLangArray[3];
    document.getElementById("composite").innerHTML = myLangArray[4];
    document.getElementById("sticker").innerHTML = myLangArray[6];
    document.getElementById("cartoon").innerHTML = myLangArray[5];
    document.getElementById("album").innerHTML = myLangArray[0];
    document.getElementById("hotrank").innerHTML = myLangArray[2];
    document.getElementById("Unionazer").innerHTML = myLangArray[19];
    //
    document.getElementById("wAlbummage").innerHTML = myLangArray[1];
    document.getElementById("wmyalbum").innerHTML = myLangArray[0];
    document.getElementById("wusermanage").innerHTML = myLangArray[8];
    document.getElementById("wFriendsManage").innerHTML = myLangArray[9];
    document.getElementById("wCommentManage").innerHTML = myLangArray[10];
    document.getElementById("wLogout").innerHTML = myLangArray[35];

    document.getElementById("AmazerID").innerHTML = myLangArray[21];
    document.getElementById("Signup").innerHTML = myLangArray[22];
    document.getElementById("Password").innerHTML = myLangArray[23];
    document.getElementById("Forget").innerHTML = myLangArray[24];
    document.getElementById("randomimg").innerHTML = myLangArray[38];
    document.getElementById("wpicmovein").innerHTML = myLangArray[37];
    
 
    document.getElementById("policy").innerHTML = myLangArray[11];
    document.getElementById("privacy").innerHTML = myLangArray[12];
    document.getElementById("contact").innerHTML = myLangArray[13];
    document.getElementById("question").innerHTML = myLangArray[14];  

    document.getElementById("cooperation").innerHTML = myLangArray[15];
    document.getElementById("about").innerHTML = myLangArray[16];
    //document.getElementById("copyright").innerHTML = myLangArray[17];
    $("#copyright").html(myLangArray[17]);
}
$(document).ready(function() {
    //$(".content").hide();
    $(".container").hide();

    imgsetting();
    preloader();
    AspAjax.set_defaultSucceededCallback(SucceededCallback);
    AspAjax.set_defaultFailedCallback(FailedCallback);

    checkLanguage();
    $(this).Pop({ theme: 'dark_rounded', closeCallBack: function() {
        document.getElementById("pp_full_res").style.display = "none";
    }
    });
    $('.pp_content').css("background-color", "");
    headerID = parseInt((Math.random() * 9)) % 4;
    // alert(headerID);
    $('#index_header').css('background', 'url("../image/default/index_header0' + headerID + '.jpg")');
    timer = setInterval(function() {
        headerID = (parseInt(headerID) + 1) % 4;
        $('#index_header').css('background', 'url("../image/default/index_header0' + headerID + '.jpg")');

    }, 60000);
    //index_header01
    AspAjax.checkviews();
    timer = setInterval(function() {

        AspAjax.checkviews();
    }, 60000);
    setSample(0, 5);
});
function SucceededCallback(result, userContext, methodName) {
  //  alert(methodName);
    if (methodName == "IsAuthenticated") {
        if (result.length > 0) {
            username = result;
            Authenticated = true;
            document.getElementById("username").innerHTML = "<p>" + myLangArray[26] + username +
            " "+ myLangArray[27]+"</p>";
            $("#main").css({ 'top': -190 });
            setlogimg(false);
            //取得新留言數
            AspAjax.getNewCommentNum(username);

            //取得新朋友數
            AspAjax.getNewFriendNum(username);
            //取得使用者群組
            AspAjax.getUserRoles();

        }
        else {
            Authenticated = false;
            $("#main").css({ 'top': 0 });
            setlogimg(true);
        }
        $(".content").show();


    }
    else if (methodName == "getLanguage") {
   
        myLangArray = result;
        setLanguage();
        //alert(myLangArray[0]);
        $(".container").show();

        if (ie6check()) {
            
            var inner = '<p>' + myLangArray[25] + '</p><br/>' +
            '<a href="http://windows.microsoft.com/zh-TW/internet-explorer/products/ie-9/home?os=winvista&arch=a&browser=other">IE8</a> ' +
            '<a href="http://moztw.org/firefox/">FireFox</a> ' +
            '<a href="http://www.google.com.tw/chrome">Google chrome</a></p>';
            $.Pop.open();
            $('.pp_content').css("background-color", "#F8FAE3");
            $.Pop.expand(inner, 420, 150, function() { });
        }
        AspAjax.IsAuthenticated();
    }
    else if (methodName == "Logout") {

        //   document.getElementById("result").innerHTML = "";
        document.getElementById("username").innerHTML = "";
        $("#main").animate({ "top": 0 }, 500, function() { });
        setlogimg(true);
        Authenticated = false;
        $("#newComment").html("");
        $("#newFriend").html("");
        $("#spanM").html("");

    }
    else if (methodName == "getNewCommentNum") {

        if (result != 0) {
            $("#CommentManage .button22content").html(myLangArray[10]+"(" + result + ")");
        }
    }
    else if (methodName == "getNewFriendNum") {
        if (result != 0) {
            newFriend = true;

            $("#FriendsManage .button22content").html(myLangArray[9] + "(" + result + ")");
        }
    }
    else if (methodName == "getUserRoles") {
        if (result != null) {
            for (var item in result) {
                if (result[item] == "Manager") {
                    $("#spanM").html("<a href='Manage/PictureMange.aspx?title=相片檢舉管理'> | 管理介面</a>");
                    break;
                }
            }
        }
    }
}
function FailedCallback(error, userContext, methodName) {
  //  alert("功能有問題 請再試一次 或重新整理" + error.get_message() + " " + methodName);

}
function setlogimg(islogin) {

  //  alert("setlogimg");
    if (islogin) {
        //alert("setlogimg  OnClickLogin");
        $("#imglog").attr('class', 'login').unbind('click').bind('click', OnClickLogin);
    }
    else {
       // alert("setlogimg  btn_SighOut");
    //    $("#imglog").attr('class', 'logout_00').unbind('click').bind('click', btn_SighOut); 
    }
    
}
function OnLoginCompleted(validCredentials, userContext, methodName) {

    if (validCredentials == true) {
        username = userContext; //myLangArray[14]
        document.getElementById("username").innerHTML = "<p>" + myLangArray[26] + username + " " + myLangArray[27] + "</p>";
        
       // document.getElementById("result").innerHTML = "登入成功";
        $("#main").animate({ "top": -190 }, 500, function() { });
        setlogimg(false);
        Authenticated = true;

        //取得新留言數
        AspAjax.getNewCommentNum(username);

        //取得新朋友數
        AspAjax.getNewFriendNum(username);

        //取得使用者群組
        AspAjax.getUserRoles();
    }
    else {
        AspAjax.FailedPasswordAttemptCount(userContext);
        $.Pop.open();
        
        
        $.Pop.expand('<div class="little_window_bg"/><p>' + myLangArray[28] + '</p>', 420, 150, function() {
        //$('.pp_content').css("background-color", "#000");
        });
    }
}
function OnLogoutCompleted(result, userContext, methodName) {
    username = "";
}
function OnFailed(error, userContext, methodName) {
    var msg = "";
    msg += "錯誤訊息：" + error.get_message() + "\n\n";
    msg += "逾時時間：" + error.get_timedOut() + "\n\n";
    msg += "狀態代碼：" + error.get_statusCode() + "\n\n";
    msg += "堆疊追蹤：" + error.get_stackTrace() + "\n\n";
    msg += "例外類型：" + error.get_exceptionType();
  //  alert(msg);
}

function OnClickLogin() {
    // alert("OnClickLogin");
  //  alert(validateInjection());
    if (validateInjection()) {
        if (document.getElementById("LoginUserName").value.length == 0) {
            $.Pop.open();
            $.Pop.expand('<div class="little_window_bg"/><p>' + myLangArray[29] + '</p>', 420, 150, function() { });
          //  document.getElementById("result").innerHTML = "您沒有鍵入使用者帳號";
        }
        else if (document.getElementById("LoginPassword").value.length == 0) {
        $.Pop.open();
        $.Pop.expand('<div class="little_window_bg"/><p>' + myLangArray[30] + '</p>', 420, 150, function() { });
            //document.getElementById("result").innerHTML = "您沒有鍵入密碼";
        }
        else {
            var name = document.getElementById("LoginUserName").value;
            var LoginPassword = document.getElementById("LoginPassword").value;
            Sys.Services.AuthenticationService.login(name, LoginPassword,
            false, null, null, OnLoginCompleted, OnFailed, name);
        }
    }
}

function GoHotrank_Click() {
    window.location = "./hotrank.aspx";
}
function GoEdit_Click() {
    window.location = "./edit.aspx";
}
function GoComposite_Click(){
    window.location = "./composite.aspx";
}
function GoSticker_Click() {

    window.location = "./Sticker.aspx";
}
function GoAllinone_Click() {

    window.location = "./allinone.aspx";
} 
function btn_SighOut() {

   // alert("btn_SighOut");
    AspAjax.Logout();
  //  Sys.Services.AuthenticationService.logout(null, OnLogoutCompleted, OnFailed, null);

}

function btnUserManage_Click() {
   
    window.location = "./manage.aspx";
   
}

function forgetpassword_click() {
    $.Pop.open();
    var ifrme = '<iframe src="PasswordReset.aspx" id="ifPasswordReset" frameborder="no" scrolling="no" style="width:420px; height:150px;"></iframe>';
    $.Pop.expand(ifrme, 420, 150, function() { });
}

function CreateUser_click() {
    head.CreateUserEnter();
}


function ManageImage_Click() {
    //alert("ManageImage_Click");
    window.location = "./MAlbum.aspx";
}

function btnMyAlbum_Click() { 
     window.location = "./Album/" + username;
 
}

function profile_Click() {

    window.location = "./profile.aspx?user=" + username;
}

function btnCommentManage_Click() {
    window.location = "./CommentNotice.aspx";
}

function btnFriend_Click() {

    if (newFriend == true) {
        window.location = "./Friend.aspx?Tag=newFriend";
    }
    else {
        window.location = "./Friend.aspx?Tag=myFriend"; 
    }
}

function validateInjection() {
   // alert("document.forms[0].elements.length:"+document.forms[0].elements.length);
    var i = 0;
    for (i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].type == 'text' &&
               document.forms[0].elements[i].getAttribute("ci") != null) {
            var elem = document.forms[0].elements[i];
            if (elem.value != null &&
                 (elem.value.indexOf('\'') != -1 ||
                  elem.value.indexOf('--') != -1 ||
                  elem.value.indexOf(' OR ') != -1)) {
                alert(myLangArray[31]);
                return false;
            }
        }
    }
    return true;
}

function checkstringinjection(checkstring) {
   // alert("validateInjection");
    var i = 0;
    if (checkstring != null &&
         (checkstring.indexOf('\'') != -1 ||
          checkstring.indexOf('--') != -1 ||
          checkstring.indexOf(' OR ') != -1)) {
        alert('possible injection detected.')
        return false;
    }
    return true;
}
function GoCartoon_Click() {

    window.location = "./Cartoon.aspx";
}
function GoAlbum_Click() {
    if (Authenticated) {
        window.location = "./Album/" + username;
    }
    else {
        $.Pop.open();
        $.Pop.expand('<div class="little_window_bg"/><p>' + myLangArray[32] + '</p>', 420, 150, function() { });
    }
}
function setSample(sampleindex, choice) {
   // alert(choice);
    $('#show_bigimg img').attr("src", "./image/default/sample/sample" + sampleindex + ".jpg");
    var herf;
    if (choice == 0)
        herf = "../allinone.aspx";
    else if (choice == 1)
        herf = "../composite.aspx";
    else if (choice == 2  )
        herf = "../edit.aspx";
    else if (choice == 3)
        herf = "../sticker.aspx";
    else if (choice == 4)
        herf = "../cartoon.aspx";
    else if (choice == 5)
        herf = "http://apps.facebook.com/amazertw/";
   // alert(herf);
    $('#show_bigimg a').attr("href", herf);
}
function btnMovePics_Click() {

    window.location = "./PicMoveIn.aspx";
}

var createusersuccess = function(result) {
$.Pop.expand('<div class="little_window_bg"/><p>' + myLangArray[18] + ' <br/>' + myLangArray[32] + '</p>', 420, 150, function() { });


/*
    imgheight = height;
    imgwidth = width;
    processingrecord[parseInt(currentStep)] = "上傳 ";
    // sethintsetting(2);
    currentStep = parseInt(currentStep) + 1;
    totalStep = parseInt(currentStep);
  
    // resizepic();
    $("#image").fadeOut("fast", function() {
        $.Pop.expand("展開圖片中", 200, 100, function() { });
        document.getElementById("image").src = newpath + '?ts=' + new Date().getTime();
    });

*/

    //  resetAreaSetting();


};
