document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body)

  if (container) {
//    var img = new Image
//    img.src = '/images/spinner.gif'

    function createSpinner() {
      return new Element('img', { src: img.src, 'class': 'spinner' })
    }

    container.observe('click', function(e) {
      var el = e.element()
      if (el.match('.pagination a')) {
//        el.up('.pagination').insert(createSpinner())
        new Ajax.Request(el.href, { method: 'get' })
        e.stop()
      }
    })
  }
})

Companies = {
  country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('company_country').appear();
    	$('kase_new_company_phone_area_code').value = "";
    	$('kase_new_company_phone_exchange_code').value = "";
    	$('kase_new_company_phone_number').value = "";
    	$('company_us_phone').hide();
    }
    else {
    	$('company_country').hide();
    	$('kase_new_company_country').value = "";
    	$('kase_new_company_country_phone').value = "";
    	$('company_us_phone').show();
    }
  },
  admin_country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('country').appear();
    	$('company_phone_area_code').value = "";
    	$('company_phone_exchange_code').value = "";
    	$('company_phone_number').value = "";
    	$('company_us_phone').hide();
    }
    else {
    	$('company_phone').value = "";
    	$('company_country').value = "";
    	$('country').hide();
    	$('company_us_phone').show();
    }
  }
}
Restaurants = {
  country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('restaurant_country').appear();
    	$('kase_new_restaurant_phone_area_code').value = "";
    	$('kase_new_restaurant_phone_exchange_code').value = "";
    	$('kase_new_restaurant_phone_number').value = "";
    	$('restaurant_us_phone').hide();
    }
    else {
    	$('restaurant_country').hide();
    	$('kase_new_restaurant_country').value = "";
    	$('kase_new_restaurant_country_phone').value = "";
    	$('restaurant_us_phone').show();
    }
  },
  admin_country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('country').appear();
    	$('restaurant_phone_area_code').value = "";
    	$('restaurant_phone_exchange_code').value = "";
    	$('restaurant_phone_number').value = "";
    	$('restaurant_us_phone').hide();
    }
    else {
    	$('restaurant_phone').value = "";
    	$('restaurant_country').value = "";
    	$('country').hide();
    	$('restaurant_us_phone').show();
    }
  }  
}
Places = {
  country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('place_country').appear();
    	$('kase_new_place_phone_area_code').value = "";
    	$('kase_new_place_phone_exchange_code').value = "";
    	$('kase_new_place_phone_number').value = "";
    	$('place_us_phone').hide();
    }
    else {
    	$('place_country').hide();
    	$('kase_new_place_country').value = "";
    	$('kase_new_place_country_phone').value = "";
    	$('place_us_phone').show();
    }
  },
  admin_country: function(select) {
    if (select.options[select.selectedIndex].value == "n/a") {
    	$('country').appear();
    	$('place_phone_area_code').value = "";
    	$('place_phone_exchange_code').value = "";
    	$('place_phone_number').value = "";
    	$('place_us_phone').hide();
    }
    else {
    	$('place_phone').value = "";
    	$('place_country').value = "";
    	$('country').hide();
    	$('place_us_phone').show();
    }
  }
}
Kases =  {
  showForm: function(select) {
    if (select.options[select.selectedIndex].value == "1")
      Effect.toggle($('new_product'),'slide');
    else if (select.options[select.selectedIndex].value == "2")
      Effect.toggle($('new_book'),'slide');
    else if (select.options[select.selectedIndex].value == "3")
      Effect.toggle($('new_company'),'slide');
    else if (select.options[select.selectedIndex].value == "4")
      Effect.toggle($('new_movie'),'slide');
    else if (select.options[select.selectedIndex].value == "7")
      Effect.toggle($('new_game'),'slide');
    else if (select.options[select.selectedIndex].value == "8")
      Effect.toggle($('new_person'),'slide');
    else if (select.options[select.selectedIndex].value == "9")
      Effect.toggle($('new_restaurant'),'slide');
    else if (select.options[select.selectedIndex].value == "10")
      Effect.toggle($('other_category'),'slide');
    else if (select.options[select.selectedIndex].value == "11")
      Effect.toggle($('new_place'),'slide');
  },
  hideShowCompanyForm: function(select) {
    if (select.options[select.selectedIndex].value == "0")
      $('new_company').show();
    else
      $('new_company').hide();
  },
  hideShowBookForm: function(select) {
	    if (select.options[select.selectedIndex].value == "2")
	      $('new_book').show();
	    else
	      $('new_book').hide();
  },
  hideShowProductForm: function(select) {
	    if (select.options[select.selectedIndex].value == "0")
	      $('new_product').show();
	    else
	      $('new_product').hide();
  },
  hideShowPersonForm: function(select) {
	    if (select.options[select.selectedIndex].value == "0")
	      $('new_person').show();
	    else
	      $('new_person').hide();
  },
  hideShowGameTypeForm: function(select) {
	    if (select.options[select.selectedIndex].value == "3") {
	      $('non_video_game').hide();
	      $('video_game').show();
	    }  
	    else if (select.options[select.selectedIndex].value == "1" ||
	        select.options[select.selectedIndex].value == "2") {
	      $('non_video_game').show();
	      $('video_game').hide();
	    }  
	    else {
	      $('video_game').hide();
	      $('non_video_game').hide();
	    }
  },
    
showOtherConsole: function(select) {
   if (select.options[select.selectedIndex].value == "8") {
      Effect.toggle($('game_other_console'),'appear');
   }
   else if (document.getElementById('game_other_console') != null) { 
		$('game_other_console').hide();     
   }	      
}
,
showOtherGenre: function(select) {
   if (select.options[select.selectedIndex].value == "13") {
      Effect.toggle($('game_other_genre'),'appear');
   }
   else if (document.getElementById('game_other_genre') != null) { 
		$('game_other_genre').hide();     
   }	      
}
,

     
  hideShowMusicTypeForm: function(select) {
	    if (select.options[select.selectedIndex].value == "1") {
	      $('artist_entry').show();
		  $('concert_entry').hide();
		  $('venue_entry').hide();
		  $('album_entry').hide();
	    }  
	    else if (select.options[select.selectedIndex].value == "2"){
	      $('concert_entry').show();
	      $('artist_entry').hide();		  
		  $('venue_entry').hide();
		  $('album_entry').hide();
	    }  
	    else if (select.options[select.selectedIndex].value == "4") {
		  $('venue_entry').show();
	      $('concert_entry').hide();
	      $('artist_entry').hide();		  
		  $('album_entry').hide();
	    }
	    else if (select.options[select.selectedIndex].value == "3") {
			$('album_entry').show();
			$('venue_entry').hide();
			$('concert_entry').hide();
			$('artist_entry').hide();
		}
		else {
			$('album_entry').hide();
			$('venue_entry').hide();
			$('concert_entry').hide();
			$('artist_entry').hide();
		}
  },
    
  hideAllForm: function(select) {
	      $('new_product').hide();
	      $('new_book').hide();
	      $('new_company').hide();
	      $('new_person').hide();          
	      $('new_restaurant').hide();          
	      $('new_movie').hide();          
	      $('new_game').hide();    
	      $('other_category').hide();		  
	      $('new_place').hide();		  
		  if (document.getElementById('book_form') != null) { 
		      	      $('book_form').hide();     
		  }	      
		  if (document.getElementById('movie_form') != null) { 
		      $('movie_form').hide();
		  }
		  if (document.getElementById('company_form') != null) { 
		      	      $('company_form').hide();     
		  }	      
		  if (document.getElementById('person_form') != null) { 
		      	      $('person_form').hide();     
		  }	      
		  if (document.getElementById('restaurant_form') != null) { 
		      	      $('restaurant_form').hide();     
		  }	      
		  if (document.getElementById('game_form') != null) { 
		      	      $('game_form').hide();     
		  }	      
		  if (document.getElementById('product_form') != null) { 
		      	      $('product_form').hide();     
		  }	      
		  if (document.getElementById('book_form') != null) { 
		      	      $('book_form').hide();     
		  }	
		  if (document.getElementById('other_category_form') != null) { 
		      	      $('other_category_form').hide();     
		  }	
},

  
  addCaseTypeError: function() {
    setTimeout(function() {    
      if (!$('case_type_error'))
      new Insertion.Bottom('props_beefs', '<div id="case_type_error">must be selected</div>')}, 10)
  }  
  
}                   

