﻿$(document).ready(function() {
    $('ul.polls li a[rel^=poll_]').live('click', function() {
        //clear checkboxes
        $('.pollchoices li input[type=checkbox]:checked').each ( function () {
            $(this).attr('checked', false);
        });
        //load popup
        centerPopup($(this).siblings('.pollchoices:first'));
        loadPopup($(this).siblings('.pollchoices:first'));
    });
    
    $('.cancelvote_button').live('click', function() {
        disablePopup($(this).parents('ul.pollchoices:first'));
    });
    
    $('.vote_button').live('click', function() {
        var selections;
        selections = '';
        var parentid = $(this).parents('ul.pollchoices:first').parent().attr('id');
        //var count = 0;
        $('li [id=' + parentid + '] ul.pollchoices li input[type=checkbox]:checked').each( function() {
             //selections[count] = $(this).attr('id').split('_')[2];
             //count++;
             if (selections == '' || selections == null) selections =  '["' + $(this).attr('id').split('_')[2] + '"';
             else selections = selections  + ',"' + $(this).attr('id').split('_')[2] + '"';
        });
        selections = selections + ']';
        //var query = { "pollid" : $(this).parents('ul.pollchoices:first').parent().attr('id').split('_')[1], "selections" : selections };
        var query = '{ "pollid" : ' + $(this).parents('ul.pollchoices:first').parent().attr('id').split('_')[1] + ', "selections" : ' + selections + '}';
        $.ajax({
            type: "POST",
            url: "/default.aspx/vote",
            data: query,//JSON.stringify(query),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(obj) {        
                alert ('Thank you for your vote.');
            }
        });
        disablePopup($(this).parents('ul.pollchoices:first'));
    });
    
    $('ul.pollchoices li input[type=checkbox]').live('click', function() {
        if ($(this).attr('checked') == true) {
            var parentid = $(this).parents('ul.pollchoices:first').parent().attr('id');
            var max = parentid.split('_')[2];
            var count = $('li [id=' + parentid + '] ul.pollchoices li input[type=checkbox]:checked').length;
            if (count > max) {
                alert('You can only select ' + max + ' from the list.');
                $(this).attr('checked', false);
            }
        }
    });
});

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function centerPopup(elem){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = elem.height();
	var popupWidth = 600;//$("#" + id).width();
	//centering
	var top = windowHeight/2-popupHeight/2;
	if (top < 10) top = 10;
	elem.css({
		"position": "absolute",
		"top": top,
		"left": windowWidth/2-popupWidth/2,
		"width": "600px",
		"z-index": "300"
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function loadPopup(elem){
	//loads popup only if it is disabled
	if(popupStatus==0){
	    $("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		//$("#" + id + " ul.pollchoices").show();
		elem.fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(elem){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").hide(); //.fadeOut("slow");
		elem.fadeOut("slow");
		popupStatus = 0;
	}
}
