﻿var t = 0;
var iImageSwitchCurrent = 0;
var total = 0;

var globalheight = 0;
var globalwidth = 0;

$(document).ready(function() {
    var divImageSwitchA = $("#divSwitchMain a");
    total = divImageSwitchA.length;
    for (var i = 0; i < divImageSwitchA.length; i++) {
        $("#divSwitchCount").append("<span style='cursor:pointer;background-color:" + (i == 0 ? "Red" : "#999999") + ";' id='span" + divImageSwitchA[i].id + "' onclick='showimage(\"" + divImageSwitchA[i].id + "\");'>" + (i + 1) + "</span>&nbsp;&nbsp;");
    }

    $("#divCurrent").empty();
    var link = divImageSwitchA[0]
    var url = link.href;
    var imgurl = $("#" + link.id + " img")[0].src;
    var title = $("#" + link.id + " img")[0].title;

    var imgModel = $("#imgModel")[0];
    var imgModelValueArr = imgModel.innerHTML.split(",");
    globalwidth = imgModelValueArr[0];
    globalheight = imgModelValueArr[1];

    $("#divCurrent").append("<a href='" + url + "'><img width='" + globalwidth + "' height='" + globalheight + "' src='" + imgurl + "' /></a>");
    $("#divCurrentTitle").append(title);

    t = setInterval("autoshow()", 4000);

    $("#divSwitchCount").hover(function() {
        clearInterval(t);
    },
    function() {
        t = setInterval("autoshow()", 4000);
    }
    );
})

function showimage(id) {
    var currentlink = $("#" + id);
    $("#divCurrent").empty();
    var link = currentlink[0];
    var url = link.href;
    
    //
    var imgurl = $("#" + link.id + " img")[0].src;
    var title = $("#" + link.id + " img")[0].title;
    //alert(imgurl);
    $("#divCurrent").append("<a href='" + url + "'><img width='" + globalwidth + "' height='" + globalheight + "' src='" + imgurl + "' /></a>");
    //alert($("#divCurrent")[0].innerHTML);
    $("#divCurrentTitle").text("");
    $("#divCurrentTitle").append(title);
    
    $("#divSwitchCount span").css("background-color", "#999999");
    $("#divSwitchCount #span" + id).css("background-color", "Red");
}

function autoshow() {
    //alert(iImageSwitchCurrent);
    //alert("ss");
    iImageSwitchCurrent = (iImageSwitchCurrent == (total-1) ? 0 : iImageSwitchCurrent+1);
    
    var divImageSwitchA = $("#divSwitchMain a");
    //alert(iImageSwitchCurrent);
    showimage(divImageSwitchA[iImageSwitchCurrent].id);
}
