﻿
var checkboxImages = new Array(
					"images/check-off.gif",
                    "images/check-on.gif",
					"images/check-hover.gif"
		 			  );

var starImages = new Array(
			"images/unrated.gif",
			"images/1-star.gif",
			"images/2-star.gif",
			"images/3-star.gif",
			"images/4-star.gif",
			"images/5-star.gif"
	);

var starMessages = new Array(
			"重新打分",
			"很不喜欢",
			"不喜欢",
			"还行",
			"喜欢",
			"非常喜欢",
			"给商品打分",
			"<span style="+'"'+"color: #990000"+'"'+">已保存</span>",
            "&nbsp;");
var ownItMessages = new Array(
			"已经有了",
			"<span style="+'"'+"color: #990000"+'"'+">已保存</span>");

var notInterestedMessages = new Array(
			"不感兴趣",
			"<span style="+'"'+"color: #990000"+'"'+">不感兴趣</span>");

var excludedMessages = new Array(
			"用来为我推荐",
			"<span style="+'"'+"color: #990000"+'"'+">已保存</span>",
            "不作为推荐给我的样本");

var starMap = new Array(
		'0,0,12,20',
		'13,0,24,20',
		'25,0,36,20',
		'37,0,48,20',
		'49,0,60,20',
		'61,0,72,20');

var savedRatings = new Array();
var changedRatings = new Array();
var savedNotInterested = new Array();
var savedIsOwned = new Array();
var savedExcluded = new Array();
var savedExcludedClickstream = new Array();
var starTwinkler = new Array();
var msgTwinkler = new Array();

var isSaved = new Array();

var isRatingsBarChanged = false;

var delayTime = 500;

var allImages = new Array();

function cunan_preloadImages(imgArray){
  for (i=0; i < imgArray.length ;i++){
    if (imgArray[i] != undefined) {
      allImages[i] = new Image();
      allImages[i].src = imgArray[i];
    }
  }
}

cunan_preloadImages(starImages);
cunan_preloadImages(checkboxImages);


function cunan_isOwnedMouseOver(asin){
	if (!savedIsOwned[asin]) {
		document.images["isOwned." + asin].src = checkboxImages[2];
	}
}

function cunan_isOwnedMouseOut(asin){
	if (!savedIsOwned[asin]) {
		document.images["isOwned." + asin].src = checkboxImages[0];
	}
}

function cunan_notInterestedMouseOver(asin){
	if (!savedNotInterested[asin]) {
		document.images["notInterested." + asin].src = checkboxImages[2];
	}
}

function cunan_notInterestedMouseOut(asin){
	if (!savedNotInterested[asin]) {
		document.images["notInterested." + asin].src = checkboxImages[0];
	}
}

function cunan_excludedMouseOver(asin, alt, showNeg){
	if (document.images["excluded." + asin].src == checkboxImages[0]) {
		document.images["excluded." + asin].src = checkboxImages[2];
	}
}

function cunan_excludedMouseOut(asin, alt,  showNeg){
	if (document.images["excluded." + asin].src == checkboxImages[2]) {
		document.images["excluded." + asin].src = checkboxImages[0];
	}
}

function cunan_excludedClickstreamMouseOver(asin){
        if (!savedExcludedClickstream[asin]) {
                document.images["excludedClickstream." + asin].src = checkboxImages[2];
        }
}

function cunan_excludedClickstreamMouseOut(asin){
        if (!savedExcludedClickstream[asin]) {
                document.images["excludedClickstream." + asin].src = checkboxImages[0];
        }
}

function cunan_showAlertMessage() {
//  var gtext = this.req.responseText;
//  if(gtext.indexOf("successful")!=-1) {
//    alert("?????????????????????");
//  } else {
//    alert("?????????????????????");
//  }
}

function cunan_sendRating(asin, ratingType, ratingValue) 
{
     var parms = new Array();
     parms[0] = new Array("pro_id",asin);
     parms[1] = new Array("type",ratingType);//评分ontofive，推荐excluded，不感兴趣nointerested 是否有了owned
     parms[2] = new Array("value",ratingValue);
     
     var result = postPageData('Ajax/cunanData_Ajax.aspx?Module=UPDATERATING', "POST",parms);
     //alert(result);
     if(result != "ok")
      alert(result);
}

function cunan_swapStars(asin, rating) {
    if (document.images["stars." + asin] != undefined) {
        if (rating == undefined) rating = savedRatings[asin];
        document.images["stars." + asin].src = starImages[rating];
    }  
}

function cunan_swapStarMsgs(asin, rating) {
    if (document.getElementById("messages." + asin)) {
        if (rating == undefined) {
            document.getElementById("messages." + asin).innerHTML = starMessages[6];
        } else {
            document.getElementById("messages." + asin).innerHTML = starMessages[rating];
            if (rating == 7) isSaved[asin]= 1;
        }
    }
}