Comment = {                             
  preview: function(url) {                  
    new Ajax.Updater("preview", url, {parameters: $('comment_form').serialize(true)})
  }
}                                       

ShoutBox = {
  lastShoutDate: null,
  maxLength: 200, 
  update: function(nbHighlight) {                        
    // Keep only ten shouts
    var shouts = $('shoutbox_content').childElements();
    if (shouts.length > 10)
      $R(1, shouts.length - 10).each(function(index) {shouts[index - 1].remove()});
      
    shouts = $('shoutbox_content').childElements();
    // Scroll bottom
    $('shoutbox_content').scrollTop = $('shoutbox_content').scrollHeight;   	
    for (var i= 0; i < nbHighlight; i++) {
      var index = shouts.length - 1 - i;
      if (index > 0)
        shouts[index].highlight();      
    }
  },
  
  check: function() {
    new Ajax.Request("/shout/poll", {parameters: {date: ShoutBox.lastShoutDate}})
  },
  
  startPoll: function() {
    setTimeout("new PeriodicalExecuter(ShoutBox.check, 45)", 45000);
  },
  
  checkMessageLength: function(textarea, event) {
	//make sure it exists first...(in case we're not logged in)
	if(!textarea){
		return false;
	}
    var text = textarea.value;   
    var doStop = false;                                 
    if (text.length > ShoutBox.maxLength) {
      text = text.substr(0, ShoutBox.maxLength);
      textarea.value = text;
      doStop = true;
    }
    // Update counter
    var left = ShoutBox.maxLength - text.length;
    $('left_char').update(left < 0 ? "0" : left);

    if (doStop && event) {
      Event.stop(event);  
      return false;
    }
  },
  
  checkMessageEvent: function(event)  {
    ShoutBox.checkMessageLength(Event.element(event), event);
  }
}       

