jQuery.fn.stars = function(a) {
	return this.each(function(i) {
		if($.browser.msie && ($.browser.version == 6)) this.src = "/sec/images/ico/stars.gif";
		var wL = $("#wrap")[0].offsetLeft;
		var pL = $(this).parents("li")[0].offsetLeft;
		var voteL = 60-Math.round((parseFloat(stars[i].vote)/5)*60);
		$(this).prev().css({"left":"-"+voteL+"px"}, 500);
		
		$(this).mousemove(function(e){
			if($.browser.msie)
				var L = 60-(Math.floor(e.originalEvent.offsetX/12)+1)*12;
			else
				var L = 60-(Math.floor(e.originalEvent.layerX/12)+1)*12;
			$(this).prev().css("left","-" + L + "px");
		}).mouseout(function(){
			var voteOut = 60-Math.round((parseFloat(stars[i].vote)/5)*60);
			$(this).prev().animate({"left":"-"+voteOut+"px"}, 500);
		}).click(function(e){
			if($.browser.msie)
				var L = Math.floor(e.originalEvent.offsetX/12)+1;
			else
				var L = Math.floor(e.originalEvent.layerX/12)+1;
			var id = $(this).parents("li").attr("class").match(/id\-\d+/)[0].substring(3);
			if($.cookies.get('usrprof', true)){
				cardVote({"user_cards_id":id,"rating":L, "card":this, "i":i});
			} else {
				$("#global-nav .popup-login").click();
				loginVote = {"user_cards_id":id,"rating":L, "card":this, "i":i};
			}
		});
	});
};

var cardVote = function(data){
	var newData = {"user_cards_id":data.user_cards_id, "rating":data.rating};
	$.getJSON("/usercards/ratecard",newData,function(d){
		if(d.status=="0"){
			if(d.message!="Rating updated.") stars[data.i].count = parseFloat(stars[data.i].count)+1;
			stars[data.i].vote = Math.round(parseFloat(d.updatedRating)*100)/100;
			var voteL = 60-Math.round((parseFloat(stars[data.i].vote)/5)*60)
			
			$(data.card).prev().animate({"left":"-"+voteL+"px"}, 500);
			$(data.card).parents("li.card").find("i:eq(2)").html(stars[data.i].vote + " (" + stars[data.i].count + " votes)");
			showMessage(d.message,"Vote Successful");
		} else
			showMessage(d.message,"Error in Voting");
	});
	loginVote = false;
}
var jk;

function flagCard(unique_id) {
	var content = '<form method="get" action="">';
	content += '<div class="fields">';
	content += '<label for="name">Name: </label><input id="name" value="" tabindex="40" name="name" type="text"><br />';
	content += '<label for="email">Email: </label><input id="email" tabindex="41" value="" name="email" type="text"><br />';
	content += '<label for="reason">Reason: </label><input id="reason" tabindex="42" value="" name="reason" type="text">';
	content +=  '</div></form>';
	

	showMessage(content,"Why do you think this card should be removed from the site?");

	$("#popup input#name").focus();

	$("#popup div.link-button a.button").html("Submit").unbind("click").click(function(){
		var popup = $("#popup");
		var inputs = $("input",popup);
		var error = false;
		
		inputs.each(function(i){
                    var jqT = $(this);
                    if(this.value == ""){
                        jqT.addClass("error").focus(function(){
                            this.value = "";
                            jqT.removeClass("error").unbind("focus");
                            //if(!$.browser.msie) if(i==1) this.type = "password";
                        }).keydown(function(){ $(this).removeClass("error").unbind("focus").unbind("keydown"); });
                        switch(i){
                            case 0:
                                this.value = "Please enter Full Name";
                                break;
                            case 1:
                                //if(!$.browser.msie) this.type = "text";
                                this.value = "Please enter E-Mail";
                                break;
                            case 2:
                                //if(!$.browser.msie) this.type = "text";
                                this.value = "Please enter Reason";
                                break;
                        }
                        error = true;
                    }
                    switch(i){
                        case 0:
                            if(this.value.toLowerCase()=="please enter full name") error = true;
                            break;
                        case 1:
                            if(this.value.toLowerCase()=="please enter e-mail") error = true;
                            break;
                        case 2:
                            if(this.value.toLowerCase()=="please enter reason") error = true;
                            break;
                    }


		});
		if(!error){
			$("div.link-button .ajax",popup).show();
			$.get("/usercards/flag/" + unique_id + "/?" + $("form",popup).serialize(),function(r){
				$("div.link-button .ajax",popup).hide();
				var ret = eval("(" + r + ")");
				if(ret.status=="OK"){
					showMessage(ret.message,"Flag Successful!");
				} else
					showMessage(ret.message,"Flag Failed!");
			});
		}
		
		return false;
	});

        //$(".close",popup).click(function(){$("#popup").jqmHide();});

	return false;
}
