﻿/// <reference path="jquery-1.4.1.min.js" />

jQuery(document).ready(function () {
    var sessionId = GetSession();
    if (sessionId == null) return;


    if (jQuery("[id^=cphMain_chk_]").attr("id") == null) {
        ShowLastSee();
        return;
    }

    var goodsid = jQuery("[id^=cphMain_chk_]").attr("id").replace("cphMain_chk_", "");
    if (goodsid != null) {
        var params = "{sessionId:'" + sessionId + "',goodsId:'" + goodsid + "'}";
        ExecService("/Fichi.asmx/LastSeeAdd", params, AjaxSucceededLS, AjaxFailedLS);
    }
});
function AjaxSucceededLS(result) {
    //проверяем каунт если больше 0 то выводим список(отдельной функцией!!!)
    ShowLastSee();
}
function AjaxFailedLS(result) {
    //jQuery('#LastSee').css("display", "none");
}
function ShowLastSee() {
    var sessionId = GetSession();
    if (sessionId == null) return;

    var paramsCount1 = "{sessionId:'" + sessionId + "'}";
    ExecService("/Fichi.asmx/LastSeeCount", paramsCount1, AjaxSucceededCountLS, AjaxFailedLS);
}
function AjaxSucceededCountLS(result) {

    if (result == null) return;
    var sessionId = GetSession();
    if (sessionId == null) return;

    if (result.d > 0) {
        jQuery('#LastSee').css("display", "block");

        var params = "{sessionId:'" + sessionId + "'}";
        ExecService("/Fichi.asmx/LastSeeGet", params, AjaxSucceededLSGet, AjaxFailedLS);
    } else {
        jQuery('#LastSee').css("display", "none");
    }
}

function AjaxSucceededLSGet(result) {
    if (result == null) return;
    var ca = result.d.split(",");

    if (ca.length <= 0) return;

    var htmlLS = "<table border='0' class='LastSeeBox'><tr>";

    var test;
    for (var i = 0; i < ca.length; i++) {
        if (ca[i] != "") {
            htmlLS += "<td id='ls_" + ca[i] + "'>" + ca[i] + "</td>";
            var params = "{goodsId:'" + ca[i] + "'}";
            ExecService("/Fichi.asmx/LastSeeGetPhoto", params, AjaxSucceededLSP, AjaxFailedLS);
        }
    }
    htmlLS += "</tr></table>";

    jQuery("#LastSeeContent").html(htmlLS);

}

function AjaxSucceededLSP(result) {
    if (result == null) return;
    var town = GetTown();
    if (town == null) return;

    var tmp = result.d.split("|");
    var id = tmp[0];
    var imaga = tmp[1];
    var name=tmp[2]

    var htmlLS = "<a href='http://" + window.location.hostname + "/" + town + "/goods/" + id + "'>";
    htmlLS += "<img id='lsi_" + id + "' style='border: none;' alt='" + name + "' height='90px'";
    if (imaga == "")
        htmlLS += " src='http://" + window.location.hostname + "/img/nophotobig.jpg'";
    else
        htmlLS += " src='http://e.intant.ru/imggoodsm/" + imaga + "'";

    htmlLS += " />";
    htmlLS += "</a>";
    jQuery("#ls_" + id).html(htmlLS);

    //resize
    var w = jQuery("#lsi_" + id).attr("width");
    var h = jQuery("#lsi_" + id).attr("height");

    if (w > h) {
        jQuery("#lsi_" + id).removeAttr("height");
        jQuery("#lsi_" + id).attr("width", "70");
    }
    if (h > w) {
        jQuery("#lsi_" + id).removeAttr("width");
        jQuery("#lsi_" + id).attr("height", "90");
    }

}