User = {
  toggleCaseWatchList: function() {  
    var lines = $$('#watch_list_table tr');
    $A(lines.slice(6, lines.length)).invoke("toggle")
  },
  
  toggleCompanyWatchList: function() {  
    var lines = $$('#campany_watch_list .other');
    lines.invoke("toggle")
  }
}
var maxrows=10;

function getCompanySelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_company').show();
  }
  else {
    $('new_company').hide();
  } 
}

function getMovieSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_movie').show();
  }
  else {
    $('new_movie').hide();
  } 
}

function getGameSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_game').show();
  }
  else {
    $('new_game').hide();
    $('video_game').hide();
  } 
}

function get_direction() {
   if (document.getElementById('map_div') != null) { 
   	$('map_div').remove();
   }   
   $('map_location').setStyle({width:'780px'});    
	var map = new GMap2(document.getElementById("map_location"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	var directionsPanel = document.getElementById("my_textual_div");
	var directions = new GDirections(map, directionsPanel);
	var from_address = document.getElementById("start_address").value;
	var to_address = document.getElementById("end_address").value;
	alert(from_address);alert(to_address);
	var address = "from: " + from_address + " to: " + to_address;
	directions.load(address);
}

function copyText(category) {
	if (document.getElementById('kase_list').value == 'Create a Book') { return;}
	if (document.getElementById('kase_list').value == 'Create a Company') { return;}
	if (document.getElementById('kase_list').value == 'Create a Game') { return;}
	if (document.getElementById('kase_list').value == 'Create a Movie') { return;}
	if (document.getElementById('kase_list').value == 'Create a Person') { return;}
	if (document.getElementById('kase_list').value == 'Create a Product') { return;}
	if (document.getElementById('kase_list').value == 'Create New Music Entry') { return;}
	if (document.getElementById('kase_list').value == 'Create a Restaurant') { return;}
	if (document.getElementById('kase_list').value == 'Create a Sport') { return;}
	
	switch (category) {
		case 'books':		     
			document.getElementById('kase_new_book_title').value = document.getElementById('kase_list').value;
			break;			
		case 'companies':
			document.getElementById('kase_new_company_name').value = document.getElementById('kase_list').value;
			break;			
		case 'movies':
			document.getElementById('kase_new_movie_title').value = document.getElementById('kase_list').value;
			break;			
		case 'people':
			document.getElementById('kase_new_person_name').value = document.getElementById('kase_list').value;
			break;			
		case 'games':
			document.getElementById('kase_new_game_name').value = document.getElementById('kase_list').value;
			break;			
		case 'sports':
			document.getElementById('kase_new_sport_name').value = document.getElementById('kase_list').value;
			break;			
		case 'restaurants':
			document.getElementById('kase_new_restaurant_name').value = document.getElementById('kase_list').value;
			break;			
		case 'music':
			document.getElementById('kase_new_artist_name').value = document.getElementById('kase_list').value;
			document.getElementById('kase_new_music_album_name').value = document.getElementById('kase_list').value;
			document.getElementById('kase_new_music_venue_name').value = document.getElementById('kase_list').value;
			document.getElementById('kase_new_music_group_name').value = document.getElementById('kase_list').value;			
			break;			
		case 'products':
			document.getElementById('kase_new_product_name').value = document.getElementById('kase_list').value;
			break;			
	    default:
			return true;		 		
	}
}

function getBookSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_book').show();
  }
  else {
    $('new_book').hide();
  } 
}

function getSportSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_sports').show();
  }
  else {
    $('new_sports').hide();
  } 
}

function getMusicSelectionId(text, li, category) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_music').show();
  }
  else {
    $('new_music').hide();
  } 
}


function getRestaurantSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_restaurant').show();
  }
  else {
      $('new_restaurant').hide();
  } 
}

function getProductSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_product').show();
  }
  else {
    $('new_product').hide();
  } 
}

function getSelectionId(text, li) {
   document.getElementById('kase_sub_category_id').value = li.id;
   if (li.id == 0) {
      $('new_person').show();
  }
  else {
    $('new_person').hide();
  } 
}

function resizeTextArea(box) {
	var txt=box.value;
	var cols=box.cols;
	var arrtxt=txt.split('\n');
	var rows=arrtxt.length;
	for (i=0;i<arrtxt.length;i++)
  	rows+=parseInt(arrtxt[i].length/cols);
	if (rows>box.maxrows)
		box.rows=box.maxrows;
	else
		box.rows=rows;
}

function checkAll(field, chkid)
{
    var chkAll = document.getElementById(chkid);
    if (chkAll.checked) {
        if (field[0]) {
            for (i = 0; i < field.length; i++)
	            field[i].checked = true ;
	    }
	    else { 
	        field.checked=true;
	    }        
    }
    else 
        uncheckAll(field);
}
function uncheckAll(field)
{
    if (field[0]) {
        for (i = 0; i < field.length; i++)
	        field[i].checked = false ;
	}
	else {
	    field.checked=false;
	}        
}

function replaceHTML(fieldId) {

	elem = document.getElementById(fieldId).innerHTML;	
	if (elem == 'Learn More') {
	    document.getElementById(fieldId).innerHTML = 'Hide More';
	} else {	    
	    document.getElementById(fieldId).innerHTML = 'Learn More';
	}
	
}             

function validate(field, form){

	   var checked=false;
	   
	   if (field[0]) {
	      for (i = 0; i < field.length; i++) {
	         if (field[i].checked) {
	            checked=true;
	         }
	      }
	   }
	   else {
	       if (field.checked) {
	         checked=true
	       }
	   }
	   
	   if (checked) {
	      form.submit();
	      return true;
	   }
	   else {
	      alert('Please select a message to delete.');
	      return false;
	   }
	 }

function clearField(item) {	 
	document.getElementById(item).value = ""; 
	document.getElementById(item).style.color="black";
}	 	 

function onGDirectionsLoad(){
      /* We will simply scroll down to the directions, but with a little delay so it's loaded */
      setTimeout('eval(\'window.location = "#driving_direction"\;\')', 500); /* Uncomment this line if you need to */
}