function cunan_swapIsOwnedMsgs(asin, isOwned) {
	document.getElementById("ownIt.messages." + asin).innerHTML = ownItMessages[isOwned];
}

function cunan_swapIsOwned(asin, isOwned) {
    if (document.images["isOwned."+asin]) document.images["isOwned." + asin].src = checkboxImages[isOwned];
}

function cunan_swapNotInterestedMsgs(asin, notInterested) {
	document.getElementById("notInterested.messages." + asin).innerHTML = notInterestedMessages[notInterested];
}

function cunan_swapNotInterested(asin, notInterested) {
    if (document.images["notInterested."+asin]) document.images["notInterested." + asin].src = checkboxImages[notInterested];
}

function cunan_swapExcludedMsgs(asin, excluded) {
	document.getElementById("excluded.messages." + asin).innerHTML = excludedMessages[excluded];
}

function cunan_swapExcludedClickstreamMsgs(asin, excluded) {
        document.getElementById("excludedClickstream.messages." + asin).innerHTML = excludedMessages[excluded];
}

function cunan_swapExcluded(asin, excluded, showNeg) {
    var excVal = (excluded == 1 && showNeg == 0) || (excluded == 0 && showNeg == 1) ? 1 : 0;
    if (document.images["excluded."+asin]) document.images["excluded." + asin].src = checkboxImages[excVal];
}

function cunan_swapExcludedClickstream(asin, excluded) {
    if (document.images["excludedClickstream."+asin]) document.images["excludedClickstream." + asin].src = checkboxImages[excluded];
}

function cunan_sendStars(asin, rating) {
    savedRatings[asin] = rating;
    changedRatings[asin] = 1;
    try {
        if (shvl) {
            shvl.rating[asin] = savedRatings[asin];
            shvl.notInterested[asin] = 0;
        }    
    } catch(e) {}     
    cunan_swapStarMsgs(asin, 7);
    if (savedRatings[asin]) {
        cunan_swapNotInterested(asin, 0);
    }
    window.setTimeout("cunan_sendRating('"+asin+"', 'onetofive', '"+rating+"')", delayTime);
    
}

function cunan_sendIsOwned(asin) {
    savedIsOwned[asin] = (savedIsOwned[asin] ? 0 : 1);
    rating = (savedIsOwned[asin] ? 'OWN' : 'NONE');
    try {
        if (shvl) {
            shvl.isOwned[asin] = savedIsOwned[asin];
            shvl.notInterested[asin] = 0;
        }    
    } catch(e) {}     
    cunan_swapIsOwned(asin,savedIsOwned[asin]);
    if (savedIsOwned[asin]) {
        cunan_swapNotInterested(asin, 0);
    }
    cunan_swapStarMsgs(asin, 7);
    window.setTimeout("cunan_sendRating('"+asin+"', 'owned', '"+rating+"')", delayTime);
}

function cunan_sendNotInterested(asin) {
    savedNotInterested[asin] = (savedNotInterested[asin] ? 0 : 1);
    try {
        if (shvl) {
            shvl.notInterested[asin] = savedNotInterested[asin];
            shvl.isOwned[asin] = 0;
            shvl.rating[asin] = 0;
        }    
    } catch(e) {}    
        
    rating = (savedNotInterested[asin] ? 'NOTINTERESTED' : 'NONE');
    cunan_swapNotInterested(asin,savedNotInterested[asin]);
    if (savedNotInterested[asin]) {
      cunan_swapIsOwned(asin, 0); 
      cunan_swapStars(asin, 0);
    }
    cunan_swapStarMsgs(asin, 7);
    window.setTimeout("cunan_sendRating('"+asin+"', 'not-interested', '"+rating+"')", delayTime);
}

function cunan_sendExcluded(asin, msg, showNeg) {
  savedExcluded[asin] = (savedExcluded[asin] ? 0 : 1);
  rating = (savedExcluded[asin] ? 'EXCLUDED' : 'NONE');
  cunan_swapExcluded(asin,savedExcluded[asin],showNeg);
  cunan_swapStarMsgs(asin, 7);
  window.setTimeout("cunan_sendRating('"+asin+"', 'excluded', '"+rating+"')", delayTime);
}

function cunan_sendExcludedClickstream(asin) {
    savedExcludedClickstream[asin] = (savedExcludedClickstream[asin] ? 0 : 1);
    rating = (savedExcludedClickstream[asin] ? 'NONE' : 'EXCLUDED');
    cunan_swapExcludedClickstream(asin,savedExcludedClickstream[asin]);
    cunan_swapStarMsgs(asin, 7);
    window.setTimeout("cunan_sendRating('"+asin+"', 'excludedClickstream', '"+rating+"')", delayTime);
}


