﻿// This js file implement all the rating/flag features. 
// All code should be copied from other files to here.

//Positive & negative
        function RateRequest(event){        
            var saveEvent = event;
            var rating = -1;
            if($(this).hasClass("upRequest")){
                rating = 1;            
                $(this).unbind('click').unbind('mouseenter').trigger('mouseleave').unbind('mouseleave');
                $(this).parent().next().find('img').unbind('click').unbind('mouseenter').unbind('mouseleave');
            }else{
                $(this).unbind('click').unbind('mouseenter').trigger('mouseleave').unbind('mouseleave');
                $(this).parent().prev().find('img').unbind('click').unbind('mouseenter').unbind('mouseleave');
            }
            var obj = $(this);
            $.post('/AjaxPostback',{Action:'rating',objId:obj.attr("rid"),objType:obj.attr('objType'),rating:rating},function(data){
                eval('ret = ' + data );
                if(ret.ret == 0){
                    if(obj.attr('objType') == "coupon"){
                        obj.parent().parent().prev().find('img').css('margin-left',ret.rate + "%");
                        obj.parent().parent().prev().prev().find('spam').html(ret.rate + "%");
                        if(obj.hasClass("upRequest")){
                            obj.next().html("+" + ret.p_rating);
                        }else{
                            obj.next().html("-" + ret.n_rating);
                        }
                    }else{
                        if(obj.hasClass("upRequest")){
                            if(obj.next().length){
                                obj.next().html("+" + ret.p_rating);
                            }else{
                                obj.html("+" + ret.p_rating);
                            }
                        }else{
                            if(obj.next().length){
                                obj.next().html("-" + ret.n_rating);
                            }else{
                                obj.html("-" + ret.n_rating);
                            }
                        }
                    }
				    var popDiv = $('<div></div>');
				    popDiv.attr('style',"width:230px; height:17px; background:transparent url(images/small_popup_1.gif) top left no-repeat; color:#b5481b; position:absolute; padding:8px 0px 8px 0px; font-size:11px; font-weight:bold; z-index:1201; text-align:center");
				    popDiv.html("Thank You! Rating has been received.");
                    mousePos(saveEvent);
				    popDiv.css('top',posy-40+"px");
				    popDiv.css('left',posx-120+"px");
				    $('body').append(popDiv);
    			    var removeIt=setTimeout(function(){ popDiv.remove() },1500);
                }else{
                    alert(ret.msg);
                }
            });
        }
        
        
