﻿
if ( swfobject.hasFlashPlayerVersion( "9.0.0" ) ) 
{
    var fn = function() 
    {
        var att = { data:"/resources/flash/map_v6.swf", width:"280", height:"190", id: "district-map", name: "district-map" };
        var par = { wmode: "transparent" };
        var id = "district-map";
        var o = swfobject.createSWF( att, par, id );
        
        // Attach click event to all district checkboxes                    
        
        var i = 1;
        
        $( ".district-checkbox-list input" ).each( function( e ) {
            
            this.expandoIndex = i;
            
            $( this ).click( function() { 
                //alert( "exp: " + this.expandoIndex + " chk: " + this.checked );                
                o.SetVariable( "chk" + this.expandoIndex, ( this.checked ) ? "on" : "off" );
            } );
            
            i++;			                        
        } );

    };
    
    swfobject.addDomLoadEvent( fn );
}


//$(document).ready(function() {

//    var jsonString = "{";

//    $(".district-checkbox-list input").each(function(e) {

//        this.expandoIndex = i;

//        $(this).click(function() {
//            //alert( "exp: " + this.expandoIndex + " chk: " + this.checked );
//            //o.SetVariable("chk" + this.expandoIndex, (this.checked) ? "on" : "off");
//            jsonString = jsonString + "chk" + this.expandoIndex + ", " + (this.checked) ? "'on'" : "'off'";
//        });

//        i++;
//    });

//    $('#district-map').flash(
//        {
//            src: '/resources/flash/map_v6.swf',
//            width: 280,
//            height: 190,
//            flashvars: { foo: 'bar', baz: 'zoo' }
//        },
//        { version: 9 }
//    );
//});


function clickCheckboxByIndex( index )
{
    // Function called from the flash to check / un-check item
    // Gets all inputs and then filters it down to one using .eq helper via index
    
    var cb = $( ".district-checkbox-list input" ).eq( ( index - 1 ) );
    
    // Set to opposite state
    $( cb ).attr( "checked", !$( cb ).attr( "checked" ) );
}


$( document ).ready( function() {

    $( ".select-deselect-all-district" ).click( function() {
    
        var o = swfobject.getObjectById( "district-map" );
        
        var selectedFound = false;
        
        $(".district-checkbox-list input").each( function()
        {
            if( this.checked == true )
            {
                selectedFound = true;
            }
        });
        
        
        if( selectedFound == true )
        {    
            $(".district-checkbox-list input").each( function()
            {
                this.checked = false;
                
                //alert( "exp: " + this.expandoIndex + " chk: " + this.checked );                
                o.SetVariable( "chk" + this.expandoIndex, ( this.checked ) ? "on" : "off" );

            });
        }
        else
        {
            $(".district-checkbox-list input").each( function()
            {
                this.checked = true;
                
                //alert( "exp: " + this.expandoIndex + " chk: " + this.checked );                
                o.SetVariable( "chk" + this.expandoIndex, ( this.checked ) ? "on" : "off" );

            });
        }
    });

} );