/* $Id: poll.js 28374 2009-06-02 12:02:07Z selvakumarp $ */
	var votedID;
    jQuery(document).ready(function(){
        if (jQuery("#poll-results").length > 0 ) {
            animateResults();
        }
    });

    function vote(voteUrl,pollQuestionId){
        var id = jQuery("input[@name='"+"pollOption"+pollQuestionId+"']:checked").attr("value");
        id = id.replace("opt",'');
        if(id ==null){
            message = "<h2 class=\"ajaxPopH2\" style=\"padding:0px\">Error</h2><div class=\"ajaxPopContent\" style=\"color:#000000;padding-bottom:20px;text-align:center\">Please select an option</div>";
            jQuery.facebox(message);
            return;
        }        
        votedID = id;
        loadResults(voteUrl+"&o="+votedID+"&q="+pollQuestionId);
    }

    function animateResults(){
        jQuery("#poll-results div").each(function(){
            var percentage = jQuery(this).next().text();
            jQuery(this).css({width: "0%"}).animate({width: percentage}, 1000);
        });
    }

    function loadResults(url) {
        var varHeight = jQuery("#poll-container").height();    	
        jQuery("#poll").fadeOut("slow",function(){
            jQuery("#poll-container").css({height: varHeight});
            jQuery("#poll-container").empty();
            //Make an Ajax call to Server.
            var ajaxReq = getHttpRequest();
            if (ajaxReq) {
                ajaxReq.onreadystatechange = function() {
                    if (ajaxReq.readyState == 4 && ajaxReq.status == 200) {
                        var results_html = ajaxReq.responseText;
                        jQuery("#poll-container").append(results_html).fadeIn("slow",function(){
                            var pollResultsHight = jQuery("#poll-results").height();
                            jQuery("#poll-container").css({height: varHeight}).animate({height: pollResultsHight+20}, 1000,function(){
                                animateResults();
                            });
                        });
                    }
                }
            };
            ajaxReq.open('POST', url);
            ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            ajaxReq.send(null);
        });
    }
    function showPoll(url){
        var varHeight = jQuery("#poll-container").height();
        jQuery("#poll-results").fadeOut("slow",function(){
            jQuery("#poll-container").css({height: varHeight});
            jQuery("#poll-container").empty();
            //Make an Ajax call to Server.
            var ajaxReq = getHttpRequest();
            if (ajaxReq) {
                ajaxReq.onreadystatechange = function() {
                    if (ajaxReq.readyState == 4 && ajaxReq.status == 200) {
                        var results_html = ajaxReq.responseText;
                        jQuery("#poll-container").append(results_html).fadeIn("slow",function(){
                            var pollHight = jQuery("#poll").height();
                            jQuery("#poll-container").css({height: varHeight}).animate({height: pollHight+20}, 1000);
                        });
                    }
                }
            };
            ajaxReq.open('POST', url);
            ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
            ajaxReq.send(null);
        });
    }
    function getHttpRequest(){
        var request;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
            request = new ActiveXObject("Microsoft.XMLHttp");
        }else{
            request = new XMLHttpRequest();
        }
        return request;
    }