//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
    }
}
// object used to download PHP voteions
var req = getXmlHttpRequestObject();
var voteElem;
// called from keyUp on textbox
function Vote(id, dir) {
    if (req.readyState == 4 || req.readyState == 0) {
        req.open("GET", '/Ajax/Vote.php?id=' + id + '&how=' + dir + '&cachekill=' + parseInt(Math.random()*99999999), true);
        req.onreadystatechange = handlevote; 
        req.send(null);
    }   
}
function VoteUp(id) {
    Vote(id, 'Up');
}
function VoteDown(id) {
    Vote(id, 'Down');
}
function handlevote() {
    //alert(input);
    if (req.readyState == 4) {
	   var content = req.responseText;
	   //alert(content);
	   var ret = new Array();
	   ret = eval('(' + content + ')');
	   var success = ret[0];
	   var id = ret[1];
	   var dir = ret[2];
	   var i;
       if (success) {
            alert("Thanks for your vote!");
            curVotes = parseInt(document.getElementById('Votes' + dir + id).innerHTML);
            curVotes++;
            document.getElementById('Votes' + dir + id).innerHTML = curVotes;
       } 
       else {
            alert("You have already voted!");
       }
    }
}
/* Rating */ 
$(function () {
    $("div#rating img").each(function (i) {
        var shopId = $(this).parent().attr('shopid');
        $(this).bind('click', function() {
            $.post("/Ajax/Rate.php", "ShopID=" + shopId + "&Vote=" + (i+1), function(data) {
                if(data) {
                    alert("Thanks for your vote!");
                }
                else {
                    alert("You have already voted!");
                }
            }, "json");
        });
        $(this).bind('mouseover', function() {
            $(this).parent().children().each(function(j) {
                this.oldSrc = this.src;
                if (j <= i) {    
                    this.src = '/Static/Images/star.png';
                }
                else {
                    this.src = '/Static/Images/starGray.png';
                }
            })
        });    
        $(this).bind('mouseout', function() {
            $(this).parent().children().each(function(j) {
                
                this.src = this.oldSrc;
            })
        });
    })
});
/*  Comments  */
function HideComments(id) {
    document.getElementById("Comments" + id).innerHTML = '';
}
function ShowComments(CouponID) {
    $.getJSON('/Ajax/CouponComments.php?CouponID=' + CouponID, function(data) {
       var content = "";
        for (i = 0; i < data.Comments.length; i++) {
        //alert(ret[i]);
         //var elems = eval('(' + ret[i] + ')');
           var comm = data.Comments[i];
           var author = '';
           if (comm.Nick != null) {
            author = "<a href='/" + data.Config.ProfileSection + "/" + comm.URLNick + "'>" + comm.Nick + "</a>";
           }
           else {
            author = comm.Author;
           }
           content += '<div class="revised_date_img"><img src="/Static/Images/date_img_03.gif" alt="" /></div>';
           content +=    '<div class="revised_date_right">';
           content +=    '<div class="revised_date_1">Posted by:<span class="commentAuthor" > ' + author + '</span> @ ' + comm.Added + '</div>';
           content +=    '<div class="revised_date_2">' + comm.Body + '</div>';
           content += '</div>'
           content += '<div class="clear"></div>'
           content += '<div class="revised_bottom_border"><img src="/Static/Images/border_img.gif" alt="" /></div>'
      
       }
        $("#comments" + CouponID).hide().html(content).slideDown(2000);

      
    });

    return false;
}