if ( window.console == undefined || window.console.log == undefined ) {	window.console = { log:function(msg){ /* msg */ } } }
if ( typeof(window.billboard)=="undefined"){ billboard={}; }

billboard.tourFinder = new ( function($) {

    var me = this;
    me.months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    me.init = function(){
		billboard.broadcaster.addListener( "pageLoaded", function() { me.onPageLoaded(); } );
    };

    me.onPageLoaded = function(){
     // $("#command").attr("onSubmit","return billboard.tourFinder.findShows(this);");
	    if(document.getElementById('tourform')) {
		document.getElementById('tourform').onsubmit=function() { return billboard.tourFinder.findShows(this) };
		}
        $("#select-state span").html($("select#state option:selected").html());
        $("#select-state select").change(function(){
            $("#select-state span").html($("select#state option:selected").html());
        }); 
        
        if (billboard.section == 'events'){
            var todaysDate = new Date();
            me.loadMonths(todaysDate);
            billboard.calendar.pickType = 'week';
            billboard.calendar.setMonth(0);
        }
    };

    me.findShows = function(form){
/* not needed
        $("#sub-content .module-middle").html(
            '<div class="units-wrapper">'
            +'<div class="unit">'
            +'<div class="load-ajax">'
            +'<img src="/images/icons/ajax-loader.gif" />'
            +'</div>'
            +'</div>'
            +'</div>'
        );
*/
        var url = $(form).attr("action") + "?" + $(form).serialize();
        billboard.history.setHash(url);
        return false;
    };

    me.loadMonths = function(currentDate){
        var firstMonth = currentDate.getMonth();
        var options = "";
        var mnt = new Date();
        var increment = 0;
		
        for (var i = 0;i <= 11;i++){
            if ((i + firstMonth) > 11){
                increment = (i + firstMonth)-12
                mnt.setFullYear(currentDate.getFullYear()+1);
            } else {
                increment = i + firstMonth;
                mnt.setFullYear(currentDate.getFullYear());
            }
            mnt.setMonth(increment);
            if(i==0)
			options += '<option SELECTED value="' + i + '">' + me.months[mnt.getMonth()] + ' ' + mnt.getFullYear() + '</option>';
			else
			options += '<option value="' + i + '">' + me.months[mnt.getMonth()] + ' ' + mnt.getFullYear() + '</option>';

        }
		var allMonth='<option value="all">All</option>';
        $("select#month").html( allMonth+options ); 
        $("#select-month span").html($("select#month option:nth-child(2)").html());
        $("select#month").change(function(){
            $("#select-month span").html($("select#month option:selected").html());
            billboard.calendar.setMonth($("select#month option:selected").val());
        }); 
    };

/*
    //On Load
    $( function(){		
        me.init();
	});//End On Load
*/

})(jQuery);