function cunan_starMouseOver(asin, rating){
   if (starTwinkler[asin] != 0){
    window.clearTimeout(starTwinkler[asin]);
    starTwinkler[asin] = 0;
  }
  if (msgTwinkler[asin] != 0){
    window.clearTimeout(msgTwinkler[asin]);
    msgTwinkler[asin] = 0;
  }  
  cunan_swapStars(asin, rating);
  cunan_swapStarMsgs(asin, rating);
}

function cunan_starMouseOut(asin){
  starTwinkler[asin] = window.setTimeout("cunan_swapStars('"+asin+"')", delayTime);
  msgTwinkler[asin] = window.setTimeout("cunan_swapStarMsgs('"+asin+"')", delayTime);
}


function cunan_showIsOwned(asin, isOwned){
  var imageID = "isOwned." + asin;
  document.write("<img src="+'"'+"" + checkboxImages[isOwned] + ""+'"'+"");
  document.write("id="+'"'+"" + imageID + ""+'"'+" ");
  document.write("onclick="+'"'+"cunan_sendIsOwned('" + asin + "', 'alt');"+'"'+" ");
  document.write("onmouseover="+'"'+"cunan_isOwnedMouseOver('" + asin + "', 'alt');"+'"'+" ");
  document.write("onmouseout="+'"'+"cunan_isOwnedMouseOut('" + asin + "', 'alt');"+'"'+" ");
  document.write("border="+'"'+"0"+'"'+" valign="+'"'+"bottom"+'"'+" />");
}


function cunan_showNotInterested(asin, notInterested){
    var imageID = "notInterested." + asin;
    document.write("<img src="+'"'+"" + checkboxImages[notInterested] + ""+'"'+"");
    document.write("onclick="+'"'+"cunan_sendNotInterested('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseover="+'"'+"cunan_notInterestedMouseOver('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_notInterestedMouseOut('" + asin + "', 'alt');"+'"'+" ");
    document.write("id="+'"'+"" + imageID + ""+'"'+" ");
    document.write("border="+'"'+"0"+'"'+" valign="+'"'+"bottom"+'"'+" />");
}

function cunan_showExcluded(asin, excluded, showNeg){
    showNeg = showNeg ? showNeg : 0;
    var imageID = "excluded." + asin;
    var excVal = (excluded == 1 && showNeg == 0) || (excluded == 0 && showNeg == 1) ? 1 : 0;
    document.write("<img src="+'"'+"" + checkboxImages[excVal] + ""+'"'+"");
    document.write("onclick="+'"'+"cunan_sendExcluded('" + asin + "', 'alt', '" + showNeg +"');"+'"'+" ");
    document.write("onmouseover="+'"'+"cunan_excludedMouseOver('" + asin + "', 'alt', '" + showNeg +"');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_excludedMouseOut('" + asin + "', 'alt', '" + showNeg +"');"+'"'+" ");
    document.write("id="+'"'+"" + imageID + ""+'"'+" ");
    document.write("border="+'"'+"0"+'"'+" valign="+'"'+"bottom"+'"'+" />");
}

function cunan_showExcludedClickstream(asin, excluded){
    var imageID = "excludedClickstream." + asin;
    document.write("<img src="+'"'+"" + checkboxImages[excluded] + ""+'"'+"");
    document.write("onclick="+'"'+"cunan_sendExcludedClickstream('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseover="+'"'+"cunan_excludedClickstreamMouseOver('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_excludedClickstreamMouseOut('" + asin + "', 'alt');"+'"'+" ");
    document.write("id="+'"'+"" + imageID + ""+'"'+" ");
    document.write("border="+'"'+"0"+'"'+" valign="+'"'+"bottom"+'"'+" />");
}


