/**
 * Track usage of the plusone button
 */
function plusone_vote( obj ) {
    _gaq.push(['_trackEvent','plusone',obj.state]);
}

jQuery(document).ready(function($){

    // Call functions
    try{
        search_text();
        initGMaps();
        formStuff();
    }catch(err){

    }


	/* Opening Times */
	var term_time = false;

	if(term_time == true){

	//Term Times
	$('#holiday').hide();

	$('#termTime p.toggle a').click(function(){
		$('#termTime').slideUp(function(){$('#holiday').slideDown();});
		return false;
	});
	$('#holiday p.toggle a').click(function(){
		$('#holiday').slideUp(function(){$('#termTime').slideDown();});
		return false;
	});
	}else{





        //Holiday Time
        $('#termTime').hide();

        $('#holiday p.toggle a').click(function(){
                $('#holiday').slideUp(function(){$('#termTime').slideDown();});
                return false;
        });
        $('#termTime p.toggle a').click(function(){
                $('#termTime').slideUp(function(){$('#holiday').slideDown();});
                return false;
        });
	}


	/* Highlight Special Sessions */
	$('.ao').hover(function(){
		$('.ao').addClass('highlight');
	},function(){
		$('.ao').removeClass('highlight');
	});
	$('.fo').hover(function(){
		$('.fo').addClass('highlight');
	},function(){
		$('.fo').removeClass('highlight');
	});

        /* Fade out message after a bit */
        $('.msg_notice, .msg_alert').hide().fadeIn(300,function(){
            $(this).delay(5000).fadeOut(300,function(){
                $(this).remove();
            });
        });

        // Confirm deletion of a confirmed booking
        $('.delete').click(function(e){
           var answer = confirm( 'Are you sure you want to delete this booking? It cannot be recoverd.' );
           if( !answer ){
                e.preventDefault();
           }
        });


       // Create a mask element to show over the other calendar items
       var mask = document.createElement('div');
       $(mask).addClass('mask');
       $('body').append( $(mask).hide() );

        $('li.manager').each(function(i,e){
            var that = $(this);
            var hiddenform = that.find('.hiddenform');

            // Click event for list item
            var clickFunction = function(theevent)
            {
                // Activate
                hiddenform.fadeIn();
                that.addClass('active');
                that.active = true;

                // unbind click event so it doesnt refire itself when the form is clicked
                that.unbind('click');

                $(mask).fadeIn();

                that.find('input, select').change(function(){
                    that.changed = true;
                    that.addClass('changed');
                    window.changes = true;
                });

                // click function for close button
                that.find('.close').click(function(e){
                    hiddenform.fadeOut();
                    that.removeClass('active');

                    $(mask).fadeOut(function(){
                        // rebind click function to the list item after its been de-activated
                        that.bind('click',clickFunction);
                    });
                    
                    that.active = false;
                });
            };


            that.active = false;

            // Initial bind of click event
            that.bind('click',clickFunction);


        });


        $('form#assign_members').submit(function(){
           window.changes = false;
        });

        // if the page is left by not submitting, add an interruption inviting them to save the changes
        window.onbeforeunload = function(e){
            if( window.changes ){
               var answer = confirm( 'You havent saved your changes, would you like to?' );
               if( answer ){
                    $('form#assign_members').trigger('submit');
                    e.preventDefault();
               }
           }
        };




        $('.direct_add_box').hide();
        $('.direct_add').click(function(){
            $(this).siblings('.direct_add_box').slideDown();
        }).css({'cursor':'pointer'});


        

	

});


// Put text in the search box and remove it and change the colour when it needs to be used.
var search_text = function()
{
    var search_advice_text = 'Enter Keyword(s)';


    $('#f_search').val( search_advice_text ).css({'color':'#ccc'});

    $('#f_search').focus(function(){
        if( $(this).val().length == 0 || $(this).val() == search_advice_text ){
            $(this).val( '' ).css({'color':'#444'});
        }
    }).blur(function(){


        if( $(this).val().length == 0 ){
            $(this).val( search_advice_text ).css({'color':'#ccc'});
        }
    });

}

// Initiate Google Maps API
var initGMaps = function() {
    if( typeof(google.maps) != 'object' )
        return false;

    //Setup the map
    var latlng = new google.maps.LatLng(51.51084014086928, -0.13);

    var myOptions = {
          zoom: 13,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          mapTypeControl:false
        }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var shape = {
      coord: [15,15,15],
      type: 'circle'
    };
/*
    var myicon = new google.maps.MarkerImage('/wp-content/themes/wsp/images/map-marker.png',
        new google.maps.Size(40, 50),
        new google.maps.Point(0,10), // origin
        new google.maps.Point(20,20)); // anchor
*/
    var marker = new google.maps.Marker({
        position: latlng,
        map: map
        //shape: shape,
        //icon: myicon

    });

}


// Form Functions

var formStuff = function()
{
    $('.errormsg').hide().delay(300).fadeIn(500);

    
    /**
     * Adjust status of checkbox on click events
     */
    var booking_item_click = function()
    {
        var $checkbox = $(this).children('input');

        if( $checkbox.is(':checked') ){
            $checkbox.removeAttr('checked');
            $checkbox.parent('li').removeClass('selected');
        }else{
            $checkbox.attr('checked', 'checked');
            $checkbox.parent('li').addClass('selected');
        }

    }

    /**
     * Add class for selected items on load
     */
    var booking_item = function()
    {
        var $checkbox = $(this).children('input');

        if( $checkbox.is(':checked') ){
            $checkbox.parent('li').addClass('selected');
        }else{
            $checkbox.parent('li').removeClass('selected');
        }

    }

    $('.booking_list li').click( booking_item_click ).each( booking_item );

    $('.prog-disc h3').click(function(){
        $(this).siblings('.inner').slideToggle().prepend( $(this) );
    }).siblings('.inner').hide();




        


}





var $buoop = {}
$buoop.ol = window.onload;
window.onload=function(){
 if ($buoop.ol) $buoop.ol();
 var e = document.createElement("script");
 e.setAttribute("type", "text/javascript");
 e.setAttribute("src", "http://browser-update.org/update.js");
 document.body.appendChild(e);
}





