$(document).ready(function(){
    if(gLoggedIn){
        //5 Start Rating function, Only take effect when user is logged in
        $(".canRate>li").hover(function(){
           $(this).find("img").attr("src","/images/star_2.gif");
           $(this).prevAll().find("img").attr("src","/images/star_2.gif");
           $(this).nextAll().find("img").attr("src","/images/star_0.gif");
        },function(){
            var tParent = $(this).parent();
            tParent.find("li:lt(" + tParent.attr('rating') + ")").find("img").attr("src","/images/star_1.gif");
        }).click(function(event){
            //Do Rating
            var tParent = $(this).parent();
            var saveEvent = event;
            tParent.find("li").unbind('mouseenter').unbind('mouseleave');
            $(this).unbind('click');
            $.post('/AjaxPostback',{Action:'rating',objId:tParent.attr("objId"),objType:tParent.attr('objType'),rating:$(this).find('img').attr('alt')},function(data){
                eval('ret = ' + data );
                if(ret.ret == 0){
                    tParent.find("li:lt(" + ret.avgRating + ")").find('img').attr("src","/images/star_1.gif");
                    if(tParent.attr('objType')=="help")
                    {
                        var votenum=tParent.parent().next().children().eq(0).text();
                        var vote=tParent.parent().next().children().eq(1).text();
                        tParent.parent().next().children().eq(0).text(++votenum);
                        if(votenum>0)
                        {
                         
                           tParent.parent().next().children().eq(1).text(" votes");
                        }
                        else
                        {
                          tParent.parent().next().children().eq(1).text(" vote");
                        }
                    }
				    var popDiv = $('<div></div>');
				    popDiv.attr('style',"width:230px; height:17px; background:transparent url(images/small_popup_1.gif) top left no-repeat; color:#b5481b; position:absolute; padding:8px 0px 8px 0px; font-size:11px; font-weight:bold; z-index:1201; text-align:center");
				    popDiv.html("Your rating has been received.");
                    mousePos(saveEvent);
				    popDiv.css('top',posy-40+"px");
				    popDiv.css('left',posx-120+"px");
				    $('body').append(popDiv);
    			    var removeIt=setTimeout(function(){ popDiv.remove() },1500);
                }else{
                    alert(ret.msg);
                }
            });
        });
        
        
        $('.upRequest').click(RateRequest);
        $(".downRequest").click(RateRequest);
        
        //add wishlist
        $(".wishit").click(function(event){
            var saveEvent = event;
            $.post("/handler/wishlist.ashx",{ productId : $(this).attr('productId'), productType : $(this).attr('productType') ,action:"add"},function(data){
                eval('ret = ' + data);
                if(ret.ret == 0){
                    //Submit successfully
                    var popDiv = $('<div></div>');
				    popDiv.attr('style',"width:230px; height:17px; background:transparent url(images/small_popup_1.gif) top left no-repeat; color:#b5481b; position:absolute; padding:8px 0px 8px 0px; font-size:11px; font-weight:bold; z-index:1201; text-align:center");
				    popDiv.html("You have add this product to your wishlist");
                    mousePos(saveEvent);
				    popDiv.css('top',posy-40+"px");
				    popDiv.css('left',posx-120+"px");
				    $('body').append(popDiv);
    			    var removeIt=setTimeout(function(){ popDiv.remove() },1500);
		        }else{
		            alert(ret.msg);
		        }
            });
        });
    }else{
        $(".canRate>li,.upRequest,.downRequest").click(function(){
            regLoginPopups(0);
            return;
        });
    }
    
    //Report Function    
    $('<div class="popUp238x150" id="confirmReport" style="display:none;">\
		<h6><span>Confirmation</span> <a>x</a></h6>\
		<div class="popUp238x150c">\
		 <ul class="confirmUl">\
		  <li id="reportTitle">Are you sure you want to cancel this request? </li>\
		  <li class="yes"><a href="javascript:void(0);" class="btnSmallGray btnSmallGrayY">Yes</a></li>\
		  <li class="no"><a href="javascript:void(0);" class="btnSmallGray btnSmallGrayN">No</a></li>\
		 </ul><div class="ie6clearFix">&nbsp;</div>\
		</div>\
	   </div>').appendTo($('body'));
});

function showReportDlg(obj,useridVal,spamidVal,typeVal,offset){
        $("#reportTitle").text("Are you sure you want to report this " + typeVal + "?");
        $("#confirmReport").find('.btnSmallGrayY').css("display","block");
        $("#confirmReport").find('.btnSmallGrayN').css("display","block");	    
        pos = findPos($(obj));
        $("#confirmReport").find('.btnSmallGrayY').unbind('click').click(function(){           
            $.get("/handler/MemberReportHandler.ashx",{userid:useridVal,spamid:spamidVal,type:typeVal},function(data){
                if(data=="true")
                    $("#reportTitle").text("Report Successfully.");
                else if(data=="repeat")
                    $("#reportTitle").text("You already report this " + typeVal + ", thanks."); 
                else
                    $("#reportTitle").text("Report Failed");
                $("#confirmReport").find('.btnSmallGrayY').css("display","none");
                $("#confirmReport").find('.btnSmallGrayN').css("display","none");
                setTimeout(function(){
                    $("#confirmReport").css("display","none");
                },"1000")
            })
        });
        $("#confirmReport").find('.btnSmallGrayN,a:eq(0)').unbind('click').click(function(){
            $("#confirmReport").css("display","none");
        });
        if(offset){
            $("#confirmReport").css('left',(pos.x-offset.x)).css('top',(pos.y-offset.y)).show();
        }else{
            $("#confirmReport").css('left',(pos.x-94)).css('top',(pos.y-151)).show();
        }
        return false;
    }