function cunan_showStars(asin, rating){
    var starID = "stars." + asin;

    starTwinkler[asin] = 0;
    msgTwinkler[asin] = 0;

    document.write("<map style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" name="+'"'+"starmap" + asin +""+'"'+">");

    var i = 0;
    for (i = 0; i < 6; i++) {
        document.write("<area shape="+'"'+"rect"+'"'+" " + 
            "coords='" + starMap[i] + "' " +
            "onmouseover="+'"'+"cunan_starMouseOver('" + asin + "'," + i + ");"+'"'+" " +
            "onmouseout="+'"'+"cunan_starMouseOut('" + asin + "');"+'"'+" " +
            "onClick="+'"'+"cunan_sendStars('" + asin + "'," + i + ");" +
            ""+'"'+" />");
    }
    document.write("</map>");
    document.write("<img src="+'"'+"" + starImages[rating] + ""+'"'+"");
    document.write(" valign="+'"'+"bottom"+'"'+" border="+'"'+"0"+'"'+" usemap="+'"'+"#starmap" + asin);
    document.write(""+'"'+" id="+'"'+"" + starID + ""+'"'+" />");
}


function cunan_showMessages(asin, defaultMsg){
    if (defaultMsg) {
        starMessages[6] = starMessages[defaultMsg];
    }
    document.write("<span id="+'"'+"messages." + asin + ""+'"'+" class="+'"'+"tiny"+'"'+">" + starMessages[6]  + "</span>");
}

function cunan_showIsOwnedMessages(asin){
    document.write("<span style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" id="+'"'+"ownIt.messages." + asin + ""+'"'+" ");
    document.write("onclick="+'"'+"cunan_sendIsOwned('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseover="+'"'+"cunan_isOwnedMouseOver('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_isOwnedMouseOut('" + asin + "', 'alt');"+'"'+" ");
    document.write("class="+'"'+"tiny"+'"'+">" + ownItMessages[0] + "</span>");
}

function cunan_showNotInterestedMessages(asin){
    document.write("<span style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" id="+'"'+"notInterested.messages." + asin + ""+'"'+" ");
    document.write("onclick="+'"'+"cunan_sendNotInterested('" + asin + "', 'alt'); "+'"'+"");
    document.write("onmouseover="+'"'+"cunan_notInterestedMouseOver('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_notInterestedMouseOut('" + asin + "', 'alt');"+'"'+" ");
    document.write("class="+'"'+"tiny"+'"'+">" + notInterestedMessages[0] + "</span>");
}

function cunan_showExcludedMessages(asin, msg, showNeg){
    msg = msg ? msg : 0;
    showNeg = showNeg ? showNeg : 0;
    document.write("<span style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" id="+'"'+"excluded.messages." + asin + ""+'"'+" ");
    document.write("onclick="+'"'+"cunan_sendExcluded('" + asin + "', 'alt', '" + showNeg +"'); "+'"'+"");
    document.write("onmouseover="+'"'+"cunan_excludedMouseOver('" + asin + "', 'alt', '" + showNeg + "');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_excludedMouseOut('" + asin + "', 'alt', '" + showNeg + "');"+'"'+" ");
    document.write("class="+'"'+"tiny"+'"'+">" + excludedMessages[msg] + "</span>");
}

function cunan_showExcludedClickstreamMessages(asin){
    document.write("<span style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" id="+'"'+"excludedClickstream.messages." + asin + ""+'"'+" ");
    document.write("onclick="+'"'+"cunan_sendExcludedClickstream('" + asin + "', 'alt'); "+'"'+"");
    document.write("onmouseover="+'"'+"cunan_excludedClickstreamMouseOver('" + asin + "', 'alt');"+'"'+" ");
    document.write("onmouseout="+'"'+"cunan_excludedClickstreamMouseOut('" + asin + "', 'alt');"+'"'+" ");
    document.write("class="+'"'+"tiny"+'"'+">" + excludedMessages[0] + "</span>");
}
var savedVoteGoodsBads = new Array();
function vote(comm_id,type,flag,isLogin){
    if(parseInt(isLogin)==1){
        var span_id = 'span_comm_'+flag+'_'+comm_id; 
        if(isInArray(savedVoteGoodsBads,comm_id)){
            oo(span_id).innerHTML = "您已经投过票了，谢谢您的关注。";
            return;
        }else
            oo(span_id).innerHTML = "已保存，谢谢您的投票。"; 
             
        savedVoteGoodsBads[savedVoteGoodsBads.length] = comm_id;    
        window.setTimeout("sendVoteGoodsBads('"+comm_id+"','"+type+"','"+ flag +"')", delayTime); 
    }else{
        window.location = "login.aspx?url="+escape(window.location);
    }       
}
function sendVoteGoodsBads(comm_id,type,flag){//有用，无用    
     var parms = new Array();
     parms[0] = new Array("comm_id",comm_id);
     parms[1] = new Array("type",type);     
     var result = postPageData('Ajax/service_do.aspx?Module=UPDATECOMMENTVOTEGOODSBADS', "POST",parms);
     var span_id = 'span_comm_'+flag+'_'+comm_id; 
     if(result == "2")
         oo(span_id).innerHTML = "您已经投过票了，谢谢您的关注。";
     else if(result == "1"){
         if(type == "good")
         {
            var o = oo("span_goods_" + comm_id);  
            o.innerHTML = parseInt(o.innerHTML) + 1 ;
         }
         var ooo = oo("span_goods_bads_" + comm_id);
         ooo.innerHTML = parseInt(ooo.innerHTML) + 1 ; 
     }
}
function isInArray(arr,value){
    var flag = false;
    for(var i=0;i<arr.length;i++){
        if(arr[i]==value){
            flag = true;
            break;
        }
    }
    return flag;
}
