 $(document).ready(function(){
	
	if($('#shopping_spree_contest_shopping_spree_school_id').val() >0){
		var url = new String(); 
	      url = "/bookstore_spree/"+$('#shopping_spree_contest_shopping_spree_school_id').val()+"/list_bookstores"; 
	      $("#school_bookstore").load(url);
	}
	$('#shopping_spree_contest_shopping_spree_school_id').change(function(){
		if ( $(this).val() > 0 ) {
	      var url = new String(); 
	      url = "/bookstore_spree/"+$(this).val()+"/list_bookstores"; 
	      $("#school_bookstore").load(url);
	    }
	});

	// Use this example, or...
	//$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
 $('#photos a.photo_album').lightBox();
 
  // For State Selector
  $('#school_state_id').change(function(){
    if ( $(this).val() > 0 ) {
      var url = new String(); 
      url = "/states/"+$(this).val()+"/list_locations"; 
      $("#school_location_id").load(url, function() {
        $("#school_location_id").attr("disabled", false);
      });
    }
  });
  
  // For School Selector
   $('#school_location_id').change(function(){
     if ( $(this).val() > 0 ) {
       var url = new String(); 
       url = "/schools/find_at_city/?city_id="+$(this).val(); 
       $("#school_id").load(url, function() {
         $("#school_id").attr("disabled", false);
       });
     }
   });
   
   // For School Selector
    $('#map_city_selection').change(function(){
      if ( $(this).val() > 0 ) {
        var url = new String(); 
        url = "/schools/find_at_city/?city_id="+$(this).val(); 
        $("#map_school_selection").load(url, function() {
          $("#map_school_selection").attr("disabled", false);
        });
      }
    });



   // To disable/enable go button 
  $('#school_id').change(function(){
    if ( $(this).val() > 0 ) {
      $("#school_submit").attr("disabled", false)
    } else {
      $("#school_submit").attr("disabled", true);  
    }
  });
   
   // For School Selector
    $('#notMySchool').click(function(){
      $('#schoolChooserPanel').addClass('ctaBorder');
      $('#schoolChooserPanel').slideToggle();
      $('#schoolChooserPanel a.twitterLink').hide();
      $('#schoolChooserPanel a.facebookLink').hide();        
    });
   
  // For Photo Gallery
 $('#photos #thumbnails a:nth-child(5n)').addClass('last');

   
  //Facebox
  $('a[rel*=facebox]').facebox(); 
  
  // On selecting an item from the state selector, show item on map. 
  $('#map_state_selection').change(function(){
	  clickObject($(this).attr('value'));
	  loadAmbassadorGrid($(this).val());
  });

  //Set initial states (?js)
  $('#map_sidebar .loader').hide();
  $('#map_bio').hide();
  $('#map_ambassadors').hide();

  $('#map_school_selection').change(function(){
    $('#map_ambassadors').show();
  })
  $('#map_ambassadors_grid li img').click(function(){
    //alert("hello");
  })
  
  $("#acceptBuyersAgreement").click(function(){
    $('#buyersAgreement').submit();
    return false; 
  })

  $('#map_school_submit').click(function(){
    if ( $('#map_school_selection').val() > 0 ) {
      $('#mapSchoolSelector').submit();
      return false; 
    } else {
      $('#mapNoSchoolMessage').show();
      return false;  
    }
  })
  
  //Rotating Billboard
  if ($("#billboard")) {
    $('#billboard').cycle({ 
          fx:      'fade', 
          speed:    400, 
          timeout:  6000,
          speed:    500,
          pasuse: 1,
          pauseOnPagerHover: 1,
          pager: '#billboard_controls_nums'
      });
      //hide the play btn at first
      $('.playbtn').hide();

      $('.pausebtn').click(function(){
        $('#billboard').cycle('pause');
        $('.pausebtn').hide();
        $('.playbtn').show();
        return false;
      });
      $('.playbtn').click(function(){
        $('#billboard').cycle('resume');
        $('.pausebtn').show();
        $('.playbtn').hide();
        return false;
      });
      $('input.postalcode').focus(function(){
        $('#billboard').cycle('pause');
      });
      $('input.postalcode').blur(function(){
        $('#billboard').cycle('resume');
      });
      $('.numbtn').click(function(){
        return false;
      });
    }
  
 });
 
 // CLICK ON OBJECT
 // flashMovie.clickObject(id);
 // this function will act as if user clicked on some object - area, movie or label. The
 // Object must have oid attribute set, for example: <area oid="UK" .....> 
 function clickObject(id){
  	var flashMovie = document.getElementById("ammap");
 	flashMovie.clickObject(id);
 }

 function loadAmbassadorGrid(state_code){
   //console.debug("Fired")
   // Load Ambassadors
   if ( state_code != 'NONE' ) {
     var url = new String(); 
     url = "/states/"+state_code+"/list_ambassadors_map"; 
     $("#map_ambassadors").load(url, function(){
       jQuery('a[rel*=facebox]').facebox() 
     })
     if ($('#map_city_selection').length > 0 ) {
  	     url = "/states/"+state_code+"/list_locations_by_code"; 
        $("#map_city_selection").load(url, function() {
          $("#map_city_selection").attr("disabled", false);
          //$("#school_location_id").attr("disabled", false);
        });
  	  }
   }
 }
 
 function checkAndLoadSelectors(state_code){

 }

 // REGISTER MOUSE CLICK ANYWHERE ON THE MAP
 // amRegisterClickAnywhere(map_id, object_id, title, value)
 // This function is called when user clicks on some object - area, movie or label.  
 function amRegisterClickAnywhere(map_id, object_id, title, value){
   if(title){
     $('#map_sidebar .loader').show();
     //$('#map_bio_content').html("<p>Clicked:" + title + "</p>");
     $('#map_state_selection').val(object_id);
      if($('#map.ambassadors')){
         $('#map_ambassadors').show();
         loadAmbassadorGrid(object_id);
      }
     // $('#map_sidebar').load("/ambassadors/get_ambassadors_for/"+title, function(){
     //   $('#map_sidebar .loader').hide();
     // });
   }
 }