function handleErrors(directions) {
   if (directions.getStatus().code != 200) {    
     $('map_location_original').show();
     $('my_textual_div').remove();
     $('driving_direction').remove();   
     $('flash').className = 'error';      
     var top = document.viewport.getScrollOffsets().top;
     $('flash').setStyle({top: (top-63) + 'px' });
     $('flash').setOpacity(0).show();
     $('flash').morph('opacity:1; top: ' + top + 'px', {duration: 0.5});
     $('flash').morph('opacity:0; top: ' + (top-63) + 'px', {delay:3, duration: 0.5});
     $('flash').update('One of your location is incorrect. Please enter correct location.');
/*   alert("We are unable to identify one of your location.  Please enter correct location.") */
   }
}
function drivingDirection() {
  if (GBrowserIsCompatible()) {
  $('map_location_original').hide();
  var driving_direction = document.getElementById('driving_direction');
  var my_textual_div = document.getElementById('my_textual_div');
  var map = new GMap2(driving_direction);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  var directionsPanel = my_textual_div;
  
  var directions = new GDirections(map, directionsPanel);
  
  /* Setup to event handlers, one: when the directions are loaded, two: if there was any error */
  GEvent.addListener(directions, 'load',  onGDirectionsLoad);
  GEvent.addListener(directions, 'error', handleErrors);
  
  var from_address = document.getElementById("start_address").value;
  var to_address = document.getElementById("end_address").value;
  var address = "from: " + from_address + " to: " + to_address;
  directions.load(address);
  }
}

function submitNomination()
{
  if (document.getElementById('nomination_name') != null) {
    document.getElementById('nominate_name').value = document.getElementById('nomination_name').value;
    document.getElementById('nominate_website').value = document.getElementById('nomination_website').value;
    document.getElementById('nominate_comments').value = document.getElementById('nomination_comments').value;
  }  
}

function hideHint() {
   var minHint = String(1);
   var maxHint = String(40);
   
   for (i = minHint; i<= maxHint; i++) {
       elementId = 'award_name_' + i;
       if (document.getElementById(elementId) != null) {
       	document.getElementById(elementId).style.display = "none";
      }
   }
}

/********************* Business Membership Methods/functions ******************/
function validate_service_notification() {
	if ( (document.getElementById('company_name').value == "") ||
		 (document.getElementById('company_phone').value == "") ||	
		 (document.getElementById('company_email').value == "") 
		) {
		alert("You must provide company name, phone and email address to get service notification.");
		return false;
	}
	return true;
}

function showCompanyForm() {
	val = $('user_company_id').value;
	if (val == '') {
		$('companyForm').appear();
		$('user_company_selected').value = 0;
	}
	else {
		$('companyForm').hide();
		$('user_company_selected').value = 1;
	}
}

function copyToBillingAddress() {
	val = $('user_copy_to_billing').checked;
	if (val) {
		$('user_billing_address_1').value = $('user_company_address_1').value;
		$('user_billing_address_2').value = $('user_company_address_2').value;
		$('user_billing_city').value = $('user_company_city').value;
		$('user_billing_state').value = $('user_company_state').value;
		$('user_billing_postal_code').value = $('user_company_postal_code').value;
	}
	else {
		$('user_billing_address_1').value = "";
		$('user_billing_address_2').value = "";
		$('user_billing_city').value = "";
		$('user_billing_state').value = "";
		$('user_billing_postal_code').value = "";
	}
}

function sortByRating(elementId) {
	var domId;
	
	domId = document.getElementById(elementId);
	if (domId.style.display == 'none') {
		Effect.SlideDown(domId);
	}
	else {
		Effect.SlideUp(domId);
	}
	return true;
}

function showAdditionalFields(select) {
    if (select.options[select.selectedIndex].value == "9") {
    	$('business_restaurant_type_id').appear();
    } 	
    else {
    	$('business_restaurant_type_id').hide();
    }
    if ( (select.options[select.selectedIndex].value == "3") ) {
    		$('business_industry_id').appear();
    } 	
    else {
    		$('business_industry_id').hide();
    }
}    

function setSubscriptionFee(str) {
	if (str == "monthly") {
		$('membership_amount').value = 24.99;
		document.getElementById('paypalcheckout_monthly').show();
		document.getElementById('paypalcheckout_annual').hide();
		
	}
	else {
		$('membership_amount').value = 249.99;
		document.getElementById('paypalcheckout_annual').show();
		document.getElementById('paypalcheckout_monthly').hide();
  }

    
}
    